VNMappingUnit implements AutoCloseable { public void virtualNetworkMapping(VirtualNetwork virtualNetwork, UserVnPnMapping userVnPnMapping, List<PhysicalPath> physicalPaths) throws VNMappingException { ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction(); InstanceIdentifier<PhysicalNodes> physicalNodesIid = InstanceIdentifier .builder(PhysicalNetwork.class) .child(PhysicalNodes.class) .build(); Optional<PhysicalNodes> result; try { result = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, physicalNodesIid).get(); } catch ( InterruptedException exception ) { throw new VNMappingException("Can not read the physical nodes."); } catch ( ExecutionException exception ) { throw new VNMappingException("Can not read the physical nodes."); } if ( !result.isPresent() ) { throw new VNMappingException("Failed virtual network mapping caused by " + "absent underlying network topology."); } PhysicalNodes physicalNodes = result.get(); List<PhysicalNode> physicalNodeList = physicalNodes.getPhysicalNode(); UserId userId = virtualNetwork.getUserId(); List<VnPnMappingResult> vnPnMappingResults = userVnPnMapping.getVnPnMappingResult(); List<VirtualNode> virtualNodes = virtualNetwork.getVirtualNodes().getVirtualNode(); List<VirtualPort> virtualPorts; PhysicalNode physicalNode; PhysicalPort physicalPort; VnPnMappingResult vnPnMappingResult; for ( VirtualNode virtualNode : virtualNodes ) { physicalNode = virtualNodeMapping(virtualNetwork.getNetworkId(), virtualNode, physicalNodeList); if ( null == physicalNode ) { pnResourcesTracker.resetResource(userId); throw new VNMappingException("Failed mapping for the virtual node " + virtualNode.getNodeId().getValue() + " in the virtual network " + virtualNetwork.getNetworkId().getValue()); } pnResourcesTracker.addPhysicalNode(userId, physicalNode); virtualPorts = virtualNode.getVirtualPort(); for ( VirtualPort virtualPort : virtualPorts ) { if ( VirtualPort.PortType.External == virtualPort.getPortType() ) { physicalPort = virtualPortMapping(virtualNetwork.getNetworkId(), virtualNode.getNodeId(), virtualPort, physicalNode); if ( null == physicalPort ) { pnResourcesTracker.resetResource(userId); throw new VNMappingException("Failed mapping for the virtual port " + virtualPort.getPortId().getValue() + " of the virtual node " + virtualNode.getNodeId().getValue() + " in the virtual network " + virtualNetwork.getNetworkId().getValue()); } pnResourcesTracker.addPhysicalPort(userId, physicalPort); vnPnMappingResult = new VnPnMappingResultBuilder() .setVirtualResourceId(new VirtualResourceId(UUID.randomUUID().toString())) .setVirtualResourceType(VirtualResourceInstance.VirtualResourceType.Vport) .setVirtualResourceEntityId(new VirtualResourceEntityId(virtualPort.getPortId().getValue())) .setParentVirtualResourceEntityId(new VirtualResourceEntityId(virtualNode.getNodeId().getValue())) .setPhysicalResourceId(new PhysicalResourceId(UUID.randomUUID().toString())) .setPhysicalResourceType(PhysicalResourceInstance.PhysicalResourceType.Port) .setPhysicalResourceEntityId(new PhysicalResourceEntityId(physicalPort.getPortId().getValue())) .setParentPhysicalResourceEntityId(new PhysicalResourceEntityId(physicalNode.getNodeId().getValue())) .build(); vnPnMappingResults.add(vnPnMappingResult); } } vnPnMappingResult = new VnPnMappingResultBuilder() .setVirtualResourceId(new VirtualResourceId(UUID.randomUUID().toString())) .setVirtualResourceType(VirtualResourceInstance.VirtualResourceType.Vnode) .setVirtualResourceEntityId(new VirtualResourceEntityId(virtualNode.getNodeId().getValue())) .setPhysicalResourceId(new PhysicalResourceId(UUID.randomUUID().toString())) .setPhysicalResourceType(PhysicalResourceInstance.PhysicalResourceType.Node) .setPhysicalResourceEntityId(new PhysicalResourceEntityId(physicalNode.getNodeId().getValue())) .build(); vnPnMappingResults.add(vnPnMappingResult); } List<VirtualLink> virtualLinks = virtualNetwork.getVirtualLinks().getVirtualLink(); List<VirtualLink> newVirtualLinks = new ArrayList<VirtualLink>(virtualLinks.size()); PhysicalPath physicalPath; VirtualLink newVirtualLink; for ( VirtualLink virtualLink : virtualLinks ) { physicalPath = virtualLinkMapping(virtualNetwork.getNetworkId(), virtualLink, userVnPnMapping); if ( null == physicalPath ) { pnResourcesTracker.resetResource(userId); throw new VNMappingException("Failed mapping for the virtual link " + virtualLink.getLinkId().getValue() + " in the virtual network " + virtualNetwork.getNetworkId().getValue()); } pnResourcesTracker.addPhysicalPath(userId, physicalPath); physicalPaths.add(physicalPath); newVirtualLink = new VirtualLinkBuilder(virtualLink) .setMetric(physicalPath.getMetric()) .setDelay(physicalPath.getDelay()) .build(); newVirtualLinks.add(newVirtualLink); vnPnMappingResult = new VnPnMappingResultBuilder() .setVirtualResourceId(new VirtualResourceId(UUID.randomUUID().toString())) .setVirtualResourceType(VirtualResourceInstance.VirtualResourceType.Vlink) .setVirtualResourceEntityId(new VirtualResourceEntityId(virtualLink.getLinkId().getValue())) .setPhysicalResourceId(new PhysicalResourceId(UUID.randomUUID().toString())) .setPhysicalResourceType(PhysicalResourceInstance.PhysicalResourceType.Path) .setPhysicalResourceEntityId(new PhysicalResourceEntityId(physicalPath.getPathId().getValue())) .build(); vnPnMappingResults.add(vnPnMappingResult); } virtualLinks.clear(); virtualLinks.addAll(newVirtualLinks); return; } VNMappingUnit(DataBroker dataBroker,
                         PNComputationUnit pnComputationUnit,
                         PNResourcesTracker pnResourcesTracker); void virtualNetworkMapping(VirtualNetwork virtualNetwork, UserVnPnMapping userVnPnMapping,
                                      List<PhysicalPath> physicalPaths); void virtualNetworkMapping(VirtualNetwork virtualNetwork, List<VirtualLink> unmappedVirtualLinks,
                                      UserVnPnMapping userVnPnMapping, List<PhysicalPath> physicalPaths); @Override void close();  }
@Test public void testVirtualNetworkMapping() throws Exception { VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); UserVnPnMapping userVnPnMapping = mock(UserVnPnMapping.class); List<PhysicalPath> physicalPaths = new ArrayList<PhysicalPath>(); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); PhysicalNodes physicalNodes = mock(PhysicalNodes.class); VirtualNodes virtualNodes_temp = mock(VirtualNodes.class); CheckedFuture checkedFuture = mock(CheckedFuture.class); VirtualNode virtualNode = mock(VirtualNode.class); VirtualLinks virtualLinks_temp = mock(VirtualLinks.class); VirtualLink virtualLink = mock(VirtualLink.class); VirtualNetworkId virtualNetworkId = mock(VirtualNetworkId.class); VirtualNodeId virtualNodeId = mock(VirtualNodeId.class); VnPnMappingResult vnPnMappingResult_virtualLinkMapping = mock(VnPnMappingResult.class); UserId userId = mock(UserId.class); VirtualLinkId virtualLinkId = mock(VirtualLinkId.class); PhysicalResourceEntityId physicalResourceEntityId_virtualLinkMapping = mock(PhysicalResourceEntityId.class); Optional<PhysicalNodes> result = Optional.of(physicalNodes); List<PhysicalNode> physicalNodeList = new ArrayList<PhysicalNode>(); List<VirtualNode> virtualNodes = new ArrayList<VirtualNode>(); List<VirtualLink> virtualLinks = new ArrayList<VirtualLink>(); List<VnPnMappingResult> vnPnMappingResults = new ArrayList<VnPnMappingResult>(); List<VnPnMappingResult> vnPnMappingResults_virtualLinkMapping = new ArrayList<VnPnMappingResult>(); virtualLinks.add(virtualLink); vnPnMappingResults_virtualLinkMapping.add(vnPnMappingResult_virtualLinkMapping); Assert.assertTrue(result.isPresent()); when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture); when(checkedFuture.get()).thenReturn(result); when(physicalNodes.getPhysicalNode()).thenReturn(physicalNodeList); when(virtualNetwork.getUserId()).thenReturn(userId); when(userVnPnMapping.getVnPnMappingResult()) .thenReturn(vnPnMappingResults) .thenReturn(vnPnMappingResults_virtualLinkMapping); when(virtualNetwork.getVirtualNodes()).thenReturn(virtualNodes_temp); when(virtualNodes_temp.getVirtualNode()).thenReturn(virtualNodes); when(virtualNetwork.getVirtualLinks()).thenReturn(virtualLinks_temp); when(virtualLinks_temp.getVirtualLink()).thenReturn(virtualLinks); when(virtualNetwork.getNetworkId()).thenReturn(virtualNetworkId); when(virtualLink.getSrcNodeId()).thenReturn(virtualNodeId); when(virtualLink.getDestNodeId()).thenReturn(virtualNodeId); when(virtualNodeId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); when(vnPnMappingResult_virtualLinkMapping.getVirtualResourceEntityId()).thenReturn(new VirtualResourceEntityId(new String("00001111-0000-0000-0000-000011112222"))); when(vnPnMappingResult_virtualLinkMapping.getPhysicalResourceEntityId()).thenReturn(physicalResourceEntityId_virtualLinkMapping); when(physicalResourceEntityId_virtualLinkMapping.getValue()).thenReturn(new String("test")); when(virtualLink.getBandwidth()).thenReturn(1L); doNothing().when(pnResourcesTracker).addPhysicalPath(any(UserId.class),any(PhysicalPath.class)); when(virtualLink.getLinkId()).thenReturn(virtualLinkId); when(virtualLinkId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); vnMappingUnit.virtualNetworkMapping(virtualNetwork, userVnPnMapping, physicalPaths); verify(pnResourcesTracker).addPhysicalPath(any(UserId.class), any(PhysicalPath.class)); verify(userVnPnMapping,times(2)).getVnPnMappingResult(); verify(virtualLink,times(2)).getSrcNodeId(); verify(virtualLink,times(2)).getDestNodeId(); verify(virtualNodeId,times(2)).getValue(); }
@Test public void testVirtualNetworkMapping1() throws Exception { VirtualNodeId virtualNodeId = mock(VirtualNodeId.class); VirtualNetworkId virtualNetworkId = mock(VirtualNetworkId.class); VnPnMappingResult vnPnMappingResult_virtualLinkMapping = mock(VnPnMappingResult.class); VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); UserVnPnMapping userVnPnMapping = mock(UserVnPnMapping.class); VirtualLinks virtualLinks_temp = mock(VirtualLinks.class); VirtualLink virtualLink = mock(VirtualLink.class); VirtualLinkId virtualLinkId = mock(VirtualLinkId.class); PhysicalResourceEntityId physicalResourceEntityId_virtualLinkMapping = mock(PhysicalResourceEntityId.class); List<VirtualLink> virtualLinks = new ArrayList<VirtualLink>(); List<VirtualLink> unmappedVirtualLinks = new ArrayList<VirtualLink>(); List<PhysicalPath> physicalPaths = new ArrayList<PhysicalPath>(); List<VnPnMappingResult> vnPnMappingResults = new ArrayList<VnPnMappingResult>(); List<VnPnMappingResult> vnPnMappingResults_virtualLinkMapping = new ArrayList<VnPnMappingResult>(); unmappedVirtualLinks.add(virtualLink); virtualLinks.add(virtualLink); vnPnMappingResults_virtualLinkMapping.add(vnPnMappingResult_virtualLinkMapping); when(virtualNetwork.getVirtualLinks()).thenReturn(virtualLinks_temp); when(virtualLinks_temp.getVirtualLink()).thenReturn(virtualLinks); when(userVnPnMapping.getVnPnMappingResult()) .thenReturn(vnPnMappingResults) .thenReturn(vnPnMappingResults_virtualLinkMapping);; when(virtualNetwork.getNetworkId()).thenReturn(virtualNetworkId); when(virtualLink.getSrcNodeId()).thenReturn(virtualNodeId); when(virtualLink.getDestNodeId()).thenReturn(virtualNodeId); when(virtualNodeId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); when(vnPnMappingResult_virtualLinkMapping.getVirtualResourceEntityId()).thenReturn(new VirtualResourceEntityId(new String("00001111-0000-0000-0000-000011112222"))); when(vnPnMappingResult_virtualLinkMapping.getPhysicalResourceEntityId()).thenReturn(physicalResourceEntityId_virtualLinkMapping); when(physicalResourceEntityId_virtualLinkMapping.getValue()).thenReturn(new String("test")); when(virtualLink.getBandwidth()).thenReturn(1L); doNothing().when(pnResourcesTracker).addPhysicalPath(any(UserId.class), any(PhysicalPath.class)); when(virtualLink.getLinkId()).thenReturn(virtualLinkId); when(virtualLinkId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); vnMappingUnit.virtualNetworkMapping(virtualNetwork, unmappedVirtualLinks, userVnPnMapping, physicalPaths); verify(vnPnMappingResult_virtualLinkMapping,times(2)).getVirtualResourceEntityId(); verify(pnResourcesTracker).addPhysicalPath(any(UserId.class), any(PhysicalPath.class)); verify(userVnPnMapping,times(2)).getVnPnMappingResult(); verify(virtualLink,times(2)).getSrcNodeId(); verify(virtualLink,times(2)).getDestNodeId(); verify(virtualNodeId,times(2)).getValue(); }