718cef1b5b53dacaa262441c7dafd82171a548a1
[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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.openvpp.vppjapi.vppInterfaceDetails;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39
40 public class InterfaceCustomizer extends io.fd.honeycomb.v3po.translate.v3po.util.VppApiCustomizer
41         implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
44
45     public InterfaceCustomizer(org.openvpp.vppjapi.vppApi vppApi) {
46         super(vppApi);
47     }
48
49     @Override
50     public InterfaceBuilder getBuilder(InstanceIdentifier<Interface> id) {
51         return new InterfaceBuilder();
52     }
53
54     @Override
55     public void readCurrentAttributes(InstanceIdentifier<Interface> id, InterfaceBuilder builder, Context ctx)
56             throws ReadFailedException {
57         vppInterfaceDetails[] ifaces;
58
59         final InterfaceKey key = id.firstKeyOf(id.getTargetType());
60         // Extract one interface detail from VPP
61         ifaces = getVppApi().swInterfaceDump((byte) 1, key.getName().getBytes());
62         if (null == ifaces) {
63             LOG.warn("VPP returned null instead of interface by key {}", key.getName().getBytes());
64             return;
65         }
66
67         if (1 != ifaces.length) {
68             LOG.error("Failed to extract interface {} details from VPP", key.getName());
69             return;
70         }
71
72         final vppInterfaceDetails iface = ifaces[0];
73
74         builder.setName(iface.interfaceName);
75         // FIXME: report interface type based on name
76         //Tunnel.class l2vlan(802.1q) bridge (transparent bridge?)
77         builder.setType(EthernetCsmacd.class);
78         builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.ifIndex));
79         builder.setAdminStatus(iface.adminUp == 1 ? AdminStatus.Up : AdminStatus.Down);
80         builder.setOperStatus(1 == iface.linkUp ? OperStatus.Up : OperStatus.Down);
81         if (0 != iface.linkSpeed) {
82             builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
83         }
84         if (iface.physAddr.length == 6) {
85             builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.physAddr)));
86         }
87     }
88
89     @Override
90     public List<InterfaceKey> getAllIds(InstanceIdentifier<Interface> id, Context context) {
91         vppInterfaceDetails[] ifaces;
92         final ArrayList<InterfaceKey> interfaceKeys = new ArrayList<>();
93
94         ifaces = getVppApi().swInterfaceDump((byte) 0, "".getBytes());
95         if (null != ifaces) {
96             for (vppInterfaceDetails ifc : ifaces) {
97                 interfaceKeys.add(new InterfaceKey(ifc.interfaceName));
98             }
99         }
100
101         return interfaceKeys;
102     }
103
104     @Override
105     public void merge(org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
106                       List<Interface> readData) {
107         ((InterfacesStateBuilder) builder).setInterface(readData);
108     }
109
110 }