2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2017 AT&T Intellectual Property.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
17 * Modifications copyright (c) 2019 Fujitsu Limited.
18 * ================================================================================
20 package org.onap.sdc.toscaparser.api.functions;
22 import org.onap.sdc.toscaparser.api.DataEntity;
23 import org.onap.sdc.toscaparser.api.TopologyTemplate;
24 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26 import org.onap.sdc.toscaparser.api.parameters.Input;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
31 public class GetInput extends Function {
33 public static final String INDEX = "INDEX";
34 public static final String INPUTS = "inputs";
35 public static final String TYPE = "type";
36 public static final String PROPERTIES = "properties";
37 public static final String ENTRY_SCHEMA = "entry_schema";
39 public GetInput(TopologyTemplate toscaTpl, Object context, String name, ArrayList<Object> _args) {
40 super(toscaTpl, context, name, _args);
47 // if(args.size() != 1) {
48 // //PA - changed to WARNING from CRITICAL after talking to Renana, 22/05/2017
49 // ThreadLocalsHolder.getCollector().appendWarning(String.format(
50 // "ValueError: Expected one argument for function \"get_input\" but received \"%s\"",
53 boolean bFound = false;
54 for (Input inp : toscaTpl.getInputs()) {
55 if (inp.getName().equals(args.get(0))) {
61 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE158", String.format(
62 "UnknownInputError: Unknown input \"%s\"", args.get(0))));
63 } else if (args.size() > 2) {
64 LinkedHashMap<String, Object> inputs = (LinkedHashMap<String, Object>) toscaTpl.getTpl().get(INPUTS);
65 LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) inputs.get(getInputName());
68 for (int argumentNumber = 1; argumentNumber < args.size(); argumentNumber++) {
69 String dataTypeName = "";
71 if (INDEX.equals(args.get(argumentNumber).toString()) || (args.get(argumentNumber) instanceof Integer)) {
74 type = (String) data.get(TYPE);
76 if (type.equals("list") || type.equals("map")) {
77 LinkedHashMap<String, Object> schema = (LinkedHashMap<String, Object>) data.get(ENTRY_SCHEMA);
78 dataTypeName = (String) schema.get(TYPE);
83 LinkedHashMap<String, Object> dataType = (LinkedHashMap<String, Object>) toscaTpl.getCustomDefs().get(dataTypeName);
84 if (dataType != null) {
85 LinkedHashMap<String, Object> props = (LinkedHashMap<String, Object>) dataType.get(PROPERTIES);
86 data = (LinkedHashMap<String, Object>) props.get(args.get(argumentNumber).toString());
93 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE282", String.format(
94 "UnknownDataType: Unknown data type \"%s\"", args.get(argumentNumber))));
100 public Object result() {
101 if (toscaTpl.getParsedParams() != null &&
102 toscaTpl.getParsedParams().get(getInputName()) != null) {
103 LinkedHashMap<String, Object> ttinp = (LinkedHashMap<String, Object>) toscaTpl.getTpl().get(INPUTS);
104 LinkedHashMap<String, Object> ttinpinp = (LinkedHashMap<String, Object>) ttinp.get(getInputName());
105 String type = (String) ttinpinp.get("type");
107 Object value = DataEntity.validateDatatype(
108 type, toscaTpl.getParsedParams().get(getInputName()), null, toscaTpl.getCustomDefs(), null);
109 //SDC resolving Get Input
110 if (value instanceof ArrayList) {
111 if (args.size() == 2 && args.get(1) instanceof Integer && ((ArrayList) value).size() > (Integer) args.get(1)) {
112 return ((ArrayList) value).get((Integer) args.get(1));
114 /* commented out for network cloud (SDNC)
115 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE273",String.format(
116 "GetInputError: cannot resolve input name \"%s\", the expected structure is an argument with a name of input type list and a second argument with an index in the list", args.get(0))));
123 Input inputDef = null;
124 for (Input inpDef : toscaTpl.getInputs()) {
125 if (getInputName().equals(inpDef.getName())) {
130 if (inputDef != null) {
131 if (args.size() == 2 && inputDef.getDefault() != null && inputDef.getDefault() instanceof ArrayList) {
132 if (args.get(1) instanceof Integer
133 && ((ArrayList) inputDef.getDefault()).size() > ((Integer) args.get(1)).intValue()) {
134 return ((ArrayList) inputDef.getDefault()).get(((Integer) args.get(1)).intValue());
137 commented out for network cloud (SDNC)
138 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE274",(String.format(
139 "GetInputError: cannot resolve input Def name \"%s\", the expected structure is an argument with a name of input type list and a second argument with an index in the list", args.get(0)))));
143 return inputDef.getDefault();
148 public String getInputName() {
149 return (String) args.get(0);
152 public LinkedHashMap<String, Object> getEntrySchema() {
153 LinkedHashMap<String, Object> inputs = (LinkedHashMap<String, Object>) toscaTpl.getTpl().get(INPUTS);
154 LinkedHashMap<String, Object> inputValue = (LinkedHashMap<String, Object>) inputs.get(getInputName());
155 return (LinkedHashMap<String, Object>) inputValue.get(ENTRY_SCHEMA);
158 public ArrayList<Object> getArguments() {
165 class GetInput(Function):
166 """Get a property value declared within the input of the service template.
178 if len(self.args) != 1:
179 ValidationIssueCollector.appendException(
181 'Expected one argument for function "get_input" but '
182 'received "%s".') % self.args))
183 inputs = [input.name for input in self.tosca_tpl.inputs]
184 if self.args[0] not in inputs:
185 ValidationIssueCollector.appendException(
186 UnknownInputError(input_name=self.args[0]))
189 if self.tosca_tpl.parsed_params and \
190 self.input_name in self.tosca_tpl.parsed_params:
191 return DataEntity.validate_datatype(
192 self.tosca_tpl.tpl['inputs'][self.input_name]['type'],
193 self.tosca_tpl.parsed_params[self.input_name])
195 input = [input_def for input_def in self.tosca_tpl.inputs
196 if self.input_name == input_def.name][0]
200 def input_name(self):