@SuppressWarnings("unchecked") @Test public void validateIovisorModuleInstanceTest() { DataBroker dataBroker = Mockito.mock(DataBroker.class); when(dataBroker.newReadOnlyTransaction()).thenReturn(mock(ReadOnlyTransaction.class)); PowerMockito.mockStatic(DataStoreHelper.class); Optional<IovisorModuleInstances> result = mock(Optional.class); PowerMockito.when( DataStoreHelper.readFromDs( any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(ReadOnlyTransaction.class))) .thenReturn(result); Uri goodUri = mock(Uri.class); when(result.isPresent()).thenReturn(false); Assert.assertFalse(IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, goodUri)); when(result.isPresent()).thenReturn(true); IovisorModuleInstance iovisorModuleInstance = mock(IovisorModuleInstance.class); when(iovisorModuleInstance.getUri()).thenReturn(goodUri); List<IovisorModuleInstance> iovisorModuleInstanceList = new ArrayList<>(); iovisorModuleInstanceList.add(iovisorModuleInstance); IovisorModuleInstances iovisorModuleInstances = mock(IovisorModuleInstances.class); when(iovisorModuleInstances.getIovisorModuleInstance()).thenReturn(iovisorModuleInstanceList); when(result.get()).thenReturn(iovisorModuleInstances); Assert.assertTrue(IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, goodUri)); Uri wrongUri = mock(Uri.class); Assert.assertFalse(IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, wrongUri)); }
@Test public void testCreateForwardingContext1() { // case #1 - L2FloodDomain is not present in DS DataBroker dataBroker = getDataBroker(); ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction(); MappingUtils.ForwardingCtx forwardingCtx = MappingUtils.createForwardingContext(tenantId, l2FloodDomainBuilder.build().getId(), rwTx); assertNotNull(forwardingCtx); assertNull(forwardingCtx.getL2FloodDomain()); assertNull(forwardingCtx.getL2BridgeDomain()); assertNull(forwardingCtx.getL3Context()); }
@Test public void testCreateForwardingContext2() { // case #2 - L2FloodDomain is present in DS, but its parent is null DataBroker dataBroker = getDataBroker(); ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction(); l2FloodDomainBuilder.setParent(null); L2FloodDomain l2FloodDomain = l2FloodDomainBuilder.build(); writeL2FloodDomain(tenantId, l2FloodDomain, rwTx); MappingUtils.ForwardingCtx forwardingCtx = MappingUtils.createForwardingContext(tenantId, l2FloodDomainBuilder.build().getId(), rwTx); assertNotNull(forwardingCtx); assertTrue(forwardingCtx.getL2FloodDomain().equals(l2FloodDomain)); assertNull(forwardingCtx.getL2BridgeDomain()); assertNull(forwardingCtx.getL3Context()); }
private void initialize(final DataBroker dataBroker) { Preconditions.checkNotNull(dataBroker); listenerRegistration = dataBroker.registerDataChangeListener( LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this, DataChangeScope.SUBTREE); LOG.info("NetconfEventSourceManager initialized."); }
@Override public void writeList() { int txSubmitted = 0; int writeCnt = 0; BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this); WriteTransaction tx = chain.newWriteOnlyTransaction(); for (OuterList element : this.list) { InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class).child(OuterList.class, element.getKey()); if (oper == StartTestInput.Operation.PUT) { tx.put(LogicalDatastoreType.CONFIGURATION, iid, element); } else { tx.merge(LogicalDatastoreType.CONFIGURATION, iid, element); } writeCnt++; if (writeCnt == writesPerTx) { txSubmitted++; Futures.addCallback( tx.submit(), new FutureCallback<Void>() { @Override public void onSuccess(final Void result) { txOk++; } @Override public void onFailure(final Throwable t) { LOG.error("Transaction failed, {}", t); txError++; } }); tx = chain.newWriteOnlyTransaction(); writeCnt = 0; } } // *** Clean up and close the transaction chain *** // Submit the outstanding transaction even if it's empty and wait for it to finish // We need to empty the transaction chain before closing it try { txSubmitted++; tx.submit().checkedGet(); txOk++; } catch (TransactionCommitFailedException e) { LOG.error("Transaction failed", e); txError++; } try { chain.close(); } catch (IllegalStateException e) { LOG.error("Transaction close failed,", e); } LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError)); }
/** * Constructor * * @param broker * @param notifyService */ public KafkaUserAgentFactory( final DataBroker broker, final DOMNotificationService notifyService) { if (LOG.isDebugEnabled()) { LOG.debug("in KafkaUserAgentFactory()"); } this.dataBroker = Preconditions.checkNotNull(broker, "broker"); this.notificationService = Preconditions.checkNotNull(notifyService, "notifyService"); // register as data change listener to data broker kafkaProducerConfigReg = broker.registerDataTreeChangeListener(KAFKA_CONFIG_PATH, this); }
public void start() { InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class) .child(Topology.class, new TopologyKey(MdsalHelper.OVSDB_TOPOLOGY_ID)) .child(Node.class); registration = dataBroker.registerDataChangeListener( LogicalDatastoreType.OPERATIONAL, path, this, DataChangeScope.SUBTREE); LOG.info( "netvirt OvsdbDataChangeListener: dataBroker= {}, registration= {}", dataBroker, registration); triggerUpdates(); }
@Override public Future<RpcResult<ReadServiceFunctionOutput>> readServiceFunction( ReadServiceFunctionInput input) { printTraceStart(LOG); LOG.info("Input: " + input); if (dataBroker != null) { ServiceFunctionKey sfkey = new ServiceFunctionKey(input.getName()); InstanceIdentifier<ServiceFunction> sfIID; sfIID = InstanceIdentifier.builder(ServiceFunctions.class) .child(ServiceFunction.class, sfkey) .toInstance(); ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction(); Optional<ServiceFunction> dataObject = null; try { dataObject = readTx.read(LogicalDatastoreType.CONFIGURATION, sfIID).get(); } catch (InterruptedException | ExecutionException e) { LOG.debug("Failed to readServiceFunction : {}", e.getMessage()); } if (dataObject instanceof ServiceFunction) { LOG.debug("readServiceFunction Success: {}", ((ServiceFunction) dataObject).getName()); ServiceFunction serviceFunction = (ServiceFunction) dataObject; ReadServiceFunctionOutput readServiceFunctionOutput = null; ReadServiceFunctionOutputBuilder outputBuilder = new ReadServiceFunctionOutputBuilder(); outputBuilder .setName(serviceFunction.getName()) .setIpMgmtAddress(serviceFunction.getIpMgmtAddress()) .setType(serviceFunction.getType()); readServiceFunctionOutput = outputBuilder.build(); printTraceStop(LOG); return Futures.immediateFuture( Rpcs.<ReadServiceFunctionOutput>getRpcResult( true, readServiceFunctionOutput, Collections.<RpcError>emptySet())); } printTraceStop(LOG); return Futures.immediateFuture( Rpcs.<ReadServiceFunctionOutput>getRpcResult( true, null, Collections.<RpcError>emptySet())); } else { LOG.warn("\n####### Data Provider is NULL : {}", Thread.currentThread().getStackTrace()[1]); printTraceStop(LOG); return Futures.immediateFuture( Rpcs.<ReadServiceFunctionOutput>getRpcResult( true, null, Collections.<RpcError>emptySet())); } }
@Override public Future<RpcResult<Void>> putServiceFunction(PutServiceFunctionInput input) { printTraceStart(LOG); LOG.info("\n####### Input: " + input); if (dataBroker != null) { // Data PLane Locator List<SfDataPlaneLocator> sfDataPlaneLocatorList = input.getSfDataPlaneLocator(); ServiceFunctionBuilder sfbuilder = new ServiceFunctionBuilder(); ServiceFunctionKey sfkey = new ServiceFunctionKey(input.getName()); ServiceFunction sf = sfbuilder .setName(input.getName()) .setType(input.getType()) .setKey(sfkey) .setIpMgmtAddress(input.getIpMgmtAddress()) .setSfDataPlaneLocator(sfDataPlaneLocatorList) .build(); InstanceIdentifier<ServiceFunction> sfEntryIID = InstanceIdentifier.builder(ServiceFunctions.class) .child(ServiceFunction.class, sf.getKey()) .toInstance(); WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction(); writeTx.merge(LogicalDatastoreType.CONFIGURATION, sfEntryIID, sf); writeTx.commit(); } else { LOG.warn("\n####### Data Provider is NULL : {}", Thread.currentThread().getStackTrace()[1]); } printTraceStop(LOG); return Futures.immediateFuture(Rpcs.<Void>getRpcResult(true, Collections.<RpcError>emptySet())); }
@Before public void setUp() { final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent()); Mockito.when(rTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)) .thenReturn(noExistNodeFuture); Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx); Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class))) .thenReturn(txChainFactory); Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent); final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create(); final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply = SettableFuture.create(); Mockito.when(requestContext.getFuture()).thenReturn(settableFuture); Mockito.doAnswer( new Answer<Object>() { @SuppressWarnings("unchecked") @Override public Object answer(final InvocationOnMock invocation) { settableFuture.set((RpcResult<GetAsyncReply>) invocation.getArguments()[0]); return null; } }) .when(requestContext) .setResult(any(RpcResult.class)); Mockito.when(requestContextMultiReply.getFuture()).thenReturn(settableFutureMultiReply); Mockito.doAnswer( new Answer<Object>() { @SuppressWarnings("unchecked") @Override public Object answer(final InvocationOnMock invocation) { settableFutureMultiReply.set( (RpcResult<MultipartReply>) invocation.getArguments()[0]); return null; } }) .when(requestContextMultiReply) .setResult(any(RpcResult.class)); Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx); Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx); Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider); Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter); Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3); Mockito.when( messageTranslatorPacketReceived.translate( any(Object.class), any(DeviceContext.class), any(Object.class))) .thenReturn(mock(PacketReceived.class)); Mockito.when( messageTranslatorFlowCapableNodeConnector.translate( any(Object.class), any(DeviceContext.class), any(Object.class))) .thenReturn(mock(FlowCapableNodeConnector.class)); Mockito.when( translatorLibrary.lookupTranslator( eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PacketIn.class.getName())))) .thenReturn(messageTranslatorPacketReceived); Mockito.when( translatorLibrary.lookupTranslator( eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PortGrouping.class.getName())))) .thenReturn(messageTranslatorFlowCapableNodeConnector); deviceContext = new DeviceContextImpl( connectionContext, deviceState, dataBroker, timer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, txChainManager); xid = new Xid(atomicLong.incrementAndGet()); xidMulti = new Xid(atomicLong.incrementAndGet()); }
/** * TODO * * @param virtualNetwork TODO * @param userVnPnMapping TODO * @param physicalPaths TODO */ 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) { // If mapping failed, reset the user physical resources. pnResourcesTracker.resetResource(userId); throw new VNMappingException( "Failed mapping for the virtual node " + virtualNode.getNodeId().getValue() + " in the virtual network " + virtualNetwork.getNetworkId().getValue()); } // Keep physical resource. 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) { // If mapping failed, reset the user physical resources. 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()); } // Keep physical resource. 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) { // If mapping failed, reset the user physical resources. pnResourcesTracker.resetResource(userId); throw new VNMappingException( "Failed mapping for the virtual link " + virtualLink.getLinkId().getValue() + " in the virtual network " + virtualNetwork.getNetworkId().getValue()); } // Keep physical resource. 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; }
/** * TODO * * @param attribute TODO * @param physicalResourceRequirement TODO * @return TODO */ private boolean checkPhysicalNodeAttributeSatisfied( org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network .rev151010.physical.node.instance.Attribute attribute, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network .rev151010.virtual.node.instance.PhysicalResourceRequirement physicalResourceRequirement) throws VNMappingException { ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction(); InstanceIdentifier<PhysicalNodeAttributeDefinition> physicalNodeAttributeDefinitionIid = InstanceIdentifier.builder(PhysicalNodeAttributeDefinitions.class) .child( PhysicalNodeAttributeDefinition.class, new PhysicalNodeAttributeDefinitionKey(attribute.getAttributeName())) .build(); Optional<PhysicalNodeAttributeDefinition> result; try { result = readOnlyTransaction .read(LogicalDatastoreType.CONFIGURATION, physicalNodeAttributeDefinitionIid) .get(); } catch (InterruptedException exception) { throw new VNMappingException( "Can not read the physical node attribute definition " + "with attribute name " + attribute.getAttributeName().getValue() + "."); } catch (ExecutionException exception) { throw new VNMappingException( "Can not read the physical node attribute definition " + "with attribute name " + attribute.getAttributeName().getValue() + "."); } if (!result.isPresent()) { throw new VNMappingException( "The physical node attribute definition with attribute name " + attribute.getAttributeName().getValue() + " does not exist."); } PhysicalNodeAttributeDefinition physicalNodeAttributeDefinition = result.get(); List<AttributeMatchPatterns.AttributeMatchPattern> attributeMatchPatterns = physicalNodeAttributeDefinition.getAttributeMatchPatterns().getAttributeMatchPattern(); org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network .rev151010.virtual.node.instance.PhysicalResourceRequirement.AttributeMatchPattern attributeMatchPattern = physicalResourceRequirement.getAttributeMatchPattern(); if (!checkAttributeMatchPatternSpecified(attributeMatchPatterns, attributeMatchPattern)) { throw new VNMappingException( "The attribute match pattern " + attributeMatchPattern + " is not specified in the physical node attribute definition " + "with attribute name " + attribute.getAttributeName().getValue() + "."); } switch (physicalNodeAttributeDefinition.getAttributeValueType()) { case String: return checkAttributeStringValueSatisfied( attribute.getAttributeValue().getStringValue(), physicalResourceRequirement.getAttributeValue().getStringValue(), attributeMatchPattern); case Int: return checkAttributeIntegerValueSatisfied( attribute.getAttributeValue().getIntValue(), physicalResourceRequirement.getAttributeValue().getIntValue(), attributeMatchPattern); case Range: return checkAttributeRangeValueSatisfied( attribute.getAttributeValue().getIntValue(), physicalResourceRequirement.getAttributeValue().getRangeValue(), attributeMatchPattern); default: throw new VNMappingException( "Unsupported physical node attribute value type " + physicalNodeAttributeDefinition.getAttributeValueType() + "."); // break; } // return false; }