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.List;
13 import java.util.concurrent.ExecutionException;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.AbstractInterfaceCommand;
22 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.LoopbackCommand;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.TapPortCommand;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand;
25 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand.VhostUserCommandBuilder;
26 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.interfaces.ConfigCommand;
27 import org.opendaylight.groupbasedpolicy.renderer.vpp.config.ConfigUtil;
28 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent;
29 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent;
30 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.flat.overlay.FlatOverlayManager;
31 import org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl.AccessListWrapper;
32 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.GbpNetconfTransaction;
33 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
34 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
35 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
36 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppRendererProcessingException;
37 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
38 import org.opendaylight.vbd.impl.transaction.VbdNetconfTransaction;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.ExcludeFromPolicy;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes.InterfaceTypeChoice;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.LoopbackCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.TapCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.VhostUserCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VhostUserRole;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VppInterfaceAugmentation;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.L2;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.L2Builder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.attributes.Interconnection;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.attributes.interconnection.BridgeBased;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.attributes.interconnection.BridgeBasedBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.common.base.Optional;
66 import com.google.common.base.Preconditions;
67 import com.google.common.base.Strings;
68 import com.google.common.collect.HashMultimap;
69 import com.google.common.collect.SetMultimap;
70 import com.google.common.eventbus.Subscribe;
71 import com.google.common.util.concurrent.MoreExecutors;
72 import com.google.common.util.concurrent.AsyncFunction;
73 import com.google.common.util.concurrent.Futures;
74 import com.google.common.util.concurrent.ListenableFuture;
76 public class InterfaceManager implements AutoCloseable {
78 private static final Logger LOG = LoggerFactory.getLogger(InterfaceManager.class);
79 private final MountedDataBrokerProvider mountDataProvider;
80 private final VppEndpointLocationProvider vppEndpointLocationProvider;
81 private final SetMultimap<NodeId, String> excludedFromPolicy = HashMultimap.create();
82 private final FlatOverlayManager flatOverlayManager;
84 public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider,
85 FlatOverlayManager flatOverlayManager) {
86 this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider);
87 this.vppEndpointLocationProvider = new VppEndpointLocationProvider(dataProvider);
88 this.flatOverlayManager = flatOverlayManager;
92 @SuppressWarnings("OptionalGetWithoutIsPresent")
93 public synchronized void vppEndpointChanged(VppEndpointConfEvent event) {
95 final VppEndpoint oldVppEndpoint = event.getBefore().orNull();
96 final VppEndpoint newVppEndpoint = event.getAfter().orNull();
98 switch (event.getDtoModificationType()) {
100 Preconditions.checkNotNull(newVppEndpoint);
101 vppEndpointCreated(newVppEndpoint).get();
102 message = String.format("Vpp endpoint %s on node %s and interface %s created",
103 newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(),
104 newVppEndpoint.getVppInterfaceName());
105 updatePolicyExcludedEndpoints(newVppEndpoint, true).get();
109 Preconditions.checkNotNull(oldVppEndpoint);
110 Preconditions.checkNotNull(newVppEndpoint);
111 vppEndpointUpdated(oldVppEndpoint, newVppEndpoint).get();
112 message = String.format("Vpp endpoint %s on node %s and interface %s updated",
113 newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(),
114 newVppEndpoint.getVppInterfaceName());
115 updatePolicyExcludedEndpoints(oldVppEndpoint, true).get();
119 Preconditions.checkNotNull(oldVppEndpoint);
120 vppEndpointDeleted(oldVppEndpoint).get();
121 message = String.format("Vpp endpoint %s on node %s and interface %s removed",
122 oldVppEndpoint.getAddress(), oldVppEndpoint.getVppNodeId().getValue(),
123 oldVppEndpoint.getVppInterfaceName());
124 updatePolicyExcludedEndpoints(event.getBefore().get(), false).get();
128 message = "Unknown event modification type: " + event.getDtoModificationType();
129 LOG.error("Failed to process VPP endpoint {}. {}",
130 (oldVppEndpoint != null) ? oldVppEndpoint.getKey() : newVppEndpoint.getKey(),
131 event.getAfter(), new VppRendererProcessingException(message));
135 } catch (InterruptedException | ExecutionException e) {
136 // TODO Auto-generated catch block
137 LOG.error("Failed to process changed vpp endpoint - before: {}, after {}: ", event.getBefore(),
142 private ListenableFuture<Boolean> updatePolicyExcludedEndpoints(VppEndpoint vppEndpoint, boolean created) {
143 if (vppEndpoint == null || vppEndpoint.getAugmentation(ExcludeFromPolicy.class) == null) {
144 return Futures.immediateFuture(true);
147 excludedFromPolicy.put(vppEndpoint.getVppNodeId(), vppEndpoint.getVppInterfaceName());
148 return Futures.immediateFuture(true);
150 return Futures.immediateFuture(excludedFromPolicy.remove(vppEndpoint.getVppNodeId(),
151 vppEndpoint.getVppInterfaceName()));
154 private ListenableFuture<Void> vppEndpointCreated(VppEndpoint vppEndpoint) {
155 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
156 LOG.trace("Creating VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice);
157 Optional<AbstractInterfaceCommand> potentialIfaceCommand = Optional.absent();
158 if (interfaceTypeChoice instanceof VhostUserCase) {
159 potentialIfaceCommand = createVhostInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
160 } else if (interfaceTypeChoice instanceof TapCase) {
161 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
162 } else if (interfaceTypeChoice instanceof LoopbackCase){
163 if (!ConfigUtil.getInstance().isL3FlatEnabled()) {
164 potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT);
167 LOG.trace("L3 flat enabled: Creating of Loopback BVI disabled in InterfaceManager. LISP in VPP renderer will take care of this.");
170 if (!potentialIfaceCommand.isPresent()) {
171 LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint);
172 return Futures.immediateFuture(null);
174 ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
175 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
176 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid);
177 if (!potentialVppDataProvider.isPresent()) {
178 final String message = "Cannot get data broker for mount point " + vppNodeIid;
180 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
182 return createInterfaceWithEndpointLocation(ifaceWithoutBdCommand, vppNodeIid, vppEndpoint);
185 public ListenableFuture<Void> createInterfaceOnVpp(final ConfigCommand createIfaceWithoutBdCommand,
186 final InstanceIdentifier<Node> vppIid) {
187 final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppIid, createIfaceWithoutBdCommand,
188 GbpNetconfTransaction.RETRY_COUNT);
189 if (transactionState) {
190 LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand);
191 return Futures.immediateFuture(null);
193 final String message = "Failed to create Interface on VPP: " + createIfaceWithoutBdCommand;
195 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
199 private ListenableFuture<Void> createInterfaceWithEndpointLocation(final ConfigCommand createIfaceWithoutBdCommand,
200 final InstanceIdentifier<Node> vppIid,
201 final VppEndpoint vppEndpoint) {
202 final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppIid, createIfaceWithoutBdCommand,
203 GbpNetconfTransaction.RETRY_COUNT);
204 if (transactionState) {
205 LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppIid,
206 createIfaceWithoutBdCommand);
207 return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint);
209 final String message = "Create interface on VPP command was not successful. VPP: " + vppIid
210 + " Command: " + createIfaceWithoutBdCommand;
212 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
216 private ListenableFuture<Void> vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint,
217 @Nonnull final VppEndpoint newVppEndpoint) {
218 if(!oldVppEndpoint.equals(newVppEndpoint)) {
219 LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint);
220 return Futures.transformAsync(vppEndpointDeleted(oldVppEndpoint),
221 input -> vppEndpointCreated(newVppEndpoint), MoreExecutors.directExecutor());
223 LOG.debug("Update skipped, provided before/after vpp endpoints are equal");
224 return Futures.immediateFuture(null);
227 private ListenableFuture<Void> vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) {
228 InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
229 LOG.trace("Deleting VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice.toString());
230 Optional<AbstractInterfaceCommand> potentialIfaceCommand = Optional.absent();
231 if (interfaceTypeChoice instanceof VhostUserCase) {
232 potentialIfaceCommand = createVhostInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
233 } else if (interfaceTypeChoice instanceof TapCase) {
234 potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
235 } else if (interfaceTypeChoice instanceof LoopbackCase){
236 if (!ConfigUtil.getInstance().isL3FlatEnabled()) {
237 potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE);
240 LOG.trace("L3 flat enabled: Deleting of Loopback BVI disabled in InterfaceManager. LISP in VPP renderer will take care of this.");
245 if (!potentialIfaceCommand.isPresent()) {
246 LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint);
247 return Futures.immediateFuture(null);
249 AbstractInterfaceCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
250 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
251 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid);
252 if (!potentialVppDataProvider.isPresent()) {
253 final String message = "Cannot get data broker for mount point " + vppNodeIid;
255 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
257 DataBroker vppDataBroker = potentialVppDataProvider.get();
259 if (ConfigUtil.getInstance().isL3FlatEnabled()) {
260 flatOverlayManager.handleInterfaceDeleteForFlatOverlay(vppDataBroker, vppEndpoint);
262 return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppNodeIid, vppEndpoint);
265 private ListenableFuture<Void> deleteIfaceOnVpp(AbstractInterfaceCommand deleteIfaceWithoutBdCommand, InstanceIdentifier<Node> vppIid,
266 VppEndpoint vppEndpoint) {
267 InterfaceBuilder intfBuilder = deleteIfaceWithoutBdCommand.getInterfaceBuilder();
268 final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(vppIid,
269 deleteIfaceWithoutBdCommand, GbpNetconfTransaction.RETRY_COUNT);
270 if (transactionState) {
271 LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppIid,
272 deleteIfaceWithoutBdCommand);
273 AccessListWrapper.removeAclsForInterface(vppIid, new InterfaceKey(intfBuilder.getName()));
274 return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint);
276 final String message = "Delete interface on VPP command was not successful: VPP: " + vppIid
277 + " Command: " + deleteIfaceWithoutBdCommand;
279 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
284 public synchronized void vppNodeChanged(NodeOperEvent event) {
285 switch (event.getDtoModificationType()) {
287 if (event.isAfterConnected()) {
288 // TODO read VppEndpoints or cache them during vppEndpointChanged()
292 if (!event.isBeforeConnected() && event.isAfterConnected()) {
293 // TODO reconciliation - diff between disconnected snapshot and current snapshot
297 if (event.isBeforeConnected()) {
298 // TODO we could do snapshot of VppEndpoints
299 // which can be used for reconciliation
305 private Optional<AbstractInterfaceCommand> createVhostInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
306 @Nonnull Operations operations) {
307 if (!hasNodeAndInterface(vppEp)) {
308 LOG.debug("Interface command is not created for {}", vppEp);
309 return Optional.absent();
311 VhostUserCommandBuilder builder = VhostUserCommand.builder();
312 builder.setName(vppEp.getVppInterfaceName());
313 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
314 if (interfaceTypeChoice instanceof VhostUserCase) {
315 VhostUserCase vhostUserIface = (VhostUserCase) interfaceTypeChoice;
316 String socket = vhostUserIface.getSocket();
317 if (Strings.isNullOrEmpty(socket)) {
318 LOG.debug("Vhost user interface command is not created because socket is missing. {}", vppEp);
319 return Optional.absent();
321 builder.setSocket(socket);
322 builder.setRole(VhostUserRole.Client);
325 if (ConfigUtil.getInstance().isL3FlatEnabled()) {
326 builder.setEnableProxyArp(true);
329 VhostUserCommand vhostUserCommand =
330 builder.setOperation(operations).setDescription(vppEp.getDescription()).build();
331 return Optional.of(vhostUserCommand);
334 private Optional<AbstractInterfaceCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
335 @Nonnull Operations operation) {
336 if (!hasNodeAndInterface(vppEp)) {
337 LOG.debug("Interface command is not created for {}", vppEp);
338 return Optional.absent();
340 TapPortCommand.TapPortCommandBuilder builder = TapPortCommand.builder();
341 InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
342 if (interfaceTypeChoice instanceof TapCase) {
343 TapCase tapIface = (TapCase) interfaceTypeChoice;
344 String name = tapIface.getName();
345 if (Strings.isNullOrEmpty(name)) {
346 LOG.debug("Tap interface command is not created because name is missing. {}", vppEp);
347 return Optional.absent();
349 builder.setTapName(name);
352 if (ConfigUtil.getInstance().isL3FlatEnabled()) {
353 builder.setEnableProxyArp(true);
356 TapPortCommand tapPortCommand = builder
357 .setOperation(operation)
358 .setDescription(vppEp.getDescription())
359 .setInterfaceName(vppEp.getVppInterfaceName())
361 return Optional.of(tapPortCommand);
364 private Optional<AbstractInterfaceCommand> createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp,
365 @Nonnull Operations operation) {
366 if (!hasNodeAndInterface(vppEp)) {
367 LOG.debug("Interface command is not created for {}", vppEp);
368 return Optional.absent();
370 LoopbackCommand.LoopbackCommandBuilder builder = LoopbackCommand.builder();
371 LoopbackCase loopIface = (LoopbackCase) vppEp.getInterfaceTypeChoice();
373 builder.setPhysAddress(loopIface.getPhysAddress());
374 builder.setBvi(loopIface.isBvi());
375 builder.setIpAddress(loopIface.getIpAddress());
376 builder.setIpPrefix(loopIface.getIpPrefix());
378 LoopbackCommand loopbackCommand = builder
379 .setOperation(operation)
380 .setDescription(vppEp.getDescription())
381 .setInterfaceName(vppEp.getVppInterfaceName())
384 return Optional.of(loopbackCommand);
388 * Adds bridge domain to an interface if the interface exist.<br>
389 * It rewrites bridge domain in case it already exist.<br>
390 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update location
391 * when the interface is created successfully.<br>
392 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
393 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
394 * containing message in {@link Exception#getMessage()}
396 * @param bridgeDomainName bridge domain
397 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
398 * {@link ExternalLocationCase} where
399 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
400 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
401 * @param aclWrappers wrappers for ACLs
402 * @param enableBvi BVI enabled/disabled
403 * @return {@link ListenableFuture}
405 public synchronized ListenableFuture<Void> addBridgeDomainToInterface(@Nonnull String bridgeDomainName,
406 @Nonnull AddressEndpointWithLocation addrEpWithLoc,
407 @Nonnull List<AccessListWrapper> aclWrappers,
409 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
410 InstanceIdentifier<Node> vppNodeIid = (InstanceIdentifier<Node>) epLoc.getExternalNodeMountPoint();
411 String interfacePath = epLoc.getExternalNodeConnector();
413 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
414 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
415 if (!optInterfaceIid.isPresent()) {
416 return Futures.immediateFailedFuture(
417 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
419 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
420 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid);
421 if (!potentialVppDataProvider.isPresent()) {
422 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
424 Optional<Interface> optInterface = GbpNetconfTransaction.read(vppNodeIid, LogicalDatastoreType.CONFIGURATION,
425 interfaceIid, GbpNetconfTransaction.RETRY_COUNT);
427 if (!optInterface.isPresent()) {
428 return Futures.immediateFailedFuture(new Exception("Interface "
429 + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid));
431 String existingBridgeDomain = resolveBridgeDomain(optInterface.get());
432 if (bridgeDomainName.equals(existingBridgeDomain)) {
433 LOG.debug("Bridge domain {} already exists on interface {}", bridgeDomainName, interfacePath);
434 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
435 if (!bridgeDomainPath.equals(epLoc.getExternalNode())) {
436 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
437 .setExternalNode(bridgeDomainPath)
438 .setExternalNodeMountPoint(vppNodeIid)
439 .setExternalNodeConnector(interfacePath)
440 .build(), addrEpWithLoc.getKey());
442 return Futures.immediateFuture(null);
444 InstanceIdentifier<L2> l2Iid =
445 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
446 Optional<L2> optL2 = GbpNetconfTransaction.read(vppNodeIid, LogicalDatastoreType.CONFIGURATION,
447 l2Iid, GbpNetconfTransaction.RETRY_COUNT);
448 L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
449 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
450 .setBridgeDomain(bridgeDomainName)
451 .setBridgedVirtualInterface(enableBvi)
453 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath);
454 LOG.info("Debugging L2: iid={}, data={}", l2Iid, l2);
455 final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppNodeIid, l2Iid, l2,
456 GbpNetconfTransaction.RETRY_COUNT);
457 if (transactionState) {
458 LOG.debug("Adding bridge domain {} to interface {} successful", bridgeDomainName, interfacePath);
459 Set<String> excludedIfaces = excludedFromPolicy.get(vppNodeIid.firstKeyOf(Node.class).getNodeId());
460 if (!isExcludedFromPolicy(vppNodeIid.firstKeyOf(Node.class).getNodeId(),
461 interfaceIid.firstKeyOf(Interface.class).getName())) {
462 // can apply ACLs on interfaces in bridge domains
463 aclWrappers.forEach(aclWrapper -> {
464 LOG.debug("Writing access list for interface {} on a node {}.", interfaceIid, vppNodeIid);
465 aclWrapper.writeAcl(vppNodeIid, interfaceIid.firstKeyOf(Interface.class));
466 aclWrapper.writeAclRefOnIface(vppNodeIid, interfaceIid);
469 String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
470 return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
471 .setExternalNode(bridgeDomainPath)
472 .setExternalNodeMountPoint(vppNodeIid)
473 .setExternalNodeConnector(interfacePath)
474 .build(), addrEpWithLoc.getKey());
476 final String message = "Adding bridge domain " + bridgeDomainName + " to interface " + interfacePath + " failed";
478 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
482 public boolean isExcludedFromPolicy(@Nonnull NodeId nodeId,@Nonnull String interfaceName) {
483 Set<String> excludedIfaces = excludedFromPolicy.get(nodeId);
484 if(excludedIfaces != null && excludedIfaces.contains(interfaceName)) {
490 public ListenableFuture<Void> configureInterface(InstanceIdentifier<Node> vppIid, InterfaceKey ifaceKey, @Nullable String bridgeDomainName,
491 @Nullable Boolean enableBvi) {
492 L2Builder l2Builder = readL2ForInterface(vppIid, ifaceKey);
493 L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
494 .setBridgeDomain(bridgeDomainName)
495 .setBridgedVirtualInterface(enableBvi)
497 final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppIid,
498 VppIidFactory.getL2ForInterfaceIid(ifaceKey), l2, GbpNetconfTransaction.RETRY_COUNT);
499 if (transactionState) {
500 LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, VppIidFactory.getInterfaceIID(ifaceKey));
501 return Futures.immediateFuture(null);
503 final String message = "Failed to add bridge domain " + bridgeDomainName + " to interface "
504 + VppIidFactory.getInterfaceIID(ifaceKey);
506 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
510 public ListenableFuture<Void> removeInterfaceFromBridgeDomain(InstanceIdentifier<Node> vppIid, InterfaceKey ifaceKey) {
511 L2Builder l2Builder = readL2ForInterface(vppIid, ifaceKey);
512 if (l2Builder.getInterconnection() == null || !(l2Builder.getInterconnection() instanceof BridgeBased)) {
513 LOG.warn("Interface already not in bridge domain {} ", ifaceKey);
514 return Futures.immediateFuture(null);
516 final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(vppIid,
517 VppIidFactory.getL2ForInterfaceIid(ifaceKey), GbpNetconfTransaction.RETRY_COUNT);
518 if (transactionState) {
519 LOG.debug("Removing bridge domain from interface {}", VppIidFactory.getInterfaceIID(ifaceKey));
520 return Futures.immediateFuture(null);
522 final String message = "Failed to remove bridge domain from interface "
523 + VppIidFactory.getInterfaceIID(ifaceKey);
525 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
529 private L2Builder readL2ForInterface(InstanceIdentifier<Node> vppIid, InterfaceKey ifaceKey) {
530 InstanceIdentifier<L2> l2Iid = VppIidFactory.getL2ForInterfaceIid(ifaceKey);
531 final ReadOnlyTransaction rwTxRead = VbdNetconfTransaction.NODE_DATA_BROKER_MAP.get(vppIid).getKey().newReadOnlyTransaction();
532 Optional<L2> optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead);
534 return (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
538 * Removes bridge domain (if exist) from an interface (if exist).<br>
539 * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint
542 * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
543 * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
544 * containing message in {@link Exception#getMessage()}
546 * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
547 * {@link ExternalLocationCase} where
548 * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
549 * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null}
550 * @return {@link ListenableFuture}
552 public synchronized @Nonnull ListenableFuture<Void> deleteBridgeDomainFromInterface(
553 @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
554 // TODO update ACLs for peers
555 ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
556 InstanceIdentifier<Node> vppNodeIid = (InstanceIdentifier<Node>) epLoc.getExternalNodeMountPoint();
557 String interfacePath = epLoc.getExternalNodeConnector();
559 Optional<InstanceIdentifier<Interface>> optInterfaceIid =
560 VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
561 if (!optInterfaceIid.isPresent()) {
562 return Futures.immediateFailedFuture(
563 new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
565 InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
566 Optional<DataBroker> potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid);
567 if (!potentialVppDataProvider.isPresent()) {
568 return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
570 final Optional<Interface> optInterface = GbpNetconfTransaction.read(vppNodeIid,
571 LogicalDatastoreType.CONFIGURATION, interfaceIid, GbpNetconfTransaction.RETRY_COUNT);
572 if (!optInterface.isPresent()) {
573 // interface does not exist so we consider job done
574 return Futures.immediateFuture(null);
576 String existingBridgeDomain = resolveBridgeDomain(optInterface.get());
577 if (Strings.isNullOrEmpty(existingBridgeDomain)) {
578 LOG.debug("Bridge domain does not exist therefore it is considered as deleted for interface {}",
580 // bridge domain does not exist on interface so we consider job done
581 return vppEndpointLocationProvider.replaceLocationForEndpoint(
582 new ExternalLocationCaseBuilder().setExternalNode(null)
583 .setExternalNodeMountPoint(vppNodeIid)
584 .setExternalNodeConnector(interfacePath)
586 addrEpWithLoc.getKey());
588 InstanceIdentifier<L2> l2Iid =
589 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
590 LOG.debug("Deleting bridge domain from interface {}", interfacePath);
591 final boolean transactionState =
592 GbpNetconfTransaction.netconfSyncedDelete(vppNodeIid, l2Iid, GbpNetconfTransaction.RETRY_COUNT);
593 if (transactionState) {
594 AccessListWrapper.removeAclRefFromIface(vppNodeIid, interfaceIid.firstKeyOf(Interface.class));
595 AccessListWrapper.removeAclsForInterface(vppNodeIid, interfaceIid.firstKeyOf(Interface.class));
596 return vppEndpointLocationProvider.replaceLocationForEndpoint(
597 new ExternalLocationCaseBuilder().setExternalNode(null)
598 .setExternalNodeMountPoint(vppNodeIid)
599 .setExternalNodeConnector(interfacePath)
601 addrEpWithLoc.getKey());
603 final String message = "Failed to delete bridge domain from interface " + interfacePath;
605 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
609 public static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) {
610 LocationType locationType = addrEpWithLoc.getAbsoluteLocation().getLocationType();
611 if (!(locationType instanceof ExternalLocationCase)) {
612 throw new IllegalArgumentException("Endpoint does not have external location " + addrEpWithLoc);
614 ExternalLocationCase result = (ExternalLocationCase) locationType;
615 if (result.getExternalNodeMountPoint() == null || result.getExternalNodeConnector() == null) {
616 throw new IllegalArgumentException(
617 "Endpoint does not have external-node-mount-point or external-node-connector " + addrEpWithLoc);
622 private static @Nullable String resolveBridgeDomain(@Nonnull Interface iface) {
623 VppInterfaceAugmentation vppInterfaceAugmentation = iface.getAugmentation(VppInterfaceAugmentation.class);
624 L2 existingL2 = vppInterfaceAugmentation.getL2();
625 if (existingL2 != null) {
626 Interconnection interconnection = existingL2.getInterconnection();
627 if (interconnection instanceof BridgeBased) {
628 return ((BridgeBased) interconnection).getBridgeDomain();
634 private static boolean hasNodeAndInterface(VppEndpoint vppEp) {
635 if (vppEp.getVppNodeId() == null) {
636 LOG.debug("vpp-node is missing. {}", vppEp);
639 if (Strings.isNullOrEmpty(vppEp.getVppInterfaceName())) {
640 LOG.debug("vpp-interface-name is missing. {}", vppEp);
647 public void close() throws Exception {
648 vppEndpointLocationProvider.close();