764054d55a7ea9a46ca08b957f9de6015565619a
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / VxlanGpeCustomizer.java
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.translate.v3po.interfacesstate;
18
19 import static com.google.common.base.Preconditions.checkState;
20
21 import io.fd.honeycomb.translate.read.ReadContext;
22 import io.fd.honeycomb.translate.read.ReadFailedException;
23 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.vpp.jvpp.core.dto.VxlanGpeTunnelDetails;
28 import io.fd.vpp.jvpp.core.dto.VxlanGpeTunnelDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.dto.VxlanGpeTunnelDump;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.util.Arrays;
34 import java.util.concurrent.CompletionStage;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeNextProtocol;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeVni;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanGpe;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanGpeBuilder;
47 import org.opendaylight.yangtools.concepts.Builder;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class VxlanGpeCustomizer extends FutureJVppCustomizer
54         implements ReaderCustomizer<VxlanGpe, VxlanGpeBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
55
56     private static final Logger LOG = LoggerFactory.getLogger(VxlanGpeCustomizer.class);
57     private NamingContext interfaceContext;
58
59     public VxlanGpeCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
60         super(jvpp);
61         this.interfaceContext = interfaceContext;
62     }
63
64     @Override
65     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
66                       @Nonnull VxlanGpe readValue) {
67         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVxlanGpe(readValue);
68     }
69
70     @Nonnull
71     @Override
72     public VxlanGpeBuilder getBuilder(@Nonnull InstanceIdentifier<VxlanGpe> id) {
73         return new VxlanGpeBuilder();
74     }
75
76     @Override
77     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VxlanGpe> id,
78                                       @Nonnull final VxlanGpeBuilder builder,
79                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
80
81         final InterfaceKey key = id.firstKeyOf(Interface.class);
82         final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
83         if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index, VxlanGpeTunnel.class, LOG)) {
84             return;
85         }
86
87         LOG.debug("Reading attributes for VxlanGpe tunnel: {}", key.getName());
88         // Dump just a single
89         final VxlanGpeTunnelDump request = new VxlanGpeTunnelDump();
90         request.swIfIndex = index;
91
92         final CompletionStage<VxlanGpeTunnelDetailsReplyDump> swInterfaceVxlanGpeDetailsReplyDumpCompletionStage =
93                 getFutureJVpp().vxlanGpeTunnelDump(request);
94         final VxlanGpeTunnelDetailsReplyDump reply =
95                 getReplyForRead(swInterfaceVxlanGpeDetailsReplyDumpCompletionStage.toCompletableFuture(),
96                         id);
97
98         // VPP keeps VxlanGpe tunnel interfaces even after they were deleted (optimization)
99         // However there are no longer any VxlanGpe tunnel specific fields assigned to it and this call
100         // returns nothing
101         if (reply == null || reply.vxlanGpeTunnelDetails == null || reply.vxlanGpeTunnelDetails.isEmpty()) {
102             LOG.debug(
103                     "VxlanGpe tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
104                             "after delete", key.getName(), index);
105             return;
106         }
107
108         checkState(reply.vxlanGpeTunnelDetails.size() == 1,
109                 "Unexpected number of returned VxlanGpe tunnels: {} for tunnel: {}", reply.vxlanGpeTunnelDetails,
110                 key.getName());
111         LOG.trace("VxlanGpe tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
112
113         final VxlanGpeTunnelDetails swInterfaceVxlanGpeDetails = reply.vxlanGpeTunnelDetails.get(0);
114         if (swInterfaceVxlanGpeDetails.isIpv6 == 1) {
115             final Ipv6Address remote6 =
116                     new Ipv6Address(parseAddress(swInterfaceVxlanGpeDetails.remote).getHostAddress());
117             builder.setRemote(new IpAddress(remote6));
118             final Ipv6Address local6 =
119                     new Ipv6Address(parseAddress(swInterfaceVxlanGpeDetails.local).getHostAddress());
120             builder.setLocal(new IpAddress(local6));
121         } else {
122             final byte[] dstBytes = Arrays.copyOfRange(swInterfaceVxlanGpeDetails.remote, 0, 4);
123             final Ipv4Address remote4 = new Ipv4Address(parseAddress(dstBytes).getHostAddress());
124             builder.setRemote(new IpAddress(remote4));
125             final byte[] srcBytes = Arrays.copyOfRange(swInterfaceVxlanGpeDetails.local, 0, 4);
126             final Ipv4Address local4 = new Ipv4Address(parseAddress(srcBytes).getHostAddress());
127             builder.setLocal(new IpAddress(local4));
128         }
129         builder.setVni(new VxlanGpeVni((long) swInterfaceVxlanGpeDetails.vni));
130         builder.setNextProtocol(VxlanGpeNextProtocol.forValue(swInterfaceVxlanGpeDetails.protocol));
131         builder.setEncapVrfId((long) swInterfaceVxlanGpeDetails.encapVrfId);
132         builder.setDecapVrfId((long) swInterfaceVxlanGpeDetails.decapVrfId);
133         LOG.debug("VxlanGpe tunnel: {}, id: {} attributes read as: {}", key.getName(), index, builder);
134     }
135
136     @Nonnull
137     private static InetAddress parseAddress(@Nonnull final byte[] addr) {
138         try {
139             return InetAddress.getByAddress(addr);
140         } catch (UnknownHostException e) {
141             throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);
142         }
143     }
144 }