c4971867c8634059b6ec3c0a12deb62d0b4588ef
[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 static com.google.common.base.Preconditions.checkState;
20 import static io.fd.honeycomb.v3po.translate.v3po.util.SubInterfaceUtils.getSubInterfaceName;
21 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte;
22
23 import com.google.common.base.Preconditions;
24 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
27 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
28 import io.fd.honeycomb.v3po.translate.v3po.util.WriteTimeoutException;
29 import io.fd.honeycomb.v3po.translate.write.WriteContext;
30 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
31 import java.util.List;
32 import java.util.Objects;
33 import java.util.concurrent.CompletionStage;
34 import javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.MatchType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.Default;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTagged;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Tags;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.openvpp.jvpp.VppBaseCallException;
51 import org.openvpp.jvpp.dto.CreateSubif;
52 import org.openvpp.jvpp.dto.CreateSubifReply;
53 import org.openvpp.jvpp.dto.SwInterfaceSetFlags;
54 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsReply;
55 import org.openvpp.jvpp.future.FutureJVpp;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * Writer Customizer responsible for sub interface creation.<br> Sends {@code create_subif} message to VPP.<br>
61  * Equivalent of invoking {@code vppclt create subif} command.
62  */
63 public class SubInterfaceCustomizer extends FutureJVppCustomizer
64     implements ListWriterCustomizer<SubInterface, SubInterfaceKey> {
65
66     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
67     private final NamingContext interfaceContext;
68
69     public SubInterfaceCustomizer(@Nonnull final FutureJVpp futureJvpp, @Nonnull final NamingContext interfaceContext) {
70         super(futureJvpp);
71         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
72     }
73
74     @Override
75     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
76                                        @Nonnull final SubInterface dataAfter, @Nonnull final WriteContext writeContext)
77         throws WriteFailedException {
78         final String superIfName = id.firstKeyOf(Interface.class).getName();
79         try {
80             createSubInterface(id, superIfName, dataAfter, writeContext);
81         } catch (VppBaseCallException e) {
82             LOG.warn("Failed to create sub interface for: {}, subInterface: {}", superIfName, dataAfter);
83             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
84         }
85     }
86
87     private void createSubInterface(final InstanceIdentifier<SubInterface> id, @Nonnull final String superIfName,
88                                     @Nonnull final SubInterface subInterface,
89                                     final WriteContext writeContext) throws VppBaseCallException,
90         WriteTimeoutException {
91         final int superIfIndex = interfaceContext.getIndex(superIfName, writeContext.getMappingContext());
92         final CompletionStage<CreateSubifReply> createSubifReplyCompletionStage =
93             getFutureJVpp().createSubif(getCreateSubifRequest(subInterface, superIfIndex));
94
95         final CreateSubifReply reply =
96             TranslateUtils.getReplyForWrite(createSubifReplyCompletionStage.toCompletableFuture(), id);
97
98         setInterfaceState(id, reply.swIfIndex, booleanToByte(subInterface.isEnabled()));
99         interfaceContext.addName(reply.swIfIndex,
100             getSubInterfaceName(superIfName, Math.toIntExact(subInterface.getIdentifier())),
101             writeContext.getMappingContext());
102         LOG.debug("Sub interface created successfully for: {}, subInterface: {}", superIfName, subInterface);
103     }
104
105     private CreateSubif getCreateSubifRequest(@Nonnull final SubInterface subInterface, final int swIfIndex) {
106         // TODO add validation
107         CreateSubif request = new CreateSubif();
108         request.subId = Math.toIntExact(subInterface.getIdentifier().intValue());
109         request.swIfIndex = swIfIndex;
110
111         final int numberOfTags = getNumberOfTags(subInterface);
112         switch (numberOfTags) {
113             case 0:
114                 request.noTags = 1;
115                 break;
116             case 1:
117                 request.oneTag = 1;
118                 break;
119             case 2:
120                 request.twoTags = 1;
121                 break;
122         }
123         request.dot1Ad = booleanToByte(_802dot1ad.class == subInterface.getVlanType());
124
125         final MatchType matchType = subInterface.getMatch().getMatchType(); // todo match should be mandatory
126         request.exactMatch =
127             booleanToByte(matchType instanceof VlanTagged && ((VlanTagged) matchType).isMatchExactTags());
128         request.defaultSub = booleanToByte(matchType instanceof Default);
129
130         if (numberOfTags > 0) {
131             for (final Tag tag : subInterface.getTags().getTag()) {
132                 if (tag.getIndex() == 0) {
133                     setOuterTag(request, tag);
134                 } else if (tag.getIndex() == 1) {
135                     setInnerTag(request, tag);
136                 }
137             }
138         }
139         return request;
140     }
141
142     private void setOuterTag(final CreateSubif request, final Tag outerTag) {
143         checkState(SVlan.class == outerTag.getDot1qTag().getTagType(), "Service Tag expected at index 0");
144         final Dot1qTag.VlanId vlanId = outerTag.getDot1qTag().getVlanId();
145
146         request.outerVlanId = dot1qVlanIdToShort(vlanId.getDot1qVlanId());
147         request.outerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
148     }
149
150     private void setInnerTag(final CreateSubif request, final Tag innerTag) {
151         checkState(CVlan.class == innerTag.getDot1qTag().getTagType(), "Customer Tag expected at index 1");
152         final Dot1qTag.VlanId vlanId = innerTag.getDot1qTag().getVlanId();
153
154         request.innerVlanId = dot1qVlanIdToShort(vlanId.getDot1qVlanId());
155         request.innerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
156     }
157
158     private static int getNumberOfTags(@Nonnull final SubInterface subInterface) {
159         final Tags tags = subInterface.getTags();
160         if (tags == null) {
161             return 0;
162         }
163         final List<Tag> tagList = tags.getTag();
164         if (tagList == null) {
165             return 0;
166         }
167         return tagList.size();
168     }
169
170     private static short dot1qVlanIdToShort(@Nullable Dot1qVlanId dot1qVlanId) {
171         if (dot1qVlanId == null) {
172             return 0; // tell VPP that optional argument is missing
173         } else {
174             return dot1qVlanId.getValue().shortValue();
175         }
176     }
177
178     @Override
179     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
180                                         @Nonnull final SubInterface dataBefore, @Nonnull final SubInterface dataAfter,
181                                         @Nonnull final WriteContext writeContext)
182         throws WriteFailedException {
183         if (Objects.equals(dataBefore.isEnabled(), dataAfter.isEnabled())) {
184             LOG.debug("No state update will be performed. Ignoring config");
185             return; // TODO shouldn't we throw exception here (if there will be dedicated L2 customizer)?
186         }
187         final String subIfaceName = getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
188             Math.toIntExact(dataAfter.getIdentifier()));
189         try {
190             setInterfaceState(id, interfaceContext.getIndex(subIfaceName, writeContext.getMappingContext()),
191                 booleanToByte(dataAfter.isEnabled()));
192         } catch (VppBaseCallException e) {
193             LOG.warn("Failed to update interface state for: interface if={}, enabled: {}",
194                 subIfaceName, booleanToByte(dataAfter.isEnabled()));
195             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
196         }
197     }
198
199     private void setInterfaceState(final InstanceIdentifier<SubInterface> id, final int swIfIndex, final byte enabled)
200         throws VppBaseCallException, WriteTimeoutException {
201         final SwInterfaceSetFlags swInterfaceSetFlags = new SwInterfaceSetFlags();
202         swInterfaceSetFlags.swIfIndex = swIfIndex;
203         swInterfaceSetFlags.adminUpDown = enabled;
204
205         final CompletionStage<SwInterfaceSetFlagsReply> swInterfaceSetFlagsReplyFuture =
206             getFutureJVpp().swInterfaceSetFlags(swInterfaceSetFlags);
207
208         LOG.debug("Updating interface state for interface if={}, enabled: {}", swIfIndex, enabled);
209
210         SwInterfaceSetFlagsReply reply =
211             TranslateUtils.getReplyForWrite(swInterfaceSetFlagsReplyFuture.toCompletableFuture(), id);
212         LOG.debug("Interface state updated successfully for interface index: {}, enabled: {}, ctxId: {}",
213             swIfIndex, enabled, reply.context);
214     }
215
216     @Override
217     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
218                                         @Nonnull final SubInterface dataBefore,
219                                         @Nonnull final WriteContext writeContext)
220         throws WriteFailedException.DeleteFailedException {
221         throw new UnsupportedOperationException("Sub interface delete is not supported");
222     }
223 }