2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ExecutorService;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
22 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.TapPortCommand;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand.VhostUserCommandBuilder;
25 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent;
26 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent;
27 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
28 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
29 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint.InterfaceTypeChoice;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.TapCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.VhostUserCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.Interconnection;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBased;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 import com.google.common.base.Optional;
51 import com.google.common.base.Preconditions;
52 import com.google.common.base.Strings;
53 import com.google.common.eventbus.Subscribe;
54 import com.google.common.util.concurrent.AsyncFunction;
55 import com.google.common.util.concurrent.CheckedFuture;
56 import com.google.common.util.concurrent.Futures;
57 import com.google.common.util.concurrent.ListenableFuture;
59 public class InterfaceManager implements AutoCloseable {
61 private static final Logger LOG = LoggerFactory.getLogger(InterfaceManager.class);
62 private final MountedDataBrokerProvider mountDataProvider;
63 private final VppEndpointLocationProvider vppEndpointLocationProvider;
64 private final ExecutorService netconfWorker;
66 public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider,
67 @Nonnull ExecutorService netconfWorker) {
68 this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider);
69 this.netconfWorker = Preconditions.checkNotNull(netconfWorker);
70 this.vppEndpointLocationProvider = new VppEndpointLocationProvider(dataProvider);
74 public synchronized void vppEndpointChanged(VppEndpointConfEvent event) {
76 switch (event.getDtoModificationType()) {
78 vppEndpointCreated(event.getAfter().get()).get();
81 vppEndpointDeleted(event.getBefore().get()).get();
82 vppEndpointCreated(event.getAfter().get()).get();
85 vppEndpointDeleted(event.getBefore().get()).get();
88 } catch (InterruptedException | ExecutionException e) {
89 LOG.error("Failed to update Vpp Endpoint. {}", event, e);
93 private ListenableFuture<Void> vppEndpointCreated(VppEndpoint vppEndpoint) {
94 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
95 Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
96 if (interfaceTypeChoice instanceof VhostUserCase) {
97 potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
98 } else if (interfaceTypeChoice instanceof TapCase) {
99 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
102 if (!potentialIfaceCommand.isPresent()) {
103 LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint);
104 return Futures.immediateFuture(null);
106 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
107 InstanceIdentifier<?> vppNodeIid = vppEndpoint.getVppNodePath();
108 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
109 if (!potentialVppDataProvider.isPresent()) {
110 LOG.debug("Cannot get data broker for mount point {}", vppNodeIid);
111 return Futures.immediateFuture(null);
113 DataBroker vppDataBroker = potentialVppDataProvider.get();
114 return createIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
117 private ListenableFuture<Void> createIfaceOnVpp(ConfigCommand createIfaceWithoutBdCommand,
118 DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
119 ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction();
120 createIfaceWithoutBdCommand.execute(rwTx);
121 return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
124 public ListenableFuture<Void> apply(Void input) {
125 LOG.debug("Create interface on VPP command was successful:\nVPP: {}\nCommand: {}", vppNodeIid,
126 createIfaceWithoutBdCommand);
127 return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint);
132 private ListenableFuture<Void> vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) {
133 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
134 Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
135 if (interfaceTypeChoice instanceof VhostUserCase) {
136 potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
137 } else if (interfaceTypeChoice instanceof TapCase) {
138 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
141 if (!potentialIfaceCommand.isPresent()) {
142 LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint);
143 return Futures.immediateFuture(null);
145 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
146 InstanceIdentifier<?> vppNodeIid = vppEndpoint.getVppNodePath();
147 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
148 if (!potentialVppDataProvider.isPresent()) {
149 LOG.debug("Cannot get data broker for mount point {}", vppNodeIid);
150 return Futures.immediateFuture(null);
152 DataBroker vppDataBroker = potentialVppDataProvider.get();
153 return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
156 private ListenableFuture<Void> deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand,
157 DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
158 ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction();
159 deleteIfaceWithoutBdCommand.execute(rwTx);
161 return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
164 public ListenableFuture<Void> apply(Void input) {
165 LOG.debug("Delete interface on VPP command was successful:\nVPP: {}\nCommand: {}", vppNodeIid,
166 deleteIfaceWithoutBdCommand);
167 return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint);
173 public synchronized void vppNodeChanged(NodeOperEvent event) {
174 switch (event.getDtoModificationType()) {
176 if (event.isAfterConnected()) {
177 // TODO read VppEndpoints or cache them during vppEndpointChanged()
181 if (!event.isBeforeConnected() && event.isAfterConnected()) {
182 // TODO reconciliation - diff between disconnected snapshot and current snapshot
186 if (event.isBeforeConnected()) {
187 // TODO we could do snapshot of VppEndpoints
188 // which can be used for reconciliation
194 private static Optional<ConfigCommand> createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
195 @Nonnull Operations operations) {
196 if (!hasNodeAndInterface(vppEp)) {
197 LOG.debug("Interface command is not created for {}", vppEp);
198 return Optional.absent();
200 VhostUserCommandBuilder builder = VhostUserCommand.builder();
201 builder.setName(vppEp.getVppInterfaceName());
202 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
203 if (interfaceTypeChoice instanceof VhostUserCase) {
204 VhostUserCase vhostUserIface = (VhostUserCase) interfaceTypeChoice;
205 String socket = vhostUserIface.getSocket();
206 if (Strings.isNullOrEmpty(socket)) {
207 LOG.debug("Vhost user interface command is not created because socket is missing. {}", vppEp);
208 return Optional.absent();
210 builder.setSocket(socket);
211 builder.setRole(VhostUserRole.Client);
213 VhostUserCommand vhostUserCommand =
214 builder.setOperation(operations).setDescription(vppEp.getDescription()).build();
215 return Optional.of(vhostUserCommand);
218 private static Optional<ConfigCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
219 @Nonnull Operations operation) {
220 if (!hasNodeAndInterface(vppEp)) {
221 LOG.debug("Interface command is not created for {}", vppEp);
222 return Optional.absent();
224 TapPortCommand.TapPortCommandBuilder builder = TapPortCommand.builder();
225 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
226 if (interfaceTypeChoice instanceof TapCase) {
227 TapCase tapIface = (TapCase) interfaceTypeChoice;
228 String name = tapIface.getName();
229 if (Strings.isNullOrEmpty(name)) {
230 LOG.debug("Tap interface command is not created because name is missing. {}", vppEp);
231 return Optional.absent();
233 builder.setTapName(name);
234 builder.setPhysAddress(tapIface.getPhysicalAddress());
236 TapPortCommand tapPortCommand = builder
237 .setOperation(operation)
238 .setDescription(vppEp.getDescription())
239 .setInterfaceName(vppEp.getVppInterfaceName())
241 return Optional.of(tapPortCommand);
245 * Adds bridge domain to an interface if the interface exist.<br>
246 * It rewrites bridge domain in case it already exist.<br>
247 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update location
248 * when the interface is created successfully.<br>
249 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
250 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
251 * containing message in {@link Exception#getMessage()}
253 * @param bridgeDomainName bridge domain
254 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
255 * {@link ExternalLocationCase} where
256 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
257 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
258 * @return {@link ListenableFuture}
260 public synchronized @Nonnull ListenableFuture<Void> addBridgeDomainToInterface(@Nonnull String bridgeDomainName,
261 @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
262 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
263 InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
264 String interfacePath = epLoc.getExternalNodeConnector();
266 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
267 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
268 if (!optInterfaceIid.isPresent()) {
269 return Futures.immediateFailedFuture(
270 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
272 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
274 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
275 if (!potentialVppDataProvider.isPresent()) {
276 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
279 ReadWriteTransaction rwTx = potentialVppDataProvider.get().newReadWriteTransaction();
280 CheckedFuture<Optional<Interface>, ReadFailedException> futureIface =
281 rwTx.read(LogicalDatastoreType.CONFIGURATION, interfaceIid);
282 return Futures.transform(futureIface, new AsyncFunction<Optional<Interface>, Void>() {
285 public ListenableFuture<Void> apply(Optional<Interface> optIface) throws Exception {
286 if (!optIface.isPresent()) {
287 return Futures.immediateFailedFuture(new Exception("Iterface "
288 + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid));
291 String existingBridgeDomain = resolveBridgeDomain(optIface.get());
292 if (bridgeDomainName.equals(existingBridgeDomain)) {
293 LOG.debug("Bridge domain {} already exists on interface {}", bridgeDomainName, interfacePath);
294 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
295 if (!bridgeDomainPath.equals(epLoc.getExternalNode())) {
296 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
297 .setExternalNode(bridgeDomainPath)
298 .setExternalNodeMountPoint(vppNodeIid)
299 .setExternalNodeConnector(interfacePath)
300 .build(), addrEpWithLoc.getKey());
302 return Futures.immediateFuture(null);
305 InstanceIdentifier<L2> l2Iid =
306 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
307 Optional<L2> optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTx);
308 L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
309 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder().setBridgeDomain(bridgeDomainName).build()).build();
310 rwTx.put(LogicalDatastoreType.CONFIGURATION, l2Iid, l2);
311 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath);
312 return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
315 public ListenableFuture<Void> apply(Void input) {
316 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
317 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
318 .setExternalNode(bridgeDomainPath)
319 .setExternalNodeMountPoint(vppNodeIid)
320 .setExternalNodeConnector(interfacePath)
321 .build(), addrEpWithLoc.getKey());
329 * Removes bridge domain (if exist) from an interface (if exist).<br>
330 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint
333 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
334 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
335 * containing message in {@link Exception#getMessage()}
337 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
338 * {@link ExternalLocationCase} where
339 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
340 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
341 * @return {@link ListenableFuture}
343 public synchronized @Nonnull ListenableFuture<Void> deleteBridgeDomainFromInterface(
344 @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
345 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
346 InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
347 String interfacePath = epLoc.getExternalNodeConnector();
349 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
350 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
351 if (!optInterfaceIid.isPresent()) {
352 return Futures.immediateFailedFuture(
353 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
355 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
357 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
358 if (!potentialVppDataProvider.isPresent()) {
359 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
362 ReadWriteTransaction rwTx = potentialVppDataProvider.get().newReadWriteTransaction();
363 CheckedFuture<Optional<Interface>, ReadFailedException> futureIface =
364 rwTx.read(LogicalDatastoreType.CONFIGURATION, interfaceIid);
365 return Futures.transform(futureIface, new AsyncFunction<Optional<Interface>, Void>() {
368 public ListenableFuture<Void> apply(Optional<Interface> optIface) throws Exception {
369 if (!optIface.isPresent()) {
370 // interface does not exist so we consider job done
371 return Futures.immediateFuture(null);
374 String existingBridgeDomain = resolveBridgeDomain(optIface.get());
375 if (Strings.isNullOrEmpty(existingBridgeDomain)) {
376 LOG.debug("Bridge domain does not exist therefore it is cosidered as"
377 + "deleted for interface {}", interfacePath);
378 // bridge domain does not exist on interface so we consider job done
379 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
380 .setExternalNode(null)
381 .setExternalNodeMountPoint(vppNodeIid)
382 .setExternalNodeConnector(interfacePath)
383 .build(), addrEpWithLoc.getKey());
386 InstanceIdentifier<L2> l2Iid =
387 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
388 rwTx.delete(LogicalDatastoreType.CONFIGURATION, l2Iid);
389 LOG.debug("Deleting bridge domain from interface {}", interfacePath);
390 return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
393 public ListenableFuture<Void> apply(Void input) {
394 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
395 .setExternalNode(null)
396 .setExternalNodeMountPoint(vppNodeIid)
397 .setExternalNodeConnector(interfacePath)
398 .build(), addrEpWithLoc.getKey());
405 private static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) {
406 LocationType locationType = addrEpWithLoc.getAbsoluteLocation().getLocationType();
407 if (!(locationType instanceof ExternalLocationCase)) {
408 throw new IllegalArgumentException("Endpoint does not have external location " + addrEpWithLoc);
410 ExternalLocationCase result = (ExternalLocationCase) locationType;
411 if (result.getExternalNodeMountPoint() == null || result.getExternalNodeConnector() == null) {
412 throw new IllegalArgumentException(
413 "Endpoint does not have external-node-mount-point or external-node-connector " + addrEpWithLoc);
418 private static @Nullable String resolveBridgeDomain(@Nonnull Interface iface) {
419 VppInterfaceAugmentation vppInterfaceAugmentation = iface.getAugmentation(VppInterfaceAugmentation.class);
420 L2 existingL2 = vppInterfaceAugmentation.getL2();
421 if (existingL2 != null) {
422 Interconnection interconnection = existingL2.getInterconnection();
423 if (interconnection instanceof BridgeBased) {
424 return ((BridgeBased) interconnection).getBridgeDomain();
430 private static boolean hasNodeAndInterface(VppEndpoint vppEp) {
431 if (vppEp.getVppNodePath() == null) {
432 LOG.debug("vpp-node is missing. {}", vppEp);
435 if (Strings.isNullOrEmpty(vppEp.getVppInterfaceName())) {
436 LOG.debug("vpp-interface-name is missing. {}", vppEp);
443 public void close() throws Exception {
444 vppEndpointLocationProvider.close();