IosXeServiceForwarderMapper { public void syncForwarders(List<ServiceFunctionForwarder> forwarders, boolean delete) { for (ServiceFunctionForwarder forwarder : forwarders) { IpAddress forwarderMgmtIp = forwarder.getIpMgmtAddress(); if (forwarderMgmtIp == null) { LOG.warn("Service function forwarder {} has no management Ip address, cannot be created", forwarder.getName().getValue()); continue; } for (Node netconfNode : nodeManager.getConnectedNodes().values()) { IpAddress netconfNodeIp = nodeManager.getNetconfNodeIp(netconfNode); if (netconfNodeIp.equals(forwarderMgmtIp)) { DataBroker mountPoint = nodeManager.getActiveMountPoints() .get(netconfNode.getNodeId()); if (mountPoint != null) { for (SffDataPlaneLocator forwarderDpl : forwarder.getSffDataPlaneLocator()) { DataPlaneLocator dpl = forwarderDpl.getDataPlaneLocator(); LocatorType locatorType = dpl.getLocatorType(); Ip sffIp = null; if (locatorType instanceof Ip) { LOG.debug("IP locator found: {} ", locatorType); sffIp = (Ip) locatorType; } if (sffIp != null && sffIp.getIp() != null) { IpAddress ipAddress = sffIp.getIp(); org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.ServiceFunctionForwarder localForwarder = SfcIosXeUtils.createLocalForwarder(ipAddress); if (localForwarder != null && !delete) { IosXeDataStoreAPI writeServiceFunction = new IosXeDataStoreAPI(mountPoint, localForwarder.getLocal(), WRITE_LOCAL, LogicalDatastoreType.CONFIGURATION); Object result = writeServiceFunction.call(); if (result != null && result == Boolean.TRUE) { LOG.info("Local forwarder with ip {} created on node {}", forwarder.getIpMgmtAddress().toString(), netconfNode.getNodeId().getValue()); } } if (localForwarder != null && delete) { IosXeDataStoreAPI writeServiceFunction = new IosXeDataStoreAPI(mountPoint, localForwarder.getLocal(), DELETE_LOCAL, LogicalDatastoreType.CONFIGURATION); Object result = writeServiceFunction.call(); if (result != null && result == Boolean.TRUE) { LOG.info("Local forwarder removed from node {}", netconfNode.getNodeId() .getValue()); } } } } } } else { LOG.warn("Node not found for SFF {}", forwarder.getName()); } } } } IosXeServiceForwarderMapper(DataBroker dataBroker, NodeManager nodeManager); void syncForwarders(List<ServiceFunctionForwarder> forwarders, boolean delete); void unregisterSffListener();  }
@Test public void syncForwarder_create() { List<ServiceFunctionForwarder> forwarders = new ArrayList<>(); ServiceFunctionForwarderBuilder noMgmtIpForwarder = new ServiceFunctionForwarderBuilder(); String sffName1 = "forwarder1"; noMgmtIpForwarder.setName(new SffName(sffName1)) .setKey(new ServiceFunctionForwarderKey(new SffName(sffName1))); List<SffDataPlaneLocator> dataPlaneLocators = new ArrayList<>(); DataPlaneLocatorBuilder dataPlaneLocatorBuilder = new DataPlaneLocatorBuilder(); dataPlaneLocatorBuilder.setLocatorType(new IpBuilder().setIp(new IpAddress(new Ipv4Address(ipAddress))) .build()); SffDataPlaneLocatorBuilder sffDataPlaneLocatorBuilder = new SffDataPlaneLocatorBuilder(); sffDataPlaneLocatorBuilder.setDataPlaneLocator(dataPlaneLocatorBuilder.build()); dataPlaneLocators.add(sffDataPlaneLocatorBuilder.build()); ServiceFunctionForwarderBuilder testSff = new ServiceFunctionForwarderBuilder(); testSff.setName(new SffName(sffName2)) .setKey(new ServiceFunctionForwarderKey(new SffName(sffName2))) .setIpMgmtAddress(new IpAddress(new Ipv4Address(ipAddress))) .setSffDataPlaneLocator(dataPlaneLocators); forwarders.add(noMgmtIpForwarder.build()); forwarders.add(testSff.build()); Map<NodeId, Node> nodeMap = new HashMap<>(); Map<NodeId, DataBroker> nodeDbMap = new HashMap<>(); NodeBuilder nodeBuilder = new NodeBuilder(); nodeBuilder.setNodeId(new NodeId(nodeId)) .setKey(new NodeKey(new NodeId(nodeId))); Node node = nodeBuilder.build(); nodeMap.put(node.getNodeId(), node); nodeDbMap.put(node.getNodeId(), dataBroker); when(nodeManager.getConnectedNodes()).thenReturn(nodeMap); when(nodeManager.getNetconfNodeIp(node)).thenReturn(new IpAddress(new Ipv4Address(ipAddress))); when(nodeManager.getActiveMountPoints()).thenReturn(nodeDbMap); when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); sffMapper = new IosXeServiceForwarderMapper(dataBroker, nodeManager); sffMapper.syncForwarders(forwarders, false); verify(nodeManager, times(1)).getConnectedNodes(); verify(nodeManager,times(1)).getNetconfNodeIp(node); verify(nodeManager, times(1)).getActiveMountPoints(); verify(dataBroker, times(1)).newWriteOnlyTransaction(); }
@Test public void syncForwarder_delete() { List<ServiceFunctionForwarder> forwarders = new ArrayList<>(); List<SffDataPlaneLocator> dataPlaneLocators = new ArrayList<>(); DataPlaneLocatorBuilder dataPlaneLocatorBuilder = new DataPlaneLocatorBuilder(); dataPlaneLocatorBuilder.setLocatorType(new IpBuilder().setIp(new IpAddress(new Ipv4Address(ipAddress))) .build()); SffDataPlaneLocatorBuilder sffDataPlaneLocatorBuilder = new SffDataPlaneLocatorBuilder(); sffDataPlaneLocatorBuilder.setDataPlaneLocator(dataPlaneLocatorBuilder.build()); dataPlaneLocators.add(sffDataPlaneLocatorBuilder.build()); ServiceFunctionForwarderBuilder testSff = new ServiceFunctionForwarderBuilder(); testSff.setName(new SffName(sffName2)) .setKey(new ServiceFunctionForwarderKey(new SffName(sffName2))) .setIpMgmtAddress(new IpAddress(new Ipv4Address(ipAddress))) .setSffDataPlaneLocator(dataPlaneLocators); forwarders.add(testSff.build()); Map<NodeId, Node> nodeMap = new HashMap<>(); Map<NodeId, DataBroker> nodeDbMap = new HashMap<>(); NodeBuilder nodeBuilder = new NodeBuilder(); nodeBuilder.setNodeId(new NodeId(nodeId)) .setKey(new NodeKey(new NodeId(nodeId))); Node node = nodeBuilder.build(); nodeMap.put(node.getNodeId(), node); nodeDbMap.put(node.getNodeId(), dataBroker); when(nodeManager.getConnectedNodes()).thenReturn(nodeMap); when(nodeManager.getNetconfNodeIp(node)).thenReturn(new IpAddress(new Ipv4Address(ipAddress))); when(nodeManager.getActiveMountPoints()).thenReturn(nodeDbMap); when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); sffMapper = new IosXeServiceForwarderMapper(dataBroker, nodeManager); sffMapper.syncForwarders(forwarders, true); verify(nodeManager, times(1)).getConnectedNodes(); verify(nodeManager,times(1)).getNetconfNodeIp(node); verify(nodeManager, times(1)).getActiveMountPoints(); verify(dataBroker, times(1)).newWriteOnlyTransaction(); }