use of org.alien4cloud.tosca.model.definitions.IValue in project alien4cloud by alien4cloud.
the class ToscaParserSimpleProfileAlien130Test method testAttributesConcatValid.
@SuppressWarnings("unchecked")
@Test
public void testAttributesConcatValid() throws Throwable {
Csar csar = new Csar("tosca-normative-types", "1.0.0-SNAPSHOT-wd03");
// Mockito.when(csarRepositorySearchService.getArchive(csar.getId())).thenReturn(csar);
NodeType mockedResult = Mockito.mock(NodeType.class);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
Mockito.when(mockedResult.getId()).thenReturn("tosca.nodes.Compute:1.0.0-SNAPSHOT-wd03");
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-node-type-inputs.yml"));
Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion());
assertNoBlocker(parsingResult);
ArchiveRoot archiveRoot = parsingResult.getResult();
assertNotNull(archiveRoot.getArchive());
Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
// check nodetype elements
Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
Assert.assertEquals("alien.test.TestComputeConcat", entry.getKey());
NodeType nodeType = entry.getValue();
nodeType.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID);
Map<String, IValue> attributes = nodeType.getAttributes();
IValue simpleDefinition = attributes.get("simple_definition");
IValue ipAddressDefinition = attributes.get("ip_address");
IValue simpleConcat = attributes.get("simple_concat");
IValue complexConcat = attributes.get("complex_concat");
// check attributes types
assertTrue(simpleDefinition.getClass().equals(AttributeDefinition.class));
assertTrue(ipAddressDefinition.getClass().equals(AttributeDefinition.class));
assertTrue(simpleConcat.getClass().equals(ConcatPropertyValue.class));
assertTrue(complexConcat.getClass().equals(ConcatPropertyValue.class));
// Test nodeType serialization
String nodeTypeJson = JsonUtil.toString(nodeType);
// recover node from serialized string
NodeType nodeTypeDeserialized = JsonUtil.readObject(nodeTypeJson, NodeType.class);
assertNotNull(nodeTypeDeserialized);
attributes = nodeTypeDeserialized.getAttributes();
simpleDefinition = attributes.get("simple_definition");
ipAddressDefinition = attributes.get("ip_address");
simpleConcat = attributes.get("simple_concat");
complexConcat = attributes.get("complex_concat");
// check attributes types after deserialization
assertTrue(simpleDefinition.getClass().equals(AttributeDefinition.class));
assertTrue(ipAddressDefinition.getClass().equals(AttributeDefinition.class));
assertTrue(simpleConcat.getClass().equals(ConcatPropertyValue.class));
assertTrue(complexConcat.getClass().equals(ConcatPropertyValue.class));
}
use of org.alien4cloud.tosca.model.definitions.IValue in project alien4cloud by alien4cloud.
the class InterfaceParser method parseInterfaceDefinition.
private Interface parseInterfaceDefinition(MappingNode node, ParsingContextExecution context) {
Interface interfaz = new Interface();
Map<String, Operation> operations = Maps.newHashMap();
interfaz.setOperations(operations);
Map<String, IValue> interfaceInputs = null;
for (NodeTuple entry : node.getValue()) {
String key = scalarParser.parse(entry.getKeyNode(), context);
if (INPUTS_KEY.equals(key)) {
interfaceInputs = inputsParser.parse(entry.getValueNode(), context);
} else if (DESCRIPTION_KEY.equals(key)) {
interfaz.setDescription(scalarParser.parse(entry.getValueNode(), context));
} else if (TYPE_KEY.equals(key)) {
interfaz.setType(getInterfaceType(scalarParser.parse(entry.getValueNode(), context)));
} else {
if (entry.getValueNode() instanceof ScalarNode) {
Operation operation = new Operation();
// implementation artifact parsing should be done using a deferred parser as we need to look for artifact types.
operation.setImplementationArtifact(implementationArtifactParser.parse(entry.getValueNode(), context));
operations.put(key, operation);
} else {
operations.put(key, operationParser.parse(entry.getValueNode(), context));
}
}
}
if (interfaceInputs != null) {
for (Operation operation : operations.values()) {
if (operation.getInputParameters() == null) {
operation.setInputParameters(Maps.newHashMap());
}
for (Entry<String, IValue> inputEntry : interfaceInputs.entrySet()) {
if (!operation.getInputParameters().containsKey(inputEntry.getKey())) {
operation.getInputParameters().put(inputEntry.getKey(), inputEntry.getValue());
}
}
}
}
return interfaz;
}
use of org.alien4cloud.tosca.model.definitions.IValue in project alien4cloud by alien4cloud.
the class TopologyTreeBuilderService method processIValueForOperationOutput.
private <V extends AbstractInstantiableToscaType> void processIValueForOperationOutput(String name, IValue iValue, final IPaaSTemplate<V> paaSTemplate, final Map<String, PaaSNodeTemplate> paaSNodeTemplates, final boolean fromAttributes) {
if (iValue instanceof FunctionPropertyValue) {
FunctionPropertyValue function = (FunctionPropertyValue) iValue;
if (ToscaFunctionConstants.GET_OPERATION_OUTPUT.equals(function.getFunction())) {
String formatedAttributeName = null;
List<? extends IPaaSTemplate> paaSTemplates = FunctionEvaluator.getPaaSTemplatesFromKeyword(paaSTemplate, function.getTemplateName(), paaSNodeTemplates);
if (fromAttributes) {
// nodeId:attributeName
formatedAttributeName = AlienUtils.prefixWith(AlienUtils.COLON_SEPARATOR, name, paaSTemplate.getId());
}
registerOperationOutput(paaSTemplates, function.getInterfaceName(), function.getOperationName(), function.getElementNameToFetch(), formatedAttributeName);
}
} else if (iValue instanceof ConcatPropertyValue) {
ConcatPropertyValue concatFunction = (ConcatPropertyValue) iValue;
for (IValue param : concatFunction.getParameters()) {
processIValueForOperationOutput(name, param, paaSTemplate, paaSNodeTemplates, false);
}
}
}
use of org.alien4cloud.tosca.model.definitions.IValue in project alien4cloud by alien4cloud.
the class ToscaParserSimpleProfileAlien130Test method testGetOperationOutputFunction.
@Test
@SuppressWarnings("unchecked")
public void testGetOperationOutputFunction() throws Throwable {
Csar csar = new Csar("tosca-normative-types", "1.0.0-SNAPSHOT-wd03");
// Mockito.when(csarRepositorySearchService.getArchive(csar.getId())).thenReturn(csar);
NodeType mockedResult = Mockito.mock(NodeType.class);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Container"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Endpoint"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
RelationshipType hostedOn = new RelationshipType();
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-functions.yml"));
Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion());
assertNoBlocker(parsingResult);
ArchiveRoot archiveRoot = parsingResult.getResult();
assertNotNull(archiveRoot.getArchive());
Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
// check nodetype elements
Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
Assert.assertEquals("my_company.my_types.MyAppNodeType", entry.getKey());
NodeType nodeType = entry.getValue();
// on input level
Map<String, Interface> interfaces = nodeType.getInterfaces();
Interface customInterface = interfaces.get("custom");
Map<String, IValue> doSomethingInputs = customInterface.getOperations().get("do_something").getInputParameters();
assertNotNull(doSomethingInputs);
Assert.assertFalse(doSomethingInputs.isEmpty());
IValue operationOutput_input = doSomethingInputs.get("operationOutput_input");
assertTrue(operationOutput_input instanceof FunctionPropertyValue);
FunctionPropertyValue function = (FunctionPropertyValue) operationOutput_input;
Assert.assertEquals("get_operation_output", function.getFunction());
Assert.assertEquals(4, function.getParameters().size());
Map<String, IValue> attributes = nodeType.getAttributes();
IValue operationOutputAttr = attributes.get("url");
// check attributes types
assertTrue(operationOutputAttr instanceof FunctionPropertyValue);
function = (FunctionPropertyValue) operationOutputAttr;
Assert.assertEquals("get_operation_output", function.getFunction());
Assert.assertEquals(4, function.getParameters().size());
}
use of org.alien4cloud.tosca.model.definitions.IValue in project alien4cloud by alien4cloud.
the class RuntimeController method validateParameters.
private void validateParameters(Interface interfass, OperationExecRequest operationRequest, Set<CSARDependency> dependencies) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException, ConstraintRequiredParameterException {
try {
if (dependencies != null) {
ToscaContext.init(dependencies);
}
ArrayList<String> missingParams = Lists.newArrayList();
Operation operation = interfass.getOperations().get(operationRequest.getOperationName());
if (operation.getInputParameters() != null) {
for (Entry<String, IValue> inputParameter : operation.getInputParameters().entrySet()) {
if (inputParameter.getValue().isDefinition()) {
Object requestInputParameter = operationRequest.getParameters() == null ? null : operationRequest.getParameters().get(inputParameter.getKey());
PropertyDefinition currentOperationParameter = (PropertyDefinition) inputParameter.getValue();
if (requestInputParameter != null) {
if (!(requestInputParameter instanceof Map) || !FunctionEvaluator.containGetSecretFunction(PropertyService.asFunctionPropertyValue(requestInputParameter))) {
// recover the good property definition for the current parameter
ConstraintPropertyService.checkPropertyConstraint(inputParameter.getKey(), requestInputParameter, currentOperationParameter);
}
} else if (currentOperationParameter.isRequired()) {
// input param not in the request, id required this is a missing parameter...
missingParams.add(inputParameter.getKey());
} else {
// set the value to null
operation.getInputParameters().put(inputParameter.getKey(), null);
}
}
}
}
// check required input issue
if (!missingParams.isEmpty()) {
log.error("Missing required parameter", missingParams);
ConstraintInformation constraintInformation = new ConstraintInformation(null, null, missingParams.toString(), "required");
throw new ConstraintRequiredParameterException("Missing required parameters", null, constraintInformation);
}
} finally {
if (ToscaContext.get() != null) {
ToscaContext.destroy();
}
}
}
Aggregations