ebd1cff406167c36c9eec6dfb4240656a1fbbde1
[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.isInterfaceOfType;
20
21 import com.google.common.base.Preconditions;
22 import io.fd.honeycomb.v3po.translate.read.ReadContext;
23 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
24 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
27 import javax.annotation.Nonnull;
28 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
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.rev150105.VlanTag;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.SubInterface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.SubInterfaceBuilder;
36 import org.opendaylight.yangtools.concepts.Builder;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.openvpp.jvpp.dto.SwInterfaceDetails;
40 import org.openvpp.jvpp.future.FutureJVpp;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Customizer for reading sub interfaces form the VPP.
46  */
47 public class SubInterfaceCustomizer extends FutureJVppCustomizer
48         implements ChildReaderCustomizer<SubInterface, SubInterfaceBuilder> {
49
50     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
51     private NamingContext interfaceContext;
52
53     public SubInterfaceCustomizer(@Nonnull final FutureJVpp jvpp,
54                                   @Nonnull final NamingContext interfaceContext) {
55         super(jvpp);
56         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
57     }
58
59     @Override
60     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
61                       @Nonnull final SubInterface readValue) {
62         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setSubInterface(readValue);
63     }
64
65     @Nonnull
66     @Override
67     public SubInterfaceBuilder getBuilder(@Nonnull final InstanceIdentifier<SubInterface> id) {
68         return new SubInterfaceBuilder();
69     }
70
71     @Override
72     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
73                                       @Nonnull final SubInterfaceBuilder builder, @Nonnull final ReadContext ctx)
74             throws ReadFailedException {
75         final InterfaceKey key = id.firstKeyOf(Interface.class);
76         // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
77         // to fill in the context with initial ifc mapping
78         final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
79         if (!isInterfaceOfType(ctx.getModificationCache(), index, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class)) {
80             return;
81         }
82
83         LOG.debug("Reading attributes for sub interface: {}", id);
84         final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), key, index, ctx.getModificationCache());
85         LOG.debug("VPP interface details: {}", ReflectionToStringBuilder.toString(iface));
86
87         if (iface.subId == 0) {
88             // Not a sub interface type
89             return;
90         }
91
92         builder.setIdentifier(Long.valueOf(iface.subId));
93         builder.setSuperInterface(interfaceContext.getName(iface.supSwIfIndex, ctx.getMappingContext()));
94         builder.setNumberOfTags(Short.valueOf(iface.subNumberOfTags));
95         builder.setVlanType(iface.subDot1Ad == 1 ? VlanType._802dot1q : VlanType._802dot1ad);
96         if (iface.subExactMatch == 1) {
97             builder.setExactMatch(true);
98         }
99         if (iface.subDefault == 1) {
100             builder.setDefaultSubif(true);
101         }
102         if (iface.subOuterVlanIdAny == 1) {
103             builder.setMatchAnyOuterId(true);
104         }
105         if (iface.subOuterVlanIdAny == 1) {
106             builder.setMatchAnyInnerId(true);
107         }
108         if (iface.subOuterVlanId != 0) { // optional
109             builder.setOuterId(new VlanTag(Integer.valueOf(iface.subOuterVlanId)));
110         }
111         if (iface.subInnerVlanId != 0) { // optional
112             builder.setInnerId(new VlanTag(Integer.valueOf(iface.subInnerVlanId)));
113         }
114     }
115 }