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;
20 import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.isInterfaceOfType;
22 import io.fd.honeycomb.v3po.translate.read.ReadContext;
23 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
24 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
27 import io.fd.honeycomb.v3po.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.VxlanGpeNextProtocol;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeVni;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanGpe;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VxlanGpeBuilder;
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.openvpp.jvpp.VppBaseCallException;
48 import org.openvpp.jvpp.dto.VxlanGpeTunnelDetails;
49 import org.openvpp.jvpp.dto.VxlanGpeTunnelDetailsReplyDump;
50 import org.openvpp.jvpp.dto.VxlanGpeTunnelDump;
51 import org.openvpp.jvpp.future.FutureJVpp;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class VxlanGpeCustomizer extends FutureJVppCustomizer
56 implements ChildReaderCustomizer<VxlanGpe, VxlanGpeBuilder> {
58 private static final Logger LOG = LoggerFactory.getLogger(VxlanGpeCustomizer.class);
59 private NamingContext interfaceContext;
61 public VxlanGpeCustomizer(@Nonnull final FutureJVpp jvpp, @Nonnull final NamingContext interfaceContext) {
63 this.interfaceContext = interfaceContext;
67 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
68 @Nonnull VxlanGpe readValue) {
69 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVxlanGpe(readValue);
74 public VxlanGpeBuilder getBuilder(@Nonnull InstanceIdentifier<VxlanGpe> id) {
75 return new VxlanGpeBuilder();
79 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VxlanGpe> id,
80 @Nonnull final VxlanGpeBuilder builder,
81 @Nonnull final ReadContext ctx) throws ReadFailedException {
83 final InterfaceKey key = id.firstKeyOf(Interface.class);
84 final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
85 if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index, VxlanGpeTunnel.class)) {
89 LOG.debug("Reading attributes for VxlanGpe tunnel: {}", key.getName());
91 final VxlanGpeTunnelDump request = new VxlanGpeTunnelDump();
92 request.swIfIndex = index;
94 final CompletionStage<VxlanGpeTunnelDetailsReplyDump> swInterfaceVxlanGpeDetailsReplyDumpCompletionStage =
95 getFutureJVpp().vxlanGpeTunnelDump(request);
96 final VxlanGpeTunnelDetailsReplyDump reply =
97 TranslateUtils.getReplyForRead(swInterfaceVxlanGpeDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
99 // VPP keeps VxlanGpe tunnel interfaces even after they were deleted (optimization)
100 // However there are no longer any VxlanGpe tunnel specific fields assigned to it and this call
102 if (reply == null || reply.vxlanGpeTunnelDetails == null || reply.vxlanGpeTunnelDetails.isEmpty()) {
104 "VxlanGpe tunnel {}, id {} has no attributes assigned in VPP. Probably is a leftover interface placeholder" +
105 "after delete", key.getName(), index);
109 checkState(reply.vxlanGpeTunnelDetails.size() == 1,
110 "Unexpected number of returned VxlanGpe tunnels: {} for tunnel: {}", reply.vxlanGpeTunnelDetails, key.getName());
111 LOG.trace("VxlanGpe tunnel: {} attributes returned from VPP: {}", key.getName(), reply);
113 final VxlanGpeTunnelDetails swInterfaceVxlanGpeDetails = reply.vxlanGpeTunnelDetails.get(0);
114 if (swInterfaceVxlanGpeDetails.isIpv6 == 1) {
115 final Ipv6Address remote6 =
116 new Ipv6Address(parseAddress(swInterfaceVxlanGpeDetails.remote).getHostAddress());
117 builder.setRemote(new IpAddress(remote6));
118 final Ipv6Address local6 =
119 new Ipv6Address(parseAddress(swInterfaceVxlanGpeDetails.local).getHostAddress());
120 builder.setLocal(new IpAddress(local6));
122 final byte[] dstBytes = Arrays.copyOfRange(swInterfaceVxlanGpeDetails.remote, 0, 4);
123 final Ipv4Address remote4 = new Ipv4Address(parseAddress(dstBytes).getHostAddress());
124 builder.setRemote(new IpAddress(remote4));
125 final byte[] srcBytes = Arrays.copyOfRange(swInterfaceVxlanGpeDetails.local, 0, 4);
126 final Ipv4Address local4 = new Ipv4Address(parseAddress(srcBytes).getHostAddress());
127 builder.setLocal(new IpAddress(local4));
129 builder.setVni(new VxlanGpeVni((long) swInterfaceVxlanGpeDetails.vni));
130 builder.setNextProtocol(VxlanGpeNextProtocol.forValue(swInterfaceVxlanGpeDetails.protocol));
131 builder.setEncapVrfId((long) swInterfaceVxlanGpeDetails.encapVrfId);
132 builder.setDecapVrfId((long) swInterfaceVxlanGpeDetails.decapVrfId);
133 LOG.debug("VxlanGpe tunnel: {}, id: {} attributes read as: {}", key.getName(), index, builder);
134 } catch (VppBaseCallException e) {
135 LOG.warn("Failed to readCurrentAttributes for: {}", id);
136 throw new ReadFailedException( id, e );
141 private static InetAddress parseAddress(@Nonnull final byte[] addr) {
143 return InetAddress.getByAddress(addr);
144 } catch (UnknownHostException e) {
145 throw new IllegalArgumentException("Cannot create InetAddress from " + Arrays.toString(addr), e);