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