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