1c1d0703e186f6390c5d68e7ce759d60a11f3426
[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.rev150527._802dot1ad;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.L2Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.tag.rewrite.PushTags;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.tag.rewrite.PushTagsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.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     private static final Logger LOG = LoggerFactory.getLogger(RewriteCustomizer.class);
62     private final NamingContext interfaceContext;
63
64     public RewriteCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
65                              @Nonnull final NamingContext interfaceContext) {
66         super(futureJVppCore);
67         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
68     }
69
70     @Override
71     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
72                       @Nonnull final Rewrite readValue) {
73         ((L2Builder) parentBuilder).setRewrite(readValue);
74     }
75
76     @Nonnull
77     @Override
78     public RewriteBuilder getBuilder(@Nonnull final InstanceIdentifier<Rewrite> id) {
79         return new RewriteBuilder();
80     }
81
82     @Override
83     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Rewrite> id,
84                                       @Nonnull final RewriteBuilder builder, @Nonnull final ReadContext ctx)
85             throws ReadFailedException {
86         final String subInterfaceName = getSubInterfaceName(id);
87         LOG.debug("Reading attributes for sub interface: {}", subInterfaceName);
88
89         final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, subInterfaceName,
90                 interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()), ctx.getModificationCache(), LOG);
91         LOG.debug("VPP sub-interface details: {}", iface);
92
93         checkState(iface.subId != 0, "Interface returned by the VPP is not a sub-interface");
94
95         final TagRewriteOperation operation = TagRewriteOperation.get(iface.vtrOp);
96         if (TagRewriteOperation.disabled == operation) {
97             LOG.debug("Tag rewrite operation is disabled for ");
98             return;
99         }
100
101         builder.setVlanType(iface.vtrPushDot1Q == 1
102                 ? _802dot1q.class
103                 : _802dot1ad.class);
104
105         setPushTags(builder, iface);
106         setPopTags(builder, operation);
107     }
108
109     private static String getSubInterfaceName(final InstanceIdentifier<Rewrite> id) {
110         return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
111                 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
112     }
113
114     private void setPopTags(final RewriteBuilder builder, final TagRewriteOperation operation) {
115         final byte numberOfTagsToPop = operation.getPopTags();
116         if (numberOfTagsToPop != 0) {
117             builder.setPopTags(Short.valueOf(numberOfTagsToPop));
118         }
119     }
120
121     private void setPushTags(final RewriteBuilder builder, final SwInterfaceDetails iface) {
122         final List<PushTags> tags = new ArrayList<>();
123         if (iface.vtrTag1 != 0) {
124             tags.add(buildTag((short) 0, SVlan.class, iface.vtrTag1));
125         }
126         if (iface.vtrTag2 != 0) {
127             tags.add(buildTag((short) 1, CVlan.class, iface.vtrTag2));
128         }
129         if (tags.size() > 0) {
130             builder.setPushTags(tags);
131         }
132     }
133
134     private PushTags buildTag(final short index, final Class<? extends Dot1qTagVlanType> tagType, final int vlanId) {
135         final PushTagsBuilder tag = new PushTagsBuilder();
136         tag.setIndex(index);
137         tag.setKey(new PushTagsKey(index));
138         final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
139         dtag.setTagType(tagType);
140         dtag.setVlanId(new Dot1qVlanId(vlanId));
141         tag.setDot1qTag(dtag.build());
142         return tag.build();
143     }
144 }