X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=renderers%2Fvpp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fgroupbasedpolicy%2Frenderer%2Fvpp%2Fiface%2FInterfaceManager.java;h=07c90336243fa896eb21704d5885c422e3730684;hb=634c1483eb0ce65b0f9947afca4c2fc03f62014d;hp=c4940e2162c277129b4e012955c463201257bb58;hpb=433e422d60c59f47b76682f18affa161d0301be4;p=groupbasedpolicy.git diff --git a/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/iface/InterfaceManager.java b/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/iface/InterfaceManager.java index c4940e216..07c903362 100644 --- a/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/iface/InterfaceManager.java +++ b/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/iface/InterfaceManager.java @@ -8,21 +8,56 @@ package org.opendaylight.groupbasedpolicy.renderer.vpp.iface; +import java.util.List; +import java.util.Set; + import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.google.common.eventbus.Subscribe; +import com.google.common.util.concurrent.AsyncFunction; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand; +import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.LoopbackCommand; +import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.TapPortCommand; import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand; import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand.VhostUserCommandBuilder; import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent; import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent; +import org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl.AccessListWrapper; +import org.opendaylight.groupbasedpolicy.renderer.vpp.util.GbpNetconfTransaction; import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations; import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider; +import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory; +import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppRendererProcessingException; +import org.opendaylight.groupbasedpolicy.util.DataStoreHelper; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.ExcludeFromPolicy; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes.InterfaceTypeChoice; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.LoopbackCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.TapCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.VhostUserCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint.InterfaceTypeChoice; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.VhostUserCase; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.Interconnection; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBased; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,15 +65,15 @@ import org.slf4j.LoggerFactory; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import com.google.common.eventbus.Subscribe; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.SetMultimap; public class InterfaceManager implements AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(InterfaceManager.class); private final MountedDataBrokerProvider mountDataProvider; private final VppEndpointLocationProvider vppEndpointLocationProvider; + private final SetMultimap excludedFromPolicy = HashMultimap.create(); public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider) { this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider); @@ -46,97 +81,207 @@ public class InterfaceManager implements AutoCloseable { } @Subscribe - public void vppEndpointChanged(VppEndpointConfEvent event) { + @SuppressWarnings("OptionalGetWithoutIsPresent") + public synchronized void vppEndpointChanged(VppEndpointConfEvent event) { + ListenableFuture modificationFuture; + ListenableFuture policyExcludedFuture; + String message; + final VppEndpoint oldVppEndpoint = event.getBefore().orNull(); + final VppEndpoint newVppEndpoint = event.getAfter().orNull(); switch (event.getDtoModificationType()) { - case CREATED: - vppEndpointCreated(event.getAfter().get()); - break; - case UPDATED: - vppEndpointDeleted(event.getBefore().get()); - vppEndpointCreated(event.getAfter().get()); - break; - case DELETED: - vppEndpointDeleted(event.getBefore().get()); - break; + case CREATED: { + Preconditions.checkNotNull(newVppEndpoint); + modificationFuture = vppEndpointCreated(newVppEndpoint); + message = String.format("Vpp endpoint %s on node %s and interface %s created", + newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(), + newVppEndpoint.getVppInterfaceName()); + policyExcludedFuture = updatePolicyExcludedEndpoints(newVppEndpoint, true); + } + break; + case UPDATED: { + Preconditions.checkNotNull(oldVppEndpoint); + Preconditions.checkNotNull(newVppEndpoint); + modificationFuture = vppEndpointUpdated(oldVppEndpoint, newVppEndpoint); + message = String.format("Vpp endpoint %s on node %s and interface %s updated", + newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(), + newVppEndpoint.getVppInterfaceName()); + final ListenableFuture partialOldPolicyExcludedFuture = + updatePolicyExcludedEndpoints(oldVppEndpoint, false); + policyExcludedFuture = + Futures.transform(partialOldPolicyExcludedFuture, (AsyncFunction) input -> + updatePolicyExcludedEndpoints(newVppEndpoint, true)); + } + break; + case DELETED: { + Preconditions.checkNotNull(oldVppEndpoint); + modificationFuture = vppEndpointDeleted(oldVppEndpoint); + message = String.format("Vpp endpoint %s on node %s and interface %s removed", + oldVppEndpoint.getAddress(), oldVppEndpoint.getVppNodeId().getValue(), + oldVppEndpoint.getVppInterfaceName()); + policyExcludedFuture = updatePolicyExcludedEndpoints(event.getBefore().get(), false); + } + break; + default: { + message = "Unknown event modification type: " + event.getDtoModificationType(); + modificationFuture = Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + policyExcludedFuture = Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } + // Modification + Futures.addCallback(modificationFuture, new FutureCallback() { + @Override + public void onSuccess(@Nullable Void result) { + LOG.info(message); + } + + @Override + public void onFailure(@Nonnull Throwable t) { + LOG.warn("Vpp endpoint change event failed. Old ep: {}, new ep: {}", oldVppEndpoint, newVppEndpoint); + } + }); + + // Excluded policy + Futures.addCallback(policyExcludedFuture, new FutureCallback() { + @Override + public void onSuccess(@Nullable Boolean input) { + // NO-OP + } + + @Override + public void onFailure(@Nonnull Throwable throwable) { + LOG.warn("Vpp endpoint exclusion failed. Odl ep: {}, new ep: {}", oldVppEndpoint, newVppEndpoint); + } + }); + } + + private ListenableFuture updatePolicyExcludedEndpoints(VppEndpoint vppEndpoint, boolean created) { + if (vppEndpoint == null || vppEndpoint.getAugmentation(ExcludeFromPolicy.class) == null) { + return Futures.immediateFuture(true); } + if (created) { + excludedFromPolicy.put(vppEndpoint.getVppNodeId(), vppEndpoint.getVppInterfaceName()); + return Futures.immediateFuture(true); + } + return Futures.immediateFuture(excludedFromPolicy.remove(vppEndpoint.getVppNodeId(), + vppEndpoint.getVppInterfaceName())); } - private void vppEndpointCreated(VppEndpoint vppEndpoint) { - Optional potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT); + private ListenableFuture vppEndpointCreated(VppEndpoint vppEndpoint) { + InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice(); + LOG.trace("Creating VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice); + Optional potentialIfaceCommand = Optional.absent(); + if (interfaceTypeChoice instanceof VhostUserCase) { + potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT); + } else if (interfaceTypeChoice instanceof TapCase) { + potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT); + } else if (interfaceTypeChoice instanceof LoopbackCase){ + potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT); + } if (!potentialIfaceCommand.isPresent()) { - return; + LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint); + return Futures.immediateFuture(null); } ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get(); - InstanceIdentifier vppNodeIid = vppEndpoint.getVppNodePath(); + InstanceIdentifier vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId()); Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); if (!potentialVppDataProvider.isPresent()) { - LOG.debug("Cannot get data broker for mount point {}", vppNodeIid); - return; + final String message = "Cannot get data broker for mount point " + vppNodeIid; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); } DataBroker vppDataBroker = potentialVppDataProvider.get(); - createIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid); + return createInterfaceWithEndpointLocation(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid); } - private void createIfaceOnVpp(ConfigCommand createIfaceWithoutBdCommand, DataBroker vppDataBroker, - VppEndpoint vppEndpoint, InstanceIdentifier vppNodeIid) { - ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction(); - createIfaceWithoutBdCommand.execute(rwTx); - Futures.addCallback(rwTx.submit(), new FutureCallback() { + public ListenableFuture createInterfaceOnVpp(final ConfigCommand createIfaceWithoutBdCommand, + final DataBroker vppDataBroker) { + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppDataBroker, createIfaceWithoutBdCommand, + GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand); + return Futures.immediateFuture(null); + } else { + final String message = "Failed to create Interface on VPP: " + createIfaceWithoutBdCommand; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } - @Override - public void onSuccess(Void result) { - LOG.debug("Create interface on VPP command was successful:\nVPP: {}\nCommand: {}", vppNodeIid, - createIfaceWithoutBdCommand); - vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint); - } + private ListenableFuture createInterfaceWithEndpointLocation(final ConfigCommand createIfaceWithoutBdCommand, + final DataBroker vppDataBroker, + final VppEndpoint vppEndpoint, + final InstanceIdentifier vppNodeIid) { + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppDataBroker, createIfaceWithoutBdCommand, + GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppNodeIid, + createIfaceWithoutBdCommand); + return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint); + } else { + final String message = "Create interface on VPP command was not successful. VPP: " + vppNodeIid + + " Command: " + createIfaceWithoutBdCommand; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } - @Override - public void onFailure(Throwable t) { - LOG.error("Create interface on VPP command was NOT successful:\nVPP: {}\nCommand: {}", vppNodeIid, - createIfaceWithoutBdCommand, t); - } - }); + private ListenableFuture vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint, + @Nonnull final VppEndpoint newVppEndpoint) { + if(!oldVppEndpoint.equals(newVppEndpoint)) { + LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint); + return Futures.transform(vppEndpointDeleted(oldVppEndpoint), + (AsyncFunction) input -> vppEndpointCreated(newVppEndpoint)); + } + LOG.debug("Update skipped, provided before/after vpp endpoints are equal"); + return Futures.immediateFuture(null); } - private void vppEndpointDeleted(VppEndpoint vppEndpoint) { - Optional potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE); + private ListenableFuture vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) { + InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice(); + LOG.trace("Deleting VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice.toString()); + Optional potentialIfaceCommand = Optional.absent(); + if (interfaceTypeChoice instanceof VhostUserCase) { + potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE); + } else if (interfaceTypeChoice instanceof TapCase) { + potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE); + } else if (interfaceTypeChoice instanceof LoopbackCase){ + potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE); + } + if (!potentialIfaceCommand.isPresent()) { - return; + LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint); + return Futures.immediateFuture(null); } ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get(); - InstanceIdentifier vppNodeIid = vppEndpoint.getVppNodePath(); + InstanceIdentifier vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId()); Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); if (!potentialVppDataProvider.isPresent()) { - LOG.debug("Cannot get data broker for mount point {}", vppNodeIid); - return; + final String message = "Cannot get data broker for mount point " + vppNodeIid; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); } DataBroker vppDataBroker = potentialVppDataProvider.get(); - deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid); + return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid); } - private void deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand, DataBroker vppDataBroker, - VppEndpoint vppEndpoint, InstanceIdentifier vppNodeIid) { - ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction(); - deleteIfaceWithoutBdCommand.execute(rwTx); - Futures.addCallback(rwTx.submit(), new FutureCallback() { - - @Override - public void onSuccess(Void result) { - LOG.debug("Delete interface on VPP command was successful:\nVPP: {}\nCommand: {}", vppNodeIid, - deleteIfaceWithoutBdCommand); - vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint); - } - - @Override - public void onFailure(Throwable t) { - LOG.error("Delete interface on VPP command was NOT successful:\nVPP: {}\nCommand: {}", vppNodeIid, - deleteIfaceWithoutBdCommand, t); - } - }); + private ListenableFuture deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand, + DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier vppNodeIid) { + final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(vppDataBroker, + deleteIfaceWithoutBdCommand, GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppNodeIid, + deleteIfaceWithoutBdCommand); + return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint); + } else { + final String message = "Delete interface on VPP command was not successful: VPP: " + vppNodeIid + + " Command: " + deleteIfaceWithoutBdCommand; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } } @Subscribe - public void vppNodeChanged(NodeOperEvent event) { + public synchronized void vppNodeChanged(NodeOperEvent event) { switch (event.getDtoModificationType()) { case CREATED: if (event.isAfterConnected()) { @@ -150,14 +295,14 @@ public class InterfaceManager implements AutoCloseable { break; case DELETED: if (event.isBeforeConnected()) { - // TODO we could do snapshot of VppEndpoints + // TODO we could do snapshot of VppEndpoints // which can be used for reconciliation } break; } } - private static Optional createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, + private Optional createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, @Nonnull Operations operations) { if (!hasNodeAndInterface(vppEp)) { LOG.debug("Interface command is not created for {}", vppEp); @@ -181,13 +326,301 @@ public class InterfaceManager implements AutoCloseable { return Optional.of(vhostUserCommand); } + private Optional createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, + @Nonnull Operations operation) { + if (!hasNodeAndInterface(vppEp)) { + LOG.debug("Interface command is not created for {}", vppEp); + return Optional.absent(); + } + TapPortCommand.TapPortCommandBuilder builder = TapPortCommand.builder(); + InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice(); + if (interfaceTypeChoice instanceof TapCase) { + TapCase tapIface = (TapCase) interfaceTypeChoice; + String name = tapIface.getName(); + if (Strings.isNullOrEmpty(name)) { + LOG.debug("Tap interface command is not created because name is missing. {}", vppEp); + return Optional.absent(); + } + builder.setTapName(name); + builder.setPhysAddress(tapIface.getPhysicalAddress()); + } + TapPortCommand tapPortCommand = builder + .setOperation(operation) + .setDescription(vppEp.getDescription()) + .setInterfaceName(vppEp.getVppInterfaceName()) + .build(); + return Optional.of(tapPortCommand); + } + + private Optional createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp, + @Nonnull Operations operation) { + if (!hasNodeAndInterface(vppEp)) { + LOG.debug("Interface command is not created for {}", vppEp); + return Optional.absent(); + } + LoopbackCommand.LoopbackCommandBuilder builder = LoopbackCommand.builder(); + LoopbackCase loopIface = (LoopbackCase) vppEp.getInterfaceTypeChoice(); + + builder.setPhysAddress(loopIface.getPhysAddress()); + builder.setBvi(loopIface.isBvi()); + builder.setIpAddress(loopIface.getIpAddress()); + builder.setIpPrefix(loopIface.getIpPrefix()); + + LoopbackCommand loopbackCommand = builder + .setOperation(operation) + .setDescription(vppEp.getDescription()) + .setInterfaceName(vppEp.getVppInterfaceName()) + .build(); + + return Optional.of(loopbackCommand); + } + + /** + * Adds bridge domain to an interface if the interface exist.
+ * It rewrites bridge domain in case it already exist.
+ * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update location + * when the interface is created successfully.
+ * If the interface does not exist or other problems occur {@link ListenableFuture} will fail + * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception} + * containing message in {@link Exception#getMessage()} + * + * @param bridgeDomainName bridge domain + * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing + * {@link ExternalLocationCase} where + * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null} + * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null} + * @return {@link ListenableFuture} + */ + public synchronized ListenableFuture addBridgeDomainToInterface(@Nonnull String bridgeDomainName, + @Nonnull AddressEndpointWithLocation addrEpWithLoc, + @Nonnull List aclWrappers, + boolean enableBvi) { + ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc); + InstanceIdentifier vppNodeIid = epLoc.getExternalNodeMountPoint(); + String interfacePath = epLoc.getExternalNodeConnector(); + + Optional> optInterfaceIid = + VppPathMapper.interfaceToInstanceIdentifier(interfacePath); + if (!optInterfaceIid.isPresent()) { + return Futures.immediateFailedFuture( + new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath)); + } + InstanceIdentifier interfaceIid = optInterfaceIid.get(); + Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); + if (!potentialVppDataProvider.isPresent()) { + return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid)); + } + final DataBroker mountpoint = potentialVppDataProvider.get(); + Optional optInterface = GbpNetconfTransaction.read(mountpoint, LogicalDatastoreType.CONFIGURATION, + interfaceIid, GbpNetconfTransaction.RETRY_COUNT); + + if (!optInterface.isPresent()) { + return Futures.immediateFailedFuture(new Exception("Interface " + + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid)); + } + String existingBridgeDomain = resolveBridgeDomain(optInterface.get()); + if (bridgeDomainName.equals(existingBridgeDomain)) { + LOG.debug("Bridge domain {} already exists on interface {}", bridgeDomainName, interfacePath); + String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName); + if (!bridgeDomainPath.equals(epLoc.getExternalNode())) { + return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder() + .setExternalNode(bridgeDomainPath) + .setExternalNodeMountPoint(vppNodeIid) + .setExternalNodeConnector(interfacePath) + .build(), addrEpWithLoc.getKey()); + } + return Futures.immediateFuture(null); + } + InstanceIdentifier l2Iid = + interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build(); + Optional optL2 = GbpNetconfTransaction.read(mountpoint, LogicalDatastoreType.CONFIGURATION, + l2Iid, GbpNetconfTransaction.RETRY_COUNT); + L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder(); + L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder() + .setBridgeDomain(bridgeDomainName) + .setBridgedVirtualInterface(enableBvi) + .build()).build(); + LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath); + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(mountpoint, l2Iid, l2, + GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Adding bridge domain {} to interface {} successful", bridgeDomainName, interfacePath); + Set excludedIfaces = excludedFromPolicy.get(vppNodeIid.firstKeyOf(Node.class).getNodeId()); + if(excludedIfaces == null || !excludedIfaces.contains(interfaceIid.firstKeyOf(Interface.class).getName())) { + // can apply ACLs on interfaces in bridge domains + aclWrappers.forEach(aclWrapper -> { + LOG.debug("Writing access list for interface {} on a node {}.", interfaceIid, + vppNodeIid); + aclWrapper.writeAcl(mountpoint, interfaceIid.firstKeyOf(Interface.class)); + aclWrapper.writeAclRefOnIface(mountpoint, interfaceIid); + }); + } + String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName); + return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder() + .setExternalNode(bridgeDomainPath) + .setExternalNodeMountPoint(vppNodeIid) + .setExternalNodeConnector(interfacePath) + .build(), addrEpWithLoc.getKey()); + } else { + final String message = "Adding bridge domain " + bridgeDomainName + " to interface " + interfacePath + " failed"; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } + + public ListenableFuture configureInterface(DataBroker mountPoint, InterfaceKey ifaceKey, @Nullable String bridgeDomainName, + @Nullable Boolean enableBvi) { + L2Builder l2Builder = readL2ForInterface(mountPoint, ifaceKey); + L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder() + .setBridgeDomain(bridgeDomainName) + .setBridgedVirtualInterface(enableBvi) + .build()).build(); + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(mountPoint, + VppIidFactory.getL2ForInterfaceIid(ifaceKey), l2, GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, VppIidFactory.getInterfaceIID(ifaceKey)); + return Futures.immediateFuture(null); + } else { + final String message = "Failed to add bridge domain " + bridgeDomainName + " to interface " + + VppIidFactory.getInterfaceIID(ifaceKey); + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } + + public ListenableFuture removeInterfaceFromBridgeDomain(DataBroker mountPoint, InterfaceKey ifaceKey) { + L2Builder l2Builder = readL2ForInterface(mountPoint, ifaceKey); + if (l2Builder.getInterconnection() == null || !(l2Builder.getInterconnection() instanceof BridgeBased)) { + LOG.warn("Interface already not in bridge domain {} ", ifaceKey); + return Futures.immediateFuture(null); + } + final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(mountPoint, + VppIidFactory.getL2ForInterfaceIid(ifaceKey), GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Removing bridge domain from interface {}", VppIidFactory.getInterfaceIID(ifaceKey)); + return Futures.immediateFuture(null); + } else { + final String message = "Failed to remove bridge domain from interface " + + VppIidFactory.getInterfaceIID(ifaceKey); + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } + + private L2Builder readL2ForInterface(DataBroker mountpoint, InterfaceKey ifaceKey) { + InstanceIdentifier l2Iid = VppIidFactory.getL2ForInterfaceIid(ifaceKey); + final ReadOnlyTransaction rwTxRead = mountpoint.newReadOnlyTransaction(); + Optional optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead); + rwTxRead.close(); + return (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder(); + } + + /** + * Removes bridge domain (if exist) from an interface (if exist).
+ * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint + * location. + *

+ * If the interface does not exist or other problems occur {@link ListenableFuture} will fail + * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception} + * containing message in {@link Exception#getMessage()} + * + * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing + * {@link ExternalLocationCase} where + * {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null} + * and {@link ExternalLocationCase#getExternalNodeConnector()} MUST NOT be {@code null} + * @return {@link ListenableFuture} + */ + public synchronized @Nonnull ListenableFuture deleteBridgeDomainFromInterface( + @Nonnull AddressEndpointWithLocation addrEpWithLoc) { + // TODO update ACLs for peers + ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc); + InstanceIdentifier vppNodeIid = epLoc.getExternalNodeMountPoint(); + String interfacePath = epLoc.getExternalNodeConnector(); + + Optional> optInterfaceIid = + VppPathMapper.interfaceToInstanceIdentifier(interfacePath); + if (!optInterfaceIid.isPresent()) { + return Futures.immediateFailedFuture( + new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath)); + } + InstanceIdentifier interfaceIid = optInterfaceIid.get(); + + Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); + if (!potentialVppDataProvider.isPresent()) { + return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid)); + } + final DataBroker mountpoint = potentialVppDataProvider.get(); + final Optional optInterface = GbpNetconfTransaction.read(mountpoint, + LogicalDatastoreType.CONFIGURATION, interfaceIid, GbpNetconfTransaction.RETRY_COUNT); + if (!optInterface.isPresent()) { + // interface does not exist so we consider job done + return Futures.immediateFuture(null); + } + String existingBridgeDomain = resolveBridgeDomain(optInterface.get()); + if (Strings.isNullOrEmpty(existingBridgeDomain)) { + LOG.debug("Bridge domain does not exist therefore it is considered as deleted for interface {}", + interfacePath); + // bridge domain does not exist on interface so we consider job done + return vppEndpointLocationProvider.replaceLocationForEndpoint( + new ExternalLocationCaseBuilder().setExternalNode(null) + .setExternalNodeMountPoint(vppNodeIid) + .setExternalNodeConnector(interfacePath) + .build(), + addrEpWithLoc.getKey()); + } + InstanceIdentifier l2Iid = + interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build(); + LOG.debug("Deleting bridge domain from interface {}", interfacePath); + final boolean transactionState = + GbpNetconfTransaction.netconfSyncedDelete(mountpoint, l2Iid, GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + AccessListWrapper.removeAclRefFromIface(mountpoint, interfaceIid.firstKeyOf(Interface.class)); + AccessListWrapper.removeAclsForInterface(mountpoint, interfaceIid.firstKeyOf(Interface.class)); + return vppEndpointLocationProvider.replaceLocationForEndpoint( + new ExternalLocationCaseBuilder().setExternalNode(null) + .setExternalNodeMountPoint(vppNodeIid) + .setExternalNodeConnector(interfacePath) + .build(), + addrEpWithLoc.getKey()); + } else { + final String message = "Failed to delete bridge domain from interface " + interfacePath; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + } + + static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) { + LocationType locationType = addrEpWithLoc.getAbsoluteLocation().getLocationType(); + if (!(locationType instanceof ExternalLocationCase)) { + throw new IllegalArgumentException("Endpoint does not have external location " + addrEpWithLoc); + } + ExternalLocationCase result = (ExternalLocationCase) locationType; + if (result.getExternalNodeMountPoint() == null || result.getExternalNodeConnector() == null) { + throw new IllegalArgumentException( + "Endpoint does not have external-node-mount-point or external-node-connector " + addrEpWithLoc); + } + return result; + } + + private static @Nullable String resolveBridgeDomain(@Nonnull Interface iface) { + VppInterfaceAugmentation vppInterfaceAugmentation = iface.getAugmentation(VppInterfaceAugmentation.class); + L2 existingL2 = vppInterfaceAugmentation.getL2(); + if (existingL2 != null) { + Interconnection interconnection = existingL2.getInterconnection(); + if (interconnection instanceof BridgeBased) { + return ((BridgeBased) interconnection).getBridgeDomain(); + } + } + return null; + } + private static boolean hasNodeAndInterface(VppEndpoint vppEp) { - if (vppEp.getVppNodePath() == null) { - LOG.trace("vpp-node is missing. {}", vppEp); + if (vppEp.getVppNodeId() == null) { + LOG.debug("vpp-node is missing. {}", vppEp); return false; } if (Strings.isNullOrEmpty(vppEp.getVppInterfaceName())) { - LOG.trace("vpp-interface-name is missing. {}", vppEp); + LOG.debug("vpp-interface-name is missing. {}", vppEp); return false; } return true; @@ -197,5 +630,4 @@ public class InterfaceManager implements AutoCloseable { public void close() throws Exception { vppEndpointLocationProvider.close(); } - }