HONEYCOMB-117: add support for jvpp plugins
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / VxlanCustomizer.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.translate.v3po.interfaces;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.net.InetAddresses;
22 import io.fd.honeycomb.translate.v3po.util.AbstractInterfaceTypeCustomizer;
23 import io.fd.honeycomb.translate.write.WriteContext;
24 import io.fd.honeycomb.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
26 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import java.net.InetAddress;
29 import java.util.concurrent.CompletionStage;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanTunnel;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Vxlan;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.openvpp.jvpp.VppBaseCallException;
38 import org.openvpp.jvpp.core.dto.VxlanAddDelTunnel;
39 import org.openvpp.jvpp.core.dto.VxlanAddDelTunnelReply;
40 import org.openvpp.jvpp.core.future.FutureJVppCore;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 // TODO extract common code from all Interface type specific writer customizers into a superclass
45 public class VxlanCustomizer extends AbstractInterfaceTypeCustomizer<Vxlan> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(VxlanCustomizer.class);
48     private final NamingContext interfaceContext;
49
50     public VxlanCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
51         super(vppApi);
52         this.interfaceContext = interfaceContext;
53     }
54
55     @Override
56     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
57         return VxlanTunnel.class;
58     }
59
60     @Override
61     protected final void writeInterface(@Nonnull final InstanceIdentifier<Vxlan> id, @Nonnull final Vxlan dataAfter,
62                                        @Nonnull final WriteContext writeContext)
63             throws WriteFailedException {
64         final String swIfName = id.firstKeyOf(Interface.class).getName();
65         try {
66             createVxlanTunnel(id, swIfName, dataAfter, writeContext);
67         } catch (VppBaseCallException | IllegalInterfaceTypeException e) {
68             LOG.debug("Failed to set vxlan tunnel for interface: {}, vxlan: {}", swIfName, dataAfter);
69             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
70         }
71     }
72
73     @Override
74     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id, @Nonnull final Vxlan dataBefore,
75                                         @Nonnull final Vxlan dataAfter, @Nonnull final WriteContext writeContext)
76             throws WriteFailedException.UpdateFailedException {
77         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
78                 new UnsupportedOperationException("Vxlan tunnel update is not supported"));
79     }
80
81     @Override
82     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id, @Nonnull final Vxlan dataBefore,
83                                         @Nonnull final WriteContext writeContext)
84             throws WriteFailedException {
85         final String swIfName = id.firstKeyOf(Interface.class).getName();
86         try {
87             deleteVxlanTunnel(id, swIfName, dataBefore, writeContext);
88         } catch (VppBaseCallException e) {
89             LOG.debug("Failed to delete vxlan tunnel for interface: {}, vxlan: {}", swIfName, dataBefore);
90             throw new WriteFailedException.DeleteFailedException(id, e);
91         }
92     }
93
94     private void createVxlanTunnel(final InstanceIdentifier<Vxlan> id, final String swIfName, final Vxlan vxlan,
95                                    final WriteContext writeContext) throws VppBaseCallException, WriteTimeoutException {
96         final byte isIpv6 = (byte) (isIpv6(vxlan) ? 1 : 0);
97         final InetAddress srcAddress = InetAddresses.forString(getAddressString(vxlan.getSrc()));
98         final InetAddress dstAddress = InetAddresses.forString(getAddressString(vxlan.getDst()));
99
100         int encapVrfId = vxlan.getEncapVrfId().intValue();
101         int vni = vxlan.getVni().getValue().intValue();
102
103         LOG.debug("Setting vxlan tunnel for interface: {}. Vxlan: {}", swIfName, vxlan);
104         final CompletionStage<VxlanAddDelTunnelReply> vxlanAddDelTunnelReplyCompletionStage =
105                 getFutureJVpp().vxlanAddDelTunnel(getVxlanTunnelRequest((byte) 1 /* is add */, srcAddress.getAddress(),
106                         dstAddress.getAddress(), encapVrfId, -1, vni, isIpv6));
107
108         final VxlanAddDelTunnelReply reply =
109                 TranslateUtils.getReplyForWrite(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture(), id);
110         LOG.debug("Vxlan tunnel set successfully for: {}, vxlan: {}", swIfName, vxlan);
111         if(interfaceContext.containsName(reply.swIfIndex, writeContext.getMappingContext())) {
112             // VPP keeps vxlan tunnels present even after they are delete(reserving ID for next tunnel)
113             // This may cause inconsistencies in mapping context when configuring tunnels like this:
114             // 1. Add tunnel 2. Delete tunnel 3. Read interfaces (reserved mapping e.g. vxlan_tunnel0 -> 6
115             // will get into mapping context) 4. Add tunnel (this will add another mapping with the same
116             // reserved ID and context is invalid)
117             // That's why a check has to be performed here removing mapping vxlan_tunnel0 -> 6 mapping and storing
118             // new name for that ID
119             final String formerName = interfaceContext.getName(reply.swIfIndex, writeContext.getMappingContext());
120             LOG.debug("Removing updated mapping of a vxlan tunnel, id: {}, former name: {}, new name: {}",
121                 reply.swIfIndex, formerName, swIfName);
122             interfaceContext.removeName(formerName, writeContext.getMappingContext());
123         }
124         // Add new interface to our interface context
125         interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
126     }
127
128     private boolean isIpv6(final Vxlan vxlan) {
129         if (vxlan.getSrc().getIpv4Address() == null) {
130             checkArgument(vxlan.getDst().getIpv4Address() == null, "Inconsistent ip addresses: %s, %s", vxlan.getSrc(),
131                 vxlan.getDst());
132             return true;
133         } else {
134             checkArgument(vxlan.getDst().getIpv6Address() == null, "Inconsistent ip addresses: %s, %s", vxlan.getSrc(),
135                 vxlan.getDst());
136             return false;
137         }
138     }
139
140     private String getAddressString(final IpAddress addr) {
141         return addr.getIpv4Address() == null ? addr.getIpv6Address().getValue() : addr.getIpv4Address().getValue();
142     }
143
144     private void deleteVxlanTunnel(final InstanceIdentifier<Vxlan> id, final String swIfName, final Vxlan vxlan,
145                                    final WriteContext writeContext) throws VppBaseCallException, WriteTimeoutException {
146         final byte isIpv6 = (byte) (isIpv6(vxlan) ? 1 : 0);
147         final InetAddress srcAddress = InetAddresses.forString(getAddressString(vxlan.getSrc()));
148         final InetAddress dstAddress = InetAddresses.forString(getAddressString(vxlan.getDst()));
149
150         int encapVrfId = vxlan.getEncapVrfId().intValue();
151         int vni = vxlan.getVni().getValue().intValue();
152
153         LOG.debug("Deleting vxlan tunnel for interface: {}. Vxlan: {}", swIfName, vxlan);
154         final CompletionStage<VxlanAddDelTunnelReply> vxlanAddDelTunnelReplyCompletionStage =
155                 getFutureJVpp().vxlanAddDelTunnel(getVxlanTunnelRequest((byte) 0 /* is add */, srcAddress.getAddress(),
156                         dstAddress.getAddress(), encapVrfId, -1, vni, isIpv6));
157
158         TranslateUtils.getReplyForWrite(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture(), id);
159         LOG.debug("Vxlan tunnel deleted successfully for: {}, vxlan: {}", swIfName, vxlan);
160         // Remove interface from our interface context
161         interfaceContext.removeName(swIfName, writeContext.getMappingContext());
162     }
163
164     private static VxlanAddDelTunnel getVxlanTunnelRequest(final byte isAdd, final byte[] srcAddr, final byte[] dstAddr,
165                                                     final int encapVrfId,
166                                                     final int decapNextIndex, final int vni, final byte isIpv6) {
167         final VxlanAddDelTunnel vxlanAddDelTunnel = new VxlanAddDelTunnel();
168         vxlanAddDelTunnel.isAdd = isAdd;
169         vxlanAddDelTunnel.srcAddress = srcAddr;
170         vxlanAddDelTunnel.dstAddress = dstAddr;
171         vxlanAddDelTunnel.encapVrfId = encapVrfId;
172         vxlanAddDelTunnel.vni = vni;
173         vxlanAddDelTunnel.decapNextIndex = decapNextIndex;
174         vxlanAddDelTunnel.isIpv6 = isIpv6;
175         return vxlanAddDelTunnel;
176     }
177 }