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 io.fd.honeycomb.v3po.translate.ModificationCache;
20 import io.fd.honeycomb.v3po.translate.read.ReadContext;
21 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
22 import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer;
23 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.openvpp.jvpp.VppBaseCallException;
36 import org.openvpp.jvpp.dto.SwInterfaceDetails;
37 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
38 import org.openvpp.jvpp.dto.SwInterfaceDump;
39 import org.openvpp.jvpp.future.FutureJVpp;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import javax.annotation.Nonnull;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.concurrent.CompletableFuture;
49 import java.util.stream.Collectors;
52 * Customizer for reading ietf-interfaces:interfaces-state/interface.
54 public class InterfaceCustomizer extends FutureJVppCustomizer
55 implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
57 private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
58 public static final String DUMPED_IFCS_CONTEXT_KEY =
59 InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
61 private final NamingContext interfaceContext;
63 public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
65 this.interfaceContext = interfaceContext;
70 public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
71 return new InterfaceBuilder();
75 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
76 @Nonnull ReadContext ctx) throws ReadFailedException {
77 LOG.debug("Reading attributes for interface: {}", id);
78 final InterfaceKey key = id.firstKeyOf(id.getTargetType());
80 // Pass cached details from getAllIds to getDetails to avoid additional dumps
81 final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), id, key.getName(),
82 interfaceContext.getIndex(key.getName(), ctx.getMappingContext()), ctx.getModificationCache());
83 LOG.debug("Interface details for interface: {}, details: {}", key.getName(), iface);
85 builder.setName(key.getName());
86 builder.setType(InterfaceUtils.getInterfaceType(new String(iface.interfaceName).intern()));
87 builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
88 builder.setAdminStatus(1 == iface.adminUpDown
91 builder.setOperStatus(1 == iface.linkUpDown
94 if (0 != iface.linkSpeed) {
95 builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
97 if (iface.l2AddressLength == 6) {
98 builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
100 LOG.trace("Base attributes read for interface: {} as: {}", key.getName(), builder);
104 @SuppressWarnings("unchecked")
105 public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(@Nonnull final ModificationCache ctx) {
106 return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
108 // allow customizers to update the cache
109 : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
114 public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
115 @Nonnull final ReadContext context) throws ReadFailedException {
117 final List<InterfaceKey> interfacesKeys;
118 LOG.trace("Dumping all interfaces to get all IDs");
120 final SwInterfaceDump request = new SwInterfaceDump();
121 request.nameFilter = "".getBytes();
122 request.nameFilterValid = 0;
124 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
125 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
126 final SwInterfaceDetailsReplyDump ifaces =
127 TranslateUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture);
129 if (null == ifaces || null == ifaces.swInterfaceDetails) {
130 LOG.debug("No interfaces for :{} found in VPP", id);
131 return Collections.emptyList();
134 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
135 context.getModificationCache().put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
136 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
138 interfacesKeys = ifaces.swInterfaceDetails.stream()
139 .filter(elt -> elt != null)
141 // Store interface name from VPP in context if not yet present
142 if (!interfaceContext.containsName(elt.swIfIndex, context.getMappingContext())) {
143 interfaceContext.addName(elt.swIfIndex, TranslateUtils.toString(elt.interfaceName),
144 context.getMappingContext());
146 LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
147 interfaceContext.getName(elt.swIfIndex, context.getMappingContext()), elt.interfaceName,
150 return new InterfaceKey(interfaceContext.getName(elt.swIfIndex, context.getMappingContext()));
152 .collect(Collectors.toList());
154 LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
155 return interfacesKeys;
156 } catch (VppBaseCallException e) {
157 LOG.warn("getAllIds for id :{} failed with exception ", id, e);
158 throw new ReadFailedException(id, e);
163 public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
164 @Nonnull final List<Interface> readData) {
165 ((InterfacesStateBuilder) builder).setInterface(readData);