8e5cc89320b0c2abf3654c1008432283e8f4a09a
[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 static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.getVppInterfaceDetails;
20
21 import io.fd.honeycomb.v3po.translate.Context;
22 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
23 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Ethernet;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.EthernetBuilder;
32 import org.opendaylight.yangtools.concepts.Builder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.openvpp.vppjapi.vppInterfaceDetails;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 public class VppInterfaceStateCustomizer extends io.fd.honeycomb.v3po.translate.v3po.util.VppApiCustomizer
41         implements ChildReaderCustomizer<VppInterfaceStateAugmentation, VppInterfaceStateAugmentationBuilder> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(VppInterfaceStateCustomizer.class);
44
45     public VppInterfaceStateCustomizer(org.openvpp.vppjapi.vppApi vppApi) {
46         super(vppApi);
47     }
48
49     @Override
50     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull VppInterfaceStateAugmentation readValue) {
51         ((InterfaceBuilder) parentBuilder).addAugmentation(VppInterfaceStateAugmentation.class, readValue);
52     }
53
54     @Nonnull
55     @Override
56     public VppInterfaceStateAugmentationBuilder getBuilder(@Nonnull InstanceIdentifier<VppInterfaceStateAugmentation> id) {
57         return new VppInterfaceStateAugmentationBuilder();
58     }
59
60     @Override
61     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VppInterfaceStateAugmentation> id,
62                                       @Nonnull final VppInterfaceStateAugmentationBuilder builder,
63                                       @Nonnull final Context ctx) throws ReadFailedException {
64
65         final InterfaceKey key = id.firstKeyOf(Interface.class);
66         vppInterfaceDetails[] ifaces = getVppInterfaceDetails(getVppApi(), true, key.getName());
67         if (null == ifaces || ifaces.length != 1) {
68             return;
69         }
70
71         final vppInterfaceDetails iface = ifaces[0];
72         final EthernetBuilder ethernet = new EthernetBuilder();
73
74         ethernet.setMtu(iface.linkMtu);
75         switch (iface.linkDuplex) {
76             case 1:
77                 ethernet.setDuplex(Ethernet.Duplex.Half);
78                 break;
79             case 2:
80                 ethernet.setDuplex(Ethernet.Duplex.Full);
81                 break;
82             default:
83                 break;
84         }
85
86         builder.setEthernet(ethernet.build());
87     }
88 }