+ private static Optional<ConfigCommand> 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 static Optional<ConfigCommand> 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.<br>
+ * It rewrites bridge domain in case it already exist.<br>
+ * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update location
+ * when the interface is created successfully.<br>
+ * 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<Void> addBridgeDomainToInterface(@Nonnull String bridgeDomainName,
+ @Nonnull AddressEndpointWithLocation addrEpWithLoc,
+ @Nonnull List<AccessListWrapper> aclWrappers,
+ boolean enableBvi) {
+ ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
+ InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
+ String interfacePath = epLoc.getExternalNodeConnector();
+
+ Optional<InstanceIdentifier<Interface>> optInterfaceIid =
+ VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
+ if (!optInterfaceIid.isPresent()) {
+ return Futures.immediateFailedFuture(
+ new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
+ }
+ InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
+ Optional<DataBroker> 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<Interface> 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<L2> l2Iid =
+ interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
+ Optional<L2> 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<String> 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<Void> 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<Void> 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<L2> l2Iid = VppIidFactory.getL2ForInterfaceIid(ifaceKey);
+ final ReadOnlyTransaction rwTxRead = mountpoint.newReadOnlyTransaction();
+ Optional<L2> 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).<br>
+ * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint
+ * location.
+ * <p>
+ * 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<Void> deleteBridgeDomainFromInterface(
+ @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
+ // TODO update ACLs for peers
+ ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
+ InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
+ String interfacePath = epLoc.getExternalNodeConnector();
+
+ Optional<InstanceIdentifier<Interface>> optInterfaceIid =
+ VppPathMapper.interfaceToInstanceIdentifier(interfacePath);
+ if (!optInterfaceIid.isPresent()) {
+ return Futures.immediateFailedFuture(
+ new Exception("Cannot resolve interface instance-identifier for interface path" + interfacePath));
+ }
+ InstanceIdentifier<Interface> interfaceIid = optInterfaceIid.get();
+
+ Optional<DataBroker> 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<Interface> 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<L2> 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;
+ }
+