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.write.WriteContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.vpp.jvpp.core.dto.CreateSubif;
32 import io.fd.vpp.jvpp.core.dto.CreateSubifReply;
33 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlags;
34 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
35 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
36 import java.util.concurrent.CompletionStage;
37 import javax.annotation.Nonnull;
38 import javax.annotation.Nullable;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
41 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
42 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214._802dot1ad;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.MatchType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.Default;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.vlan.tagged.VlanTagged;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.Tag;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Writer Customizer responsible for sub interface creation.<br> Sends {@code create_subif} message to VPP.<br>
57 * Equivalent of invoking {@code vppclt create subif} command.
59 public class SubInterfaceCustomizer extends FutureJVppCustomizer
60 implements ListWriterCustomizer<SubInterface, SubInterfaceKey>, ByteDataTranslator, JvppReplyConsumer {
62 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
63 private final NamingContext interfaceContext;
65 public SubInterfaceCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
66 @Nonnull final NamingContext interfaceContext) {
67 super(futureJVppCore);
68 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
72 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
73 @Nonnull final SubInterface dataAfter, @Nonnull final WriteContext writeContext)
74 throws WriteFailedException {
75 final String superIfName = id.firstKeyOf(Interface.class).getName();
76 createSubInterface(id, superIfName, dataAfter, writeContext);
79 private void createSubInterface(final InstanceIdentifier<SubInterface> id, @Nonnull final String superIfName,
80 @Nonnull final SubInterface subInterface, final WriteContext writeContext)
81 throws WriteFailedException {
82 final int superIfIndex = interfaceContext.getIndex(superIfName, writeContext.getMappingContext());
83 final CompletionStage<CreateSubifReply> createSubifReplyCompletionStage =
84 getFutureJVpp().createSubif(getCreateSubifRequest(subInterface, superIfIndex));
86 final CreateSubifReply reply =
87 getReplyForCreate(createSubifReplyCompletionStage.toCompletableFuture(), id, subInterface);
89 setInterfaceState(id, reply.swIfIndex, booleanToByte(subInterface.isEnabled()));
90 interfaceContext.addName(reply.swIfIndex,
91 getSubInterfaceName(superIfName, Math.toIntExact(subInterface.getIdentifier())),
92 writeContext.getMappingContext());
93 LOG.debug("Sub interface created successfully for: {}, subInterface: {}", superIfName, subInterface);
96 private CreateSubif getCreateSubifRequest(@Nonnull final SubInterface subInterface, final int swIfIndex) {
97 // TODO HONEYCOMB-183 add validation
98 CreateSubif request = new CreateSubif();
99 request.subId = Math.toIntExact(subInterface.getIdentifier().intValue());
100 request.swIfIndex = swIfIndex;
102 final int numberOfTags = getNumberOfTags(subInterface.getTags());
103 switch (numberOfTags) {
114 request.dot1Ad = booleanToByte(_802dot1ad.class == subInterface.getVlanType());
116 // TODO HONEYCOMB-183 match should be mandatory
117 final MatchType matchType = subInterface.getMatch().getMatchType();
119 booleanToByte(matchType instanceof VlanTagged && ((VlanTagged) matchType).isMatchExactTags());
120 request.defaultSub = booleanToByte(matchType instanceof Default);
122 if (numberOfTags > 0) {
123 for (final Tag tag : subInterface.getTags().getTag()) {
124 if (tag.getIndex() == 0) {
125 setOuterTag(request, tag);
126 } else if (tag.getIndex() == 1) {
127 setInnerTag(request, tag);
134 private void setOuterTag(final CreateSubif request, final Tag outerTag) {
135 checkState(SVlan.class == outerTag.getDot1qTag().getTagType(), "Service Tag expected at index 0");
136 final Dot1qTag.VlanId vlanId = outerTag.getDot1qTag().getVlanId();
138 request.outerVlanId = dot1qVlanIdToShort(vlanId.getDot1qVlanId());
139 request.outerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
142 private void setInnerTag(final CreateSubif request, final Tag innerTag) {
143 checkState(CVlan.class == innerTag.getDot1qTag().getTagType(), "Customer Tag expected at index 1");
144 final Dot1qTag.VlanId vlanId = innerTag.getDot1qTag().getVlanId();
146 request.innerVlanId = dot1qVlanIdToShort(vlanId.getDot1qVlanId());
147 request.innerVlanIdAny = booleanToByte(Dot1qTag.VlanId.Enumeration.Any.equals(vlanId.getEnumeration()));
150 private static short dot1qVlanIdToShort(@Nullable Dot1qVlanId dot1qVlanId) {
151 if (dot1qVlanId == null) {
152 return 0; // tell VPP that optional argument is missing
154 return dot1qVlanId.getValue().shortValue();
159 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
160 @Nonnull final SubInterface dataBefore, @Nonnull final SubInterface dataAfter,
161 @Nonnull final WriteContext writeContext)
162 throws WriteFailedException {
163 final String subIfaceName = getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
164 Math.toIntExact(dataAfter.getIdentifier()));
165 setInterfaceState(id, interfaceContext.getIndex(subIfaceName, writeContext.getMappingContext()),
166 booleanToByte(dataAfter.isEnabled()));
169 private void setInterfaceState(final InstanceIdentifier<SubInterface> id, final int swIfIndex, final byte enabled)
170 throws WriteFailedException {
171 final SwInterfaceSetFlags swInterfaceSetFlags = new SwInterfaceSetFlags();
172 swInterfaceSetFlags.swIfIndex = swIfIndex;
173 swInterfaceSetFlags.adminUpDown = enabled;
175 final CompletionStage<SwInterfaceSetFlagsReply> swInterfaceSetFlagsReplyFuture =
176 getFutureJVpp().swInterfaceSetFlags(swInterfaceSetFlags);
178 LOG.debug("Updating interface state for interface if={}, enabled: {}", swIfIndex, enabled);
180 SwInterfaceSetFlagsReply reply =
181 getReplyForWrite(swInterfaceSetFlagsReplyFuture.toCompletableFuture(), id);
182 LOG.debug("Interface state updated successfully for interface index: {}, enabled: {}, ctxId: {}",
183 swIfIndex, enabled, reply.context);
187 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
188 @Nonnull final SubInterface dataBefore,
189 @Nonnull final WriteContext writeContext)
190 throws WriteFailedException.DeleteFailedException {
191 throw new UnsupportedOperationException("Sub interface delete is not supported");