HONEYCOMB-204 exclude deleted interfaces from operational data
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / InterfaceCustomizer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
18
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.ListReaderCustomizer;
24 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
25 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.v3po.util.NamingContext;
27 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.stream.Collectors;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.VppBaseCallException;
46 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
47 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
48 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
49 import org.openvpp.jvpp.core.future.FutureJVppCore;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * Customizer for reading ietf-interfaces:interfaces-state/interface.
55  */
56 public class InterfaceCustomizer extends FutureJVppCustomizer
57     implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
58
59     private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
60     public static final String DUMPED_IFCS_CONTEXT_KEY =
61         InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
62
63     private final NamingContext interfaceNamingContext;
64     private final DisabledInterfacesManager interfaceDisableContext;
65
66     public InterfaceCustomizer(@Nonnull final FutureJVppCore jvpp,
67                                @Nonnull final NamingContext interfaceNamingContext,
68                                @Nonnull final DisabledInterfacesManager interfaceDisableContext) {
69         super(jvpp);
70         this.interfaceNamingContext = interfaceNamingContext;
71         this.interfaceDisableContext = interfaceDisableContext;
72     }
73
74     @Nonnull
75     @Override
76     public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
77         return new InterfaceBuilder();
78     }
79
80     @Override
81     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
82                                       @Nonnull ReadContext ctx) throws ReadFailedException {
83         LOG.debug("Reading attributes for interface: {}", id);
84         final String ifaceName = id.firstKeyOf(id.getTargetType()).getName();
85
86         final int index = interfaceNamingContext.getIndex(ifaceName, ctx.getMappingContext());
87
88         // Ignore disabled interface (such as deleted VXLAN tunnels)
89         if (interfaceDisableContext.isInterfaceDisabled(index, ctx.getMappingContext())) {
90             LOG.debug("Skipping disabled interface: {}", id);
91             return;
92         }
93
94         // Pass cached details from getAllIds to getDetails to avoid additional dumps
95         final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), id, ifaceName,
96                 index, ctx.getModificationCache());
97         LOG.debug("Interface details for interface: {}, details: {}", ifaceName, iface);
98
99         if (!isRegularInterface(iface)) {
100             LOG.debug("Interface: {} is a sub-interface. Ignoring read request.", ifaceName);
101             return;
102         }
103
104         builder.setName(ifaceName);
105         builder.setType(InterfaceUtils.getInterfaceType(new String(iface.interfaceName).intern()));
106         builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
107         builder.setAdminStatus(1 == iface.adminUpDown
108             ? AdminStatus.Up
109             : AdminStatus.Down);
110         builder.setOperStatus(1 == iface.linkUpDown
111             ? OperStatus.Up
112             : OperStatus.Down);
113         if (0 != iface.linkSpeed) {
114             builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
115         }
116         if (iface.l2AddressLength == 6) {
117             builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
118         }
119         LOG.trace("Base attributes read for interface: {} as: {}", ifaceName, builder);
120     }
121
122     @Nonnull
123     @SuppressWarnings("unchecked")
124     public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(@Nonnull final ModificationCache ctx) {
125         return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
126             ? new HashMap<>()
127             // allow customizers to update the cache
128             : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
129     }
130
131     @Nonnull
132     @Override
133     public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
134                                         @Nonnull final ReadContext context) throws ReadFailedException {
135         try {
136             final List<InterfaceKey> interfacesKeys;
137             LOG.trace("Dumping all interfaces to get all IDs");
138
139             final SwInterfaceDump request = new SwInterfaceDump();
140             request.nameFilter = "".getBytes();
141             request.nameFilterValid = 0;
142
143             final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
144                 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
145             final SwInterfaceDetailsReplyDump ifaces =
146                 TranslateUtils.getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
147
148             if (null == ifaces || null == ifaces.swInterfaceDetails) {
149                 LOG.debug("No interfaces for :{} found in VPP", id);
150                 return Collections.emptyList();
151             }
152
153             // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
154             context.getModificationCache().put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
155                 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
156
157             final MappingContext mappingCtx = context.getMappingContext();
158             final Set<Integer> interfacesIdxs = ifaces.swInterfaceDetails.stream()
159                     .filter(elt -> elt != null)
160                     // Filter out disabled interfaces, dont read them
161                     // This also prevents child readers in being invoked such as vxlan (which relies on disabling interfaces)
162                     .filter(elt -> !interfaceDisableContext
163                             .isInterfaceDisabled(elt.swIfIndex, mappingCtx))
164                     .map((elt) -> {
165                         // Store interface name from VPP in context if not yet present
166                         if (!interfaceNamingContext.containsName(elt.swIfIndex, mappingCtx)) {
167                             interfaceNamingContext.addName(elt.swIfIndex, TranslateUtils.toString(elt.interfaceName),
168                                     mappingCtx);
169                         }
170                         LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
171                                 interfaceNamingContext.getName(elt.swIfIndex, mappingCtx),
172                                 elt.interfaceName,
173                                 elt.swIfIndex);
174
175                         return elt;
176                     })
177                     // filter out sub-interfaces
178                     .filter(InterfaceCustomizer::isRegularInterface)
179                     .map(elt -> elt.swIfIndex)
180                     .collect(Collectors.toSet());
181
182             // Clean disabled interfaces list
183             interfaceDisableContext.getDisabledInterfaces(mappingCtx).stream()
184                     // Find indices not currently in VPP
185                     .filter(interfacesIdxs::contains)
186                     // Remove from disabled list ... not disabled if not existing
187                     .forEach(idx -> interfaceDisableContext.removeDisabledInterface(idx, mappingCtx));
188
189             // Transform indices to keys
190             interfacesKeys = interfacesIdxs.stream()
191                     .map(index -> new InterfaceKey(interfaceNamingContext.getName(index, context.getMappingContext())))
192                     .collect(Collectors.toList());
193
194             LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
195             return interfacesKeys;
196         } catch (VppBaseCallException e) {
197             LOG.warn("getAllIds for id :{} failed with exception ", id, e);
198             throw new ReadFailedException(id, e);
199         }
200     }
201
202     private static boolean isRegularInterface(final SwInterfaceDetails iface) {
203         return iface.subId == 0;
204     }
205
206     @Override
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);
210     }
211 }