IntentResolver implements AutoCloseable { public void resolveIntent(UserId userId) throws Exception { VNComputationUnit vnComputationUnit = vnComputationUnits.get(userId); VirtualNetworkId virtualNetworkId = new VirtualNetworkId(userId.getValue()); VirtualNetworkKey virtualNetworkKey = new VirtualNetworkKey(virtualNetworkId); InstanceIdentifier<VirtualNetwork> virtualNetworkIid = InstanceIdentifier .builder(VirtualNetworks.class) .child(VirtualNetwork.class, virtualNetworkKey) .build(); InstanceIdentifier<UserIntentVnMapping> userIntentVnMappingIid = InstanceIdentifier .builder(IntentVnMappingResults.class) .child(UserIntentVnMapping.class, new UserIntentVnMappingKey(userId)) .build(); InstanceIdentifier<UserVnPnMapping> userVnPnMappingIid = InstanceIdentifier .builder(VnPnMappingResults.class) .child(UserVnPnMapping.class, new UserVnPnMappingKey(virtualNetworkId)) .build(); if ( null != vnComputationUnit ) { conditionManager.clear(userId); vnComputationUnit.close(); vnComputationUnits.remove(userId); ReadWriteTransaction readWriteTransaction = dataBroker.newReadWriteTransaction(); Optional<UserVnPnMapping> result; try { result = readWriteTransaction.read(LogicalDatastoreType.CONFIGURATION, userVnPnMappingIid).get(); } catch ( InterruptedException exception ) { throw new IntentResolutionException("Can not read the vn-pn mapping results for the user " + userId.getValue() + "."); } catch ( ExecutionException exception ) { throw new IntentResolutionException("Can not read the vn-pn mapping results for the user " + userId.getValue() + "."); } if ( result.isPresent() ) { UserVnPnMapping userVnPnMapping = result.get(); List<VnPnMappingResult> vnPnMappingResults = userVnPnMapping.getVnPnMappingResult(); InstanceIdentifier<PhysicalPath> physicalPathIid; PhysicalPathId physicalPathId; for ( VnPnMappingResult vnPnMappingResult : vnPnMappingResults ) { if ( VnPnMappingResult.VirtualResourceType.Vlink == vnPnMappingResult.getVirtualResourceType() ) { physicalPathId = new PhysicalPathId(vnPnMappingResult.getPhysicalResourceEntityId().getValue()); physicalPathIid = InstanceIdentifier.builder(PhysicalNetwork.class) .child(PhysicalPaths.class) .child(PhysicalPath.class, new PhysicalPathKey(physicalPathId)) .build(); readWriteTransaction.delete(LogicalDatastoreType.OPERATIONAL, physicalPathIid); } } } readWriteTransaction.submit(); } ReadWriteTransaction readWriteTransaction = dataBroker.newReadWriteTransaction(); InstanceIdentifier<User> userIid = InstanceIdentifier.builder(Users.class) .child(User.class, new UserKey(userId)) .build(); Optional<User> result; try { result = readWriteTransaction.read(LogicalDatastoreType.CONFIGURATION, userIid).get(); } catch ( InterruptedException exception ) { throw new IntentResolutionException("Can not read the data of the user " + userId.getValue() + "."); } catch ( ExecutionException exception ) { throw new IntentResolutionException("Can not read the data of the user " + userId.getValue() + "."); } if ( !result.isPresent() ) { throw new IntentResolutionException("The data of the user " + userId.getValue() + " does not exist."); } User user = result.get(); if ( null != user.getObjects() ) { VirtualNodes virtualNodes = new VirtualNodesBuilder() .setVirtualNode(new LinkedList<VirtualNode>()) .build(); VirtualLinks virtualLinks = new VirtualLinksBuilder() .setVirtualLink(new LinkedList<VirtualLink>()) .build(); VirtualPaths virtualPaths = new VirtualPathsBuilder() .setVirtualPath(new LinkedList<VirtualPath>()) .build(); VirtualRoutes virtualRoutes = new VirtualRoutesBuilder() .setVirtualRoute(new LinkedList<VirtualRoute>()) .build(); VirtualArps virtualArps = new VirtualArpsBuilder() .setVirtualArp(new LinkedList<VirtualArp>()) .build(); VirtualNetwork virtualNetwork = new VirtualNetworkBuilder() .setNetworkId(virtualNetworkId) .setUserId(userId) .setVirtualNodes(virtualNodes) .setVirtualLinks(virtualLinks) .setVirtualPaths(virtualPaths) .setVirtualRoutes(virtualRoutes) .setVirtualArps(virtualArps) .build(); UserIntentVnMapping userIntentVnMapping = new UserIntentVnMappingBuilder() .setUserId(userId) .setVirtualNetworkId(virtualNetworkId) .setIntentVnMappingResult(new LinkedList<IntentVnMappingResult>()) .build(); UserVnPnMapping userVnPnMapping = new UserVnPnMappingBuilder() .setVirtualNetworkId(virtualNetworkId) .setUserId(userId) .setVnPnMappingResult(new LinkedList<VnPnMappingResult>()) .build(); List<PhysicalPath> physicalPaths = new LinkedList<PhysicalPath>(); List<Node> nodes = user.getObjects().getNode(); List<Node> hosts = new LinkedList<Node>(); List<Node> layer2Groups = new LinkedList<Node>(); List<Node> layer3Groups = new LinkedList<Node>(); List<Node> externalLayer3Groups = new LinkedList<Node>(); List<Node> serviceChainGroups = new LinkedList<Node>(); List<Node> serviceFunctions = new LinkedList<Node>(); NodeType hostNodeType = new NodeType("host"); NodeType layer2GroupNodeType = new NodeType("l2-group"); NodeType layer3GroupNodeType = new NodeType("l3-group"); NodeType externalGroupNodeType = new NodeType("ext-group"); NodeType serviceChainGroupNodeType = new NodeType("chain-group"); NodeType firewallNodeType = new NodeType("fw"); NodeType loadbalancerNodeType = new NodeType("lb"); NodeType cacheNodeType = new NodeType("cache"); NodeType nodeType; if ( null != nodes ) { for ( Node node : nodes ) { nodeType = node.getNodeType(); if ( nodeType.equals(hostNodeType) ) { hosts.add(node); } if ( nodeType.equals(layer2GroupNodeType) ) { layer2Groups.add(node); } if ( nodeType.equals(layer3GroupNodeType) ) { layer3Groups.add(node); } if ( nodeType.equals(externalGroupNodeType) ) { if ( IntentResolverUtils.checkExternalLayer3Group(node) ) { externalLayer3Groups.add(node); } } if ( nodeType.equals(serviceChainGroupNodeType) ) { serviceChainGroups.add(node); } if ( nodeType.equals(firewallNodeType) || nodeType.equals(loadbalancerNodeType) || nodeType.equals(cacheNodeType) ) { serviceFunctions.add(node); } } IntentResolverUtils.copyPhysicalNetworkConfigToOperational(dataBroker); for ( Node node : hosts ) { nodeMapper.resolveHost(user, node, virtualNetwork, userIntentVnMapping); } for ( Node node : layer2Groups ) { nodeMapper.resolveLayer2Group(user, node, virtualNetwork, userIntentVnMapping); } for ( Node node : externalLayer3Groups ) { nodeMapper.resolveExternalLayer3Group(user, node, virtualNetwork, userIntentVnMapping); } for ( Node node : layer3Groups ) { nodeMapper.resolveLayer3Group(user, node, virtualNetwork, userIntentVnMapping); } for ( Node node : serviceFunctions ) { nodeMapper.resolveServiceFunction(user, node, virtualNetwork, userIntentVnMapping); } for ( Node node : serviceChainGroups ) { nodeMapper.resolveServiceChainGroup(user, node, virtualNetwork, userIntentVnMapping); } } List<Connection> connections = user.getObjects().getConnection(); if ( null != connections ) { for ( Connection connection : connections ) { connectionMapper.resolveConnection(user, connection, virtualNetwork, userIntentVnMapping); } } List<Flow> flows = user.getObjects().getFlow(); if ( null != flows ) { for ( Flow flow : flows ) { flowManager.resolveFlow(userId, flow); } } List<Operation> operationsApplyingToNode = new LinkedList<Operation>(); List<Operation> operationsApplyingToConnection = new LinkedList<Operation>(); List<Operation> operationsApplyingToFlow = new LinkedList<Operation>(); if ( null != user.getOperations() ) { List<Operation> operations = user.getOperations().getOperation(); if ( null != operations ) { operationResolver.classifyOperations(user, operations, operationsApplyingToNode, operationsApplyingToConnection, operationsApplyingToFlow); } } if ( !operationsApplyingToNode.isEmpty() ) { for ( Operation operation : operationsApplyingToNode ) { operationResolver.resolveOperation(user, operation, virtualNetwork, userIntentVnMapping); } } if ( !operationsApplyingToConnection.isEmpty() ) { for ( Operation operation : operationsApplyingToConnection ) { operationResolver.resolveOperation(user, operation, virtualNetwork, userIntentVnMapping); } } vnMappingUnit.virtualNetworkMapping(virtualNetwork, userVnPnMapping, physicalPaths); vnComputationUnit = new VNComputationUnit(dataBroker, virtualNetwork); vnComputationUnits.put(userId, vnComputationUnit); int currentVirtualLinkNum = virtualLinks.getVirtualLink().size(); if ( !operationsApplyingToFlow.isEmpty() ) { for ( Operation operation : operationsApplyingToFlow ) { operationResolver.resolveOperation(user, operation, virtualNetwork, userIntentVnMapping); } } List<VirtualLink> virtualLinkList = virtualLinks.getVirtualLink(); List<VirtualLink> unmappedVirtualLinkList = virtualLinkList.subList(currentVirtualLinkNum, virtualLinkList.size()); vnMappingUnit.virtualNetworkMapping(virtualNetwork, unmappedVirtualLinkList, userVnPnMapping, physicalPaths); LOG.debug("{}", virtualNetwork); LOG.debug("{}", userIntentVnMapping); LOG.debug("{}", userVnPnMapping); LOG.debug("{}", physicalPaths); readWriteTransaction.put(LogicalDatastoreType.CONFIGURATION, virtualNetworkIid, virtualNetwork, true); readWriteTransaction.put(LogicalDatastoreType.CONFIGURATION, userIntentVnMappingIid, userIntentVnMapping, true); readWriteTransaction.put(LogicalDatastoreType.CONFIGURATION, userVnPnMappingIid, userVnPnMapping, true); InstanceIdentifier<PhysicalPath> physicalPathIid; for ( PhysicalPath physicalPath : physicalPaths ) { physicalPathIid = InstanceIdentifier.builder(PhysicalNetwork.class) .child(PhysicalPaths.class) .child(PhysicalPath.class, new PhysicalPathKey(physicalPath.getPathId())) .build(); readWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, physicalPathIid, physicalPath, true); } readWriteTransaction.submit(); } return; } IntentResolver(DataBroker dataBroker); void resolveIntent(UserId userId); @Override void close();  }
@Test public void testResolveIntent() throws Exception { PowerMockito.mockStatic(IntentResolverUtils.class); PowerMockito.doNothing().when(IntentResolverUtils.class); IntentResolverUtils.copyPhysicalNetworkConfigToOperational(dataBroker); Class<IntentResolver> class1 = IntentResolver.class; CheckedFuture userVnPnMappingIidFuture = mock(CheckedFuture.class); CheckedFuture userFuture = mock(CheckedFuture.class); CheckedFuture physicalHostsFuture = mock(CheckedFuture.class); ReadWriteTransaction readWriteTransaction = mock(ReadWriteTransaction.class); when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readWriteTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(userVnPnMappingIidFuture).thenReturn(userFuture); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(physicalHostsFuture); when(userId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); Map<UserId, VNComputationUnit> vnComputationUnits = new HashMap<UserId, VNComputationUnit>(); VNComputationUnit vnComputationUnit = mock(VNComputationUnit.class); vnComputationUnits.put(userId, vnComputationUnit); Field field1 = class1.getDeclaredField("vnComputationUnits"); field1.setAccessible(true); field1.set(intentResolver, vnComputationUnits); UserVnPnMapping userVnPnMapping = mock(UserVnPnMapping.class); Optional<UserVnPnMapping> result1 = Optional.of(userVnPnMapping); Assert.assertTrue(result1.isPresent()); when(userVnPnMappingIidFuture.get()).thenReturn(result1); when(userVnPnMapping.getVnPnMappingResult()).thenReturn(vnPnMappingResults); when(vnPnMappingResult.getVirtualResourceType()).thenReturn(virtualResourceType); when(vnPnMappingResult.getPhysicalResourceEntityId()).thenReturn(physicalResourceEntityId); when(vnPnMappingResult.getPhysicalResourceEntityId().getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); User user = mock(User.class); Optional<User> result2 = Optional.of(user); Assert.assertTrue(result2.isPresent()); when(userFuture.get()).thenReturn(result2); when(user.getObjects()).thenReturn(objects); when(user.getObjects().getNode()).thenReturn(nodeList); nodeType = new NodeType("host"); when(node.getNodeType()).thenReturn(nodeType); NodeMapper nodeMapper = mock(NodeMapper.class); Field field2 = class1.getDeclaredField("nodeMapper"); field2.setAccessible(true); field2.set(intentResolver, nodeMapper); doNothing().when(nodeMapper).resolveHost(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("l2-group"); when(node.getNodeType()).thenReturn(nodeType); doNothing().when(nodeMapper).resolveLayer2Group(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("l3-group"); when(node.getNodeType()).thenReturn(nodeType); doNothing().when(nodeMapper).resolveLayer3Group(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("ext-group"); when(node.getNodeType()).thenReturn(nodeType); when(node.getProperty()).thenReturn(propertyList); PropertyName propertyName = new PropertyName("ac-info-network"); when(property.getPropertyName()).thenReturn(propertyName); when(property.getPropertyValues()).thenReturn(propertyValues); when(property.getPropertyValues().getStringValue()).thenReturn(stringValueList); when(property.getPropertyValues().getStringValue().get(0).getValue()).thenReturn(new String("layer3")); doNothing().when(nodeMapper).resolveExternalLayer3Group(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("chain-group"); when(node.getNodeType()).thenReturn(nodeType); doNothing().when(nodeMapper).resolveServiceChainGroup(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("fw"); when(node.getNodeType()).thenReturn(nodeType); doNothing().when(nodeMapper).resolveServiceFunction(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("lb"); when(node.getNodeType()).thenReturn(nodeType); doNothing().when(nodeMapper).resolveServiceFunction(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); nodeType = new NodeType("cache"); when(node.getNodeType()).thenReturn(nodeType); doNothing().when(nodeMapper).resolveServiceFunction(any(User.class), any(Node.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); when(user.getObjects().getConnection()).thenReturn(connections); ConnectionMapper connectionMapper = mock(ConnectionMapper.class); Field field4 = class1.getDeclaredField("connectionMapper"); field4.setAccessible(true); field4.set(intentResolver, connectionMapper); doNothing().when(connectionMapper).resolveConnection(any(User.class), any(Connection.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); when(user.getObjects().getFlow()).thenReturn(flows); when(user.getOperations()).thenReturn(operations); when(user.getOperations().getOperation()).thenReturn(operationList); OperationResolver operationResolver =mock(OperationResolver.class); Field field3 = class1.getDeclaredField("operationResolver"); field3.setAccessible(true); field3.set(intentResolver, operationResolver); doNothing().when(operationResolver).classifyOperations(any(User.class), any(List.class), any(List.class), any(List.class), any(List.class)); doNothing().when(operationResolver).resolveOperation(any(User.class), any(Operation.class), any(VirtualNetwork.class), any(UserIntentVnMapping.class)); VNMappingUnit vnMappingUnit = mock(VNMappingUnit.class); Field field5 = class1.getDeclaredField("vnMappingUnit"); field5.setAccessible(true); field5.set(intentResolver, vnMappingUnit); doNothing().when(vnMappingUnit).virtualNetworkMapping(any(VirtualNetwork.class), any(UserVnPnMapping.class), any(List.class)); intentResolver.resolveIntent(userId); }