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 com.google.common.base.Preconditions.checkState;
21 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.valueOf;
22 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.FilterType;
23 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.MappingsDumpParamsBuilder;
24 import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MapReplyAction.NoAction;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableSet;
28 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
29 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.LocatorDumpParams;
30 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.LocatorDumpParams.LocatorDumpParamsBuilder;
31 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams;
32 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.QuantityType;
33 import io.fd.honeycomb.lisp.translate.read.trait.LocatorReader;
34 import io.fd.honeycomb.lisp.translate.read.trait.MappingReader;
35 import io.fd.honeycomb.lisp.translate.util.EidTranslator;
36 import io.fd.honeycomb.translate.MappingContext;
37 import io.fd.honeycomb.translate.ModificationCache;
38 import io.fd.honeycomb.translate.read.ReadContext;
39 import io.fd.honeycomb.translate.read.ReadFailedException;
40 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
41 import io.fd.honeycomb.translate.util.RWUtils;
42 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
43 import io.fd.honeycomb.translate.util.read.cache.IdentifierCacheKeyFactory;
44 import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
45 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
46 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
47 import io.fd.honeycomb.translate.vpp.util.NamingContext;
48 import io.fd.vpp.jvpp.core.dto.LispEidTableDetails;
49 import io.fd.vpp.jvpp.core.dto.LispEidTableDetailsReplyDump;
50 import io.fd.vpp.jvpp.core.dto.LispLocatorDetails;
51 import io.fd.vpp.jvpp.core.dto.LispLocatorDetailsReplyDump;
52 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
53 import java.util.Collections;
54 import java.util.List;
55 import java.util.stream.Collectors;
56 import javax.annotation.Nonnull;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MapReplyAction;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.RemoteMappingsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMapping;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMappingBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMappingKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.EidBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.NegativeMappingBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.PositiveMappingBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.negative.mapping.MapReplyBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.RlocsBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.Locator;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.LocatorBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.LocatorKey;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.LocatorSets;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.Interface;
78 import org.opendaylight.yangtools.concepts.Builder;
79 import org.opendaylight.yangtools.yang.binding.DataObject;
80 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
85 * Customizer for reading {@code RemoteMapping}<br>
87 public class RemoteMappingCustomizer extends FutureJVppCustomizer
88 implements ListReaderCustomizer<RemoteMapping, RemoteMappingKey, RemoteMappingBuilder>,
89 EidTranslator, AddressTranslator, ByteDataTranslator, MappingReader, LocatorReader {
91 private static final Logger LOG = LoggerFactory.getLogger(RemoteMappingCustomizer.class);
93 private final DumpCacheManager<LispEidTableDetailsReplyDump, MappingsDumpParams> dumpManager;
94 private final DumpCacheManager<LispLocatorDetailsReplyDump, LocatorDumpParams> locatorsDumpManager;
95 private final NamingContext locatorSetContext;
96 private final EidMappingContext remoteMappingContext;
98 public RemoteMappingCustomizer(@Nonnull final FutureJVppCore futureJvpp,
99 @Nonnull final NamingContext locatorSetContext,
100 @Nonnull final EidMappingContext remoteMappingContext) {
102 this.locatorSetContext = checkNotNull(locatorSetContext, "Locator sets context not present");
103 this.remoteMappingContext = checkNotNull(remoteMappingContext, "Remote mappings not present");
104 // this one should have default scope == RemoteMapping
106 new DumpCacheManager.DumpCacheManagerBuilder<LispEidTableDetailsReplyDump, MappingsDumpParams>()
107 .withExecutor(createMappingDumpExecutor(futureJvpp))
110 // cache key needs to have locator set scope to not mix with cached data
111 this.locatorsDumpManager =
112 new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorDetailsReplyDump, LocatorDumpParams>()
113 .withExecutor(createLocatorDumpExecutor(futureJvpp))
114 .withCacheKeyFactory(new IdentifierCacheKeyFactory(ImmutableSet.of(LocatorSet.class)))
120 public RemoteMappingBuilder getBuilder(InstanceIdentifier<RemoteMapping> id) {
121 return new RemoteMappingBuilder();
125 org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid eid) {
126 return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
127 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
131 public void readCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMappingBuilder builder,
133 throws ReadFailedException {
134 checkState(id.firstKeyOf(RemoteMapping.class) != null, "No key present for id({})", id);
135 checkState(id.firstKeyOf(VniTable.class) != null, "Parent VNI table not specified");
137 final MappingId mappingId = id.firstKeyOf(RemoteMapping.class).getId();
138 checkState(remoteMappingContext.containsEid(mappingId, ctx.getMappingContext()),
139 "No mapping stored for id %s", mappingId);
141 final long vni = id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier();
142 final String remoteMappingId = id.firstKeyOf(RemoteMapping.class).getId().getValue();
143 final Eid eid = copyEid(remoteMappingContext.getEid(mappingId, ctx.getMappingContext()));
144 final MappingsDumpParams dumpParams = new MappingsDumpParamsBuilder()
145 .setVni(Long.valueOf(vni).intValue())
146 .setEidSet(QuantityType.SPECIFIC)
147 .setEidType(getEidType(eid))
148 .setEid(getEidAsByteArray(eid))
149 .setPrefixLength(getPrefixLength(eid))
150 .setFilter(FilterType.REMOTE)
153 LOG.debug("Dumping data for LocalMappings(id={})", id);
154 final Optional<LispEidTableDetailsReplyDump> replyOptional =
155 dumpManager.getDump(id, ctx.getModificationCache(), dumpParams);
157 if (!replyOptional.isPresent() || replyOptional.get().lispEidTableDetails.isEmpty()) {
161 LOG.debug("Valid dump loaded");
163 LispEidTableDetails details = replyOptional.get().lispEidTableDetails.stream()
164 .filter(subtableFilterForRemoteMappings(id))
165 .filter(a -> compareAddresses(eid.getAddress(),
166 getArrayAsEidLocal(valueOf(a.eidType), a.eid, a.vni).getAddress()))
168 RWUtils.singleItemCollector());
170 builder.setEid(getArrayAsEidRemote(valueOf(details.eidType), details.eid, details.vni));
171 builder.setKey(new RemoteMappingKey(new MappingId(id.firstKeyOf(RemoteMapping.class).getId())));
172 builder.setTtl(resolveTtl(details.ttl));
173 builder.setAuthoritative(
174 new RemoteMapping.Authoritative(byteToBoolean(details.authoritative)));
175 resolveMappings(id, builder, details, ctx.getModificationCache(), ctx.getMappingContext());
178 //compensate ~0 as default value of ttl
179 private static long resolveTtl(final int ttlValue) {
180 return ttlValue == -1
186 public List<RemoteMappingKey> getAllIds(InstanceIdentifier<RemoteMapping> id, ReadContext context)
187 throws ReadFailedException {
189 checkState(id.firstKeyOf(VniTable.class) != null, "Parent VNI table not specified");
190 final int vni = id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue();
193 // ignoring default vni mapping
194 // it's not relevant for us and we also don't store mapping for such eid's
195 // such mapping is used to create helper local mappings to process remote ones
196 return Collections.emptyList();
199 //requesting all remote with specific vni
200 final MappingsDumpParams dumpParams = new MappingsDumpParamsBuilder()
201 .setEidSet(QuantityType.ALL)
202 .setFilter(FilterType.REMOTE)
205 LOG.debug("Dumping data for LocalMappings(id={})", id);
206 final Optional<LispEidTableDetailsReplyDump> replyOptional =
207 dumpManager.getDump(id, context.getModificationCache(), dumpParams);
209 if (!replyOptional.isPresent() || replyOptional.get().lispEidTableDetails.isEmpty()) {
210 return Collections.emptyList();
213 return replyOptional.get()
216 .filter(a -> a.vni == vni)
217 .filter(subtableFilterForRemoteMappings(id))
218 .map(detail -> getArrayAsEidRemote(valueOf(detail.eidType), detail.eid, detail.vni))
219 .map(remoteEid -> remoteMappingContext.getId(remoteEid, context.getMappingContext()))
221 .map(RemoteMappingKey::new)
222 .collect(Collectors.toList());
226 public void merge(Builder<? extends DataObject> builder, List<RemoteMapping> readData) {
227 ((RemoteMappingsBuilder) builder).setRemoteMapping(readData);
230 private void resolveMappings(final InstanceIdentifier id, final RemoteMappingBuilder builder,
231 final LispEidTableDetails details,
232 final ModificationCache cache,
233 final MappingContext mappingContext) throws ReadFailedException {
235 if (details.action != 0) {
236 // in this case ,negative action was defined
237 bindNegativeMapping(builder, MapReplyAction.forValue(details.action));
239 // in this case, there is no clear determination whether negative action with NO_ACTION(value == 0) was defined,
240 // or if its default value and remote locators, are defined, so only chance to determine so, is to dump locators for this mapping
242 // cache key needs to have locator set scope to not mix with cached data
243 final Optional<LispLocatorDetailsReplyDump> reply;
245 // this will serve to achieve that locators have locator set scope
246 final InstanceIdentifier<Interface> locatorIfaceIdentifier = InstanceIdentifier.create(LocatorSets.class)
247 .child(LocatorSet.class,
248 new LocatorSetKey(locatorSetContext.getName(details.locatorSetIndex, mappingContext)))
249 .child(Interface.class);
251 reply = locatorsDumpManager.getDump(locatorIfaceIdentifier, cache,
252 new LocatorDumpParamsBuilder().setLocatorSetIndex(details.locatorSetIndex).build());
253 } catch (ReadFailedException e) {
254 throw new ReadFailedException(id,
255 new IllegalStateException("Unable to resolve Positive/Negative mapping for RemoteMapping",
259 if (!reply.isPresent() || reply.get().lispLocatorDetails.isEmpty()) {
260 // no remote locators exist, therefore there was NO_ACTION defined
261 bindNegativeMapping(builder, NoAction);
263 // bind remote locators
264 bindPositiveMapping(builder, reply.get());
269 private void bindNegativeMapping(final RemoteMappingBuilder builder,
270 final MapReplyAction action) {
271 builder.setLocatorList(
272 new NegativeMappingBuilder().setMapReply(new MapReplyBuilder().setMapReplyAction(action).build())
276 private void bindPositiveMapping(final RemoteMappingBuilder builder, final LispLocatorDetailsReplyDump reply) {
277 builder.setLocatorList(
278 new PositiveMappingBuilder()
284 .map(this::detailsToLocator)
285 .collect(Collectors.toList()))
292 private Locator detailsToLocator(final LispLocatorDetails details) {
293 final IpAddress address = arrayToIpAddressReversed(byteToBoolean(details.isIpv6), details.ipAddress);
294 return new LocatorBuilder()
296 .setKey(new LocatorKey(address))
297 .setPriority((short) details.priority)
298 .setWeight((short) details.weight)