IosXeServiceFunctionMapper { public void syncFunctions(List<ServiceFunction> functions, boolean delete) { for (ServiceFunction function : functions) { IpAddress forwarderMgmtIp = function.getIpMgmtAddress(); if (forwarderMgmtIp == null) { LOG.warn("Service function forwarder {} has no management Ip address, cannot be created", function.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 && !delete) { org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.ServiceFunction serviceFunction = createNetconfServiceFunction(function); if (serviceFunction != null) { IosXeDataStoreAPI writeServiceFunction = new IosXeDataStoreAPI(mountPoint, serviceFunction, WRITE_FUNCTION, LogicalDatastoreType.CONFIGURATION); Object result = writeServiceFunction.call(); if (result != null && result == Boolean.TRUE) { LOG.info("Service function {} created on node {}", serviceFunction.getName(), netconfNode.getNodeId().getValue()); } } } if (mountPoint != null && delete) { org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.ServiceFunction serviceFunction = createNetconfServiceFunction(function); if (serviceFunction != null) { IosXeDataStoreAPI writeServiceFunction = new IosXeDataStoreAPI(mountPoint, serviceFunction.getKey(), DELETE_FUNCTION, LogicalDatastoreType.CONFIGURATION); Object result = writeServiceFunction.call(); if (result != null && result == Boolean.TRUE) { LOG.info("Service function {} removed", serviceFunction.getName()); } } } } } } } IosXeServiceFunctionMapper(DataBroker dataBroker, NodeManager nodeManager); void syncFunctions(List<ServiceFunction> functions, boolean delete); void unregisterSfListener();  }
@Test public void syncFunctions_update() { String sfName1 = "function1"; String sfName2 = "function2"; SfDataPlaneLocatorBuilder macLocatorType = new SfDataPlaneLocatorBuilder(); macLocatorType.setLocatorType(new MacBuilder().build()); SfDataPlaneLocatorBuilder ipLocatorType = new SfDataPlaneLocatorBuilder(); ipLocatorType.setLocatorType(new IpBuilder().setIp(new IpAddress(new Ipv4Address(ipAddress))).build()) .setTransport(Gre.class); SfDataPlaneLocator macDpl = macLocatorType.build(); SfDataPlaneLocator ipDpl = ipLocatorType.build(); List<SfDataPlaneLocator> dataPlaneLocatorList; List<ServiceFunction> serviceFunctions = new ArrayList<>(); ServiceFunctionBuilder emptySfBuilder = new ServiceFunctionBuilder(); emptySfBuilder.setName(new SfName(sfName1)) .setKey(new ServiceFunctionKey(new SfName(sfName1))); ServiceFunctionBuilder noDplSfBuilder = new ServiceFunctionBuilder(); noDplSfBuilder.setName(new SfName(sfName2)) .setKey(new ServiceFunctionKey(new SfName(sfName2))) .setIpMgmtAddress(new IpAddress(new Ipv4Address(ipAddress))); ServiceFunctionBuilder noIpDplSfBuilder = new ServiceFunctionBuilder(); dataPlaneLocatorList = new ArrayList<>(); dataPlaneLocatorList.add(macDpl); noIpDplSfBuilder.setName(new SfName(sfName2)) .setKey(new ServiceFunctionKey(new SfName(sfName2))) .setIpMgmtAddress(new IpAddress(new Ipv4Address(ipAddress))) .setSfDataPlaneLocator(dataPlaneLocatorList); ServiceFunctionBuilder testSfBuilder = new ServiceFunctionBuilder(); dataPlaneLocatorList = new ArrayList<>(); dataPlaneLocatorList.add(ipDpl); testSfBuilder.setName(new SfName(sfName2)) .setKey(new ServiceFunctionKey(new SfName(sfName2))) .setIpMgmtAddress(new IpAddress(new Ipv4Address(ipAddress))) .setSfDataPlaneLocator(dataPlaneLocatorList); serviceFunctions.add(emptySfBuilder.build()); serviceFunctions.add(noDplSfBuilder.build()); serviceFunctions.add(noIpDplSfBuilder.build()); serviceFunctions.add(testSfBuilder.build()); Map<NodeId, Node> nodeMap = new HashMap<>(); NodeId nodeId = new NodeId(nodeIdString); NodeBuilder nodeBuilder = new NodeBuilder(); NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder(); netconfNodeBuilder.setHost(new Host(new IpAddress(new Ipv4Address(ipAddress)))); nodeBuilder.setNodeId(nodeId) .addAugmentation(NetconfNode.class, netconfNodeBuilder.build()); Node node = nodeBuilder.build(); nodeMap.put(nodeId, node); Map<NodeId, DataBroker> nodeWithDataBrokerMap = new HashMap<>(); nodeWithDataBrokerMap.put(nodeId, dataBroker); when(nodeManager.getConnectedNodes()).thenReturn(nodeMap); when(nodeManager.getNetconfNodeIp(node)).thenReturn(new IpAddress(new Ipv4Address(ipAddress))); when(nodeManager.getActiveMountPoints()).thenReturn(nodeWithDataBrokerMap); when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); sfMapper = new IosXeServiceFunctionMapper(dataBroker, nodeManager); sfMapper.syncFunctions(serviceFunctions, false); verify(nodeManager, times(3)).getConnectedNodes(); verify(nodeManager, times(3)).getNetconfNodeIp(any(Node.class)); verify(nodeManager, times(3)).getActiveMountPoints(); verify(dataBroker, times(1)).newWriteOnlyTransaction(); }
@Test public void syncFunctions_delete() { String sfName2 = "function2"; sfMapper = new IosXeServiceFunctionMapper(dataBroker, nodeManager); SfDataPlaneLocatorBuilder macLocatorType = new SfDataPlaneLocatorBuilder(); macLocatorType.setLocatorType(new MacBuilder().build()); SfDataPlaneLocatorBuilder ipLocatorType = new SfDataPlaneLocatorBuilder(); ipLocatorType.setLocatorType(new IpBuilder().setIp(new IpAddress(new Ipv4Address(ipAddress))).build()) .setTransport(Gre.class); SfDataPlaneLocator ipDpl = ipLocatorType.build(); List<SfDataPlaneLocator> dataPlaneLocatorList; List<ServiceFunction> serviceFunctions = new ArrayList<>(); ServiceFunctionBuilder testSfBuilder = new ServiceFunctionBuilder(); dataPlaneLocatorList = new ArrayList<>(); dataPlaneLocatorList.add(ipDpl); testSfBuilder.setName(new SfName(sfName2)) .setKey(new ServiceFunctionKey(new SfName(sfName2))) .setIpMgmtAddress(new IpAddress(new Ipv4Address(ipAddress))) .setSfDataPlaneLocator(dataPlaneLocatorList); serviceFunctions.add(testSfBuilder.build()); Map<NodeId, Node> nodeMap = new HashMap<>(); NodeId nodeId = new NodeId(nodeIdString); NodeBuilder nodeBuilder = new NodeBuilder(); NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder(); netconfNodeBuilder.setHost(new Host(new IpAddress(new Ipv4Address(ipAddress)))); nodeBuilder.setNodeId(nodeId) .addAugmentation(NetconfNode.class, netconfNodeBuilder.build()); Node node = nodeBuilder.build(); nodeMap.put(nodeId, node); Map<NodeId, DataBroker> nodeWithDataBrokerMap = new HashMap<>(); nodeWithDataBrokerMap.put(nodeId, dataBroker); when(nodeManager.getConnectedNodes()).thenReturn(nodeMap); when(nodeManager.getNetconfNodeIp(node)).thenReturn(new IpAddress(new Ipv4Address(ipAddress))); when(nodeManager.getActiveMountPoints()).thenReturn(nodeWithDataBrokerMap); when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); sfMapper.syncFunctions(serviceFunctions, true); verify(nodeManager, times(1)).getConnectedNodes(); verify(nodeManager, times(1)).getNetconfNodeIp(any(Node.class)); verify(nodeManager, times(1)).getActiveMountPoints(); verify(dataBroker, times(1)).newWriteOnlyTransaction(); }