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 javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import java.util.concurrent.ExecutionException;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Strings;
17 import com.google.common.eventbus.Subscribe;
18 import com.google.common.util.concurrent.AsyncFunction;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
25 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.LoopbackCommand;
26 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.TapPortCommand;
27 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand;
28 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand.VhostUserCommandBuilder;
29 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent;
30 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent;
31 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.GbpNetconfTransaction;
32 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
33 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
34 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
35 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppRendererProcessingException;
36 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes.InterfaceTypeChoice;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.LoopbackCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.TapCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.VhostUserCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2Builder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.Interconnection;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBased;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 public class InterfaceManager implements AutoCloseable {
62 private static final Logger LOG = LoggerFactory.getLogger(InterfaceManager.class);
63 private final MountedDataBrokerProvider mountDataProvider;
64 private final VppEndpointLocationProvider vppEndpointLocationProvider;
66 public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider) {
67 this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider);
68 this.vppEndpointLocationProvider = new VppEndpointLocationProvider(dataProvider);
72 @SuppressWarnings("OptionalGetWithoutIsPresent")
73 public synchronized void vppEndpointChanged(VppEndpointConfEvent event) {
75 switch (event.getDtoModificationType()) {
77 vppEndpointCreated(event.getAfter().get()).get();
81 vppEndpointUpdated(event.getBefore().get(), event.getAfter().get()).get();
84 vppEndpointDeleted(event.getBefore().get()).get();
87 } catch (InterruptedException | ExecutionException e) {
88 LOG.warn("Failed to update Vpp Endpoint. {}", event, e);
92 private ListenableFuture<Void> vppEndpointCreated(VppEndpoint vppEndpoint) {
93 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
94 LOG.trace("Creating VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice);
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);
100 } else if (interfaceTypeChoice instanceof LoopbackCase){
101 potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT);
104 if (!potentialIfaceCommand.isPresent()) {
105 LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint);
106 return Futures.immediateFuture(null);
108 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
109 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
110 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
111 if (!potentialVppDataProvider.isPresent()) {
112 final String message = "Cannot get data broker for mount point " + vppNodeIid;
114 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
116 DataBroker vppDataBroker = potentialVppDataProvider.get();
117 return createInterfaceWithEndpointLocation(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
120 public ListenableFuture<Void> createInterfaceOnVpp(final ConfigCommand createIfaceWithoutBdCommand,
121 final DataBroker vppDataBroker) {
122 final boolean transactionState = GbpNetconfTransaction.write(vppDataBroker, createIfaceWithoutBdCommand,
123 GbpNetconfTransaction.RETRY_COUNT);
124 if (transactionState) {
125 LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand);
126 return Futures.immediateFuture(null);
128 final String message = "Failed to create Interface on VPP: " + createIfaceWithoutBdCommand;
130 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
134 private ListenableFuture<Void> createInterfaceWithEndpointLocation(final ConfigCommand createIfaceWithoutBdCommand,
135 final DataBroker vppDataBroker,
136 final VppEndpoint vppEndpoint,
137 final InstanceIdentifier<?> vppNodeIid) {
138 final boolean transactionState = GbpNetconfTransaction.write(vppDataBroker, createIfaceWithoutBdCommand,
139 GbpNetconfTransaction.RETRY_COUNT);
140 if (transactionState) {
141 LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppNodeIid,
142 createIfaceWithoutBdCommand);
143 return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint);
145 final String message = "Create interface on VPP command was not successful. VPP: " + vppNodeIid
146 + " Command: " + createIfaceWithoutBdCommand;
148 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
152 private ListenableFuture<Void> vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint,
153 @Nonnull final VppEndpoint newVppEndpoint)
154 throws ExecutionException, InterruptedException {
155 if(!oldVppEndpoint.equals(newVppEndpoint)) {
156 LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint);
157 return Futures.transform(vppEndpointDeleted(oldVppEndpoint),
158 (AsyncFunction<Void, Void>) input -> vppEndpointCreated(newVppEndpoint));
160 LOG.debug("Update skipped, provided before/after vpp endpoints are equal");
161 return Futures.immediateFuture(null);
164 private ListenableFuture<Void> vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) {
165 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
166 LOG.trace("Deleting VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice.toString());
167 Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
168 if (interfaceTypeChoice instanceof VhostUserCase) {
169 potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
170 } else if (interfaceTypeChoice instanceof TapCase) {
171 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
172 } else if (interfaceTypeChoice instanceof LoopbackCase){
173 potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE);
176 if (!potentialIfaceCommand.isPresent()) {
177 LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint);
178 return Futures.immediateFuture(null);
180 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
181 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
182 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
183 if (!potentialVppDataProvider.isPresent()) {
184 final String message = "Cannot get data broker for mount point " + vppNodeIid;
186 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
188 DataBroker vppDataBroker = potentialVppDataProvider.get();
189 return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
192 private ListenableFuture<Void> deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand,
193 DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
194 final boolean transactionState = GbpNetconfTransaction.delete(vppDataBroker, deleteIfaceWithoutBdCommand,
195 GbpNetconfTransaction.RETRY_COUNT);
196 if (transactionState) {
197 LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppNodeIid,
198 deleteIfaceWithoutBdCommand);
199 return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint);
201 final String message = "Delete interface on VPP command was not successful: VPP: " + vppNodeIid +
202 " Command: " + deleteIfaceWithoutBdCommand;
204 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
209 public synchronized void vppNodeChanged(NodeOperEvent event) {
210 switch (event.getDtoModificationType()) {
212 if (event.isAfterConnected()) {
213 // TODO read VppEndpoints or cache them during vppEndpointChanged()
217 if (!event.isBeforeConnected() && event.isAfterConnected()) {
218 // TODO reconciliation - diff between disconnected snapshot and current snapshot
222 if (event.isBeforeConnected()) {
223 // TODO we could do snapshot of VppEndpoints
224 // which can be used for reconciliation
230 public static Optional<ConfigCommand> createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
231 @Nonnull Operations operations) {
232 if (!hasNodeAndInterface(vppEp)) {
233 LOG.debug("Interface command is not created for {}", vppEp);
234 return Optional.absent();
236 VhostUserCommandBuilder builder = VhostUserCommand.builder();
237 builder.setName(vppEp.getVppInterfaceName());
238 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
239 if (interfaceTypeChoice instanceof VhostUserCase) {
240 VhostUserCase vhostUserIface = (VhostUserCase) interfaceTypeChoice;
241 String socket = vhostUserIface.getSocket();
242 if (Strings.isNullOrEmpty(socket)) {
243 LOG.debug("Vhost user interface command is not created because socket is missing. {}", vppEp);
244 return Optional.absent();
246 builder.setSocket(socket);
247 builder.setRole(VhostUserRole.Client);
249 VhostUserCommand vhostUserCommand =
250 builder.setOperation(operations).setDescription(vppEp.getDescription()).build();
251 return Optional.of(vhostUserCommand);
254 private static Optional<ConfigCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
255 @Nonnull Operations operation) {
256 if (!hasNodeAndInterface(vppEp)) {
257 LOG.debug("Interface command is not created for {}", vppEp);
258 return Optional.absent();
260 TapPortCommand.TapPortCommandBuilder builder = TapPortCommand.builder();
261 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
262 if (interfaceTypeChoice instanceof TapCase) {
263 TapCase tapIface = (TapCase) interfaceTypeChoice;
264 String name = tapIface.getName();
265 if (Strings.isNullOrEmpty(name)) {
266 LOG.debug("Tap interface command is not created because name is missing. {}", vppEp);
267 return Optional.absent();
269 builder.setTapName(name);
270 builder.setPhysAddress(tapIface.getPhysicalAddress());
272 TapPortCommand tapPortCommand = builder
273 .setOperation(operation)
274 .setDescription(vppEp.getDescription())
275 .setInterfaceName(vppEp.getVppInterfaceName())
277 return Optional.of(tapPortCommand);
280 private static Optional<ConfigCommand> createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp,
281 @Nonnull Operations operation) {
282 if (!hasNodeAndInterface(vppEp)) {
283 LOG.debug("Interface command is not created for {}", vppEp);
284 return Optional.absent();
286 LoopbackCommand.LoopbackCommandBuilder builder = LoopbackCommand.builder();
287 LoopbackCase loopIface = (LoopbackCase) vppEp.getInterfaceTypeChoice();
289 builder.setPhysAddress(loopIface.getPhysAddress());
290 builder.setBvi(loopIface.isBvi());
291 builder.setIpAddress(loopIface.getIpAddress());
292 builder.setIpPrefix(loopIface.getIpPrefix());
294 LoopbackCommand loopbackCommand = builder
295 .setOperation(operation)
296 .setDescription(vppEp.getDescription())
297 .setInterfaceName(vppEp.getVppInterfaceName())
300 return Optional.of(loopbackCommand);
304 * Adds bridge domain to an interface if the interface exist.<br>
305 * It rewrites bridge domain in case it already exist.<br>
306 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update location
307 * when the interface is created successfully.<br>
308 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
309 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
310 * containing message in {@link Exception#getMessage()}
312 * @param bridgeDomainName bridge domain
313 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
314 * {@link ExternalLocationCase} where
315 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
316 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
317 * @return {@link ListenableFuture}
319 public synchronized ListenableFuture<Void> addBridgeDomainToInterface(@Nonnull String bridgeDomainName,
320 @Nonnull AddressEndpointWithLocation addrEpWithLoc,
322 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
323 InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
324 String interfacePath = epLoc.getExternalNodeConnector();
326 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
327 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
328 if (!optInterfaceIid.isPresent()) {
329 return Futures.immediateFailedFuture(
330 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
332 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
333 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
334 if (!potentialVppDataProvider.isPresent()) {
335 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
337 final DataBroker mountpoint = potentialVppDataProvider.get();
338 Optional<Interface> optInterface = GbpNetconfTransaction.read(mountpoint, LogicalDatastoreType.CONFIGURATION,
339 interfaceIid, GbpNetconfTransaction.RETRY_COUNT);
341 if (!optInterface.isPresent()) {
342 return Futures.immediateFailedFuture(new Exception("Interface "
343 + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid));
345 String existingBridgeDomain = resolveBridgeDomain(optInterface.get());
346 if (bridgeDomainName.equals(existingBridgeDomain)) {
347 LOG.debug("Bridge domain {} already exists on interface {}", bridgeDomainName, interfacePath);
348 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
349 if (!bridgeDomainPath.equals(epLoc.getExternalNode())) {
350 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
351 .setExternalNode(bridgeDomainPath)
352 .setExternalNodeMountPoint(vppNodeIid)
353 .setExternalNodeConnector(interfacePath)
354 .build(), addrEpWithLoc.getKey());
356 return Futures.immediateFuture(null);
358 InstanceIdentifier<L2> l2Iid =
359 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
360 Optional<L2> optL2 = GbpNetconfTransaction.read(mountpoint, LogicalDatastoreType.CONFIGURATION,
361 l2Iid, GbpNetconfTransaction.RETRY_COUNT);
362 L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
363 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
364 .setBridgeDomain(bridgeDomainName)
365 .setBridgedVirtualInterface(enableBvi)
367 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath);
368 final boolean transactionState = GbpNetconfTransaction.write(mountpoint, l2Iid, l2,
369 GbpNetconfTransaction.RETRY_COUNT);
370 if (transactionState) {
371 LOG.debug("Adding bridge domain {} to interface {} successful", bridgeDomainName, interfacePath);
372 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
373 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
374 .setExternalNode(bridgeDomainPath)
375 .setExternalNodeMountPoint(vppNodeIid)
376 .setExternalNodeConnector(interfacePath)
377 .build(), addrEpWithLoc.getKey());
379 final String message = "Adding bridge domain " + bridgeDomainName + " to interface " + interfacePath + " failed";
381 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
385 public ListenableFuture<Void> configureInterface(DataBroker mountPoint, InterfaceKey ifaceKey, @Nullable String bridgeDomainName,
386 @Nullable Boolean enableBvi) {
387 L2Builder l2Builder = readL2ForInterface(mountPoint, ifaceKey);
388 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
389 .setBridgeDomain(bridgeDomainName)
390 .setBridgedVirtualInterface(enableBvi)
392 final boolean transactionState = GbpNetconfTransaction.write(mountPoint, VppIidFactory.getL2ForInterfaceIid(ifaceKey),
393 l2, GbpNetconfTransaction.RETRY_COUNT);
394 if (transactionState) {
395 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, VppIidFactory.getInterfaceIID(ifaceKey));
396 return Futures.immediateFuture(null);
398 final String message = "Failed to add bridge domain " + bridgeDomainName + " to interface "
399 + VppIidFactory.getInterfaceIID(ifaceKey);
401 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
405 public ListenableFuture<Void> removeInterfaceFromBridgeDomain(DataBroker mountPoint, InterfaceKey ifaceKey) {
406 L2Builder l2Builder = readL2ForInterface(mountPoint, ifaceKey);
407 if (l2Builder.getInterconnection() == null || !(l2Builder.getInterconnection() instanceof BridgeBased)) {
408 LOG.warn("Interface already not in bridge domain {} ", ifaceKey);
409 return Futures.immediateFuture(null);
411 final boolean transactionState = GbpNetconfTransaction.delete(mountPoint,
412 VppIidFactory.getL2ForInterfaceIid(ifaceKey), GbpNetconfTransaction.RETRY_COUNT);
413 if (transactionState) {
414 LOG.debug("Removing bridge domain from interface {}", VppIidFactory.getInterfaceIID(ifaceKey));
415 return Futures.immediateFuture(null);
417 final String message = "Failed to remove bridge domain from interface "
418 + VppIidFactory.getInterfaceIID(ifaceKey);
420 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
424 private L2Builder readL2ForInterface(DataBroker mountpoint, InterfaceKey ifaceKey) {
425 InstanceIdentifier<L2> l2Iid = VppIidFactory.getL2ForInterfaceIid(ifaceKey);
426 final ReadWriteTransaction rwTxRead = mountpoint.newReadWriteTransaction();
427 Optional<L2> optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead);
428 return (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
432 * Removes bridge domain (if exist) from an interface (if exist).<br>
433 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint
436 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
437 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
438 * containing message in {@link Exception#getMessage()}
440 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
441 * {@link ExternalLocationCase} where
442 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
443 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
444 * @return {@link ListenableFuture}
448 ListenableFuture<Void> deleteBridgeDomainFromInterface(
449 @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
450 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
451 InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
452 String interfacePath = epLoc.getExternalNodeConnector();
454 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
455 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
456 if (!optInterfaceIid.isPresent()) {
457 return Futures.immediateFailedFuture(
458 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
460 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
462 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
463 if (!potentialVppDataProvider.isPresent()) {
464 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
466 final DataBroker mountpoint = potentialVppDataProvider.get();
467 final Optional<Interface> optInterface = GbpNetconfTransaction.read(mountpoint,
468 LogicalDatastoreType.CONFIGURATION, interfaceIid, GbpNetconfTransaction.RETRY_COUNT);
469 if (!optInterface.isPresent()) {
470 // interface does not exist so we consider job done
471 return Futures.immediateFuture(null);
473 String existingBridgeDomain = resolveBridgeDomain(optInterface.get());
474 if (Strings.isNullOrEmpty(existingBridgeDomain)) {
475 LOG.debug("Bridge domain does not exist therefore it is considered as deleted for interface {}",
477 // bridge domain does not exist on interface so we consider job done
478 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
479 .setExternalNode(null)
480 .setExternalNodeMountPoint(vppNodeIid)
481 .setExternalNodeConnector(interfacePath)
482 .build(), addrEpWithLoc.getKey());
484 InstanceIdentifier<L2> l2Iid =
485 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
486 LOG.debug("Deleting bridge domain from interface {}", interfacePath);
487 final boolean transactionState = GbpNetconfTransaction.delete(mountpoint, l2Iid,
488 GbpNetconfTransaction.RETRY_COUNT);
489 if (transactionState) {
490 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
491 .setExternalNode(null)
492 .setExternalNodeMountPoint(vppNodeIid)
493 .setExternalNodeConnector(interfacePath)
494 .build(), addrEpWithLoc.getKey());
496 final String message = "Failed to delete bridge domain from interface " + interfacePath;
498 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
502 private static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) {
503 LocationType locationType = addrEpWithLoc.getAbsoluteLocation().getLocationType();
504 if (!(locationType instanceof ExternalLocationCase)) {
505 throw new IllegalArgumentException("Endpoint does not have external location " + addrEpWithLoc);
507 ExternalLocationCase result = (ExternalLocationCase) locationType;
508 if (result.getExternalNodeMountPoint() == null || result.getExternalNodeConnector() == null) {
509 throw new IllegalArgumentException(
510 "Endpoint does not have external-node-mount-point or external-node-connector " + addrEpWithLoc);
515 private static @Nullable String resolveBridgeDomain(@Nonnull Interface iface) {
516 VppInterfaceAugmentation vppInterfaceAugmentation = iface.getAugmentation(VppInterfaceAugmentation.class);
517 L2 existingL2 = vppInterfaceAugmentation.getL2();
518 if (existingL2 != null) {
519 Interconnection interconnection = existingL2.getInterconnection();
520 if (interconnection instanceof BridgeBased) {
521 return ((BridgeBased) interconnection).getBridgeDomain();
527 private static boolean hasNodeAndInterface(VppEndpoint vppEp) {
528 if (vppEp.getVppNodeId() == null) {
529 LOG.debug("vpp-node is missing. {}", vppEp);
532 if (Strings.isNullOrEmpty(vppEp.getVppInterfaceName())) {
533 LOG.debug("vpp-interface-name is missing. {}", vppEp);
540 public void close() throws Exception {
541 vppEndpointLocationProvider.close();