2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.lisp.translate.read;
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.lisp.translate.read.dump.executor.LocatorSetsDumpExecutor;
24 import io.fd.honeycomb.translate.read.ReadContext;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
28 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import io.fd.honeycomb.translate.vpp.util.NamingContext;
31 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetails;
32 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.LocatorSetsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class LocatorSetCustomizer extends FutureJVppCustomizer
49 implements ListReaderCustomizer<LocatorSet, LocatorSetKey, LocatorSetBuilder>, ByteDataTranslator {
51 //TODO - temporary as public because of hack in write customizer in *.write.LocatorSetCustomizer
52 public static final String LOCATOR_SETS_CACHE_ID = LocatorSetCustomizer.class.getName();
53 private static final Logger LOG = LoggerFactory.getLogger(LocatorSetCustomizer.class);
55 private final DumpCacheManager<LispLocatorSetDetailsReplyDump, Void> dumpManager;
56 private final NamingContext locatorSetContext;
58 public LocatorSetCustomizer(@Nonnull final FutureJVppCore futureJvpp,
59 @Nonnull final NamingContext locatorSetContext) {
61 this.locatorSetContext = checkNotNull(locatorSetContext, "Locator Set mapping context cannot be null");
62 this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorSetDetailsReplyDump, Void>()
63 .withExecutor(new LocatorSetsDumpExecutor(futureJvpp))
68 public LocatorSetBuilder getBuilder(InstanceIdentifier<LocatorSet> id) {
69 return new LocatorSetBuilder();
73 public void readCurrentAttributes(InstanceIdentifier<LocatorSet> id, LocatorSetBuilder builder, ReadContext ctx)
74 throws ReadFailedException {
75 LOG.debug("Reading attributes for Locator Set {}", id);
77 final Optional<LispLocatorSetDetailsReplyDump> dumpOptional =
78 dumpManager.getDump(id, LOCATOR_SETS_CACHE_ID, ctx.getModificationCache(), NO_PARAMS);
80 if (!dumpOptional.isPresent() || dumpOptional.get().lispLocatorSetDetails.isEmpty()) {
84 String keyName = id.firstKeyOf(LocatorSet.class).getName();
85 LispLocatorSetDetailsReplyDump dump = dumpOptional.get();
87 java.util.Optional<LispLocatorSetDetails> details = dump.lispLocatorSetDetails.stream()
88 .filter(n -> keyName.equals(toString(n.lsName)))
91 if (details.isPresent()) {
92 final String name = toString(details.get().lsName);
94 builder.setName(name);
95 builder.setKey(new LocatorSetKey(name));
97 LOG.warn("Locator Set {} not found in dump", id);
102 public List<LocatorSetKey> getAllIds(InstanceIdentifier<LocatorSet> id, ReadContext context)
103 throws ReadFailedException {
104 LOG.debug("Dumping Locator Set {}", id);
106 final Optional<LispLocatorSetDetailsReplyDump> dumpOptional =
107 dumpManager.getDump(id, LOCATOR_SETS_CACHE_ID, context.getModificationCache(), NO_PARAMS);
109 if (!dumpOptional.isPresent() || dumpOptional.get().lispLocatorSetDetails.isEmpty()) {
110 return Collections.emptyList();
113 return dumpOptional.get().lispLocatorSetDetails.stream()
116 final String locatorSetName = toString(set.lsName);
117 //creates mapping for existing locator-set(if it is'nt already existing one)
118 if (!locatorSetContext.containsIndex(locatorSetName, context.getMappingContext())) {
119 locatorSetContext.addName(set.lsIndex, locatorSetName, context.getMappingContext());
122 LOG.trace("Locator Set with name: {}, VPP name: {} and index: {} found in VPP",
123 locatorSetContext.getName(set.lsIndex, context.getMappingContext()),
129 .map(set -> new LocatorSetKey(toString(set.lsName)))
130 .collect(Collectors.toList());
134 public void merge(Builder<? extends DataObject> builder, List<LocatorSet> readData) {
135 ((LocatorSetsBuilder) builder).setLocatorSet(readData);