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.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
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;
65 public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider) {
66 this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider);
67 this.vppEndpointLocationProvider = new VppEndpointLocationProvider(dataProvider);
71 @SuppressWarnings("OptionalGetWithoutIsPresent")
72 public synchronized void vppEndpointChanged(VppEndpointConfEvent event) {
74 switch (event.getDtoModificationType()) {
76 vppEndpointCreated(event.getAfter().get()).get();
80 vppEndpointUpdated(event.getBefore().get(), event.getAfter().get()).get();
83 vppEndpointDeleted(event.getBefore().get()).get();
86 } catch (InterruptedException | ExecutionException e) {
87 LOG.warn("Failed to update Vpp Endpoint. {}", event, e);
91 private ListenableFuture<Void> vppEndpointCreated(VppEndpoint vppEndpoint) {
92 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
93 LOG.trace("Creating VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice);
94 Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
95 if (interfaceTypeChoice instanceof VhostUserCase) {
96 potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
97 } else if (interfaceTypeChoice instanceof TapCase) {
98 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
99 } else if (interfaceTypeChoice instanceof LoopbackCase){
100 potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT);
103 if (!potentialIfaceCommand.isPresent()) {
104 LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint);
105 return Futures.immediateFuture(null);
107 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
108 InstanceIdentifier<?> vppNodeIid = vppEndpoint.getVppNodePath();
109 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
110 if (!potentialVppDataProvider.isPresent()) {
111 final String message = "Cannot get data broker for mount point " + vppNodeIid;
113 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
115 DataBroker vppDataBroker = potentialVppDataProvider.get();
116 return createInterfaceWithEndpointLocation(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
119 public ListenableFuture<Void> createInterfaceOnVpp(final ConfigCommand createIfaceWithoutBdCommand,
120 final DataBroker vppDataBroker) {
121 final boolean transactionState = GbpNetconfTransaction.write(vppDataBroker, createIfaceWithoutBdCommand,
122 GbpNetconfTransaction.RETRY_COUNT);
123 if (transactionState) {
124 LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand);
125 return Futures.immediateFuture(null);
127 final String message = "Failed to create Interface on VPP: " + createIfaceWithoutBdCommand;
129 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
133 private ListenableFuture<Void> createInterfaceWithEndpointLocation(final ConfigCommand createIfaceWithoutBdCommand,
134 final DataBroker vppDataBroker,
135 final VppEndpoint vppEndpoint,
136 final InstanceIdentifier<?> vppNodeIid) {
137 final boolean transactionState = GbpNetconfTransaction.write(vppDataBroker, createIfaceWithoutBdCommand,
138 GbpNetconfTransaction.RETRY_COUNT);
139 if (transactionState) {
140 LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppNodeIid,
141 createIfaceWithoutBdCommand);
142 return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint);
144 final String message = "Create interface on VPP command was not successful. VPP: " + vppNodeIid
145 + " Command: " + createIfaceWithoutBdCommand;
147 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
151 private ListenableFuture<Void> vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint,
152 @Nonnull final VppEndpoint newVppEndpoint)
153 throws ExecutionException, InterruptedException {
154 if(!oldVppEndpoint.equals(newVppEndpoint)) {
155 LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint);
156 return Futures.transform(vppEndpointDeleted(oldVppEndpoint),
157 (AsyncFunction<Void, Void>) input -> vppEndpointCreated(newVppEndpoint));
159 LOG.debug("Update skipped, provided before/after vpp endpoints are equal");
160 return Futures.immediateFuture(null);
163 private ListenableFuture<Void> vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) {
164 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
165 LOG.trace("Deleting VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice.toString());
166 Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
167 if (interfaceTypeChoice instanceof VhostUserCase) {
168 potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
169 } else if (interfaceTypeChoice instanceof TapCase) {
170 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
171 } else if (interfaceTypeChoice instanceof LoopbackCase){
172 potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE);
175 if (!potentialIfaceCommand.isPresent()) {
176 LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint);
177 return Futures.immediateFuture(null);
179 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
180 InstanceIdentifier<?> vppNodeIid = vppEndpoint.getVppNodePath();
181 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
182 if (!potentialVppDataProvider.isPresent()) {
183 final String message = "Cannot get data broker for mount point " + vppNodeIid;
185 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
187 DataBroker vppDataBroker = potentialVppDataProvider.get();
188 return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
191 private ListenableFuture<Void> deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand,
192 DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
193 final boolean transactionState = GbpNetconfTransaction.delete(vppDataBroker, deleteIfaceWithoutBdCommand,
194 GbpNetconfTransaction.RETRY_COUNT);
195 if (transactionState) {
196 LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppNodeIid,
197 deleteIfaceWithoutBdCommand);
198 return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint);
200 final String message = "Delete interface on VPP command was not successful: VPP: " + vppNodeIid +
201 " Command: " + deleteIfaceWithoutBdCommand;
203 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
208 public synchronized void vppNodeChanged(NodeOperEvent event) {
209 switch (event.getDtoModificationType()) {
211 if (event.isAfterConnected()) {
212 // TODO read VppEndpoints or cache them during vppEndpointChanged()
216 if (!event.isBeforeConnected() && event.isAfterConnected()) {
217 // TODO reconciliation - diff between disconnected snapshot and current snapshot
221 if (event.isBeforeConnected()) {
222 // TODO we could do snapshot of VppEndpoints
223 // which can be used for reconciliation
229 public static Optional<ConfigCommand> createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
230 @Nonnull Operations operations) {
231 if (!hasNodeAndInterface(vppEp)) {
232 LOG.debug("Interface command is not created for {}", vppEp);
233 return Optional.absent();
235 VhostUserCommandBuilder builder = VhostUserCommand.builder();
236 builder.setName(vppEp.getVppInterfaceName());
237 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
238 if (interfaceTypeChoice instanceof VhostUserCase) {
239 VhostUserCase vhostUserIface = (VhostUserCase) interfaceTypeChoice;
240 String socket = vhostUserIface.getSocket();
241 if (Strings.isNullOrEmpty(socket)) {
242 LOG.debug("Vhost user interface command is not created because socket is missing. {}", vppEp);
243 return Optional.absent();
245 builder.setSocket(socket);
246 builder.setRole(VhostUserRole.Client);
248 VhostUserCommand vhostUserCommand =
249 builder.setOperation(operations).setDescription(vppEp.getDescription()).build();
250 return Optional.of(vhostUserCommand);
253 private static Optional<ConfigCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
254 @Nonnull Operations operation) {
255 if (!hasNodeAndInterface(vppEp)) {
256 LOG.debug("Interface command is not created for {}", vppEp);
257 return Optional.absent();
259 TapPortCommand.TapPortCommandBuilder builder = TapPortCommand.builder();
260 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
261 if (interfaceTypeChoice instanceof TapCase) {
262 TapCase tapIface = (TapCase) interfaceTypeChoice;
263 String name = tapIface.getName();
264 if (Strings.isNullOrEmpty(name)) {
265 LOG.debug("Tap interface command is not created because name is missing. {}", vppEp);
266 return Optional.absent();
268 builder.setTapName(name);
269 builder.setPhysAddress(tapIface.getPhysicalAddress());
271 TapPortCommand tapPortCommand = builder
272 .setOperation(operation)
273 .setDescription(vppEp.getDescription())
274 .setInterfaceName(vppEp.getVppInterfaceName())
276 return Optional.of(tapPortCommand);
279 private static Optional<ConfigCommand> createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp,
280 @Nonnull Operations operation) {
281 if (!hasNodeAndInterface(vppEp)) {
282 LOG.debug("Interface command is not created for {}", vppEp);
283 return Optional.absent();
285 LoopbackCommand.LoopbackCommandBuilder builder = LoopbackCommand.builder();
286 LoopbackCase loopIface = (LoopbackCase) vppEp.getInterfaceTypeChoice();
288 builder.setPhysAddress(loopIface.getPhysAddress());
289 builder.setBvi(loopIface.isBvi());
290 builder.setIpAddress(loopIface.getIpAddress());
291 builder.setIpPrefix(loopIface.getIpPrefix());
293 LoopbackCommand loopbackCommand = builder
294 .setOperation(operation)
295 .setDescription(vppEp.getDescription())
296 .setInterfaceName(vppEp.getVppInterfaceName())
299 return Optional.of(loopbackCommand);
303 * Adds bridge domain to an interface if the interface exist.<br>
304 * It rewrites bridge domain in case it already exist.<br>
305 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update location
306 * when the interface is created successfully.<br>
307 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
308 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
309 * containing message in {@link Exception#getMessage()}
311 * @param bridgeDomainName bridge domain
312 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
313 * {@link ExternalLocationCase} where
314 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
315 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
316 * @return {@link ListenableFuture}
318 public synchronized ListenableFuture<Void> addBridgeDomainToInterface(@Nonnull String bridgeDomainName,
319 @Nonnull AddressEndpointWithLocation addrEpWithLoc,
321 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
322 InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
323 String interfacePath = epLoc.getExternalNodeConnector();
325 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
326 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
327 if (!optInterfaceIid.isPresent()) {
328 return Futures.immediateFailedFuture(
329 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
331 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
332 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
333 if (!potentialVppDataProvider.isPresent()) {
334 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
336 final DataBroker mountpoint = potentialVppDataProvider.get();
337 Optional<Interface> optInterface = GbpNetconfTransaction.read(mountpoint, LogicalDatastoreType.CONFIGURATION,
338 interfaceIid, GbpNetconfTransaction.RETRY_COUNT);
340 if (!optInterface.isPresent()) {
341 return Futures.immediateFailedFuture(new Exception("Interface "
342 + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid));
344 String existingBridgeDomain = resolveBridgeDomain(optInterface.get());
345 if (bridgeDomainName.equals(existingBridgeDomain)) {
346 LOG.debug("Bridge domain {} already exists on interface {}", bridgeDomainName, interfacePath);
347 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
348 if (!bridgeDomainPath.equals(epLoc.getExternalNode())) {
349 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
350 .setExternalNode(bridgeDomainPath)
351 .setExternalNodeMountPoint(vppNodeIid)
352 .setExternalNodeConnector(interfacePath)
353 .build(), addrEpWithLoc.getKey());
355 return Futures.immediateFuture(null);
357 InstanceIdentifier<L2> l2Iid =
358 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
359 Optional<L2> optL2 = GbpNetconfTransaction.read(mountpoint, LogicalDatastoreType.CONFIGURATION,
360 l2Iid, GbpNetconfTransaction.RETRY_COUNT);
361 L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
362 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
363 .setBridgeDomain(bridgeDomainName)
364 .setBridgedVirtualInterface(enableBvi)
366 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath);
367 final boolean transactionState = GbpNetconfTransaction.write(mountpoint, l2Iid, l2,
368 GbpNetconfTransaction.RETRY_COUNT);
369 if (transactionState) {
370 LOG.debug("Adding bridge domain {} to interface {} successful", bridgeDomainName, interfacePath);
371 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
372 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
373 .setExternalNode(bridgeDomainPath)
374 .setExternalNodeMountPoint(vppNodeIid)
375 .setExternalNodeConnector(interfacePath)
376 .build(), addrEpWithLoc.getKey());
378 final String message = "Adding bridge domain " + bridgeDomainName + " to interface " + interfacePath + " failed";
380 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
384 public ListenableFuture<Void> configureInterface(DataBroker mountPoint, InterfaceKey ifaceKey, @Nullable String bridgeDomainName,
385 @Nullable Boolean enableBvi) {
386 L2Builder l2Builder = readL2ForInterface(mountPoint, ifaceKey);
387 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
388 .setBridgeDomain(bridgeDomainName)
389 .setBridgedVirtualInterface(enableBvi)
391 final boolean transactionState = GbpNetconfTransaction.write(mountPoint, VppIidFactory.getL2ForInterfaceIid(ifaceKey),
392 l2, GbpNetconfTransaction.RETRY_COUNT);
393 if (transactionState) {
394 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, VppIidFactory.getInterfaceIID(ifaceKey));
395 return Futures.immediateFuture(null);
397 final String message = "Failed to add bridge domain " + bridgeDomainName + " to interface "
398 + VppIidFactory.getInterfaceIID(ifaceKey);
400 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
404 public ListenableFuture<Void> removeInterfaceFromBridgeDomain(DataBroker mountPoint, InterfaceKey ifaceKey) {
405 L2Builder l2Builder = readL2ForInterface(mountPoint, ifaceKey);
406 if (l2Builder.getInterconnection() == null || !(l2Builder.getInterconnection() instanceof BridgeBased)) {
407 LOG.warn("Interface already not in bridge domain {} ", ifaceKey);
408 return Futures.immediateFuture(null);
410 BridgeBased bridgeBased = new BridgeBasedBuilder((BridgeBased) l2Builder.getInterconnection()).setBridgeDomain(
412 L2 l2 = l2Builder.setInterconnection(bridgeBased).build();
413 final boolean transactionState = GbpNetconfTransaction.write(mountPoint, VppIidFactory.getL2ForInterfaceIid(ifaceKey),
414 l2, GbpNetconfTransaction.RETRY_COUNT);
415 if (transactionState) {
416 LOG.debug("Removing bridge domain from interface {}", VppIidFactory.getInterfaceIID(ifaceKey));
417 return Futures.immediateFuture(null);
419 final String message = "Failed to remove bridge domain from interface "
420 + VppIidFactory.getInterfaceIID(ifaceKey);
422 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
427 private L2Builder readL2ForInterface(DataBroker mountpoint, InterfaceKey ifaceKey) {
428 InstanceIdentifier<L2> l2Iid = VppIidFactory.getL2ForInterfaceIid(ifaceKey);
429 final ReadWriteTransaction rwTxRead = mountpoint.newReadWriteTransaction();
430 Optional<L2> optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead);
431 return (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
435 * Removes bridge domain (if exist) from an interface (if exist).<br>
436 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint
439 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
440 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
441 * containing message in {@link Exception#getMessage()}
443 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
444 * {@link ExternalLocationCase} where
445 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
446 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
447 * @return {@link ListenableFuture}
451 ListenableFuture<Void> deleteBridgeDomainFromInterface(
452 @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
453 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
454 InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
455 String interfacePath = epLoc.getExternalNodeConnector();
457 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
458 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
459 if (!optInterfaceIid.isPresent()) {
460 return Futures.immediateFailedFuture(
461 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
463 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
465 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
466 if (!potentialVppDataProvider.isPresent()) {
467 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
469 final DataBroker mountpoint = potentialVppDataProvider.get();
470 final Optional<Interface> optInterface = GbpNetconfTransaction.read(mountpoint,
471 LogicalDatastoreType.CONFIGURATION, interfaceIid, GbpNetconfTransaction.RETRY_COUNT);
472 if (!optInterface.isPresent()) {
473 // interface does not exist so we consider job done
474 return Futures.immediateFuture(null);
476 String existingBridgeDomain = resolveBridgeDomain(optInterface.get());
477 if (Strings.isNullOrEmpty(existingBridgeDomain)) {
478 LOG.debug("Bridge domain does not exist therefore it is considered as deleted for interface {}",
480 // bridge domain does not exist on interface so we consider job done
481 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
482 .setExternalNode(null)
483 .setExternalNodeMountPoint(vppNodeIid)
484 .setExternalNodeConnector(interfacePath)
485 .build(), addrEpWithLoc.getKey());
487 InstanceIdentifier<L2> l2Iid =
488 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
489 LOG.debug("Deleting bridge domain from interface {}", interfacePath);
490 final boolean transactionState = GbpNetconfTransaction.delete(mountpoint, l2Iid,
491 GbpNetconfTransaction.RETRY_COUNT);
492 if (transactionState) {
493 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
494 .setExternalNode(null)
495 .setExternalNodeMountPoint(vppNodeIid)
496 .setExternalNodeConnector(interfacePath)
497 .build(), addrEpWithLoc.getKey());
499 final String message = "Failed to delete bridge domain from interface " + interfacePath;
501 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
505 private static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) {
506 LocationType locationType = addrEpWithLoc.getAbsoluteLocation().getLocationType();
507 if (!(locationType instanceof ExternalLocationCase)) {
508 throw new IllegalArgumentException("Endpoint does not have external location " + addrEpWithLoc);
510 ExternalLocationCase result = (ExternalLocationCase) locationType;
511 if (result.getExternalNodeMountPoint() == null || result.getExternalNodeConnector() == null) {
512 throw new IllegalArgumentException(
513 "Endpoint does not have external-node-mount-point or external-node-connector " + addrEpWithLoc);
518 private static @Nullable String resolveBridgeDomain(@Nonnull Interface iface) {
519 VppInterfaceAugmentation vppInterfaceAugmentation = iface.getAugmentation(VppInterfaceAugmentation.class);
520 L2 existingL2 = vppInterfaceAugmentation.getL2();
521 if (existingL2 != null) {
522 Interconnection interconnection = existingL2.getInterconnection();
523 if (interconnection instanceof BridgeBased) {
524 return ((BridgeBased) interconnection).getBridgeDomain();
530 private static boolean hasNodeAndInterface(VppEndpoint vppEp) {
531 if (vppEp.getVppNodePath() == null) {
532 LOG.debug("vpp-node is missing. {}", vppEp);
535 if (Strings.isNullOrEmpty(vppEp.getVppInterfaceName())) {
536 LOG.debug("vpp-interface-name is missing. {}", vppEp);
543 public void close() throws Exception {
544 vppEndpointLocationProvider.close();