f72148dec1b15b0d8d830b8beedf755d0b646a2c
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / v3po / translate / v3po / interfaces / VlanTagRewriteCustomizer.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.v3po.translate.v3po.interfaces;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import io.fd.honeycomb.v3po.translate.v3po.util.AbstractInterfaceTypeCustomizer;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
24 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
25 import io.fd.honeycomb.v3po.translate.write.WriteContext;
26 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
27 import java.util.concurrent.CompletionStage;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.TagRewriteOperation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.attributes.VlanTagRewrite;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.attributes.VlanTagRewriteBuilder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
41 import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
42 import org.openvpp.jvpp.future.FutureJVpp;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Writer Customizer responsible for vlan tag rewrite.<br>
48  * Sends {@code l2_interface_vlan_tag_rewrite} message to VPP.<br>
49  * Equivalent of invoking {@code vppctl set interface l2 tag-rewrite} command.
50  */
51 public class VlanTagRewriteCustomizer extends AbstractInterfaceTypeCustomizer<VlanTagRewrite> {
52
53     private static final Logger LOG = LoggerFactory.getLogger(VlanTagRewriteCustomizer.class);
54     private final NamingContext interfaceContext;
55
56     public VlanTagRewriteCustomizer(@Nonnull final FutureJVpp futureJvpp,
57                                     @Nonnull final NamingContext interfaceContext) {
58         super(futureJvpp);
59         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
60     }
61
62     @Nonnull
63     @Override
64     public Optional<VlanTagRewrite> extract(@Nonnull final InstanceIdentifier<VlanTagRewrite> currentId,
65                                             @Nonnull final DataObject parentData) {
66         return Optional.fromNullable(((L2) parentData).getVlanTagRewrite());
67     }
68
69     @Override
70     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
71         return SubInterface.class;
72     }
73
74     @Override
75     protected void writeInterface(final InstanceIdentifier<VlanTagRewrite> id, final VlanTagRewrite dataAfter,
76                                   final WriteContext writeContext) throws WriteFailedException.CreateFailedException {
77         try {
78             setTagRewrite(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
79         } catch (VppApiInvocationException e) {
80             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
81         }
82     }
83
84     private void setTagRewrite(final String ifname, final VlanTagRewrite cfg, final WriteContext writeContext)
85         throws VppApiInvocationException {
86         final int swIfIndex = interfaceContext.getIndex(ifname, writeContext.getMappingContext());
87         LOG.debug("Setting tag rewrite for interface {}(id=): {}", ifname, swIfIndex, cfg);
88
89         final CompletionStage<L2InterfaceVlanTagRewriteReply> replyCompletionStage =
90                 getFutureJVpp().l2InterfaceVlanTagRewrite(getTagRewriteRequest(swIfIndex, cfg));
91
92         final L2InterfaceVlanTagRewriteReply reply = TranslateUtils.getReply(replyCompletionStage.toCompletableFuture());
93         if (reply.retval < 0) {
94             LOG.debug("Failed to set tag rewrite for interface {}(id=): {}", ifname, swIfIndex, cfg);
95             throw new VppApiInvocationException("l2InterfaceVlanTagRewrite", reply.context, reply.retval);
96         } else {
97             LOG.debug("Tag rewrite for interface {}(id=) set successfully: {}", ifname, swIfIndex, cfg);
98         }
99     }
100
101     private L2InterfaceVlanTagRewrite getTagRewriteRequest(final int swIfIndex, final VlanTagRewrite cfg) {
102         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
103         request.swIfIndex = swIfIndex;
104
105         request.vtrOp = cfg.getRewriteOperation().getIntValue(); // TODO make mandatory
106         request.pushDot1Q = (byte) (VlanType._802dot1q.equals(cfg.getFirstPushed())
107                 ? 1
108                 : 0);
109         final VlanTag tag1 = cfg.getTag1();
110         if (tag1 != null) {
111             request.tag1 = tag1.getValue();
112         }
113         final VlanTag tag2 = cfg.getTag2();
114         if (tag2 != null) {
115             request.tag2 = tag2.getValue();
116         }
117         return request;
118     }
119
120     @Override
121     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<VlanTagRewrite> id,
122                                         @Nonnull final VlanTagRewrite dataBefore,
123                                         @Nonnull final VlanTagRewrite dataAfter, @Nonnull final WriteContext writeContext)
124             throws WriteFailedException {
125         try {
126             setTagRewrite(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
127         } catch (VppApiInvocationException e) {
128             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
129         }
130     }
131
132     @Override
133     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<VlanTagRewrite> id,
134                                         @Nonnull final VlanTagRewrite dataBefore, @Nonnull final WriteContext writeContext)
135             throws WriteFailedException.DeleteFailedException {
136         try {
137             // disable tag rewrite
138             final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
139             builder.setRewriteOperation(TagRewriteOperation.Disabled);
140             setTagRewrite(id.firstKeyOf(Interface.class).getName(), builder.build(), writeContext);
141         } catch (VppApiInvocationException e) {
142             throw new WriteFailedException.DeleteFailedException(id, e);
143         }
144     }
145 }