2 * Copyright (c) 2016 Cisco 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.v3po.translate.v3po.interfacesstate;
19 import static com.google.common.base.Preconditions.checkState;
21 import io.fd.honeycomb.v3po.translate.read.ReadContext;
22 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
23 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.Arrays;
30 import java.util.concurrent.CompletionStage;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeVni;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanGpe;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanGpeBuilder;
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.dto.VxlanGpeTunnelDetails;
46 import org.openvpp.jvpp.dto.VxlanGpeTunnelDetailsReplyDump;
47 import org.openvpp.jvpp.dto.VxlanGpeTunnelDump;
48 import org.openvpp.jvpp.future.FutureJVpp;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class VxlanGpeCustomizer extends FutureJVppCustomizer
53 implements ChildReaderCustomizer<VxlanGpe, VxlanGpeBuilder> {
55 private static final Logger LOG = LoggerFactory.getLogger(VxlanGpeCustomizer.class);
56 private NamingContext interfaceContext;
58 public VxlanGpeCustomizer(@Nonnull final FutureJVpp jvpp, @Nonnull final NamingContext interfaceContext) {
60 this.interfaceContext = interfaceContext;
64 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
65 @Nonnull VxlanGpe readValue) {
66 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVxlanGpe(readValue);
71 public VxlanGpeBuilder getBuilder(@Nonnull InstanceIdentifier<VxlanGpe> id) {
72 return new VxlanGpeBuilder();
76 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VxlanGpe> id,
77 @Nonnull final VxlanGpeBuilder builder,
78 @Nonnull final ReadContext ctx) throws ReadFailedException {
79 final InterfaceKey key = id.firstKeyOf(Interface.class);
80 // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
81 // to fill in the context with initial ifc mapping
82 final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
83 if (!InterfaceUtils.isInterfaceOfType(ctx.getModificationCache(), index, VxlanGpeTunnel.class)) {
87 LOG.debug("Reading attributes for VxlanGpe tunnel: {}", key.getName());
89 final VxlanGpeTunnelDump request = new VxlanGpeTunnelDump();
90 request.swIfIndex = index;
92 final CompletionStage<VxlanGpeTunnelDetailsReplyDump> swInterfaceVxlanGpeDetailsReplyDumpCompletionStage =
93 getFutureJVpp().vxlanGpeTunnelDump(request);
94 final VxlanGpeTunnelDetailsReplyDump reply =
95 TranslateUtils.getReply(swInterfaceVxlanGpeDetailsReplyDumpCompletionStage.toCompletableFuture());
97 // VPP keeps VxlanGpe tunnel interfaces even after they were deleted (optimization)
98 // However there are no longer any VxlanGpe tunnel specific fields assigned to it and this call
100 if (reply == null || reply.vxlanGpeTunnelDetails == null || reply.vxlanGpeTunnelDetails.isEmpty()) {
102 "VxlanGpe tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
103 "after delete", key.getName(), index);
107 checkState(reply.vxlanGpeTunnelDetails.size() == 1,
108 "Unexpected number of returned VxlanGpe tunnels: {} for tunnel: {}", reply.vxlanGpeTunnelDetails, key.getName());
109 LOG.trace("VxlanGpe tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
111 final VxlanGpeTunnelDetails swInterfaceVxlanGpeDetails = reply.vxlanGpeTunnelDetails.get(0);
112 if (swInterfaceVxlanGpeDetails.isIpv6 == 1) {
113 final Ipv6Address remote6 =
114 new Ipv6Address(parseAddress(swInterfaceVxlanGpeDetails.remote).getHostAddress());
115 builder.setRemote(new IpAddress(remote6));
116 final Ipv6Address local6 =
117 new Ipv6Address(parseAddress(swInterfaceVxlanGpeDetails.local).getHostAddress());
118 builder.setLocal(new IpAddress(local6));
120 final byte[] dstBytes = Arrays.copyOfRange(swInterfaceVxlanGpeDetails.remote, 0, 4);
121 final Ipv4Address remote4 = new Ipv4Address(parseAddress(dstBytes).getHostAddress());
122 builder.setRemote(new IpAddress(remote4));
123 final byte[] srcBytes = Arrays.copyOfRange(swInterfaceVxlanGpeDetails.local, 0, 4);
124 final Ipv4Address local4 = new Ipv4Address(parseAddress(srcBytes).getHostAddress());
125 builder.setLocal(new IpAddress(local4));
127 builder.setVni(new VxlanGpeVni((long) swInterfaceVxlanGpeDetails.vni));
128 builder.setEncapVrfId((long) swInterfaceVxlanGpeDetails.encapVrfId);
129 builder.setDecapVrfId((long) swInterfaceVxlanGpeDetails.decapVrfId);
130 LOG.debug("VxlanGpe tunnel: {}, id: {} attributes read as: {}", key.getName(), index, builder);
134 private static InetAddress parseAddress(@Nonnull final byte[] addr) {
136 return InetAddress.getByAddress(addr);
137 } catch (UnknownHostException e) {
138 throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);