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;
20 import static io.fd.honeycomb.translate.v3po.interfacesstate.InterfaceUtils.isInterfaceOfType;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.v3po.util.NamingContext;
27 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.util.Arrays;
31 import java.util.concurrent.CompletionStage;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.GreTunnel;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Gre;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.GreBuilder;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.VppBaseCallException;
46 import org.openvpp.jvpp.dto.GreTunnelDetails;
47 import org.openvpp.jvpp.dto.GreTunnelDetailsReplyDump;
48 import org.openvpp.jvpp.dto.GreTunnelDump;
49 import org.openvpp.jvpp.future.FutureJVpp;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class GreCustomizer extends FutureJVppCustomizer
54 implements ReaderCustomizer<Gre, GreBuilder> {
56 private static final Logger LOG = LoggerFactory.getLogger(GreCustomizer.class);
57 private NamingContext interfaceContext;
59 public GreCustomizer(@Nonnull final FutureJVpp 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 {
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, GreTunnel.class)) {
87 LOG.debug("Reading attributes for gre tunnel: {}", key.getName());
89 final GreTunnelDump request = new GreTunnelDump();
90 request.swIfIndex = index;
92 final CompletionStage<GreTunnelDetailsReplyDump> swInterfaceGreDetailsReplyDumpCompletionStage =
93 getFutureJVpp().greTunnelDump(request);
94 final GreTunnelDetailsReplyDump reply =
95 TranslateUtils.getReplyForRead(swInterfaceGreDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
97 // VPP keeps gre tunnel interfaces even after they were deleted (optimization)
98 // However there ar no longer any gre tunnel specific fields assigned to it and this call
100 if (reply == null || reply.greTunnelDetails == null || reply.greTunnelDetails.isEmpty()) {
102 "Gre tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
103 "after delete", key.getName(), index);
107 checkState(reply.greTunnelDetails.size() == 1,
108 "Unexpected number of returned gre tunnels: {} for tunnel: {}", reply.greTunnelDetails, key.getName());
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);
129 } catch (VppBaseCallException e) {
130 LOG.warn("Failed to readCurrentAttributes for: {}", id, e);
131 throw new ReadFailedException( id, e );
136 private static InetAddress parseAddress(@Nonnull final byte[] addr) {
138 return InetAddress.getByAddress(addr);
139 } catch (UnknownHostException e) {
140 throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);