2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
19 import static com.google.common.base.Preconditions.checkState;
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;
56 * Customizer for reading vlan tag-rewrite configuration state form the VPP.
58 public class RewriteCustomizer extends FutureJVppCustomizer
59 implements ReaderCustomizer<Rewrite, RewriteBuilder>, InterfaceDataTranslator {
61 // No initialization necessary since its parent Subinterface L2 customzier sets the entire subtree during
64 private static final Logger LOG = LoggerFactory.getLogger(RewriteCustomizer.class);
65 private final NamingContext interfaceContext;
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");
74 public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
75 @Nonnull final Rewrite readValue) {
76 ((L2Builder) parentBuilder).setRewrite(readValue);
81 public RewriteBuilder getBuilder(@Nonnull final InstanceIdentifier<Rewrite> id) {
82 return new RewriteBuilder();
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);
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);
96 checkState(iface.subId != 0, "Interface returned by the VPP is not a sub-interface");
98 final TagRewriteOperation operation = TagRewriteOperation.get(iface.vtrOp);
99 if (TagRewriteOperation.disabled == operation) {
100 LOG.debug("Tag rewrite operation is disabled for ");
104 builder.setVlanType(iface.vtrPushDot1Q == 1
108 setPushTags(builder, iface);
109 setPopTags(builder, operation);
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()));
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));
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));
129 if (iface.vtrTag2 != 0) {
130 tags.add(buildTag((short) 1, CVlan.class, iface.vtrTag2));
132 if (tags.size() > 0) {
133 builder.setPushTags(tags);
137 private PushTags buildTag(final short index, final Class<? extends Dot1qTagVlanType> tagType, final int vlanId) {
138 final PushTagsBuilder tag = new PushTagsBuilder();
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());