b2533a2308121d6d2a089fd936b252d1b4a06177
[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.Initialized;
22 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
23 import io.fd.honeycomb.translate.util.RWUtils;
24 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
27 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Ethernet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.EthernetBuilder;
35 import org.opendaylight.yangtools.concepts.Builder;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 public class EthernetCustomizer extends FutureJVppCustomizer
43         implements InitializingReaderCustomizer<Ethernet, EthernetBuilder>, InterfaceDataTranslator {
44
45     private static final Logger LOG = LoggerFactory.getLogger(EthernetCustomizer.class);
46     private NamingContext interfaceContext;
47
48     public EthernetCustomizer(@Nonnull final FutureJVppCore jvpp,
49                               @Nonnull final NamingContext interfaceContext) {
50         super(jvpp);
51         this.interfaceContext = interfaceContext;
52     }
53
54     @Override
55     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
56                       @Nonnull final Ethernet readValue) {
57         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setEthernet(readValue);
58     }
59
60     @Nonnull
61     @Override
62     public EthernetBuilder getBuilder(@Nonnull InstanceIdentifier<Ethernet> id) {
63         return new EthernetBuilder();
64     }
65
66     @Override
67     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Ethernet> id,
68                                       @Nonnull final EthernetBuilder builder,
69                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
70
71         final InterfaceKey key = id.firstKeyOf(Interface.class);
72         final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, key.getName(),
73                 interfaceContext.getIndex(key.getName(), ctx.getMappingContext()), ctx.getModificationCache(), LOG);
74
75         if (iface.linkMtu != 0) {
76             builder.setMtu((int) iface.linkMtu);
77         }
78
79         switch (iface.linkDuplex) {
80             case 1:
81                 builder.setDuplex(Ethernet.Duplex.Half);
82                 break;
83             case 2:
84                 builder.setDuplex(Ethernet.Duplex.Full);
85                 break;
86             default:
87                 break;
88         }
89     }
90
91     @Override
92     public Initialized<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Ethernet> init(
93             @Nonnull final InstanceIdentifier<Ethernet> id,
94             @Nonnull final Ethernet readValue,
95             @Nonnull final ReadContext ctx) {
96         return Initialized.create(getCfgId(id),
97                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.EthernetBuilder()
98                         .setMtu(readValue.getMtu())
99                         .build());
100     }
101
102     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Ethernet> getCfgId(
103             final InstanceIdentifier<Ethernet> id) {
104         return InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
105                 .augmentation(VppInterfaceAugmentation.class)
106                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Ethernet.class);
107     }
108 }