f7d473f732d71a3039d8e3d3255e1784d67c837b
[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.List;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.stream.Collectors;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.dto.SwInterfaceDetails;
41 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
42 import org.openvpp.jvpp.dto.SwInterfaceDump;
43 import org.openvpp.jvpp.future.FutureJVpp;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48 public class InterfaceCustomizer extends FutureJVppCustomizer
49         implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
50
51     private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
52     private final NamingContext interfaceContext;
53
54     public InterfaceCustomizer(@Nonnull final FutureJVpp jvpp, final NamingContext interfaceContext) {
55         super(jvpp);
56         this.interfaceContext = interfaceContext;
57     }
58
59     @Override
60     public InterfaceBuilder getBuilder(InstanceIdentifier<Interface> id) {
61         return new InterfaceBuilder();
62     }
63
64     @Override
65     public void readCurrentAttributes(InstanceIdentifier<Interface> id, InterfaceBuilder builder, Context ctx)
66             throws ReadFailedException {
67         final InterfaceKey key = id.firstKeyOf(id.getTargetType());
68
69         final SwInterfaceDetails iface;
70         try {
71             iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), key);
72         } catch (Exception e) {
73             throw new ReadFailedException(id, e);
74         }
75
76
77         builder.setName(key.getName());
78         // FIXME: report interface type based on name
79         //Tunnel.class l2vlan(802.1q) bridge (transparent bridge?)
80         builder.setType(EthernetCsmacd.class);
81         builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
82         builder.setAdminStatus(iface.adminUpDown == 1
83                 ? AdminStatus.Up
84                 : AdminStatus.Down);
85         builder.setOperStatus(1 == iface.linkUpDown
86                 ? OperStatus.Up
87                 : OperStatus.Down);
88         if (0 != iface.linkSpeed) {
89             builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
90         }
91         if (iface.l2AddressLength == 6) {
92             builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
93         }
94     }
95
96     @Nonnull
97     @Override
98     public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
99                                         @Nonnull final Context context) throws ReadFailedException {
100         final SwInterfaceDump request = new SwInterfaceDump();
101         request.nameFilter = "".getBytes();
102         request.nameFilterValid = 0;
103
104         final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
105                 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
106         final SwInterfaceDetailsReplyDump ifaces = V3poUtils.getReply(swInterfaceDetailsReplyDumpCompletableFuture);
107
108         // TODO can we get null here?
109         if (null == ifaces || null == ifaces.swInterfaceDetails) {
110             return Collections.emptyList();
111         }
112
113         return ifaces.swInterfaceDetails.stream()
114                 .filter(elt -> elt != null)
115                 .map((elt) -> {
116                     // Store interface name from VPP in context if not yet present
117                     if(!interfaceContext.containsName(elt.swIfIndex)){
118                         interfaceContext.addName(elt.swIfIndex, V3poUtils.toString(elt.interfaceName));
119                     }
120                     return new InterfaceKey(interfaceContext.getName(elt.swIfIndex));
121                 })
122                 .collect(Collectors.toList());
123     }
124
125     @Override
126     public void merge(@Nonnull final org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
127                       @Nonnull final  List<Interface> readData) {
128         ((InterfacesStateBuilder) builder).setInterface(readData);
129     }
130
131 }