7e7e319eb5f74034a21a8f0596c5bfaf53612893
[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);
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) throws VppApiInvocationException {
80         final int swIfIndex = interfaceContext.getIndex(ifname);
81         LOG.debug("Setting tag rewrite for interface {}(id=): {}", ifname, swIfIndex, cfg);
82
83         final CompletionStage<L2InterfaceVlanTagRewriteReply> replyCompletionStage =
84                 getFutureJVpp().l2InterfaceVlanTagRewrite(getTagRewriteRequest(swIfIndex, cfg));
85
86         final L2InterfaceVlanTagRewriteReply reply = V3poUtils.getReply(replyCompletionStage.toCompletableFuture());
87         if (reply.retval < 0) {
88             LOG.debug("Failed to set tag rewrite for interface {}(id=): {}", ifname, swIfIndex, cfg);
89             throw new VppApiInvocationException("l2InterfaceVlanTagRewrite", reply.context, reply.retval);
90         } else {
91             LOG.debug("Tag rewrite for interface {}(id=) set successfully: {}", ifname, swIfIndex, cfg);
92         }
93     }
94
95     private L2InterfaceVlanTagRewrite getTagRewriteRequest(final int swIfIndex, final VlanTagRewrite cfg) {
96         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
97         request.swIfIndex = swIfIndex;
98
99         request.vtrOp = cfg.getRewriteOperation().getIntValue(); // TODO make mandatory
100         request.pushDot1Q = (byte) (VlanType._802dot1q.equals(cfg.getFirstPushed())
101                 ? 1
102                 : 0);
103         final VlanTag tag1 = cfg.getTag1();
104         if (tag1 != null) {
105             request.tag1 = tag1.getValue();
106         }
107         final VlanTag tag2 = cfg.getTag2();
108         if (tag2 != null) {
109             request.tag2 = tag2.getValue();
110         }
111         return request;
112     }
113
114     @Override
115     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<VlanTagRewrite> id,
116                                         @Nonnull final VlanTagRewrite dataBefore,
117                                         @Nonnull final VlanTagRewrite dataAfter, @Nonnull final WriteContext writeContext)
118             throws WriteFailedException {
119         if (dataBefore.equals(dataAfter)) {
120             LOG.debug("dataBefore equals dataAfter, update will not be performed");
121             return;
122         }
123         try {
124             setTagRewrite(id.firstKeyOf(Interface.class).getName(), dataAfter);
125         } catch (VppApiInvocationException e) {
126             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
127         }
128     }
129
130     @Override
131     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<VlanTagRewrite> id,
132                                         @Nonnull final VlanTagRewrite dataBefore, @Nonnull final WriteContext writeContext)
133             throws WriteFailedException.DeleteFailedException {
134         try {
135             // disable tag rewrite
136             final VlanTagRewriteBuilder builder = new VlanTagRewriteBuilder();
137             builder.setRewriteOperation(TagRewriteOperation.Disabled);
138             setTagRewrite(id.firstKeyOf(Interface.class).getName(), builder.build());
139         } catch (VppApiInvocationException e) {
140             throw new WriteFailedException.DeleteFailedException(id, e);
141         }
142     }
143 }