2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate;
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;
36 import java.util.ArrayList;
37 import java.util.List;
40 public class InterfaceCustomizer extends io.fd.honeycomb.v3po.translate.v3po.util.VppApiCustomizer
41 implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
43 private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
45 public InterfaceCustomizer(org.openvpp.vppjapi.vppApi vppApi) {
50 public InterfaceBuilder getBuilder(InstanceIdentifier<Interface> id) {
51 return new InterfaceBuilder();
55 public void readCurrentAttributes(InstanceIdentifier<Interface> id, InterfaceBuilder builder, Context ctx)
56 throws ReadFailedException {
57 vppInterfaceDetails[] ifaces;
59 final InterfaceKey key = id.firstKeyOf(id.getTargetType());
60 // Extract one interface detail from VPP
61 ifaces = getVppApi().swInterfaceDump((byte) 1, key.getName().getBytes());
63 LOG.warn("VPP returned null instead of interface by key {}", key.getName().getBytes());
67 if (1 != ifaces.length) {
68 LOG.error("Failed to extract interface {} details from VPP", key.getName());
72 final vppInterfaceDetails iface = ifaces[0];
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));
84 if (iface.physAddr.length == 6) {
85 builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.physAddr)));
90 public List<InterfaceKey> getAllIds(InstanceIdentifier<Interface> id, Context context) {
91 vppInterfaceDetails[] ifaces;
92 final ArrayList<InterfaceKey> interfaceKeys = new ArrayList<>();
94 ifaces = getVppApi().swInterfaceDump((byte) 0, "".getBytes());
96 for (vppInterfaceDetails ifc : ifaces) {
97 interfaceKeys.add(new InterfaceKey(ifc.interfaceName));
101 return interfaceKeys;
105 public void merge(org.opendaylight.yangtools.concepts.Builder<? extends DataObject> builder,
106 List<Interface> readData) {
107 ((InterfacesStateBuilder) builder).setInterface(readData);