2 * Copyright (c) 2016 Intel 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.translate.v3po.interfacesstate;
19 import static com.google.common.base.Preconditions.checkState;
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;
53 public class GreCustomizer extends FutureJVppCustomizer
54 implements InitializingReaderCustomizer<Gre, GreBuilder>, InterfaceDataTranslator {
56 private static final Logger LOG = LoggerFactory.getLogger(GreCustomizer.class);
57 private NamingContext interfaceContext;
59 public GreCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
61 this.interfaceContext = interfaceContext;
65 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
66 @Nonnull Gre readValue) {
67 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setGre(readValue);
72 public GreBuilder getBuilder(@Nonnull InstanceIdentifier<Gre> id) {
73 return new GreBuilder();
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)) {
86 LOG.debug("Reading attributes for gre tunnel: {}", key.getName());
88 final GreTunnelDump request = new GreTunnelDump();
89 request.swIfIndex = index;
91 final CompletionStage<GreTunnelDetailsReplyDump> swInterfaceGreDetailsReplyDumpCompletionStage =
92 getFutureJVpp().greTunnelDump(request);
93 final GreTunnelDetailsReplyDump reply =
94 getReplyForRead(swInterfaceGreDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
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
99 if (reply == null || reply.greTunnelDetails == null || reply.greTunnelDetails.isEmpty()) {
101 "Gre tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
102 "after delete", key.getName(), index);
106 checkState(reply.greTunnelDetails.size() == 1,
107 "Unexpected number of returned gre tunnels: {} for tunnel: {}", reply.greTunnelDetails,
109 LOG.trace("Gre tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
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));
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));
127 builder.setOuterFibId((long) swInterfaceGreDetails.outerFibId);
128 LOG.debug("Gre 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);
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())
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);