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