64abf6dc8ee59c931fcf6221f631626a97ed2953
[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.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.L2Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.l2.VlanTagRewrite;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.l2.VlanTagRewriteBuilder;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.dto.SwInterfaceDetails;
41 import org.openvpp.jvpp.future.FutureJVpp;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Customizer for reading vlan tag-rewrite configuration state form the VPP.
47  */
48 public class VlanTagRewriteCustomizer extends FutureJVppCustomizer
49         implements ChildReaderCustomizer<VlanTagRewrite, VlanTagRewriteBuilder> {
50
51     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
52     private final NamingContext interfaceContext;
53
54     public VlanTagRewriteCustomizer(@Nonnull final FutureJVpp futureJvpp,
55                                     @Nonnull final NamingContext interfaceContext) {
56         super(futureJvpp);
57         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
58     }
59
60     @Override
61     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
62                       @Nonnull final VlanTagRewrite readValue) {
63         ((L2Builder) parentBuilder).setVlanTagRewrite(readValue);
64     }
65
66     @Nonnull
67     @Override
68     public VlanTagRewriteBuilder getBuilder(@Nonnull final InstanceIdentifier<VlanTagRewrite> id) {
69         return new VlanTagRewriteBuilder();
70     }
71
72     @Override
73     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VlanTagRewrite> id,
74                                       @Nonnull final VlanTagRewriteBuilder builder, @Nonnull final ReadContext ctx)
75             throws ReadFailedException {
76         LOG.debug("Reading attributes for sub interface: {}", id);
77         final InterfaceKey key = id.firstKeyOf(Interface.class);
78
79         final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), key,
80                 interfaceContext.getIndex(key.getName(), ctx.getMappingContext()), ctx.getModificationCache());
81
82         // Tag rewrite is only possible for subinterfaces
83         if (!isInterfaceOfType(SubInterface.class, iface)) {
84             return;
85         }
86
87         builder.setFirstPushed(iface.subDot1Ad == 1 ? VlanType._802dot1q : VlanType._802dot1ad);
88         builder.setRewriteOperation(TagRewriteOperation.forValue(iface.vtrOp));
89         if (iface.vtrTag1 != 0) {
90             builder.setTag1(new VlanTag(iface.vtrTag1));
91         }
92         if (iface.vtrTag2 != 0) {
93             builder.setTag2(new VlanTag(iface.vtrTag2));
94         }
95     }
96 }