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.interfaces;
19 import com.google.common.base.Preconditions;
20 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
22 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
26 import io.fd.honeycomb.translate.vpp.util.TagRewriteOperation;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.vpp.jvpp.core.dto.L2InterfaceVlanTagRewrite;
30 import io.fd.vpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.List;
33 import java.util.concurrent.CompletionStage;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1q;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.Rewrite;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.l2.RewriteBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.tag.rewrite.PushTags;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Writer Customizer responsible for vlan tag rewrite.<br> Sends {@code l2_interface_vlan_tag_rewrite} message to
47 * VPP.<br> Equivalent of invoking {@code vppctl set interface l2 tag-rewrite} command.
49 public class RewriteCustomizer extends FutureJVppCustomizer
50 implements WriterCustomizer<Rewrite>, ByteDataTranslator, JvppReplyConsumer {
52 private static final Logger LOG = LoggerFactory.getLogger(RewriteCustomizer.class);
53 private final NamingContext interfaceContext;
55 public RewriteCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
56 @Nonnull final NamingContext interfaceContext) {
57 super(futureJVppCore);
58 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
62 public void writeCurrentAttributes(final InstanceIdentifier<Rewrite> id, final Rewrite dataAfter,
63 final WriteContext writeContext)
64 throws WriteFailedException {
65 final String subifName = getSubInterfaceName(id);
66 setTagRewrite(id, subifName, dataAfter, writeContext);
69 private static String getSubInterfaceName(final InstanceIdentifier<Rewrite> id) {
70 return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
71 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
74 private void setTagRewrite(final InstanceIdentifier<Rewrite> id, final String ifname, final Rewrite rewrite,
75 final WriteContext writeContext) throws WriteFailedException {
76 final int swIfIndex = interfaceContext.getIndex(ifname, writeContext.getMappingContext());
77 LOG.debug("Setting tag rewrite for interface {}(id=): {}", ifname, swIfIndex, rewrite);
79 final CompletionStage<L2InterfaceVlanTagRewriteReply> replyCompletionStage =
80 getFutureJVpp().l2InterfaceVlanTagRewrite(getTagRewriteRequest(swIfIndex, rewrite));
82 getReplyForWrite(replyCompletionStage.toCompletableFuture(), id);
83 LOG.debug("Tag rewrite for interface {}(id=) set successfully: {}", ifname, swIfIndex, rewrite);
86 private L2InterfaceVlanTagRewrite getTagRewriteRequest(final int swIfIndex, final Rewrite rewrite) {
87 final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
88 request.swIfIndex = swIfIndex;
89 request.pushDot1Q = booleanToByte(_802dot1q.class == rewrite.getVlanType());
91 final List<PushTags> pushTags = rewrite.getPushTags();
92 final Short popTags = rewrite.getPopTags();
94 final int numberOfTagsToPop = popTags == null
97 final int numberOfTagsToPush = pushTags == null
101 request.vtrOp = TagRewriteOperation.get(numberOfTagsToPop, numberOfTagsToPush).ordinal();
103 if (numberOfTagsToPush > 0) {
104 for (final PushTags tag : pushTags) {
105 if (tag.getIndex() == 0) {
106 request.tag1 = tag.getDot1qTag().getVlanId().getValue();
108 request.tag2 = tag.getDot1qTag().getVlanId().getValue();
113 LOG.debug("Generated tag rewrite request: {}", request);
118 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Rewrite> id,
119 @Nonnull final Rewrite dataBefore,
120 @Nonnull final Rewrite dataAfter, @Nonnull final WriteContext writeContext)
121 throws WriteFailedException {
122 final String subifName = getSubInterfaceName(id);
123 setTagRewrite(id, subifName, dataAfter, writeContext);
127 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Rewrite> id,
128 @Nonnull final Rewrite dataBefore, @Nonnull final WriteContext writeContext)
129 throws WriteFailedException {
130 final String subifName = getSubInterfaceName(id);
132 LOG.debug("Disabling tag rewrite for interface {}", subifName);
133 final Rewrite rewrite = new RewriteBuilder().build(); // rewrite without push and pops will cause delete
134 setTagRewrite(id, subifName, rewrite, writeContext);