HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / GreCustomizer.java
1 /*
2  * Copyright (c) 2016 Intel 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.NamingContext;
26 import io.fd.vpp.jvpp.core.dto.GreTunnelDetails;
27 import io.fd.vpp.jvpp.core.dto.GreTunnelDetailsReplyDump;
28 import io.fd.vpp.jvpp.core.dto.GreTunnelDump;
29 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
30 import java.net.InetAddress;
31 import java.net.UnknownHostException;
32 import java.util.Arrays;
33 import java.util.concurrent.CompletionStage;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.GreTunnel;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Gre;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.GreBuilder;
44 import org.opendaylight.yangtools.concepts.Builder;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class GreCustomizer extends FutureJVppCustomizer
51         implements ReaderCustomizer<Gre, GreBuilder>, InterfaceDataTranslator {
52
53     private static final Logger LOG = LoggerFactory.getLogger(GreCustomizer.class);
54     private NamingContext interfaceContext;
55
56     public GreCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
57         super(jvpp);
58         this.interfaceContext = interfaceContext;
59     }
60
61     @Override
62     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
63                       @Nonnull Gre readValue) {
64         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setGre(readValue);
65     }
66
67     @Nonnull
68     @Override
69     public GreBuilder getBuilder(@Nonnull InstanceIdentifier<Gre> id) {
70         return new GreBuilder();
71     }
72
73     @Override
74     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Gre> id,
75                                       @Nonnull final GreBuilder builder,
76                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
77         final InterfaceKey key = id.firstKeyOf(Interface.class);
78         final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
79         if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index, GreTunnel.class, LOG)) {
80             return;
81         }
82
83         LOG.debug("Reading attributes for gre tunnel: {}", key.getName());
84         // Dump just a single
85         final GreTunnelDump request = new GreTunnelDump();
86         request.swIfIndex = index;
87
88         final CompletionStage<GreTunnelDetailsReplyDump> swInterfaceGreDetailsReplyDumpCompletionStage =
89                 getFutureJVpp().greTunnelDump(request);
90         final GreTunnelDetailsReplyDump reply =
91                 getReplyForRead(swInterfaceGreDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
92
93         // VPP keeps gre tunnel interfaces even after they were deleted (optimization)
94         // However there ar no longer any gre tunnel specific fields assigned to it and this call
95         // returns nothing
96         if (reply == null || reply.greTunnelDetails == null || reply.greTunnelDetails.isEmpty()) {
97             LOG.debug(
98                     "Gre tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
99                             "after delete", key.getName(), index);
100             return;
101         }
102
103         checkState(reply.greTunnelDetails.size() == 1,
104                 "Unexpected number of returned gre tunnels: {} for tunnel: {}", reply.greTunnelDetails,
105                 key.getName());
106         LOG.trace("Gre tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
107
108         final GreTunnelDetails swInterfaceGreDetails = reply.greTunnelDetails.get(0);
109         if (swInterfaceGreDetails.isIpv6 == 1) {
110             final Ipv6Address dstIpv6 =
111                     new Ipv6Address(parseAddress(swInterfaceGreDetails.dstAddress).getHostAddress());
112             builder.setDst(new IpAddress(dstIpv6));
113             final Ipv6Address srcIpv6 =
114                     new Ipv6Address(parseAddress(swInterfaceGreDetails.srcAddress).getHostAddress());
115             builder.setSrc(new IpAddress(srcIpv6));
116         } else {
117             final byte[] dstBytes = Arrays.copyOfRange(swInterfaceGreDetails.dstAddress, 0, 4);
118             final Ipv4Address dstIpv4 = new Ipv4Address(parseAddress(dstBytes).getHostAddress());
119             builder.setDst(new IpAddress(dstIpv4));
120             final byte[] srcBytes = Arrays.copyOfRange(swInterfaceGreDetails.srcAddress, 0, 4);
121             final Ipv4Address srcIpv4 = new Ipv4Address(parseAddress(srcBytes).getHostAddress());
122             builder.setSrc(new IpAddress(srcIpv4));
123         }
124         builder.setOuterFibId((long) swInterfaceGreDetails.outerFibId);
125         LOG.debug("Gre tunnel: {}, id: {} attributes read as: {}", key.getName(), index, builder);
126     }
127
128     @Nonnull
129     private static InetAddress parseAddress(@Nonnull final byte[] addr) {
130         try {
131             return InetAddress.getByAddress(addr);
132         } catch (UnknownHostException e) {
133             throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);
134         }
135     }
136 }