da6bf7a10f439d383610e3f026ee5e6a21bc0a03
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / RewriteCustomizer.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 static com.google.common.base.Preconditions.checkState;
20
21 import com.google.common.base.Preconditions;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
28 import io.fd.honeycomb.translate.vpp.util.TagRewriteOperation;
29 import java.util.ArrayList;
30 import java.util.List;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
33 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
34 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
35 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.Dot1qTagBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214._802dot1ad;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214._802dot1q;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.L2Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.l2.Rewrite;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.l2.RewriteBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.tag.rewrite.PushTags;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.tag.rewrite.PushTagsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.tag.rewrite.PushTagsKey;
47 import org.opendaylight.yangtools.concepts.Builder;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
51 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * Customizer for reading vlan tag-rewrite configuration state form the VPP.
57  */
58 public class RewriteCustomizer extends FutureJVppCustomizer
59         implements ReaderCustomizer<Rewrite, RewriteBuilder>, InterfaceDataTranslator {
60
61     // No initialization necessary since its parent Subinterface L2 customzier sets the entire subtree during
62     // initialization
63
64     private static final Logger LOG = LoggerFactory.getLogger(RewriteCustomizer.class);
65     private final NamingContext interfaceContext;
66
67     public RewriteCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
68                              @Nonnull final NamingContext interfaceContext) {
69         super(futureJVppCore);
70         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
71     }
72
73     @Override
74     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
75                       @Nonnull final Rewrite readValue) {
76         ((L2Builder) parentBuilder).setRewrite(readValue);
77     }
78
79     @Nonnull
80     @Override
81     public RewriteBuilder getBuilder(@Nonnull final InstanceIdentifier<Rewrite> id) {
82         return new RewriteBuilder();
83     }
84
85     @Override
86     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Rewrite> id,
87                                       @Nonnull final RewriteBuilder builder, @Nonnull final ReadContext ctx)
88             throws ReadFailedException {
89         final String subInterfaceName = getSubInterfaceName(id);
90         LOG.debug("Reading attributes for sub interface: {}", subInterfaceName);
91
92         final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, subInterfaceName,
93                 interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()), ctx.getModificationCache(), LOG);
94         LOG.debug("VPP sub-interface details: {}", iface);
95
96         checkState(iface.subId != 0, "Interface returned by the VPP is not a sub-interface");
97
98         final TagRewriteOperation operation = TagRewriteOperation.get(iface.vtrOp);
99         if (TagRewriteOperation.disabled == operation) {
100             LOG.debug("Tag rewrite operation is disabled for ");
101             return;
102         }
103
104         builder.setVlanType(iface.vtrPushDot1Q == 1
105                 ? _802dot1q.class
106                 : _802dot1ad.class);
107
108         setPushTags(builder, iface);
109         setPopTags(builder, operation);
110     }
111
112     private static String getSubInterfaceName(final InstanceIdentifier<Rewrite> id) {
113         return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
114                 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
115     }
116
117     private void setPopTags(final RewriteBuilder builder, final TagRewriteOperation operation) {
118         final byte numberOfTagsToPop = operation.getPopTags();
119         if (numberOfTagsToPop != 0) {
120             builder.setPopTags(Short.valueOf(numberOfTagsToPop));
121         }
122     }
123
124     private void setPushTags(final RewriteBuilder builder, final SwInterfaceDetails iface) {
125         final List<PushTags> tags = new ArrayList<>();
126         if (iface.vtrTag1 != 0) {
127             tags.add(buildTag((short) 0, SVlan.class, iface.vtrTag1));
128         }
129         if (iface.vtrTag2 != 0) {
130             tags.add(buildTag((short) 1, CVlan.class, iface.vtrTag2));
131         }
132         if (tags.size() > 0) {
133             builder.setPushTags(tags);
134         }
135     }
136
137     private PushTags buildTag(final short index, final Class<? extends Dot1qTagVlanType> tagType, final int vlanId) {
138         final PushTagsBuilder tag = new PushTagsBuilder();
139         tag.setIndex(index);
140         tag.setKey(new PushTagsKey(index));
141         final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
142         dtag.setTagType(tagType);
143         dtag.setVlanId(new Dot1qVlanId(vlanId));
144         tag.setDot1qTag(dtag.build());
145         return tag.build();
146     }
147 }