a00b63e0b4a47cc10f1ee8416424fa381f816b8b
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / EthernetCustomizer.java
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.translate.v3po.interfacesstate;
18
19 import io.fd.honeycomb.translate.read.ReadContext;
20 import io.fd.honeycomb.translate.read.ReadFailedException;
21 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.NamingContext;
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.InterfaceKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Ethernet;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.EthernetBuilder;
30 import org.opendaylight.yangtools.concepts.Builder;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39 public class EthernetCustomizer extends FutureJVppCustomizer
40         implements ReaderCustomizer<Ethernet, EthernetBuilder>, InterfaceDataTranslator {
41
42     private static final Logger LOG = LoggerFactory.getLogger(EthernetCustomizer.class);
43     private NamingContext interfaceContext;
44
45     public EthernetCustomizer(@Nonnull final FutureJVppCore jvpp,
46                               @Nonnull final NamingContext interfaceContext) {
47         super(jvpp);
48         this.interfaceContext = interfaceContext;
49     }
50
51     @Override
52     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
53                       @Nonnull final Ethernet readValue) {
54         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setEthernet(readValue);
55     }
56
57     @Nonnull
58     @Override
59     public EthernetBuilder getBuilder(@Nonnull InstanceIdentifier<Ethernet> id) {
60         return new EthernetBuilder();
61     }
62
63     @Override
64     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Ethernet> id,
65                                       @Nonnull final EthernetBuilder builder,
66                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
67
68         final InterfaceKey key = id.firstKeyOf(Interface.class);
69         final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, key.getName(),
70                 interfaceContext.getIndex(key.getName(), ctx.getMappingContext()), ctx.getModificationCache(), LOG);
71
72         if (iface.linkMtu != 0) {
73             builder.setMtu((int) iface.linkMtu);
74         }
75
76         switch (iface.linkDuplex) {
77             case 1:
78                 builder.setDuplex(Ethernet.Duplex.Half);
79                 break;
80             case 2:
81                 builder.setDuplex(Ethernet.Duplex.Full);
82                 break;
83             default:
84                 break;
85         }
86     }
87 }