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.write;
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
23 import static io.fd.honeycomb.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.NEGATIVE;
24 import static io.fd.honeycomb.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.POSITIVE;
26 import com.google.common.base.Preconditions;
27 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
28 import io.fd.honeycomb.lisp.translate.read.trait.MappingProducer;
29 import io.fd.honeycomb.lisp.translate.util.EidTranslator;
30 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
31 import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
32 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
33 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
34 import io.fd.honeycomb.translate.write.WriteContext;
35 import io.fd.honeycomb.translate.write.WriteFailedException;
36 import java.io.ByteArrayOutputStream;
37 import java.io.DataOutputStream;
38 import java.io.IOException;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.concurrent.TimeoutException;
42 import javax.annotation.Nonnull;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MapReplyAction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMapping;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMappingKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.LocatorList;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.NegativeMapping;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.PositiveMapping;
52 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;
53 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;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import io.fd.vpp.jvpp.VppBaseCallException;
57 import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMapping;
58 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
62 * Customizer for {@link RemoteMapping}
64 public class RemoteMappingCustomizer extends FutureJVppCustomizer
65 implements ListWriterCustomizer<RemoteMapping, RemoteMappingKey>, EidTranslator,
66 AddressTranslator, JvppReplyConsumer, MappingProducer {
68 private final EidMappingContext remoteMappingContext;
70 public RemoteMappingCustomizer(@Nonnull final FutureJVppCore futureJvpp,
71 @Nonnull final EidMappingContext remoteMappingContext) {
73 this.remoteMappingContext = remoteMappingContext;
77 public void writeCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataAfter,
78 WriteContext writeContext) throws WriteFailedException {
79 checkNotNull(dataAfter, "Mapping is null");
80 checkNotNull(dataAfter.getEid(), "Eid is null");
81 checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
82 checkAllowedCombination(id, dataAfter);
84 //checks whether mapping not already contains such key
85 MappingId mappingId = id.firstKeyOf(RemoteMapping.class).getId();
86 checkState(!remoteMappingContext.containsEid(mappingId, writeContext.getMappingContext()),
87 "Mapping for id %s already defined", mappingId);
90 addDelRemoteMappingAndReply(true, dataAfter,
91 id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
92 } catch (VppBaseCallException | TimeoutException | IOException e) {
93 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
96 //after successfull adition adds mapping
97 remoteMappingContext.addEid(mappingId, dataAfter.getEid(), writeContext.getMappingContext());
101 public void updateCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataBefore,
102 RemoteMapping dataAfter, WriteContext writeContext)
103 throws WriteFailedException {
104 throw new UnsupportedOperationException("Operation not supported");
108 public void deleteCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataBefore,
109 WriteContext writeContext) throws WriteFailedException {
110 checkNotNull(dataBefore, "Mapping is null");
111 checkNotNull(dataBefore.getEid(), "Eid is null");
113 //checks whether mapping already contains such key
114 MappingId mappingId = id.firstKeyOf(RemoteMapping.class).getId();
115 checkState(remoteMappingContext.containsEid(mappingId, writeContext.getMappingContext()),
116 "Mapping for id %s is not existing,nothing to remove", mappingId);
119 addDelRemoteMappingAndReply(false, dataBefore,
120 id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
121 } catch (VppBaseCallException | TimeoutException | IOException e) {
122 throw new WriteFailedException.DeleteFailedException(id, e);
125 //remote mapping after successfull remove of data
126 remoteMappingContext.removeEid(mappingId, writeContext.getMappingContext());
129 private void addDelRemoteMappingAndReply(boolean add, RemoteMapping data, int vni)
130 throws VppBaseCallException, TimeoutException, IOException {
132 LispAddDelRemoteMapping request = new LispAddDelRemoteMapping();
134 request.isAdd = booleanToByte(add);
136 request.eidType = (byte) getEidType(data.getEid()).getValue();
137 request.eid = getEidAsByteArray(data.getEid());
139 //this is not length of eid array,but prefix length(bad naming by vpp)
140 request.eidLen = getPrefixLength(data.getEid());
142 if (LocatorListType.NEGATIVE
143 .equals(resolveType(data.getLocatorList()))) {
144 request.action = (byte) extractAction(data.getLocatorList()).getIntValue();
146 Rlocs rlocs = extractRemoteLocators(data.getLocatorList());
148 checkArgument(rlocs != null, "No remote locators set for Positive mapping");
150 request.rlocs = locatorsToBinaryData(rlocs.getLocator());
151 request.rlocNum = Integer.valueOf(rlocs.getLocator().size()).byteValue();
154 getReply(getFutureJVpp().lispAddDelRemoteMapping(request).toCompletableFuture());
157 private static LocatorListType resolveType(LocatorList locatorList) {
158 checkNotNull(locatorList, "Locator List cannot be null");
160 if (locatorList instanceof PositiveMapping) {
167 private static MapReplyAction extractAction(LocatorList locatorList) {
168 checkNotNull(locatorList, "Locator List cannot be null");
169 Preconditions.checkArgument(NEGATIVE.equals(resolveType(locatorList)),
170 "Action can be extracted only from Negative Mapping");
172 return ((NegativeMapping) locatorList).getMapReply().getMapReplyAction();
175 private static Rlocs extractRemoteLocators(LocatorList locatorList) {
176 checkNotNull(locatorList, "Locator List cannot be null");
177 Preconditions.checkArgument(POSITIVE.equals(resolveType(locatorList)),
178 "RLocs can be extracted only from Positive Mapping");
180 return ((PositiveMapping) locatorList).getRlocs();
183 //cant be static because of use of default methods from traits
184 private byte[] locatorsToBinaryData(List<Locator> locators) throws IOException {
185 checkNotNull(locators, "Cannot convert null list");
187 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
189 DataOutputStream out = new DataOutputStream(byteArrayOut);
192 for (Locator locator : locators) {
196 //first byte says that its v4/v6
197 isIpv4 = !isIpv6(locator.getAddress());
198 out.writeByte(booleanToByte(isIpv4));
200 //then writes priority
201 out.write(locator.getPriority());
204 out.write(locator.getWeight());
207 //vpp in this case needs address as 16 byte array,regardless if it is ivp4 or ipv6
208 address = Arrays.copyOf(
210 ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(locator.getAddress().getIpv4Address())),
216 ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(locator.getAddress().getIpv6Address())));
220 return byteArrayOut.toByteArray();
223 public enum LocatorListType {
226 * Represents locator list as negative mapping
231 * Represents locator list as positive mapping