2fb94c1e1eb66a2d4df284e32b52a248e0ac3c5c
[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.Initialized;
24 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
25 import io.fd.honeycomb.translate.util.RWUtils;
26 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.vpp.jvpp.core.dto.GreTunnelDetails;
29 import io.fd.vpp.jvpp.core.dto.GreTunnelDetailsReplyDump;
30 import io.fd.vpp.jvpp.core.dto.GreTunnelDump;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.net.InetAddress;
33 import java.net.UnknownHostException;
34 import java.util.Arrays;
35 import java.util.concurrent.CompletionStage;
36 import javax.annotation.Nonnull;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.GreTunnel;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Gre;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.GreBuilder;
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 GreCustomizer extends FutureJVppCustomizer
54         implements InitializingReaderCustomizer<Gre, GreBuilder>, InterfaceDataTranslator {
55
56     private static final Logger LOG = LoggerFactory.getLogger(GreCustomizer.class);
57     private NamingContext interfaceContext;
58
59     public GreCustomizer(@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 Gre readValue) {
67         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setGre(readValue);
68     }
69
70     @Nonnull
71     @Override
72     public GreBuilder getBuilder(@Nonnull InstanceIdentifier<Gre> id) {
73         return new GreBuilder();
74     }
75
76     @Override
77     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Gre> id,
78                                       @Nonnull final GreBuilder builder,
79                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
80         final InterfaceKey key = id.firstKeyOf(Interface.class);
81         final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
82         if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index, GreTunnel.class, LOG)) {
83             return;
84         }
85
86         LOG.debug("Reading attributes for gre tunnel: {}", key.getName());
87         // Dump just a single
88         final GreTunnelDump request = new GreTunnelDump();
89         request.swIfIndex = index;
90
91         final CompletionStage<GreTunnelDetailsReplyDump> swInterfaceGreDetailsReplyDumpCompletionStage =
92                 getFutureJVpp().greTunnelDump(request);
93         final GreTunnelDetailsReplyDump reply =
94                 getReplyForRead(swInterfaceGreDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
95
96         // VPP keeps gre tunnel interfaces even after they were deleted (optimization)
97         // However there ar no longer any gre tunnel specific fields assigned to it and this call
98         // returns nothing
99         if (reply == null || reply.greTunnelDetails == null || reply.greTunnelDetails.isEmpty()) {
100             LOG.debug(
101                     "Gre 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.greTunnelDetails.size() == 1,
107                 "Unexpected number of returned gre tunnels: {} for tunnel: {}", reply.greTunnelDetails,
108                 key.getName());
109         LOG.trace("Gre tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
110
111         final GreTunnelDetails swInterfaceGreDetails = reply.greTunnelDetails.get(0);
112         if (swInterfaceGreDetails.isIpv6 == 1) {
113             final Ipv6Address dstIpv6 =
114                     new Ipv6Address(parseAddress(swInterfaceGreDetails.dstAddress).getHostAddress());
115             builder.setDst(new IpAddress(dstIpv6));
116             final Ipv6Address srcIpv6 =
117                     new Ipv6Address(parseAddress(swInterfaceGreDetails.srcAddress).getHostAddress());
118             builder.setSrc(new IpAddress(srcIpv6));
119         } else {
120             final byte[] dstBytes = Arrays.copyOfRange(swInterfaceGreDetails.dstAddress, 0, 4);
121             final Ipv4Address dstIpv4 = new Ipv4Address(parseAddress(dstBytes).getHostAddress());
122             builder.setDst(new IpAddress(dstIpv4));
123             final byte[] srcBytes = Arrays.copyOfRange(swInterfaceGreDetails.srcAddress, 0, 4);
124             final Ipv4Address srcIpv4 = new Ipv4Address(parseAddress(srcBytes).getHostAddress());
125             builder.setSrc(new IpAddress(srcIpv4));
126         }
127         builder.setOuterFibId((long) swInterfaceGreDetails.outerFibId);
128         LOG.debug("Gre 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
140     @Override
141     public Initialized<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Gre> init(
142             @Nonnull final InstanceIdentifier<Gre> id, @Nonnull final Gre readValue, @Nonnull final ReadContext ctx) {
143         return Initialized.create(getCfgId(id),
144                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.GreBuilder()
145                         .setDst(readValue.getDst())
146                         .setSrc(readValue.getSrc())
147                         .setOuterFibId(readValue.getOuterFibId())
148                         .build());
149     }
150
151     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Gre> getCfgId(
152             final InstanceIdentifier<Gre> id) {
153         return InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
154                 .augmentation(VppInterfaceAugmentation.class)
155                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Gre.class);
156     }
157 }