51d4022d7f42e80ab1534f401ccbd157ddf008aa
[hc2vpp.git] /
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.v3po.translate.v3po.interfacesstate;
18
19 import io.fd.honeycomb.v3po.translate.Context;
20 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
21 import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer;
22 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
24 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.CompletableFuture;
30 import java.util.stream.Collectors;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.openvpp.jvpp.dto.SwInterfaceDetails;
42 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
43 import org.openvpp.jvpp.dto.SwInterfaceDump;
44 import org.openvpp.jvpp.future.FutureJVpp;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Customizer for reading ietf-interfaces:interfaces-state/interface
50  */
51 public class InterfaceCustomizer extends FutureJVppCustomizer
52         implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
53
54     private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
55     public static final String DUMPED_IFCS_CONTEXT_KEY = InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
56
57     private final NamingContext interfaceContext;
58
59     public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
60         super(jvpp);
61         this.interfaceContext = interfaceContext;
62     }
63
64     @Nonnull
65     @Override
66     public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
67         return new InterfaceBuilder();
68     }
69
70     @Override
71     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
72                                       @Nonnull Context ctx) throws ReadFailedException {
73         LOG.debug("Reading attributes for interface: {}", id);
74         final InterfaceKey key = id.firstKeyOf(id.getTargetType());
75
76         // Pass cached details from getAllIds to getDetails to avoid additional dumps
77         final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), key,
78             interfaceContext.getIndex(key.getName()), ctx);
79         LOG.debug("Interface details for interface: {}, details: {}", key.getName(), iface);
80
81         builder.setName(key.getName());
82         builder.setType(InterfaceUtils.getInterfaceType(new String(iface.interfaceName).intern()));
83         builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
84         builder.setAdminStatus(1 == iface.adminUpDown ? AdminStatus.Up : AdminStatus.Down);
85         builder.setOperStatus(1 == iface.linkUpDown ? OperStatus.Up : OperStatus.Down);
86         if (0 != iface.linkSpeed) {
87             builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
88         }
89         if (iface.l2AddressLength == 6) {
90             builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
91         }
92         LOG.trace("Base attributes read for interface: {} as: {}", key.getName(), builder);
93     }
94
95     @Nonnull
96     @SuppressWarnings("unchecked")
97     public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(final @Nonnull Context ctx) {
98         return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
99             ? new HashMap<>() // allow customizers to update the cache
100             : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
101     }
102
103     @Nonnull
104     @Override
105     public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
106                                         @Nonnull final Context context) throws ReadFailedException {
107         LOG.trace("Dumping all interfaces to get all IDs");
108
109         final SwInterfaceDump request = new SwInterfaceDump();
110         request.nameFilter = "".getBytes();
111         request.nameFilterValid = 0;
112
113         final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
114                 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
115         final SwInterfaceDetailsReplyDump ifaces = V3poUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture);
116
117         if (null == ifaces || null == ifaces.swInterfaceDetails) {
118             LOG.debug("No interfaces found in VPP");
119             return Collections.emptyList();
120         }
121
122         // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
123         context.put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
124             .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
125
126         final List<InterfaceKey> interfacesKeys = ifaces.swInterfaceDetails.stream()
127             .filter(elt -> elt != null)
128             .map((elt) -> {
129                 // Store interface name from VPP in context if not yet present
130                 if (!interfaceContext.containsName(elt.swIfIndex)) {
131                     interfaceContext.addName(elt.swIfIndex, V3poUtils.toString(elt.interfaceName));
132                 }
133                 LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
134                     interfaceContext.getName(elt.swIfIndex), elt.interfaceName, elt.swIfIndex);
135
136                 return new InterfaceKey(interfaceContext.getName(elt.swIfIndex));
137             })
138             .collect(Collectors.toList());
139
140         LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
141         return interfacesKeys;
142     }
143
144     @Override
145     public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
146                       @Nonnull final  List<Interface> readData) {
147         ((InterfacesStateBuilder) builder).setInterface(readData);
148     }
149
150 }