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.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;
44 // TODO extract common code from all Interface type specific writer customizers into a superclass
45 public class VxlanCustomizer extends AbstractInterfaceTypeCustomizer<Vxlan> {
47 private static final Logger LOG = LoggerFactory.getLogger(VxlanCustomizer.class);
48 private final NamingContext interfaceContext;
50 public VxlanCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
52 this.interfaceContext = interfaceContext;
56 protected Class<? extends InterfaceType> getExpectedInterfaceType() {
57 return VxlanTunnel.class;
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();
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);
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"));
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();
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);
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()));
100 int encapVrfId = vxlan.getEncapVrfId().intValue();
101 int vni = vxlan.getVni().getValue().intValue();
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));
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());
124 // Add new interface to our interface context
125 interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
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(),
134 checkArgument(vxlan.getDst().getIpv6Address() == null, "Inconsistent ip addresses: %s, %s", vxlan.getSrc(),
140 private String getAddressString(final IpAddress addr) {
141 return addr.getIpv4Address() == null ? addr.getIpv6Address().getValue() : addr.getIpv4Address().getValue();
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()));
150 int encapVrfId = vxlan.getEncapVrfId().intValue();
151 int vni = vxlan.getVni().getValue().intValue();
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));
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());
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;