Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / functions / GetInput.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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  * ================================================================================
19  */
20 package org.onap.sdc.toscaparser.api.functions;
21
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;
27
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30
31 public class GetInput extends Function {
32
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";
38
39     public GetInput(TopologyTemplate toscaTpl, Object context, String name, ArrayList<Object> _args) {
40         super(toscaTpl, context, name, _args);
41
42     }
43
44     @Override
45     void validate() {
46
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\"",
51 //                  args.toString()));
52 //          }
53         boolean bFound = false;
54         for (Input inp : toscaTpl.getInputs()) {
55             if (inp.getName().equals(args.get(0))) {
56                 bFound = true;
57                 break;
58             }
59         }
60         if (!bFound) {
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());
66             String type;
67
68             for (int argumentNumber = 1; argumentNumber < args.size(); argumentNumber++) {
69                 String dataTypeName = "";
70                 bFound = false;
71                 if (INDEX.equals(args.get(argumentNumber).toString()) || (args.get(argumentNumber) instanceof Integer)) {
72                     bFound = true;
73                 } else {
74                     type = (String) data.get(TYPE);
75                     //get type name
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);
79                     } else {
80                         dataTypeName = type;
81                     }
82                     //check property name
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());
87                         if (data != null) {
88                             bFound = true;
89                         }
90                     }
91                 }
92                 if (!bFound) {
93                     ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE282", String.format(
94                             "UnknownDataType: Unknown data type \"%s\"", args.get(argumentNumber))));
95                 }
96             }
97         }
98     }
99
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");
106
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));
113                 }
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))));
117                                 return null;
118 */
119             }
120             return value;
121         }
122
123         Input inputDef = null;
124         for (Input inpDef : toscaTpl.getInputs()) {
125             if (getInputName().equals(inpDef.getName())) {
126                 inputDef = inpDef;
127                 break;
128             }
129         }
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());
135                 }
136 /*
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)))));
140                                 return null;
141 */
142             }
143             return inputDef.getDefault();
144         }
145         return null;
146     }
147
148     public String getInputName() {
149         return (String) args.get(0);
150     }
151
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);
156     }
157
158     public ArrayList<Object> getArguments() {
159         return args;
160     }
161 }
162
163 /*python
164
165 class GetInput(Function):
166 """Get a property value declared within the input of the service template.
167
168 Arguments:
169
170 * Input name.
171
172 Example:
173
174 * get_input: port
175 """
176
177 def validate(self):
178     if len(self.args) != 1:
179         ValidationIssueCollector.appendException(
180             ValueError(_(
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]))
187
188 def result(self):
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])
194
195     input = [input_def for input_def in self.tosca_tpl.inputs
196              if self.input_name == input_def.name][0]
197     return input.default
198
199 @property
200 def input_name(self):
201     return self.args[0]
202
203 */