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.translate.v3po.interfaces;
19 import static com.google.common.base.Preconditions.checkState;
20 import static io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils.getNumberOfTags;
21 import static io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils.getSubInterfaceName;
23 import com.google.common.base.Preconditions;
24 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
26 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.honeycomb.translate.vpp.util.WriteTimeoutException;
30 import io.fd.honeycomb.translate.write.WriteContext;
31 import io.fd.honeycomb.translate.write.WriteFailedException;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import javax.annotation.Nullable;
35 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527._802dot1ad;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.MatchType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.Default;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTagged;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.openvpp.jvpp.VppBaseCallException;
49 import org.openvpp.jvpp.core.dto.CreateSubif;
50 import org.openvpp.jvpp.core.dto.CreateSubifReply;
51 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlags;
52 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
53 import org.openvpp.jvpp.core.future.FutureJVppCore;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * Writer Customizer responsible for sub interface creation.<br> Sends {@code create_subif} message to VPP.<br>
59 * Equivalent of invoking {@code vppclt create subif} command.
61 public class SubInterfaceCustomizer extends FutureJVppCustomizer
62 implements ListWriterCustomizer<SubInterface, SubInterfaceKey>, ByteDataTranslator, JvppReplyConsumer {
64 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
65 private final NamingContext interfaceContext;
67 public SubInterfaceCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
68 @Nonnull final NamingContext interfaceContext) {
69 super(futureJVppCore);
70 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
74 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
75 @Nonnull final SubInterface dataAfter, @Nonnull final WriteContext writeContext)
76 throws WriteFailedException {
77 final String superIfName = id.firstKeyOf(Interface.class).getName();
79 createSubInterface(id, superIfName, dataAfter, writeContext);
80 } catch (VppBaseCallException e) {
81 LOG.warn("Failed to create sub interface for: {}, subInterface: {}", superIfName, dataAfter);
82 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
86 private void createSubInterface(final InstanceIdentifier<SubInterface> id, @Nonnull final String superIfName,
87 @Nonnull final SubInterface subInterface,
88 final WriteContext writeContext) throws VppBaseCallException,
89 WriteTimeoutException {
90 final int superIfIndex = interfaceContext.getIndex(superIfName, writeContext.getMappingContext());
91 final CompletionStage<CreateSubifReply> createSubifReplyCompletionStage =
92 getFutureJVpp().createSubif(getCreateSubifRequest(subInterface, superIfIndex));
94 final CreateSubifReply reply =
95 getReplyForWrite(createSubifReplyCompletionStage.toCompletableFuture(), id);
97 setInterfaceState(id, reply.swIfIndex, booleanToByte(subInterface.isEnabled()));
98 interfaceContext.addName(reply.swIfIndex,
99 getSubInterfaceName(superIfName, Math.toIntExact(subInterface.getIdentifier())),
100 writeContext.getMappingContext());
101 LOG.debug("Sub interface created successfully for: {}, subInterface: {}", superIfName, subInterface);
104 private CreateSubif getCreateSubifRequest(@Nonnull final SubInterface subInterface, final int swIfIndex) {
105 // TODO HONEYCOMB-183 add validation
106 CreateSubif request = new CreateSubif();
107 request.subId = Math.toIntExact(subInterface.getIdentifier().intValue());
108 request.swIfIndex = swIfIndex;
110 final int numberOfTags = getNumberOfTags(subInterface.getTags());
111 switch (numberOfTags) {
122 request.dot1Ad = booleanToByte(_802dot1ad.class == subInterface.getVlanType());
124 // TODO HONEYCOMB-183 match should be mandatory
125 final MatchType matchType = subInterface.getMatch().getMatchType();
127 booleanToByte(matchType instanceof VlanTagged && ((VlanTagged) matchType).isMatchExactTags());
128 request.defaultSub = booleanToByte(matchType instanceof Default);
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);
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();
146 request.outerVlanId = dot1qVlanIdToShort(vlanId.getDot1qVlanId());
147 request.outerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
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();
154 request.innerVlanId = dot1qVlanIdToShort(vlanId.getDot1qVlanId());
155 request.innerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
158 private static short dot1qVlanIdToShort(@Nullable Dot1qVlanId dot1qVlanId) {
159 if (dot1qVlanId == null) {
160 return 0; // tell VPP that optional argument is missing
162 return dot1qVlanId.getValue().shortValue();
167 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
168 @Nonnull final SubInterface dataBefore, @Nonnull final SubInterface dataAfter,
169 @Nonnull final WriteContext writeContext)
170 throws WriteFailedException {
171 final String subIfaceName = getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
172 Math.toIntExact(dataAfter.getIdentifier()));
174 setInterfaceState(id, interfaceContext.getIndex(subIfaceName, writeContext.getMappingContext()),
175 booleanToByte(dataAfter.isEnabled()));
176 } catch (VppBaseCallException e) {
177 LOG.warn("Failed to update interface state for: interface if={}, enabled: {}",
178 subIfaceName, booleanToByte(dataAfter.isEnabled()));
179 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
183 private void setInterfaceState(final InstanceIdentifier<SubInterface> id, final int swIfIndex, final byte enabled)
184 throws VppBaseCallException, WriteTimeoutException {
185 final SwInterfaceSetFlags swInterfaceSetFlags = new SwInterfaceSetFlags();
186 swInterfaceSetFlags.swIfIndex = swIfIndex;
187 swInterfaceSetFlags.adminUpDown = enabled;
189 final CompletionStage<SwInterfaceSetFlagsReply> swInterfaceSetFlagsReplyFuture =
190 getFutureJVpp().swInterfaceSetFlags(swInterfaceSetFlags);
192 LOG.debug("Updating interface state for interface if={}, enabled: {}", swIfIndex, enabled);
194 SwInterfaceSetFlagsReply reply =
195 getReplyForWrite(swInterfaceSetFlagsReplyFuture.toCompletableFuture(), id);
196 LOG.debug("Interface state updated successfully for interface index: {}, enabled: {}, ctxId: {}",
197 swIfIndex, enabled, reply.context);
201 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
202 @Nonnull final SubInterface dataBefore,
203 @Nonnull final WriteContext writeContext)
204 throws WriteFailedException.DeleteFailedException {
205 throw new UnsupportedOperationException("Sub interface delete is not supported");