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.v3po.translate.v3po.interfacesstate;
19 import static com.google.common.base.Preconditions.checkState;
21 import io.fd.honeycomb.v3po.translate.Context;
22 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
23 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.Arrays;
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.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanTunnel;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Vxlan;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanBuilder;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.openvpp.jvpp.dto.VxlanTunnelDetails;
45 import org.openvpp.jvpp.dto.VxlanTunnelDetailsReplyDump;
46 import org.openvpp.jvpp.dto.VxlanTunnelDump;
47 import org.openvpp.jvpp.future.FutureJVpp;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class VxlanCustomizer extends FutureJVppCustomizer
52 implements ChildReaderCustomizer<Vxlan, VxlanBuilder> {
54 private static final Logger LOG = LoggerFactory.getLogger(VxlanCustomizer.class);
55 private NamingContext interfaceContext;
57 public VxlanCustomizer(@Nonnull final FutureJVpp jvpp, @Nonnull final NamingContext interfaceContext) {
59 this.interfaceContext = interfaceContext;
63 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
64 @Nonnull Vxlan readValue) {
65 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVxlan(readValue);
70 public VxlanBuilder getBuilder(@Nonnull InstanceIdentifier<Vxlan> id) {
71 return new VxlanBuilder();
75 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id,
76 @Nonnull final VxlanBuilder builder,
77 @Nonnull final Context ctx) throws ReadFailedException {
78 final InterfaceKey key = id.firstKeyOf(Interface.class);
79 // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
80 // to fill in the context with initial ifc mapping
81 final int index = interfaceContext.getIndex(key.getName());
82 if (!InterfaceUtils.isInterfaceOfType(ctx, index, VxlanTunnel.class)) {
86 LOG.debug("Reading attributes for vxlan tunnel: {}", key.getName());
88 final VxlanTunnelDump request = new VxlanTunnelDump();
89 request.swIfIndex = index;
91 final CompletionStage<VxlanTunnelDetailsReplyDump> swInterfaceVxlanDetailsReplyDumpCompletionStage =
92 getFutureJVpp().vxlanTunnelDump(request);
93 final VxlanTunnelDetailsReplyDump reply =
94 V3poUtils.getReply(swInterfaceVxlanDetailsReplyDumpCompletionStage.toCompletableFuture());
96 // VPP keeps vxlan tunnel interfaces even after they were deleted (optimization)
97 // However there ar no longer any vxlan tunnel specific fields assigned to it and this call
99 if (reply == null || reply.vxlanTunnelDetails == null || reply.vxlanTunnelDetails.isEmpty()) {
101 "Vxlan tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
102 "after delete", key.getName(), index);
106 checkState(reply.vxlanTunnelDetails.size() == 1,
107 "Unexpected number of returned vxlan tunnels: {} for tunnel: {}", reply.vxlanTunnelDetails, key.getName());
108 LOG.trace("Vxlan tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
110 final VxlanTunnelDetails swInterfaceVxlanDetails = reply.vxlanTunnelDetails.get(0);
111 if (swInterfaceVxlanDetails.isIpv6 == 1) {
112 final Ipv6Address dstIpv6 =
113 new Ipv6Address(parseAddress(swInterfaceVxlanDetails.dstAddress).getHostAddress());
114 builder.setDst(new IpAddress(dstIpv6));
115 final Ipv6Address srcIpv6 =
116 new Ipv6Address(parseAddress(swInterfaceVxlanDetails.srcAddress).getHostAddress());
117 builder.setSrc(new IpAddress(srcIpv6));
119 final byte[] dstBytes = Arrays.copyOfRange(swInterfaceVxlanDetails.dstAddress, 0, 4);
120 final Ipv4Address dstIpv4 = new Ipv4Address(parseAddress(dstBytes).getHostAddress());
121 builder.setDst(new IpAddress(dstIpv4));
122 final byte[] srcBytes = Arrays.copyOfRange(swInterfaceVxlanDetails.srcAddress, 0, 4);
123 final Ipv4Address srcIpv4 = new Ipv4Address(parseAddress(srcBytes).getHostAddress());
124 builder.setSrc(new IpAddress(srcIpv4));
126 builder.setEncapVrfId((long) swInterfaceVxlanDetails.encapVrfId);
127 builder.setVni((long) swInterfaceVxlanDetails.vni);
128 LOG.debug("Vxlan tunnel: {}, id: {} attributes read as: {}", key.getName(), index, builder);
132 private static InetAddress parseAddress(@Nonnull final byte[] addr) {
134 return InetAddress.getByAddress(addr);
135 } catch (UnknownHostException e) {
136 throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);