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