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.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;
43 public class VxlanCustomizer extends AbstractInterfaceTypeCustomizer<Vxlan> implements JvppReplyConsumer {
45 private static final Logger LOG = LoggerFactory.getLogger(VxlanCustomizer.class);
47 private final NamingContext interfaceNamingContext;
48 private final DisabledInterfacesManager interfaceDisableContext;
50 public VxlanCustomizer(@Nonnull final FutureJVppCore vppApi,
51 @Nonnull final NamingContext interfaceNamingContext,
52 @Nonnull final DisabledInterfacesManager interfaceDisableContext) {
54 this.interfaceNamingContext = interfaceNamingContext;
55 this.interfaceDisableContext = interfaceDisableContext;
59 protected Class<? extends InterfaceType> getExpectedInterfaceType() {
60 return VxlanTunnel.class;
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);
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"));
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);
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)
93 final InetAddress srcAddress = InetAddresses.forString(getAddressString(vxlan.getSrc()));
94 final InetAddress dstAddress = InetAddresses.forString(getAddressString(vxlan.getDst()));
96 int encapVrfId = vxlan.getEncapVrfId().intValue();
97 int vni = vxlan.getVni().getValue().intValue();
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));
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());
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());
129 // Add new interface to our interface context
130 interfaceNamingContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
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(),
139 checkArgument(vxlan.getDst().getIpv6Address() == null, "Inconsistent ip addresses: %s, %s", vxlan.getSrc(),
145 private String getAddressString(final IpAddress addr) {
146 return addr.getIpv4Address() == null
147 ? addr.getIpv6Address().getValue()
148 : addr.getIpv4Address().getValue();
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)
156 final InetAddress srcAddress = InetAddresses.forString(getAddressString(vxlan.getSrc()));
157 final InetAddress dstAddress = InetAddresses.forString(getAddressString(vxlan.getDst()));
159 int encapVrfId = vxlan.getEncapVrfId().intValue();
160 int vni = vxlan.getVni().getValue().intValue();
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));
167 getReplyForDelete(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture(), id);
168 LOG.debug("Vxlan tunnel deleted successfully for: {}, vxlan: {}", swIfName, vxlan);
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());
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;