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.translate.v3po.interfacesstate;
19 import io.fd.honeycomb.translate.read.ReadContext;
20 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
21 import io.fd.honeycomb.translate.ModificationCache;
22 import io.fd.honeycomb.translate.read.ReadFailedException;
23 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.stream.Collectors;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.openvpp.jvpp.VppBaseCallException;
43 import org.openvpp.jvpp.dto.SwInterfaceDetails;
44 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
45 import org.openvpp.jvpp.dto.SwInterfaceDump;
46 import org.openvpp.jvpp.future.FutureJVpp;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Customizer for reading ietf-interfaces:interfaces-state/interface.
53 public class InterfaceCustomizer extends FutureJVppCustomizer
54 implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
56 private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
57 public static final String DUMPED_IFCS_CONTEXT_KEY =
58 InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
60 private final NamingContext interfaceContext;
62 public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
64 this.interfaceContext = interfaceContext;
69 public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
70 return new InterfaceBuilder();
74 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
75 @Nonnull ReadContext ctx) throws ReadFailedException {
76 LOG.debug("Reading attributes for interface: {}", id);
77 final String ifaceName = id.firstKeyOf(id.getTargetType()).getName();
79 // Pass cached details from getAllIds to getDetails to avoid additional dumps
80 final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), id, ifaceName,
81 interfaceContext.getIndex(ifaceName, ctx.getMappingContext()), ctx.getModificationCache());
82 LOG.debug("Interface details for interface: {}, details: {}", ifaceName, iface);
84 if (!isRegularInterface(iface)) {
85 LOG.debug("Interface: {} is a sub-interface. Ignoring read request.", ifaceName);
89 builder.setName(ifaceName);
90 builder.setType(InterfaceUtils.getInterfaceType(new String(iface.interfaceName).intern()));
91 builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
92 builder.setAdminStatus(1 == iface.adminUpDown
95 builder.setOperStatus(1 == iface.linkUpDown
98 if (0 != iface.linkSpeed) {
99 builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
101 if (iface.l2AddressLength == 6) {
102 builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
104 LOG.trace("Base attributes read for interface: {} as: {}", ifaceName, builder);
108 @SuppressWarnings("unchecked")
109 public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(@Nonnull final ModificationCache ctx) {
110 return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
112 // allow customizers to update the cache
113 : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
118 public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
119 @Nonnull final ReadContext context) throws ReadFailedException {
121 final List<InterfaceKey> interfacesKeys;
122 LOG.trace("Dumping all interfaces to get all IDs");
124 final SwInterfaceDump request = new SwInterfaceDump();
125 request.nameFilter = "".getBytes();
126 request.nameFilterValid = 0;
128 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
129 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
130 final SwInterfaceDetailsReplyDump ifaces =
131 TranslateUtils.getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
133 if (null == ifaces || null == ifaces.swInterfaceDetails) {
134 LOG.debug("No interfaces for :{} found in VPP", id);
135 return Collections.emptyList();
138 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
139 context.getModificationCache().put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
140 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
142 interfacesKeys = ifaces.swInterfaceDetails.stream()
143 .filter(elt -> elt != null)
145 // Store interface name from VPP in context if not yet present
146 if (!interfaceContext.containsName(elt.swIfIndex, context.getMappingContext())) {
147 interfaceContext.addName(elt.swIfIndex, TranslateUtils.toString(elt.interfaceName),
148 context.getMappingContext());
150 LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
151 interfaceContext.getName(elt.swIfIndex, context.getMappingContext()), elt.interfaceName,
156 .filter(InterfaceCustomizer::isRegularInterface) // filter out sub-interfaces
157 .map((elt) -> new InterfaceKey(interfaceContext.getName(elt.swIfIndex, context.getMappingContext())))
158 .collect(Collectors.toList());
160 LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
161 return interfacesKeys;
162 } catch (VppBaseCallException e) {
163 LOG.warn("getAllIds for id :{} failed with exception ", id, e);
164 throw new ReadFailedException(id, e);
168 private static boolean isRegularInterface(final SwInterfaceDetails iface) {
169 return iface.subId == 0;
173 public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
174 @Nonnull final List<Interface> readData) {
175 ((InterfacesStateBuilder) builder).setInterface(readData);