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