fb47b6a9f04bfb1215c6d960488b10df95131726
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / v3po / translate / v3po / interfaces / SubInterfaceCustomizer.java
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 io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils.booleanToByte;
20
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.utils.V3poUtils;
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;
45
46 /**
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.
50  */
51 public class SubInterfaceCustomizer extends AbstractInterfaceTypeCustomizer<SubInterface> {
52
53     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
54     private final NamingContext interfaceContext;
55
56     public SubInterfaceCustomizer(@Nonnull final FutureJVpp futureJvpp, @Nonnull final NamingContext interfaceContext) {
57         super(futureJvpp);
58         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
59     }
60
61     @Nonnull
62     @Override
63     public Optional<SubInterface> extract(@Nonnull final InstanceIdentifier<SubInterface> currentId,
64                                           @Nonnull final DataObject parentData) {
65         return Optional.fromNullable(((VppInterfaceAugmentation) parentData).getSubInterface());
66     }
67
68     @Override
69     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
70         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class;
71     }
72
73     @Override
74     public void writeInterface(@Nonnull final InstanceIdentifier<SubInterface> id,
75                                        @Nonnull final SubInterface dataAfter, @Nonnull final WriteContext writeContext)
76             throws WriteFailedException.CreateFailedException {
77         try {
78             createSubInterface(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
79         } catch (VppApiInvocationException e) {
80             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
81         }
82     }
83
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));
91
92         final CreateSubifReply reply =
93                 V3poUtils.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);
97         } else {
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());
101         }
102     }
103
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()) {
109             case 0:
110                 request.noTags = 1;
111                 break;
112             case 1:
113                 request.oneTag = 1;
114                 break;
115             case 2:
116                 request.twoTags = 1;
117                 break;
118         }
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());
126         return request;
127     }
128
129     private static char vlanTagToChar(@Nullable VlanTag tag) {
130         if (tag == null) {
131             return 0; // tell VPP that optional argument is missing
132         } else {
133             return (char)tag.getValue().intValue();
134         }
135     }
136
137     @Override
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");
143             return;
144         }
145         throw new UnsupportedOperationException("Sub interface update is not supported");
146     }
147
148     @Override
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");
153     }
154 }