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 static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import io.fd.honeycomb.v3po.translate.v3po.util.AbstractInterfaceTypeCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
26 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
27 import io.fd.honeycomb.v3po.translate.write.WriteContext;
28 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
29 import java.util.concurrent.CompletionStage;
30 import javax.annotation.Nonnull;
31 import javax.annotation.Nullable;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanTag;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterface;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.dto.CreateSubif;
41 import org.openvpp.jvpp.dto.CreateSubifReply;
42 import org.openvpp.jvpp.future.FutureJVpp;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Writer Customizer responsible for sub interface creation.<br>
48 * Sends {@code create_subif} message to VPP.<br>
49 * Equivalent of invoking {@code vppclt create subif} command.
51 public class SubInterfaceCustomizer extends AbstractInterfaceTypeCustomizer<SubInterface> {
53 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
54 private final NamingContext interfaceContext;
56 public SubInterfaceCustomizer(@Nonnull final FutureJVpp futureJvpp, @Nonnull final NamingContext interfaceContext) {
58 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
63 public Optional<SubInterface> extract(@Nonnull final InstanceIdentifier<SubInterface> currentId,
64 @Nonnull final DataObject parentData) {
65 return Optional.fromNullable(((VppInterfaceAugmentation) parentData).getSubInterface());
69 protected Class<? extends InterfaceType> getExpectedInterfaceType() {
70 return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class;
74 public void writeInterface(@Nonnull final InstanceIdentifier<SubInterface> id,
75 @Nonnull final SubInterface dataAfter, @Nonnull final WriteContext writeContext)
76 throws WriteFailedException.CreateFailedException {
78 createSubInterface(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
79 } catch (VppApiInvocationException e) {
80 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
84 private void createSubInterface(final String swIfName, final SubInterface subInterface,
85 final WriteContext writeContext) throws VppApiInvocationException {
86 final String superIfName = subInterface.getSuperInterface();
87 final int swIfIndex = interfaceContext.getIndex(superIfName, writeContext.getMappingContext());
88 LOG.debug("Creating sub interface of {}(id={}): name={}, subInterface={}", superIfName, swIfIndex, swIfName, subInterface);
89 final CompletionStage<CreateSubifReply> createSubifReplyCompletionStage =
90 getFutureJVpp().createSubif(getCreateSubifRequest(subInterface, swIfIndex));
92 final CreateSubifReply reply =
93 TranslateUtils.getReply(createSubifReplyCompletionStage.toCompletableFuture());
94 if (reply.retval < 0) {
95 LOG.debug("Failed to create sub interface for: {}, subInterface: {}", swIfName, subInterface);
96 throw new VppApiInvocationException("createSubif", reply.context, reply.retval);
98 LOG.debug("Sub interface created successfully for: {}, subInterface: {}", swIfName, subInterface);
99 // Add new interface to our interface context
100 interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
104 private CreateSubif getCreateSubifRequest(final SubInterface subInterface, final int swIfIndex) {
105 CreateSubif request = new CreateSubif();
106 request.subId = Math.toIntExact(subInterface.getIdentifier().intValue());
107 request.swIfIndex = swIfIndex;
108 switch (subInterface.getNumberOfTags()) {
119 request.dot1Ad = booleanToByte(VlanType._802dot1q.equals(subInterface.getVlanType()));
120 request.exactMatch = booleanToByte(subInterface.isExactMatch());
121 request.defaultSub = booleanToByte(subInterface.isDefaultSubif());
122 request.outerVlanIdAny = booleanToByte(subInterface.isMatchAnyInnerId());
123 request.innerVlanIdAny = booleanToByte(subInterface.isMatchAnyInnerId());
124 request.outerVlanId = vlanTagToChar(subInterface.getOuterId());
125 request.innerVlanId = vlanTagToChar(subInterface.getInnerId());
129 private static char vlanTagToChar(@Nullable VlanTag tag) {
131 return 0; // tell VPP that optional argument is missing
133 return (char)tag.getValue().intValue();
138 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
139 @Nonnull final SubInterface dataBefore, @Nonnull final SubInterface dataAfter,
140 @Nonnull final WriteContext writeContext) throws WriteFailedException.UpdateFailedException {
141 if (dataBefore.equals(dataAfter)) {
142 LOG.debug("dataBefore equals dataAfter, update will not be performed");
145 throw new UnsupportedOperationException("Sub interface update is not supported");
149 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
150 @Nonnull final SubInterface dataBefore, @Nonnull final WriteContext writeContext)
151 throws WriteFailedException.DeleteFailedException {
152 throw new UnsupportedOperationException("Sub interface delete is not supported");