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