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.utils.V3poUtils;
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.dto.SwInterfaceDetails;
43 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
44 import org.openvpp.jvpp.dto.SwInterfaceDump;
45 import org.openvpp.jvpp.future.FutureJVpp;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Customizer for reading ietf-interfaces:interfaces-state/interface
52 public class InterfaceCustomizer extends FutureJVppCustomizer
53 implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
55 private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
56 public static final String DUMPED_IFCS_CONTEXT_KEY = InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
58 private final NamingContext interfaceContext;
60 public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
62 this.interfaceContext = interfaceContext;
67 public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
68 return new InterfaceBuilder();
72 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
73 @Nonnull ReadContext ctx) throws ReadFailedException {
74 LOG.debug("Reading attributes for interface: {}", id);
75 final InterfaceKey key = id.firstKeyOf(id.getTargetType());
77 // Pass cached details from getAllIds to getDetails to avoid additional dumps
78 final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), key,
79 interfaceContext.getIndex(key.getName(), ctx.getMappingContext()), ctx.getModificationCache());
80 LOG.debug("Interface details for interface: {}, details: {}", key.getName(), iface);
82 builder.setName(key.getName());
83 builder.setType(InterfaceUtils.getInterfaceType(new String(iface.interfaceName).intern()));
84 builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
85 builder.setAdminStatus(1 == iface.adminUpDown ? AdminStatus.Up : AdminStatus.Down);
86 builder.setOperStatus(1 == iface.linkUpDown ? OperStatus.Up : OperStatus.Down);
87 if (0 != iface.linkSpeed) {
88 builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
90 if (iface.l2AddressLength == 6) {
91 builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
93 LOG.trace("Base attributes read for interface: {} as: {}", key.getName(), builder);
97 @SuppressWarnings("unchecked")
98 public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(final @Nonnull ModificationCache ctx) {
99 return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
100 ? new HashMap<>() // allow customizers to update the cache
101 : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
106 public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
107 @Nonnull final ReadContext context) throws ReadFailedException {
108 LOG.trace("Dumping all interfaces to get all IDs");
110 final SwInterfaceDump request = new SwInterfaceDump();
111 request.nameFilter = "".getBytes();
112 request.nameFilterValid = 0;
114 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
115 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
116 final SwInterfaceDetailsReplyDump ifaces = V3poUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture);
118 if (null == ifaces || null == ifaces.swInterfaceDetails) {
119 LOG.debug("No interfaces found in VPP");
120 return Collections.emptyList();
123 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
124 context.getModificationCache().put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
125 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
127 final List<InterfaceKey> interfacesKeys = ifaces.swInterfaceDetails.stream()
128 .filter(elt -> elt != null)
130 // Store interface name from VPP in context if not yet present
131 if (!interfaceContext.containsName(elt.swIfIndex, context.getMappingContext())) {
132 interfaceContext.addName(elt.swIfIndex, V3poUtils.toString(elt.interfaceName), context.getMappingContext());
134 LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
135 interfaceContext.getName(elt.swIfIndex, context.getMappingContext()), elt.interfaceName, elt.swIfIndex);
137 return new InterfaceKey(interfaceContext.getName(elt.swIfIndex, context.getMappingContext()));
139 .collect(Collectors.toList());
141 LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
142 return interfacesKeys;
146 public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
147 @Nonnull final List<Interface> readData) {
148 ((InterfacesStateBuilder) builder).setInterface(readData);