public static Capability getScalableCapability(Topology topology, String nodeTemplateId, boolean throwNotFoundException) { NodeTemplate nodeTemplate = throwNotFoundException ? getNodeTemplate(topology, nodeTemplateId) : safe(topology.getNodeTemplates()).get(nodeTemplateId); if (nodeTemplate == null) { return null; } return throwNotFoundException ? NodeTemplateUtils.getCapabilityByTypeOrFail(nodeTemplate, NormativeCapabilityTypes.SCALABLE) : NodeTemplateUtils.getCapabilityByType(nodeTemplate, NormativeCapabilityTypes.SCALABLE); }
/** * Get the first capability that match the given type. * * @param nodeTemplate The node template in witch to lookup for the capability. * @param capabilityTypeName The capability type to look for. * @return The capability matching the type or null if no capability of the requested type is defined. */ public static Capability getCapabilityByType(NodeTemplate nodeTemplate, String capabilityTypeName) { Entry<String, Capability> capabilityEntry = getCapabilitEntryyByType(nodeTemplate, capabilityTypeName); return capabilityEntry == null ? null : capabilityEntry.getValue(); }
/** * Get the first capability that match the given type. * * @param nodeTemplate The node template in witch to lookup for the capability. * @param capabilityType The capability type to look for. * @return The capability matching the type. * @throws NotFoundException if the capability is not found. */ public static Capability getCapabilityByTypeOrFail(NodeTemplate nodeTemplate, String capabilityType) { Capability capability = getCapabilityByType(nodeTemplate, capabilityType); if (capability == null) { throw new NotFoundException(""); } return capability; }
private void doScaleNode(final String nodeTemplateId, final int instances, final IPaaSCallback<Object> callback, final Deployment deployment, final DeploymentTopology topology, NodeTemplate nodeTemplate, SecretProviderConfigurationAndCredentials secretProviderConfigurationAndCredentials) { final Capability capability = NodeTemplateUtils.getCapabilityByTypeOrFail(nodeTemplate, NormativeCapabilityTypes.SCALABLE); final int previousInitialInstances = TopologyUtils.getScalingProperty(NormativeComputeConstants.SCALABLE_DEFAULT_INSTANCES, capability); final int newInitialInstances = previousInitialInstances + instances; log.info("Scaling [ {} ] node from [ {} ] to [ {} ]. Updating runtime topology...", nodeTemplateId, previousInitialInstances, newInitialInstances); TopologyUtils.setScalingProperty(NormativeComputeConstants.SCALABLE_DEFAULT_INSTANCES, newInitialInstances, capability); alienMonitorDao.save(topology); IOrchestratorPlugin orchestratorPlugin = orchestratorPluginService.getOrFail(deployment.getOrchestratorId()); PaaSDeploymentContext deploymentContext = new PaaSDeploymentContext(deployment, topology, secretProviderConfigurationAndCredentials); orchestratorPlugin.scale(deploymentContext, nodeTemplateId, instances, new IPaaSCallback() { @Override public void onFailure(Throwable throwable) { log.info("Failed to scale [ {} ] node from [ {} ] to [ {} ]. rolling back to {}...", nodeTemplateId, previousInitialInstances, newInitialInstances, previousInitialInstances); TopologyUtils.setScalingProperty(NormativeComputeConstants.SCALABLE_DEFAULT_INSTANCES, previousInitialInstances, capability); alienMonitorDao.save(topology); callback.onFailure(throwable); } @Override public void onSuccess(Object data) { callback.onSuccess(data); } }); } }
/** * Add dangling requirement nodes for the specified node in the topology. * * @param topology The topology template * @param nodeTemplate The specific node template for which to add dangling requirements. */ public void addDanglingRequirements(Topology topology, TopologyContext topologyContext, NodeTemplate nodeTemplate, String requirementSkipAutoCompletion) { if (!enabled) { return; } // Get the node type NodeType nodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType()); for (RequirementDefinition requirementDefinition : nodeType.getRequirements()) { if (!requirementDefinition.getId().equals(requirementSkipAutoCompletion)) { // let's count current requirement fullfillment count int relationshipCount = countRelationshipsForRequirement(nodeTemplate, requirementDefinition); if (requirementDefinition.getLowerBound() > relationshipCount) { // we need to add some dangling requirement nodes addDanglingNodes(topology, topologyContext, nodeTemplate, requirementDefinition, requirementDefinition.getLowerBound() - relationshipCount); } } } }
@Test public void getMissingCapabilityByTypeTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); Capability capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.Unknown"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertNull(capability); }
@Test public void getCapabilityByTypeOrFailTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability type exactly equals then no tosca context and request is required Capability capability = getCapabilityByTypeOrFail(nodeTemplate, "org.alien4cloud.capabilities.SampleCapability"); assertSame(nodeCapability, capability); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByTypeOrFail(nodeTemplate, "org.alien4cloud.capabilities.TestCapability"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertSame(nodeCapability, capability); }
/** * Check if the upperBound of a requirement is reached on a node template * * @param nodeTemplate the node to check for requirement bound * @param requirementName the name of the requirement * @param dependencies the dependencies of the topology * @return true if requirement upper bound is reached, false otherwise */ public boolean isRequirementUpperBoundReachedForSource(NodeTemplate nodeTemplate, String requirementName, Set<CSARDependency> dependencies) { NodeType relatedIndexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemplate.getType(), dependencies); Requirement requirement = nodeTemplate.getRequirements().get(requirementName); if (nodeTemplate.getRelationships() == null || nodeTemplate.getRelationships().isEmpty()) { return false; } RequirementDefinition requirementDefinition = getRequirementDefinition(relatedIndexedNodeType.getRequirements(), requirementName, requirement.getType()); if (requirementDefinition.getUpperBound() == Integer.MAX_VALUE) { return false; } int count = countRelationshipsForRequirement(nodeTemplate, requirementDefinition); return count >= requirementDefinition.getUpperBound(); }
@Test public void getCapabilityByTypeTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability type exactly equals then no tosca context and request is required Capability capability = getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.SampleCapability"); assertSame(nodeCapability, capability); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.TestCapability"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertSame(nodeCapability, capability); }
@Test(expected = NotFoundException.class) public void getMissingCapabilityByTypeOrFailTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); // if the capability derives from parent type then a TOSCA context and query is required to fetch the type. CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); Capability capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByTypeOrFail(nodeTemplate, "org.alien4cloud.capabilities.Unknown"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertNull(capability); } }
if (CollectionUtils.isNotEmpty(relatedIndexedNodeType.getRequirements())) { for (RequirementDefinition reqDef : relatedIndexedNodeType.getRequirements()) { int count = countRelationshipsForRequirement(nodeTemp, reqDef); if (count < reqDef.getLowerBound()) { task.getRequirementsToImplement().add(new RequirementToSatisfy(reqDef.getId(), reqDef.getType(), reqDef.getLowerBound() - count));
private void updateRelationshipsCapabilitiesRelationships(Topology topology, NodeTemplate nodeTemplate) { List<RelationshipEntry> targetRelationships = TopologyUtils.getTargetRelationships(nodeTemplate.getName(), topology.getNodeTemplates()); for (RelationshipEntry targetRelationshipEntry : targetRelationships) { RelationshipTemplate targetRelationship = targetRelationshipEntry.getRelationship(); Capability capability = safe(nodeTemplate.getCapabilities()).get(targetRelationship.getTargetedCapabilityName()); if (capability == null || isCapabilityNotOfType(capability, targetRelationship.getRequirementType())) { Entry<String, Capability> capabilityEntry = NodeTemplateUtils.getCapabilitEntryyByType(nodeTemplate, targetRelationship.getRequirementType()); targetRelationship.setTargetedCapabilityName(capabilityEntry.getKey()); // check that the relationship type is still valid with the new capability RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, targetRelationship.getType()); if (!isValidRelationship(relationshipType, capabilityEntry.getValue())) { NodeType sourceNodeType = ToscaContext.get(NodeType.class, targetRelationshipEntry.getSource().getType()); RequirementDefinition requirementDefinition = NodeTypeUtils.getRequirementById(sourceNodeType, targetRelationshipEntry.getRelationship().getRequirementName()); NodeType targetNodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType()); CapabilityDefinition capabilityDefinition = NodeTypeUtils.getCapabilityById(targetNodeType, capabilityEntry.getKey()); RelationshipType validRelationshipType = danglingRequirementService.fetchValidRelationshipType(requirementDefinition, capabilityDefinition); targetRelationship.setType(validRelationshipType.getElementId()); targetRelationship.setType(validRelationshipType.getElementId()); targetRelationship.setArtifacts(newLinkedHashMap(safe(validRelationshipType.getArtifacts()))); targetRelationship.setAttributes(newLinkedHashMap(safe(validRelationshipType.getAttributes()))); Map<String, AbstractPropertyValue> properties = new LinkedHashMap(); TemplateBuilder.fillProperties(properties, validRelationshipType.getProperties(), null); targetRelationship.setProperties(properties); } } } }
private void doScale(final String nodeTemplateId, final int instances, final IPaaSCallback<Object> callback, final Deployment deployment, final DeploymentTopology topology, SecretProviderConfigurationAndCredentials secretProviderConfigurationAndCredentials) { NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(topology, nodeTemplateId); // get the secret provider configuration from the location Map<String, String> locationIds = TopologyLocationUtils.getLocationIds(topology); Map<String, Location> locations = deploymentTopologyService.getLocations(locationIds); SecretProviderConfigurationAndCredentials authResponse = null; if (secretProviderService.isSecretProvided(secretProviderConfigurationAndCredentials)) { authResponse = secretProviderService.generateToken(locations, secretProviderConfigurationAndCredentials.getSecretProviderConfiguration().getPluginName(), secretProviderConfigurationAndCredentials.getCredentials()); } // Get alien4cloud specific interface to support cluster controller nodes. Capability clusterControllerCapability = NodeTemplateUtils.getCapabilityByType(nodeTemplate, AlienCapabilityTypes.CLUSTER_CONTROLLER); if (clusterControllerCapability == null) { doScaleNode(nodeTemplateId, instances, callback, deployment, topology, nodeTemplate, authResponse); } else { triggerClusterManagerScaleOperation(nodeTemplateId, instances, callback, deployment, topology, clusterControllerCapability, secretProviderConfigurationAndCredentials); } }