b0e9056fcde3ceaea3f1f2f53983131ebfe09fa0
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / RemoteMappingCustomizer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.lisp.translate.read;
18
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;
25
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;
83
84 /**
85  * Customizer for reading {@code RemoteMapping}<br>
86  */
87 public class RemoteMappingCustomizer extends FutureJVppCustomizer
88         implements ListReaderCustomizer<RemoteMapping, RemoteMappingKey, RemoteMappingBuilder>,
89         EidTranslator, AddressTranslator, ByteDataTranslator, MappingReader, LocatorReader {
90
91     private static final Logger LOG = LoggerFactory.getLogger(RemoteMappingCustomizer.class);
92
93     private final DumpCacheManager<LispEidTableDetailsReplyDump, MappingsDumpParams> dumpManager;
94     private final DumpCacheManager<LispLocatorDetailsReplyDump, LocatorDumpParams> locatorsDumpManager;
95     private final NamingContext locatorSetContext;
96     private final EidMappingContext remoteMappingContext;
97
98     public RemoteMappingCustomizer(@Nonnull final FutureJVppCore futureJvpp,
99                                    @Nonnull final NamingContext locatorSetContext,
100                                    @Nonnull final EidMappingContext remoteMappingContext) {
101         super(futureJvpp);
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
105         this.dumpManager =
106                 new DumpCacheManager.DumpCacheManagerBuilder<LispEidTableDetailsReplyDump, MappingsDumpParams>()
107                         .withExecutor(createMappingDumpExecutor(futureJvpp))
108                         .build();
109
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)))
115                         .build();
116     }
117
118
119     @Override
120     public RemoteMappingBuilder getBuilder(InstanceIdentifier<RemoteMapping> id) {
121         return new RemoteMappingBuilder();
122     }
123
124     private Eid copyEid(
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();
128     }
129
130     @Override
131     public void readCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMappingBuilder builder,
132                                       ReadContext ctx)
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");
136
137         final MappingId mappingId = id.firstKeyOf(RemoteMapping.class).getId();
138         checkState(remoteMappingContext.containsEid(mappingId, ctx.getMappingContext()),
139                 "No mapping stored for id %s", mappingId);
140
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)
151                 .build();
152
153         LOG.debug("Dumping data for LocalMappings(id={})", id);
154         final Optional<LispEidTableDetailsReplyDump> replyOptional =
155                 dumpManager.getDump(id, ctx.getModificationCache(), dumpParams);
156
157         if (!replyOptional.isPresent() || replyOptional.get().lispEidTableDetails.isEmpty()) {
158             return;
159         }
160
161         LOG.debug("Valid dump loaded");
162
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()))
167                 .collect(
168                         RWUtils.singleItemCollector());
169
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());
176     }
177
178     //compensate ~0 as default value of ttl
179     private static long resolveTtl(final int ttlValue) {
180         return ttlValue == -1
181                 ? Integer.MAX_VALUE
182                 : ttlValue;
183     }
184
185     @Override
186     public List<RemoteMappingKey> getAllIds(InstanceIdentifier<RemoteMapping> id, ReadContext context)
187             throws ReadFailedException {
188
189         checkState(id.firstKeyOf(VniTable.class) != null, "Parent VNI table not specified");
190         final int vni = id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue();
191
192         if (vni == 0) {
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();
197         }
198
199         //requesting all remote with specific vni
200         final MappingsDumpParams dumpParams = new MappingsDumpParamsBuilder()
201                 .setEidSet(QuantityType.ALL)
202                 .setFilter(FilterType.REMOTE)
203                 .build();
204
205         LOG.debug("Dumping data for LocalMappings(id={})", id);
206         final Optional<LispEidTableDetailsReplyDump> replyOptional =
207                 dumpManager.getDump(id, context.getModificationCache(), dumpParams);
208
209         if (!replyOptional.isPresent() || replyOptional.get().lispEidTableDetails.isEmpty()) {
210             return Collections.emptyList();
211         }
212
213         return replyOptional.get()
214                 .lispEidTableDetails
215                 .stream()
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()))
220                 .map(MappingId::new)
221                 .map(RemoteMappingKey::new)
222                 .collect(Collectors.toList());
223     }
224
225     @Override
226     public void merge(Builder<? extends DataObject> builder, List<RemoteMapping> readData) {
227         ((RemoteMappingsBuilder) builder).setRemoteMapping(readData);
228     }
229
230     private void resolveMappings(final InstanceIdentifier id, final RemoteMappingBuilder builder,
231                                  final LispEidTableDetails details,
232                                  final ModificationCache cache,
233                                  final MappingContext mappingContext) throws ReadFailedException {
234
235         if (details.action != 0) {
236             // in this case ,negative action was defined
237             bindNegativeMapping(builder, MapReplyAction.forValue(details.action));
238         } else {
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
241
242             // cache key needs to have locator set scope to not mix with cached data
243             final Optional<LispLocatorDetailsReplyDump> reply;
244
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);
250             try {
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",
256                                 e.getCause()));
257             }
258
259             if (!reply.isPresent() || reply.get().lispLocatorDetails.isEmpty()) {
260                 // no remote locators exist, therefore there was NO_ACTION defined
261                 bindNegativeMapping(builder, NoAction);
262             } else {
263                 // bind remote locators
264                 bindPositiveMapping(builder, reply.get());
265             }
266         }
267     }
268
269     private void bindNegativeMapping(final RemoteMappingBuilder builder,
270                                      final MapReplyAction action) {
271         builder.setLocatorList(
272                 new NegativeMappingBuilder().setMapReply(new MapReplyBuilder().setMapReplyAction(action).build())
273                         .build());
274     }
275
276     private void bindPositiveMapping(final RemoteMappingBuilder builder, final LispLocatorDetailsReplyDump reply) {
277         builder.setLocatorList(
278                 new PositiveMappingBuilder()
279                         .setRlocs(
280                                 new RlocsBuilder()
281                                         .setLocator(reply
282                                                 .lispLocatorDetails
283                                                 .stream()
284                                                 .map(this::detailsToLocator)
285                                                 .collect(Collectors.toList()))
286                                         .build()
287                         )
288                         .build()
289         );
290     }
291
292     private Locator detailsToLocator(final LispLocatorDetails details) {
293         final IpAddress address = arrayToIpAddressReversed(byteToBoolean(details.isIpv6), details.ipAddress);
294         return new LocatorBuilder()
295                 .setAddress(address)
296                 .setKey(new LocatorKey(address))
297                 .setPriority((short) details.priority)
298                 .setWeight((short) details.weight)
299                 .build();
300     }
301 }