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