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.MappingContext;
20 import io.fd.honeycomb.translate.ModificationCache;
21 import io.fd.honeycomb.translate.read.ReadContext;
22 import io.fd.honeycomb.translate.read.ReadFailedException;
23 import io.fd.honeycomb.translate.spi.read.Initialized;
24 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
25 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
26 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
27 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
30 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
31 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
32 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.stream.Collectors;
40 import javax.annotation.Nonnull;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Customizer for reading ietf-interfaces:interfaces-state/interface.
57 public class InterfaceCustomizer extends FutureJVppCustomizer
58 implements InitializingListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder>, ByteDataTranslator,
59 InterfaceDataTranslator {
61 public static final String DUMPED_IFCS_CONTEXT_KEY =
62 InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
63 private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
64 private final NamingContext interfaceNamingContext;
65 private final DisabledInterfacesManager interfaceDisableContext;
67 public InterfaceCustomizer(@Nonnull final FutureJVppCore jvpp,
68 @Nonnull final NamingContext interfaceNamingContext,
69 @Nonnull final DisabledInterfacesManager interfaceDisableContext) {
71 this.interfaceNamingContext = interfaceNamingContext;
72 this.interfaceDisableContext = interfaceDisableContext;
75 public static void cacheInterfaceDump(final @Nonnull ReadContext context, final SwInterfaceDetailsReplyDump ifaces) {
76 context.getModificationCache().put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
77 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
81 @SuppressWarnings("unchecked")
82 public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(@Nonnull final ModificationCache ctx) {
83 return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
85 // allow customizers to update the cache
86 : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
89 private static boolean isRegularInterface(final SwInterfaceDetails iface) {
90 return iface.subId == 0;
95 public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
96 return new InterfaceBuilder();
100 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
101 @Nonnull ReadContext ctx) throws ReadFailedException {
102 LOG.debug("Reading attributes for interface: {}", id);
103 final String ifaceName = id.firstKeyOf(id.getTargetType()).getName();
105 final int index = interfaceNamingContext.getIndex(ifaceName, ctx.getMappingContext());
107 // Ignore disabled interface (such as deleted VXLAN tunnels)
108 if (interfaceDisableContext.isInterfaceDisabled(index, ctx.getMappingContext())) {
109 LOG.debug("Skipping disabled interface: {}", id);
113 // Pass cached details from getAllIds to getDetails to avoid additional dumps
114 final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, ifaceName,
115 index, ctx.getModificationCache(), LOG);
116 LOG.debug("Interface details for interface: {}, details: {}", ifaceName, iface);
118 if (!isRegularInterface(iface)) {
119 LOG.debug("Interface: {} is a sub-interface. Ignoring read request.", ifaceName);
123 builder.setName(ifaceName);
124 builder.setType(getInterfaceType(new String(iface.interfaceName).intern()));
125 builder.setIfIndex(vppIfIndexToYang(iface.swIfIndex));
126 builder.setAdminStatus(1 == iface.adminUpDown
129 builder.setOperStatus(1 == iface.linkUpDown
132 if (0 != iface.linkSpeed) {
133 builder.setSpeed(vppInterfaceSpeedToYang(iface.linkSpeed));
135 if (iface.l2AddressLength == 6) {
136 builder.setPhysAddress(new PhysAddress(vppPhysAddrToYang(iface.l2Address)));
138 LOG.trace("Base attributes read for interface: {} as: {}", ifaceName, builder);
143 public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
144 @Nonnull final ReadContext context) throws ReadFailedException {
145 final List<InterfaceKey> interfacesKeys;
146 LOG.trace("Dumping all interfaces to get all IDs");
148 final SwInterfaceDump request = new SwInterfaceDump();
149 request.nameFilter = "".getBytes();
150 request.nameFilterValid = 0;
152 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
153 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
154 final SwInterfaceDetailsReplyDump ifaces =
155 getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
157 if (null == ifaces || null == ifaces.swInterfaceDetails) {
158 LOG.debug("No interfaces for :{} found in VPP", id);
159 return Collections.emptyList();
162 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
163 cacheInterfaceDump(context, ifaces);
165 final MappingContext mappingCtx = context.getMappingContext();
166 final Set<Integer> interfacesIdxs = ifaces.swInterfaceDetails.stream()
167 .filter(elt -> elt != null)
168 // Filter out disabled interfaces, dont read them
169 // This also prevents child readers in being invoked such as vxlan (which relies on disabling interfaces)
170 .filter(elt -> !interfaceDisableContext
171 .isInterfaceDisabled(elt.swIfIndex, mappingCtx))
173 // Store interface name from VPP in context if not yet present
174 if (!interfaceNamingContext.containsName(elt.swIfIndex, mappingCtx)) {
175 interfaceNamingContext.addName(elt.swIfIndex, toString(elt.interfaceName),
178 LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
179 interfaceNamingContext.getName(elt.swIfIndex, mappingCtx),
185 // filter out sub-interfaces
186 .filter(InterfaceCustomizer::isRegularInterface)
187 .map(elt -> elt.swIfIndex)
188 .collect(Collectors.toSet());
190 // Clean disabled interfaces list
191 interfaceDisableContext.getDisabledInterfaces(mappingCtx).stream()
192 // Find indices not currently in VPP
193 .filter(interfacesIdxs::contains)
194 // Remove from disabled list ... not disabled if not existing
195 .forEach(idx -> interfaceDisableContext.removeDisabledInterface(idx, mappingCtx));
197 // Transform indices to keys
198 interfacesKeys = interfacesIdxs.stream()
199 .map(index -> new InterfaceKey(interfaceNamingContext.getName(index, context.getMappingContext())))
200 .collect(Collectors.toList());
202 LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
203 return interfacesKeys;
207 public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
208 @Nonnull final List<Interface> readData) {
209 ((InterfacesStateBuilder) builder).setInterface(readData);
213 public Initialized<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> init(
214 @Nonnull final InstanceIdentifier<Interface> id, @Nonnull final Interface readValue, @Nonnull final ReadContext ctx) {
215 return Initialized.create(getCfgId(id),
216 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder()
217 .setName(readValue.getName())
218 .setType(readValue.getType())
219 .setEnabled(AdminStatus.Up.equals(readValue.getAdminStatus()))
220 // Not present in interfaces-state
221 // .setLinkUpDownTrapEnable()
225 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> getCfgId(
226 final InstanceIdentifier<Interface> id) {
227 return InstanceIdentifier.create(Interfaces.class).child(
228 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class,
229 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey(
230 id.firstKeyOf(Interface.class).getName()));