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 io.fd.honeycomb.translate.vpp.util.AbstractInterfaceTypeCustomizer;
20 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
21 import io.fd.honeycomb.translate.vpp.util.MacTranslator;
22 import io.fd.honeycomb.translate.vpp.util.NamingContext;
23 import io.fd.honeycomb.translate.write.WriteContext;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import io.fd.vpp.jvpp.core.dto.TapConnect;
26 import io.fd.vpp.jvpp.core.dto.TapConnectReply;
27 import io.fd.vpp.jvpp.core.dto.TapDelete;
28 import io.fd.vpp.jvpp.core.dto.TapDeleteReply;
29 import io.fd.vpp.jvpp.core.dto.TapModify;
30 import io.fd.vpp.jvpp.core.dto.TapModifyReply;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Tap;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class TapCustomizer extends AbstractInterfaceTypeCustomizer<Tap> implements MacTranslator, JvppReplyConsumer {
44 private static final Logger LOG = LoggerFactory.getLogger(TapCustomizer.class);
45 private final NamingContext interfaceContext;
47 public TapCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
49 this.interfaceContext = interfaceContext;
53 protected Class<? extends InterfaceType> getExpectedInterfaceType() {
54 return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.Tap.class;
58 protected final void writeInterface(@Nonnull final InstanceIdentifier<Tap> id, @Nonnull final Tap dataAfter,
59 @Nonnull final WriteContext writeContext)
60 throws WriteFailedException {
61 final String ifcName = id.firstKeyOf(Interface.class).getName();
62 createTap(id, ifcName, dataAfter, writeContext);
66 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Tap> id, @Nonnull final Tap dataBefore,
67 @Nonnull final Tap dataAfter, @Nonnull final WriteContext writeContext)
68 throws WriteFailedException {
69 final String ifcName = id.firstKeyOf(Interface.class).getName();
73 index = interfaceContext.getIndex(ifcName, writeContext.getMappingContext());
74 } catch (IllegalArgumentException e) {
75 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
78 modifyTap(id, ifcName, index, dataBefore, dataAfter);
82 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Tap> id, @Nonnull final Tap dataBefore,
83 @Nonnull final WriteContext writeContext)
84 throws WriteFailedException {
85 final String ifcName = id.firstKeyOf(Interface.class).getName();
89 index = interfaceContext.getIndex(ifcName, writeContext.getMappingContext());
90 } catch (IllegalArgumentException e) {
91 throw new WriteFailedException.DeleteFailedException(id, e);
94 deleteTap(id, ifcName, index, dataBefore, writeContext);
97 private void createTap(final InstanceIdentifier<Tap> id, final String swIfName, final Tap tap,
98 final WriteContext writeContext) throws WriteFailedException {
99 LOG.debug("Setting tap interface: {}. Tap: {}", swIfName, tap);
100 final CompletionStage<TapConnectReply> tapConnectFuture = getFutureJVpp()
101 .tapConnect(getTapConnectRequest(tap.getTapName(), tap.getMac(), tap.getDeviceInstance()));
102 final TapConnectReply reply = getReplyForCreate(tapConnectFuture.toCompletableFuture(), id, tap);
103 LOG.debug("Tap set successfully for: {}, tap: {}", swIfName, tap);
104 // Add new interface to our interface context
105 interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
108 private void modifyTap(final InstanceIdentifier<Tap> id, final String swIfName, final int index,
109 final Tap tapBefore, final Tap tapAfter) throws WriteFailedException {
110 LOG.debug("Modifying tap interface: {}. Tap: {}", swIfName, tapAfter);
111 final CompletionStage<TapModifyReply> vxlanAddDelTunnelReplyCompletionStage =
113 .tapModify(getTapModifyRequest(tapAfter.getTapName(), index, tapAfter.getMac(),
114 tapAfter.getDeviceInstance()));
115 getReplyForUpdate(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture(), id, tapBefore, tapAfter);
116 LOG.debug("Tap modified successfully for: {}, tap: {}", swIfName, tapAfter);
119 private void deleteTap(final InstanceIdentifier<Tap> id, final String swIfName, final int index,
120 final Tap dataBefore, final WriteContext writeContext)
121 throws WriteFailedException {
122 LOG.debug("Deleting tap interface: {}. Tap: {}", swIfName, dataBefore);
123 final CompletionStage<TapDeleteReply> vxlanAddDelTunnelReplyCompletionStage =
124 getFutureJVpp().tapDelete(getTapDeleteRequest(index));
125 getReplyForDelete(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture(), id);
126 LOG.debug("Tap deleted successfully for: {}, tap: {}", swIfName, dataBefore);
127 // Remove deleted interface from interface context
128 interfaceContext.removeName(swIfName, writeContext.getMappingContext());
131 private TapConnect getTapConnectRequest(final String tapName, final PhysAddress mac, final Long deviceInstance) {
132 final TapConnect tapConnect = new TapConnect();
133 tapConnect.tapName = tapName.getBytes();
136 tapConnect.useRandomMac = 1;
137 tapConnect.macAddress = new byte[6];
139 tapConnect.useRandomMac = 0;
140 tapConnect.macAddress = parseMac(mac.getValue());
143 if (deviceInstance == null) {
144 tapConnect.renumber = 0;
146 tapConnect.renumber = 1;
147 tapConnect.customDevInstance = Math.toIntExact(deviceInstance);
153 private TapModify getTapModifyRequest(final String tapName, final int swIndex, final PhysAddress mac,
154 final Long deviceInstance) {
155 final TapModify tapConnect = new TapModify();
156 tapConnect.tapName = tapName.getBytes();
157 tapConnect.swIfIndex = swIndex;
160 tapConnect.useRandomMac = 1;
161 tapConnect.macAddress = new byte[6];
163 tapConnect.useRandomMac = 0;
164 tapConnect.macAddress = parseMac(mac.getValue());
167 if (deviceInstance == null) {
168 tapConnect.renumber = 0;
170 tapConnect.renumber = 1;
171 tapConnect.customDevInstance = Math.toIntExact(deviceInstance);
177 private TapDelete getTapDeleteRequest(final int swIndex) {
178 final TapDelete tapConnect = new TapDelete();
179 tapConnect.swIfIndex = swIndex;