2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfaces;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
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.TranslateUtils;
25 import io.fd.honeycomb.v3po.translate.write.WriteContext;
26 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
27 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
28 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
29 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
30 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.SubInterfaces;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.MatchType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.Default;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTagged;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Tags;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.VppBaseCallException;
44 import org.openvpp.jvpp.dto.CreateSubif;
45 import org.openvpp.jvpp.dto.CreateSubifReply;
46 import org.openvpp.jvpp.dto.SwInterfaceSetFlags;
47 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsReply;
48 import org.openvpp.jvpp.future.FutureJVpp;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import javax.annotation.Nonnull;
53 import javax.annotation.Nullable;
54 import java.util.List;
55 import java.util.Objects;
56 import java.util.concurrent.CompletionStage;
58 import static com.google.common.base.Preconditions.checkState;
59 import static io.fd.honeycomb.v3po.translate.v3po.util.SubInterfaceUtils.getSubInterfaceName;
60 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte;
63 * Writer Customizer responsible for sub interface creation.<br> Sends {@code create_subif} message to VPP.<br>
64 * Equivalent of invoking {@code vppclt create subif} command.
66 public class SubInterfaceCustomizer extends FutureJVppCustomizer
67 implements ListWriterCustomizer<SubInterface, SubInterfaceKey> {
69 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
70 private final NamingContext interfaceContext;
72 public SubInterfaceCustomizer(@Nonnull final FutureJVpp futureJvpp, @Nonnull final NamingContext interfaceContext) {
74 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
79 public Optional<List<SubInterface>> extract(@Nonnull final InstanceIdentifier<SubInterface> currentId,
80 @Nonnull final DataObject parentData) {
81 return Optional.fromNullable(((SubInterfaces) parentData).getSubInterface());
86 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
87 @Nonnull final SubInterface dataAfter, @Nonnull final WriteContext writeContext)
88 throws WriteFailedException.CreateFailedException {
90 createSubInterface(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
91 } catch (VppBaseCallException e) {
92 LOG.warn("Failed to create sub interface for: {}, subInterface: {}", id.firstKeyOf(Interface.class).getName(), dataAfter);
93 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
97 private void createSubInterface(@Nonnull final String superIfName,
98 @Nonnull final SubInterface subInterface,
99 final WriteContext writeContext) throws VppBaseCallException {
100 final int superIfIndex = interfaceContext.getIndex(superIfName, writeContext.getMappingContext());
101 final CompletionStage<CreateSubifReply> createSubifReplyCompletionStage =
102 getFutureJVpp().createSubif(getCreateSubifRequest(subInterface, superIfIndex));
104 final CreateSubifReply reply =
105 TranslateUtils.getReply(createSubifReplyCompletionStage.toCompletableFuture());
107 setInterfaceState(reply.swIfIndex, booleanToByte(subInterface.isEnabled()));
108 interfaceContext.addName(reply.swIfIndex,
109 getSubInterfaceName(superIfName, Math.toIntExact(subInterface.getIdentifier())),
110 writeContext.getMappingContext());
111 LOG.debug("Sub interface created successfully for: {}, subInterface: {}", superIfName, subInterface);
114 private CreateSubif getCreateSubifRequest(@Nonnull final SubInterface subInterface, final int swIfIndex) {
115 // TODO add validation
116 CreateSubif request = new CreateSubif();
117 request.subId = Math.toIntExact(subInterface.getIdentifier().intValue());
118 request.swIfIndex = swIfIndex;
120 final int numberOfTags = getNumberOfTags(subInterface);
121 switch (numberOfTags) {
132 request.dot1Ad = booleanToByte(_802dot1ad.class == subInterface.getVlanType());
134 final MatchType matchType = subInterface.getMatch().getMatchType(); // todo match should be mandatory
136 booleanToByte(matchType instanceof VlanTagged && ((VlanTagged) matchType).isMatchExactTags());
137 request.defaultSub = booleanToByte(matchType instanceof Default);
139 if (numberOfTags > 0) {
140 for (final Tag tag : subInterface.getTags().getTag()) {
141 if (tag.getIndex() == 0) {
142 setOuterTag(request, tag);
143 } else if (tag.getIndex() == 1) {
144 setInnerTag(request, tag);
151 private void setOuterTag(final CreateSubif request, final Tag outerTag) {
152 checkState(SVlan.class == outerTag.getDot1qTag().getTagType(), "Service Tag expected at index 0");
153 final Dot1qTag.VlanId vlanId = outerTag.getDot1qTag().getVlanId();
155 request.outerVlanId = dot1qVlanIdToChar(vlanId.getDot1qVlanId());
156 request.outerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
159 private void setInnerTag(final CreateSubif request, final Tag innerTag) {
160 checkState(CVlan.class == innerTag.getDot1qTag().getTagType(), "Customer Tag expected at index 1");
161 final Dot1qTag.VlanId vlanId = innerTag.getDot1qTag().getVlanId();
163 request.innerVlanId = dot1qVlanIdToChar(vlanId.getDot1qVlanId());
164 request.innerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
167 private static int getNumberOfTags(@Nonnull final SubInterface subInterface) {
168 final Tags tags = subInterface.getTags();
172 final List<Tag> tagList = tags.getTag();
173 if (tagList == null) {
176 return tagList.size();
179 private static char dot1qVlanIdToChar(@Nullable Dot1qVlanId dot1qVlanId) {
180 if (dot1qVlanId == null) {
181 return 0; // tell VPP that optional argument is missing
183 return (char) dot1qVlanId.getValue().intValue();
188 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
189 @Nonnull final SubInterface dataBefore, @Nonnull final SubInterface dataAfter,
190 @Nonnull final WriteContext writeContext)
191 throws WriteFailedException.UpdateFailedException {
192 if (Objects.equals(dataBefore.isEnabled(), dataAfter.isEnabled())) {
193 LOG.debug("No state update will be performed. Ignoring config");
194 return; // TODO shouldn't we throw exception here (if there will be dedicated L2 customizer)?
196 final String subIfaceName = getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
197 Math.toIntExact(dataAfter.getIdentifier()));
199 setInterfaceState(interfaceContext.getIndex(subIfaceName, writeContext.getMappingContext()),
200 booleanToByte(dataAfter.isEnabled()));
201 } catch (VppBaseCallException e) {
202 LOG.warn("Failed to update interface state for: interface if={}, enabled: {}",
203 subIfaceName, booleanToByte(dataAfter.isEnabled()));
204 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
208 private void setInterfaceState(final int swIfIndex, final byte enabled) throws VppBaseCallException {
209 final SwInterfaceSetFlags swInterfaceSetFlags = new SwInterfaceSetFlags();
210 swInterfaceSetFlags.swIfIndex = swIfIndex;
211 swInterfaceSetFlags.adminUpDown = enabled;
213 final CompletionStage<SwInterfaceSetFlagsReply> swInterfaceSetFlagsReplyFuture =
214 getFutureJVpp().swInterfaceSetFlags(swInterfaceSetFlags);
216 LOG.debug("Updating interface state for: interface if={}, enabled: {}", swIfIndex, enabled);
218 SwInterfaceSetFlagsReply reply = TranslateUtils.getReply(swInterfaceSetFlagsReplyFuture.toCompletableFuture());
219 LOG.debug("Interface state updated successfully for: {}, index: {}, enabled: {}, ctxId: {}",
220 swIfIndex, enabled, reply.context);
224 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
225 @Nonnull final SubInterface dataBefore,
226 @Nonnull final WriteContext writeContext)
227 throws WriteFailedException.DeleteFailedException {
228 throw new UnsupportedOperationException("Sub interface delete is not supported");