18079e6e4582e6f318375c0008b2484c235c9d03
[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 =
59             InterfaceCustomizer.class.getName() + "dumpedInterfacesDuringGetAllIds";
60
61     private final NamingContext interfaceContext;
62
63     public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
64         super(jvpp);
65         this.interfaceContext = interfaceContext;
66     }
67
68     @Nonnull
69     @Override
70     public InterfaceBuilder getBuilder(@Nonnull InstanceIdentifier<Interface> id) {
71         return new InterfaceBuilder();
72     }
73
74     @Override
75     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Interface> id, @Nonnull InterfaceBuilder builder,
76                                       @Nonnull ReadContext ctx) throws ReadFailedException {
77         LOG.debug("Reading attributes for interface: {}", id);
78         final InterfaceKey key = id.firstKeyOf(id.getTargetType());
79
80         // Pass cached details from getAllIds to getDetails to avoid additional dumps
81         final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), id, key.getName(),
82                 interfaceContext.getIndex(key.getName(), ctx.getMappingContext()), ctx.getModificationCache());
83         LOG.debug("Interface details for interface: {}, details: {}", key.getName(), iface);
84
85         builder.setName(key.getName());
86         builder.setType(InterfaceUtils.getInterfaceType(new String(iface.interfaceName).intern()));
87         builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
88         builder.setAdminStatus(1 == iface.adminUpDown
89                 ? AdminStatus.Up
90                 : AdminStatus.Down);
91         builder.setOperStatus(1 == iface.linkUpDown
92                 ? OperStatus.Up
93                 : OperStatus.Down);
94         if (0 != iface.linkSpeed) {
95             builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
96         }
97         if (iface.l2AddressLength == 6) {
98             builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
99         }
100         LOG.trace("Base attributes read for interface: {} as: {}", key.getName(), builder);
101     }
102
103     @Nonnull
104     @SuppressWarnings("unchecked")
105     public static Map<Integer, SwInterfaceDetails> getCachedInterfaceDump(@Nonnull final ModificationCache ctx) {
106         return ctx.get(DUMPED_IFCS_CONTEXT_KEY) == null
107                 ? new HashMap<>()
108                 // allow customizers to update the cache
109                 : (Map<Integer, SwInterfaceDetails>) ctx.get(DUMPED_IFCS_CONTEXT_KEY);
110     }
111
112     @Nonnull
113     @Override
114     public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
115                                         @Nonnull final ReadContext context) throws ReadFailedException {
116         try {
117             final List<InterfaceKey> interfacesKeys;
118             LOG.trace("Dumping all interfaces to get all IDs");
119
120             final SwInterfaceDump request = new SwInterfaceDump();
121             request.nameFilter = "".getBytes();
122             request.nameFilterValid = 0;
123
124             final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
125                     getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
126             final SwInterfaceDetailsReplyDump ifaces =
127                     TranslateUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture);
128
129             if (null == ifaces || null == ifaces.swInterfaceDetails) {
130                 LOG.debug("No interfaces for :{} found in VPP", id);
131                 return Collections.emptyList();
132             }
133
134             // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
135             context.getModificationCache().put(DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
136                     .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
137
138             interfacesKeys = ifaces.swInterfaceDetails.stream()
139                     .filter(elt -> elt != null)
140                     .map((elt) -> {
141                         // Store interface name from VPP in context if not yet present
142                         if (!interfaceContext.containsName(elt.swIfIndex, context.getMappingContext())) {
143                             interfaceContext.addName(elt.swIfIndex, TranslateUtils.toString(elt.interfaceName),
144                                     context.getMappingContext());
145                         }
146                         LOG.trace("Interface with name: {}, VPP name: {} and index: {} found in VPP",
147                                 interfaceContext.getName(elt.swIfIndex, context.getMappingContext()), elt.interfaceName,
148                                 elt.swIfIndex);
149
150                         return new InterfaceKey(interfaceContext.getName(elt.swIfIndex, context.getMappingContext()));
151                     })
152                     .collect(Collectors.toList());
153
154             LOG.debug("Interfaces found in VPP: {}", interfacesKeys);
155             return interfacesKeys;
156         } catch (VppBaseCallException e) {
157             LOG.warn("getAllIds for id :{} failed with exception ", id, e);
158             throw new ReadFailedException(id, e);
159         }
160     }
161
162     @Override
163     public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
164                       @Nonnull final List<Interface> readData) {
165         ((InterfacesStateBuilder) builder).setInterface(readData);
166     }
167
168 }