HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / RemoteMappingCustomizer.java
1 /*
2  * Copyright (c) 2015 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.write;
18
19
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;
25
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;
59
60
61 /**
62  * Customizer for {@link RemoteMapping}
63  */
64 public class RemoteMappingCustomizer extends FutureJVppCustomizer
65         implements ListWriterCustomizer<RemoteMapping, RemoteMappingKey>, EidTranslator,
66         AddressTranslator, JvppReplyConsumer, MappingProducer {
67
68     private final EidMappingContext remoteMappingContext;
69
70     public RemoteMappingCustomizer(@Nonnull final FutureJVppCore futureJvpp,
71                                    @Nonnull final EidMappingContext remoteMappingContext) {
72         super(futureJvpp);
73         this.remoteMappingContext = remoteMappingContext;
74     }
75
76     @Override
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);
83
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);
88
89         try {
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);
94         }
95
96         //after successfull adition adds mapping
97         remoteMappingContext.addEid(mappingId, dataAfter.getEid(), writeContext.getMappingContext());
98     }
99
100     @Override
101     public void updateCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataBefore,
102                                         RemoteMapping dataAfter, WriteContext writeContext)
103             throws WriteFailedException {
104         throw new UnsupportedOperationException("Operation not supported");
105     }
106
107     @Override
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");
112
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);
117
118         try {
119             addDelRemoteMappingAndReply(false, dataBefore,
120                     id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
121         } catch (VppBaseCallException | TimeoutException | IOException e) {
122             throw new WriteFailedException.DeleteFailedException(id, e);
123         }
124
125         //remote mapping after successfull remove of data
126         remoteMappingContext.removeEid(mappingId, writeContext.getMappingContext());
127     }
128
129     private void addDelRemoteMappingAndReply(boolean add, RemoteMapping data, int vni)
130             throws VppBaseCallException, TimeoutException, IOException {
131
132         LispAddDelRemoteMapping request = new LispAddDelRemoteMapping();
133
134         request.isAdd = booleanToByte(add);
135         request.vni = vni;
136         request.eidType = (byte) getEidType(data.getEid()).getValue();
137         request.eid = getEidAsByteArray(data.getEid());
138
139         //this is not length of eid array,but prefix length(bad naming by vpp)
140         request.eidLen = getPrefixLength(data.getEid());
141
142         if (LocatorListType.NEGATIVE
143                 .equals(resolveType(data.getLocatorList()))) {
144             request.action = (byte) extractAction(data.getLocatorList()).getIntValue();
145         } else {
146             Rlocs rlocs = extractRemoteLocators(data.getLocatorList());
147
148             checkArgument(rlocs != null, "No remote locators set for Positive mapping");
149
150             request.rlocs = locatorsToBinaryData(rlocs.getLocator());
151             request.rlocNum = Integer.valueOf(rlocs.getLocator().size()).byteValue();
152         }
153
154         getReply(getFutureJVpp().lispAddDelRemoteMapping(request).toCompletableFuture());
155     }
156
157     private static LocatorListType resolveType(LocatorList locatorList) {
158         checkNotNull(locatorList, "Locator List cannot be null");
159
160         if (locatorList instanceof PositiveMapping) {
161             return POSITIVE;
162         } else {
163             return NEGATIVE;
164         }
165     }
166
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");
171
172         return ((NegativeMapping) locatorList).getMapReply().getMapReplyAction();
173     }
174
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");
179
180         return ((PositiveMapping) locatorList).getRlocs();
181     }
182
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");
186
187         ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
188
189         DataOutputStream out = new DataOutputStream(byteArrayOut);
190
191
192         for (Locator locator : locators) {
193             boolean isIpv4;
194             byte[] address;
195
196             //first byte says that its v4/v6
197             isIpv4 = !isIpv6(locator.getAddress());
198             out.writeByte(booleanToByte(isIpv4));
199
200             //then writes priority
201             out.write(locator.getPriority());
202
203             //and weight
204             out.write(locator.getWeight());
205
206             if (isIpv4) {
207                 //vpp in this case needs address as 16 byte array,regardless if it is ivp4 or ipv6
208                 address = Arrays.copyOf(
209
210                         ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(locator.getAddress().getIpv4Address())),
211                         16);
212
213                 out.write(address);
214             } else {
215                 out.write(
216                         ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(locator.getAddress().getIpv6Address())));
217             }
218         }
219
220         return byteArrayOut.toByteArray();
221     }
222
223     public enum LocatorListType {
224
225         /**
226          * Represents locator list as negative mapping
227          */
228         NEGATIVE,
229
230         /**
231          * Represents locator list as positive mapping
232          */
233         POSITIVE
234     }
235
236 }