81ba34062dc03d06aea9d961e9384c5d15ab5412
[hc2vpp.git] /
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.v3po.translate.v3po.interfacesstate;
18
19 import static com.google.common.base.Preconditions.checkState;
20
21 import io.fd.honeycomb.v3po.translate.read.ReadContext;
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;
50
51 public class VxlanCustomizer extends FutureJVppCustomizer
52         implements ChildReaderCustomizer<Vxlan, VxlanBuilder> {
53
54     private static final Logger LOG = LoggerFactory.getLogger(VxlanCustomizer.class);
55     private NamingContext interfaceContext;
56
57     public VxlanCustomizer(@Nonnull final FutureJVpp jvpp, @Nonnull final NamingContext interfaceContext) {
58         super(jvpp);
59         this.interfaceContext = interfaceContext;
60     }
61
62     @Override
63     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
64                       @Nonnull Vxlan readValue) {
65         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVxlan(readValue);
66     }
67
68     @Nonnull
69     @Override
70     public VxlanBuilder getBuilder(@Nonnull InstanceIdentifier<Vxlan> id) {
71         return new VxlanBuilder();
72     }
73
74     @Override
75     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id,
76                                       @Nonnull final VxlanBuilder builder,
77                                       @Nonnull final ReadContext 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(), ctx.getMappingContext());
82         if (!InterfaceUtils.isInterfaceOfType(ctx.getModificationCache(), index, VxlanTunnel.class)) {
83             return;
84         }
85
86         LOG.debug("Reading attributes for vxlan tunnel: {}", key.getName());
87         // Dump just a single
88         final VxlanTunnelDump request = new VxlanTunnelDump();
89         request.swIfIndex = index;
90
91         final CompletionStage<VxlanTunnelDetailsReplyDump> swInterfaceVxlanDetailsReplyDumpCompletionStage =
92             getFutureJVpp().vxlanTunnelDump(request);
93         final VxlanTunnelDetailsReplyDump reply =
94             V3poUtils.getReply(swInterfaceVxlanDetailsReplyDumpCompletionStage.toCompletableFuture());
95
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
98         // returns nothing
99         if (reply == null || reply.vxlanTunnelDetails == null || reply.vxlanTunnelDetails.isEmpty()) {
100             LOG.debug(
101                 "Vxlan tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
102                     "after delete", key.getName(), index);
103             return;
104         }
105
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);
109
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));
118         } else {
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));
125         }
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);
129     }
130
131     @Nonnull
132     private static InetAddress parseAddress(@Nonnull final byte[] addr) {
133         try {
134             return InetAddress.getByAddress(addr);
135         } catch (UnknownHostException e) {
136             throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);
137         }
138     }
139 }