082b6855936bd0404772fc7ad3d816931e409f93
[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.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;
29 import java.util.Map;
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;
49
50 /**
51  * Customizer for reading ietf-interfaces:interfaces-state/interface.
52  */
53 public class InterfaceCustomizer extends FutureJVppCustomizer
54     implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
55
56     private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
57     public static final String DUMPED_IFCS_CONTEXT_KEY =
58         InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
59
60     private final NamingContext interfaceContext;
61
62     public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
63         super(jvpp);
64         this.interfaceContext = interfaceContext;
65     }
66
67     @Nonnull
68     @Override
69     public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
70         return new InterfaceBuilder();
71     }
72
73     @Override
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();
78
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);
83
84         if (!isRegularInterface(iface)) {
85             LOG.debug("Interface: {} is a sub-interface. Ignoring read request.", ifaceName);
86             return;
87         }
88
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
93             ? AdminStatus.Up
94             : AdminStatus.Down);
95         builder.setOperStatus(1 == iface.linkUpDown
96             ? OperStatus.Up
97             : OperStatus.Down);
98         if (0 != iface.linkSpeed) {
99             builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
100         }
101         if (iface.l2AddressLength == 6) {
102             builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
103         }
104         LOG.trace("Base attributes read for interface: {} as: {}", ifaceName, builder);
105     }
106
107     @Nonnull
108     @SuppressWarnings("unchecked")
109     public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(@Nonnull final ModificationCache ctx) {
110         return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
111             ? new HashMap<>()
112             // allow customizers to update the cache
113             : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
114     }
115
116     @Nonnull
117     @Override
118     public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
119                                         @Nonnull final ReadContext context) throws ReadFailedException {
120         try {
121             final List<InterfaceKey> interfacesKeys;
122             LOG.trace("Dumping all interfaces to get all IDs");
123
124             final SwInterfaceDump request = new SwInterfaceDump();
125             request.nameFilter = "".getBytes();
126             request.nameFilterValid = 0;
127
128             final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
129                 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
130             final SwInterfaceDetailsReplyDump ifaces =
131                 TranslateUtils.getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
132
133             if (null == ifaces || null == ifaces.swInterfaceDetails) {
134                 LOG.debug("No interfaces for :{} found in VPP", id);
135                 return Collections.emptyList();
136             }
137
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)));
141
142             interfacesKeys = ifaces.swInterfaceDetails.stream()
143                 .filter(elt -> elt != null)
144                 .map((elt) -> {
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());
149                     }
150                     LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
151                         interfaceContext.getName(elt.swIfIndex, context.getMappingContext()), elt.interfaceName,
152                         elt.swIfIndex);
153
154                     return elt;
155                 })
156                 .filter(InterfaceCustomizer::isRegularInterface) // filter out sub-interfaces
157                 .map((elt) -> new InterfaceKey(interfaceContext.getName(elt.swIfIndex, context.getMappingContext())))
158                 .collect(Collectors.toList());
159
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);
165         }
166     }
167
168     private static boolean isRegularInterface(final SwInterfaceDetails iface) {
169         return iface.subId == 0;
170     }
171
172     @Override
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);
176     }
177
178 }