X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=renderers%2Fvpp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fgroupbasedpolicy%2Frenderer%2Fvpp%2Fiface%2FInterfaceManager.java;h=8d0ddbf6402f3684170b8a2d647f94a334104c2b;hb=c8d5d6270466ec2a03dd2c1154b1dce174eacebe;hp=d3222249886752288cd9a304c44a1dd4d3d99bf9;hpb=d7b2e8e4b5a7f8daadcd3e8fdaa40967efabd4ae;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 d32222498..8d0ddbf64 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,50 +8,64 @@ package org.opendaylight.groupbasedpolicy.renderer.vpp.iface; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Strings; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.SetMultimap; import com.google.common.eventbus.Subscribe; -import com.google.common.util.concurrent.AsyncFunction; -import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.ExecutionException; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + 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.controller.md.sal.common.api.data.ReadFailedException; -import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand; +import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.AbstractInterfaceCommand; 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.commands.interfaces.ConfigCommand; +import org.opendaylight.groupbasedpolicy.renderer.vpp.config.ConfigUtil; 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.vbd.impl.transaction.VbdNetconfTransaction; 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.LoopbackCase; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.TapCase; -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.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.yangtools.yang.binding.DataObject; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VhostUserRole; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.L2; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.L2Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.attributes.Interconnection; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.attributes.interconnection.BridgeBased; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.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; @@ -61,90 +75,145 @@ public class InterfaceManager implements AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(InterfaceManager.class); private final MountedDataBrokerProvider mountDataProvider; private final VppEndpointLocationProvider vppEndpointLocationProvider; - private final ExecutorService netconfWorker; + private final SetMultimap excludedFromPolicy = HashMultimap.create(); - public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider, - @Nonnull ExecutorService netconfWorker) { + public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider) { this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider); - this.netconfWorker = Preconditions.checkNotNull(netconfWorker); this.vppEndpointLocationProvider = new VppEndpointLocationProvider(dataProvider); } @Subscribe @SuppressWarnings("OptionalGetWithoutIsPresent") public synchronized void vppEndpointChanged(VppEndpointConfEvent event) { + String message; + final VppEndpoint oldVppEndpoint = event.getBefore().orNull(); + final VppEndpoint newVppEndpoint = event.getAfter().orNull(); try { - switch (event.getDtoModificationType()) { - case CREATED: { - vppEndpointCreated(event.getAfter().get()).get(); - } - break; - case UPDATED: - vppEndpointUpdated(event.getBefore().get(), event.getAfter().get()).get(); - break; - case DELETED: - vppEndpointDeleted(event.getBefore().get()).get(); - break; + switch (event.getDtoModificationType()) { + case CREATED: { + Preconditions.checkNotNull(newVppEndpoint); + vppEndpointCreated(newVppEndpoint).get(); + message = String.format("Vpp endpoint %s on node %s and interface %s created", + newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(), + newVppEndpoint.getVppInterfaceName()); + updatePolicyExcludedEndpoints(newVppEndpoint, true).get(); + } + break; + case UPDATED: { + Preconditions.checkNotNull(oldVppEndpoint); + Preconditions.checkNotNull(newVppEndpoint); + vppEndpointUpdated(oldVppEndpoint, newVppEndpoint).get(); + message = String.format("Vpp endpoint %s on node %s and interface %s updated", + newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(), + newVppEndpoint.getVppInterfaceName()); + updatePolicyExcludedEndpoints(oldVppEndpoint, true).get(); + } + break; + case DELETED: { + Preconditions.checkNotNull(oldVppEndpoint); + vppEndpointDeleted(oldVppEndpoint).get(); + message = String.format("Vpp endpoint %s on node %s and interface %s removed", + oldVppEndpoint.getAddress(), oldVppEndpoint.getVppNodeId().getValue(), + oldVppEndpoint.getVppInterfaceName()); + updatePolicyExcludedEndpoints(event.getBefore().get(), false).get(); } + break; + default: { + message = "Unknown event modification type: " + event.getDtoModificationType(); + LOG.error("Failed to process VPP endpoint {}. {}", + (oldVppEndpoint != null) ? oldVppEndpoint.getKey() : newVppEndpoint.getKey(), + event.getAfter(), new VppRendererProcessingException(message)); + } + } + LOG.info(message); } catch (InterruptedException | ExecutionException e) { - LOG.error("Failed to update Vpp Endpoint. {}", event, e); + LOG.error("Failed to process changed vpp endpoint. before: {}, after: {}.Exception: {} ", event.getBefore(), + event.getAfter(), e); } } + private ListenableFuture updatePolicyExcludedEndpoints(VppEndpoint vppEndpoint, boolean created) { + if (vppEndpoint == null || vppEndpoint.getAugmentation(ExcludeFromPolicy.class) == null) { + return Futures.immediateFuture(true); + } + if (created) { + LOG.trace("Interface excluded from policy: node:{} interface:{}", vppEndpoint.getVppNodeId(), + vppEndpoint.getVppInterfaceName()); + excludedFromPolicy.put(vppEndpoint.getVppNodeId(), vppEndpoint.getVppInterfaceName()); + return Futures.immediateFuture(true); + } + return Futures.immediateFuture(excludedFromPolicy.remove(vppEndpoint.getVppNodeId(), + vppEndpoint.getVppInterfaceName())); + } + private ListenableFuture vppEndpointCreated(VppEndpoint vppEndpoint) { InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice(); LOG.trace("Creating VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice); - Optional potentialIfaceCommand = Optional.absent(); + Optional potentialIfaceCommand = Optional.absent(); if (interfaceTypeChoice instanceof VhostUserCase) { - potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT); + potentialIfaceCommand = createVhostInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT); } else if (interfaceTypeChoice instanceof TapCase) { potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT); } else if (interfaceTypeChoice instanceof LoopbackCase){ - potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT); + if (!ConfigUtil.getInstance().isL3FlatEnabled()) { + potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT); + } + else { + LOG.trace("L3 flat enabled: LISP in VPP renderer will take care of creating loopback."); + } } - if (!potentialIfaceCommand.isPresent()) { LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint); return Futures.immediateFuture(null); } ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get(); - InstanceIdentifier vppNodeIid = vppEndpoint.getVppNodePath(); - Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); + InstanceIdentifier vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId()); + Optional potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid); if (!potentialVppDataProvider.isPresent()) { - LOG.debug("Cannot get data broker for mount point {}", vppNodeIid); + final String message = "Cannot get data broker for mount point " + vppNodeIid; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } + return createInterfaceWithEndpointLocation(ifaceWithoutBdCommand, vppNodeIid, vppEndpoint); + } + + public ListenableFuture createInterfaceOnVpp(final ConfigCommand createIfaceWithoutBdCommand, + final InstanceIdentifier vppIid) { + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppIid, 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)); } - DataBroker vppDataBroker = potentialVppDataProvider.get(); - return createInterfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid); } - private ListenableFuture createInterfaceOnVpp(ConfigCommand createIfaceWithoutBdCommand, DataBroker vppDataBroker, - VppEndpoint vppEndpoint, InstanceIdentifier vppNodeIid) { - final ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction(); - createIfaceWithoutBdCommand.execute(rwTx); - LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand); - return Futures.transform(rwTx.submit(), new AsyncFunction() { - - @Override - public ListenableFuture apply(@Nonnull Void input) { - LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppNodeIid, - createIfaceWithoutBdCommand); - return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint); - } - }, netconfWorker); + private ListenableFuture createInterfaceWithEndpointLocation(final ConfigCommand createIfaceWithoutBdCommand, + final InstanceIdentifier vppIid, + final VppEndpoint vppEndpoint) { + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppIid, createIfaceWithoutBdCommand, + GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppIid, + createIfaceWithoutBdCommand); + return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint); + } else { + final String message = "Create interface on VPP command was not successful. VPP: " + vppIid + + " Command: " + createIfaceWithoutBdCommand; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } } private ListenableFuture vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint, - @Nonnull final VppEndpoint newVppEndpoint) - throws ExecutionException, InterruptedException { + @Nonnull final VppEndpoint newVppEndpoint) { if(!oldVppEndpoint.equals(newVppEndpoint)) { LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint); - return Futures.transform(vppEndpointDeleted(oldVppEndpoint), new AsyncFunction() { - @Override - public ListenableFuture apply(@Nonnull Void input) throws Exception { - return vppEndpointCreated(newVppEndpoint); - } - }); + return Futures.transformAsync(vppEndpointDeleted(oldVppEndpoint), + input -> vppEndpointCreated(newVppEndpoint), MoreExecutors.directExecutor()); } LOG.debug("Update skipped, provided before/after vpp endpoints are equal"); return Futures.immediateFuture(null); @@ -153,45 +222,51 @@ public class InterfaceManager implements AutoCloseable { private ListenableFuture vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) { InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice(); LOG.trace("Deleting VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice.toString()); - Optional potentialIfaceCommand = Optional.absent(); + Optional potentialIfaceCommand = Optional.absent(); if (interfaceTypeChoice instanceof VhostUserCase) { - potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE); + potentialIfaceCommand = createVhostInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE); } else if (interfaceTypeChoice instanceof TapCase) { potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE); } else if (interfaceTypeChoice instanceof LoopbackCase){ - potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE); + if (!ConfigUtil.getInstance().isL3FlatEnabled()) { + potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE); + } + else { + LOG.trace("L3 flat enabled: LISP in VPP renderer will take care of delete for loopback."); + } } + if (!potentialIfaceCommand.isPresent()) { LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint); return Futures.immediateFuture(null); } - ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get(); - InstanceIdentifier vppNodeIid = vppEndpoint.getVppNodePath(); - Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); + AbstractInterfaceCommand ifaceWithoutBdCommand = potentialIfaceCommand.get(); + InstanceIdentifier vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId()); + Optional potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid); if (!potentialVppDataProvider.isPresent()) { - LOG.debug("Cannot get data broker for mount point {}", vppNodeIid); - return Futures.immediateFuture(null); + final String message = "Cannot get data broker for mount point " + vppNodeIid; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); } - DataBroker vppDataBroker = potentialVppDataProvider.get(); - return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid); - } - private ListenableFuture deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand, - DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier vppNodeIid) { - ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction(); - deleteIfaceWithoutBdCommand.execute(rwTx); - LOG.trace("Deleting Interface on VPP: {}", deleteIfaceWithoutBdCommand); - - return Futures.transform(rwTx.submit(), new AsyncFunction() { + return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppNodeIid, vppEndpoint); + } - @Override - public ListenableFuture apply(Void input) { - LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppNodeIid, - deleteIfaceWithoutBdCommand); - return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint); - } - }, netconfWorker); + private ListenableFuture deleteIfaceOnVpp(AbstractInterfaceCommand interfaceCommand, + InstanceIdentifier vppIid, VppEndpoint vppEndpoint) { + final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(vppIid, interfaceCommand, + GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppIid, interfaceCommand); + AccessListWrapper.removeAclsForInterface(vppIid, new InterfaceKey(interfaceCommand.getName())); + return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint); + } else { + final String message = "Delete interface on VPP command was not successful: VPP: " + vppIid + + " Command: " + interfaceCommand; + LOG.warn(message); + return Futures.immediateFailedFuture(new VppRendererProcessingException(message)); + } } @Subscribe @@ -216,8 +291,8 @@ public class InterfaceManager implements AutoCloseable { } } - private static Optional createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, - @Nonnull Operations operations) { + private Optional createVhostInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, + @Nonnull Operations operations) { if (!hasNodeAndInterface(vppEp)) { LOG.debug("Interface command is not created for {}", vppEp); return Optional.absent(); @@ -235,12 +310,16 @@ public class InterfaceManager implements AutoCloseable { builder.setSocket(socket); builder.setRole(VhostUserRole.Client); } + if (ConfigUtil.getInstance().isL3FlatEnabled()) { + builder.setEnableProxyArp(true); + builder.setSnatEnabled(true); + } VhostUserCommand vhostUserCommand = builder.setOperation(operations).setDescription(vppEp.getDescription()).build(); return Optional.of(vhostUserCommand); } - private static Optional createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, + private Optional createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp, @Nonnull Operations operation) { if (!hasNodeAndInterface(vppEp)) { LOG.debug("Interface command is not created for {}", vppEp); @@ -256,8 +335,12 @@ public class InterfaceManager implements AutoCloseable { return Optional.absent(); } builder.setTapName(name); - builder.setPhysAddress(tapIface.getPhysicalAddress()); } + + if (ConfigUtil.getInstance().isL3FlatEnabled()) { + builder.setEnableProxyArp(true); + } + TapPortCommand tapPortCommand = builder .setOperation(operation) .setDescription(vppEp.getDescription()) @@ -266,7 +349,7 @@ public class InterfaceManager implements AutoCloseable { return Optional.of(tapPortCommand); } - private static Optional createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp, + private Optional createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp, @Nonnull Operations operation) { if (!hasNodeAndInterface(vppEp)) { LOG.debug("Interface command is not created for {}", vppEp); @@ -299,16 +382,19 @@ public class InterfaceManager implements AutoCloseable { * 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} + * @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} + * @param aclWrappers wrappers for ACLs + * @param enableBvi BVI enabled/disabled * @return {@link ListenableFuture} */ - public synchronized @Nonnull ListenableFuture addBridgeDomainToInterface(@Nonnull String bridgeDomainName, - @Nonnull AddressEndpointWithLocation addrEpWithLoc, boolean enableBvi) { + public synchronized ListenableFuture addBridgeDomainToInterface(@Nonnull String bridgeDomainName, + @Nonnull AddressEndpointWithLocation addrEpWithLoc, @Nonnull List aclWrappers, + boolean enableBvi) { ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc); - InstanceIdentifier vppNodeIid = epLoc.getExternalNodeMountPoint(); + InstanceIdentifier vppNodeIid = (InstanceIdentifier) epLoc.getExternalNodeMountPoint(); String interfacePath = epLoc.getExternalNodeConnector(); Optional> optInterfaceIid = @@ -318,62 +404,125 @@ public class InterfaceManager implements AutoCloseable { new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath)); } InstanceIdentifier interfaceIid = optInterfaceIid.get(); - - Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); + Optional potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid); if (!potentialVppDataProvider.isPresent()) { return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid)); } - final DataBroker mountpoint = potentialVppDataProvider.get(); - final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction(); - CheckedFuture, ReadFailedException> futureIface = - rwTx.read(LogicalDatastoreType.CONFIGURATION, interfaceIid); - return Futures.transform(futureIface, new AsyncFunction, Void>() { - - @Override - public ListenableFuture apply(Optional optIface) throws Exception { - if (!optIface.isPresent()) { - return Futures.immediateFailedFuture(new Exception("Interface " - + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid)); - } + Optional optInterface = GbpNetconfTransaction.read(vppNodeIid, LogicalDatastoreType.CONFIGURATION, + interfaceIid, GbpNetconfTransaction.RETRY_COUNT); - String existingBridgeDomain = resolveBridgeDomain(optIface.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(); - final ReadWriteTransaction rwTxRead = mountpoint.newReadWriteTransaction(); - Optional optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead); - L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder(); - L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder() - .setBridgeDomain(bridgeDomainName) - .setBridgedVirtualInterface(enableBvi) - .build()).build(); - final ReadWriteTransaction rwTxPut = prepareTransactionAndPutData(mountpoint, l2, l2Iid); - LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath); - return Futures.transform(rwTxPut.submit(), new AsyncFunction() { - - @Override - public ListenableFuture apply(@Nonnull Void input) { - String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName); - return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder() - .setExternalNode(bridgeDomainPath) - .setExternalNodeMountPoint(vppNodeIid) - .setExternalNodeConnector(interfacePath) - .build(), addrEpWithLoc.getKey()); - } - }, netconfWorker); + 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()); } - }, netconfWorker); + return Futures.immediateFuture(null); + } + InstanceIdentifier l2Iid = + interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build(); + Optional optL2 = GbpNetconfTransaction.read(vppNodeIid, 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); + LOG.info("Debugging L2: iid={}, data={}", l2Iid, l2); + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppNodeIid, 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 (!isExcludedFromPolicy(vppNodeIid.firstKeyOf(Node.class).getNodeId(), + 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(vppNodeIid, interfaceIid.firstKeyOf(Interface.class)); + aclWrapper.writeAclRefOnIface(vppNodeIid, 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 boolean isExcludedFromPolicy(@Nonnull NodeId nodeId,@Nonnull String interfaceName) { + Set excludedIfaces = excludedFromPolicy.get(nodeId); + if(excludedIfaces != null && excludedIfaces.contains(interfaceName)) { + return true; + } + return false; + } + + public ListenableFuture configureInterface(InstanceIdentifier vppIid, InterfaceKey ifaceKey, + @Nullable String bridgeDomainName, @Nullable Boolean enableBvi) { + L2Builder l2Builder = readL2ForInterface(vppIid, ifaceKey); + L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder() + .setBridgeDomain(bridgeDomainName) + .setBridgedVirtualInterface(enableBvi) + .build()).build(); + final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppIid, + 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(InstanceIdentifier vppIid, + InterfaceKey ifaceKey) { + L2Builder l2Builder = readL2ForInterface(vppIid, 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(vppIid, + 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(InstanceIdentifier vppIid, InterfaceKey ifaceKey) { + InstanceIdentifier l2Iid = VppIidFactory.getL2ForInterfaceIid(ifaceKey); + final ReadOnlyTransaction rwTxRead = VbdNetconfTransaction.NODE_DATA_BROKER_MAP.get(vppIid).getKey() + .newReadOnlyTransaction(); + Optional optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead); + rwTxRead.close(); + return (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder(); } /** @@ -386,15 +535,16 @@ public class InterfaceManager implements AutoCloseable { * 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} + * {@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(); + InstanceIdentifier vppNodeIid = (InstanceIdentifier) epLoc.getExternalNodeMountPoint(); String interfacePath = epLoc.getExternalNodeConnector(); Optional> optInterfaceIid = @@ -404,54 +554,50 @@ public class InterfaceManager implements AutoCloseable { new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath)); } InstanceIdentifier interfaceIid = optInterfaceIid.get(); - - Optional potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid); + Optional potentialVppDataProvider = mountDataProvider.resolveDataBrokerForMountPoint(vppNodeIid); if (!potentialVppDataProvider.isPresent()) { return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid)); } - - ReadWriteTransaction rwTx = potentialVppDataProvider.get().newReadWriteTransaction(); - CheckedFuture, ReadFailedException> futureIface = - rwTx.read(LogicalDatastoreType.CONFIGURATION, interfaceIid); - return Futures.transform(futureIface, new AsyncFunction, Void>() { - - @Override - public ListenableFuture apply(Optional optIface) throws Exception { - if (!optIface.isPresent()) { - // interface does not exist so we consider job done - return Futures.immediateFuture(null); - } - String existingBridgeDomain = resolveBridgeDomain(optIface.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(); - rwTx.delete(LogicalDatastoreType.CONFIGURATION, l2Iid); - LOG.debug("Deleting bridge domain from interface {}", interfacePath); - return Futures.transform(rwTx.submit(), new AsyncFunction() { - - @Override - public ListenableFuture apply(@Nonnull Void input) { - return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder() - .setExternalNode(null) - .setExternalNodeMountPoint(vppNodeIid) - .setExternalNodeConnector(interfacePath) - .build(), addrEpWithLoc.getKey()); - } - }, netconfWorker); - } - }, netconfWorker); + final Optional optInterface = GbpNetconfTransaction.read(vppNodeIid, + 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(vppNodeIid, l2Iid, GbpNetconfTransaction.RETRY_COUNT); + if (transactionState) { + AccessListWrapper.removeAclRefFromIface(vppNodeIid, interfaceIid.firstKeyOf(Interface.class)); + AccessListWrapper.removeAclsForInterface(vppNodeIid, 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)); + } } - private static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) { + public static ExternalLocationCase resolveAndValidateLocation(AddressEndpointWithLocation addrEpWithLoc) { LocationType locationType = addrEpWithLoc.getAbsoluteLocation().getLocationType(); if (!(locationType instanceof ExternalLocationCase)) { throw new IllegalArgumentException("Endpoint does not have external location " + addrEpWithLoc); @@ -477,7 +623,7 @@ public class InterfaceManager implements AutoCloseable { } private static boolean hasNodeAndInterface(VppEndpoint vppEp) { - if (vppEp.getVppNodePath() == null) { + if (vppEp.getVppNodeId() == null) { LOG.debug("vpp-node is missing. {}", vppEp); return false; } @@ -492,20 +638,4 @@ public class InterfaceManager implements AutoCloseable { public void close() throws Exception { vppEndpointLocationProvider.close(); } - - // TODO workaround for netconf, remove when fixed - private synchronized ReadWriteTransaction prepareTransactionAndPutData(final DataBroker mountpoint, - final T data, - InstanceIdentifier iid) { - final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction(); - try { - rwTx.put(LogicalDatastoreType.CONFIGURATION, iid, data); - } - catch (IllegalStateException e) { - LOG.error("Assuming netconf transaction failed, restarting ...", e.getMessage()); - return prepareTransactionAndPutData(mountpoint, data, iid); - } - return rwTx; - } - }