HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / TapCustomizer.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.translate.v3po.interfaces;
18
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;
41
42 public class TapCustomizer extends AbstractInterfaceTypeCustomizer<Tap> implements MacTranslator, JvppReplyConsumer {
43
44     private static final Logger LOG = LoggerFactory.getLogger(TapCustomizer.class);
45     private final NamingContext interfaceContext;
46
47     public TapCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
48         super(vppApi);
49         this.interfaceContext = interfaceContext;
50     }
51
52     @Override
53     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
54         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.Tap.class;
55     }
56
57     @Override
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);
63     }
64
65     @Override
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();
70
71         final int index;
72         try {
73             index = interfaceContext.getIndex(ifcName, writeContext.getMappingContext());
74         } catch (IllegalArgumentException e) {
75             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
76         }
77
78         modifyTap(id, ifcName, index, dataBefore, dataAfter);
79     }
80
81     @Override
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();
86
87         final int index;
88         try {
89             index = interfaceContext.getIndex(ifcName, writeContext.getMappingContext());
90         } catch (IllegalArgumentException e) {
91             throw new WriteFailedException.DeleteFailedException(id, e);
92         }
93
94         deleteTap(id, ifcName, index, dataBefore, writeContext);
95     }
96
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());
106     }
107
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 =
112                 getFutureJVpp()
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);
117     }
118
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());
129     }
130
131     private TapConnect getTapConnectRequest(final String tapName, final PhysAddress mac, final Long deviceInstance) {
132         final TapConnect tapConnect = new TapConnect();
133         tapConnect.tapName = tapName.getBytes();
134
135         if (mac == null) {
136             tapConnect.useRandomMac = 1;
137             tapConnect.macAddress = new byte[6];
138         } else {
139             tapConnect.useRandomMac = 0;
140             tapConnect.macAddress = parseMac(mac.getValue());
141         }
142
143         if (deviceInstance == null) {
144             tapConnect.renumber = 0;
145         } else {
146             tapConnect.renumber = 1;
147             tapConnect.customDevInstance = Math.toIntExact(deviceInstance);
148         }
149
150         return tapConnect;
151     }
152
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;
158
159         if (mac == null) {
160             tapConnect.useRandomMac = 1;
161             tapConnect.macAddress = new byte[6];
162         } else {
163             tapConnect.useRandomMac = 0;
164             tapConnect.macAddress = parseMac(mac.getValue());
165         }
166
167         if (deviceInstance == null) {
168             tapConnect.renumber = 0;
169         } else {
170             tapConnect.renumber = 1;
171             tapConnect.customDevInstance = Math.toIntExact(deviceInstance);
172         }
173
174         return tapConnect;
175     }
176
177     private TapDelete getTapDeleteRequest(final int swIndex) {
178         final TapDelete tapConnect = new TapDelete();
179         tapConnect.swIfIndex = swIndex;
180         return tapConnect;
181     }
182 }