Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / functions / GetOperationOutput.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.functions;
22
23 import org.onap.sdc.toscaparser.api.EntityTemplate;
24 import org.onap.sdc.toscaparser.api.NodeTemplate;
25 import org.onap.sdc.toscaparser.api.RelationshipTemplate;
26 import org.onap.sdc.toscaparser.api.TopologyTemplate;
27 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
28 import org.onap.sdc.toscaparser.api.elements.InterfacesDef;
29 import org.onap.sdc.toscaparser.api.elements.RelationshipType;
30 import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
31 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
32
33 import java.util.ArrayList;
34
35
36 public class GetOperationOutput extends Function {
37
38     public GetOperationOutput(TopologyTemplate ttpl, Object context, String name, ArrayList<Object> args) {
39         super(ttpl, context, name, args);
40     }
41
42     @Override
43     public void validate() {
44         if (args.size() == 4) {
45             _findNodeTemplate((String) args.get(0));
46             String interfaceName = _findInterfaceName((String) args.get(1));
47             _findOperationName(interfaceName, (String) args.get(2));
48         } else {
49             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE159",
50                     "ValueError: Illegal arguments for function \"get_operation_output\". " +
51                             "Expected arguments: \"template_name\",\"interface_name\"," +
52                             "\"operation_name\",\"output_variable_name\""));
53         }
54     }
55
56     private String _findInterfaceName(String _interfaceName) {
57         boolean bFound = false;
58         for (String sect : InterfacesDef.SECTIONS) {
59             if (sect.equals(_interfaceName)) {
60                 bFound = true;
61                 break;
62             }
63         }
64         if (bFound) {
65             return _interfaceName;
66         } else {
67             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE160", String.format(
68                     "ValueError: invalid interface name \"%s\" in \"get_operation_output\"",
69                     _interfaceName)));
70             return null;
71         }
72     }
73
74     private String _findOperationName(String interfaceName, String operationName) {
75
76         if (interfaceName.equals("Configure") ||
77                 interfaceName.equals("tosca.interfaces.node.relationship.Configure")) {
78             boolean bFound = false;
79             for (String sect : StatefulEntityType.INTERFACE_RELATIONSHIP_CONFIGURE_OPERATIONS) {
80                 if (sect.equals(operationName)) {
81                     bFound = true;
82                     break;
83                 }
84             }
85             if (bFound) {
86                 return operationName;
87             } else {
88                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE161", String.format(
89                         "ValueError: Invalid operation of Configure interface \"%s\" in \"get_operation_output\"",
90                         operationName)));
91                 return null;
92             }
93         }
94         if (interfaceName.equals("Standard") ||
95                 interfaceName.equals("tosca.interfaces.node.lifecycle.Standard")) {
96             boolean bFound = false;
97             for (String sect : StatefulEntityType.INTERFACE_NODE_LIFECYCLE_OPERATIONS) {
98                 if (sect.equals(operationName)) {
99                     bFound = true;
100                     break;
101                 }
102             }
103             if (bFound) {
104                 return operationName;
105             } else {
106                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE162", String.format(
107                         "ValueError: Invalid operation of Configure interface \"%s\" in \"get_operation_output\"",
108                         operationName)));
109                 return null;
110             }
111         } else {
112             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE163", String.format(
113                     "ValueError: Invalid interface name \"%s\" in \"get_operation_output\"",
114                     interfaceName)));
115             return null;
116         }
117     }
118
119     private NodeTemplate _findNodeTemplate(String nodeTemplateName) {
120         if (nodeTemplateName.equals(TARGET)) {
121             if (!(((EntityTemplate) context).getTypeDefinition() instanceof RelationshipType)) {
122                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE164",
123                         "KeyError: \"TARGET\" keyword can only be used in context " +
124                                 " to \"Relationships\" target node"));
125                 return null;
126             }
127             return ((RelationshipTemplate) context).getTarget();
128         }
129         if (nodeTemplateName.equals(SOURCE)) {
130             if (!(((EntityTemplate) context).getTypeDefinition() instanceof RelationshipType)) {
131                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE165",
132                         "KeyError: \"SOURCE\" keyword can only be used in context " +
133                                 " to \"Relationships\" source node"));
134                 return null;
135             }
136             return ((RelationshipTemplate) context).getTarget();
137         }
138         String name;
139         if (nodeTemplateName.equals(SELF) && !(context instanceof ArrayList)) {
140             name = ((NodeTemplate) context).getName();
141         } else {
142             name = nodeTemplateName;
143         }
144         for (NodeTemplate nt : toscaTpl.getNodeTemplates()) {
145             if (nodeTemplateName.equals(name)) {
146                 return nt;
147             }
148         }
149         ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE166", String.format(
150                 "KeyError: Node template \"%s\" was not found", nodeTemplateName)));
151         return null;
152     }
153
154     @Override
155     public Object result() {
156         return this;
157     }
158
159 }
160
161 /*python 
162
163 class GetOperationOutput(Function):
164 def validate(self):
165     if len(self.args) == 4:
166         self._find_node_template(self.args[0])
167         interface_name = self._find_interface_name(self.args[1])
168         self._find_operation_name(interface_name, self.args[2])
169     else:
170         ValidationIssueCollector.appendException(
171             ValueError(_('Illegal arguments for function "{0}". Expected '
172                          'arguments: "template_name","interface_name",'
173                          '"operation_name","output_variable_name"'
174                          ).format(GET_OPERATION_OUTPUT)))
175         return
176
177 def _find_interface_name(self, interface_name):
178     if interface_name in toscaparser.elements.interfaces.SECTIONS:
179         return interface_name
180     else:
181         ValidationIssueCollector.appendException(
182             ValueError(_('Enter a valid interface name'
183                          ).format(GET_OPERATION_OUTPUT)))
184         return
185
186 def _find_operation_name(self, interface_name, operation_name):
187     if(interface_name == 'Configure' or
188        interface_name == 'tosca.interfaces.node.relationship.Configure'):
189         if(operation_name in
190            StatefulEntityType.
191            interfaces_relationship_configure_operations):
192             return operation_name
193         else:
194             ValidationIssueCollector.appendException(
195                 ValueError(_('Enter an operation of Configure interface'
196                              ).format(GET_OPERATION_OUTPUT)))
197             return
198     elif(interface_name == 'Standard' or
199          interface_name == 'tosca.interfaces.node.lifecycle.Standard'):
200         if(operation_name in
201            StatefulEntityType.interfaces_node_lifecycle_operations):
202             return operation_name
203         else:
204             ValidationIssueCollector.appendException(
205                 ValueError(_('Enter an operation of Standard interface'
206                              ).format(GET_OPERATION_OUTPUT)))
207             return
208     else:
209         ValidationIssueCollector.appendException(
210             ValueError(_('Enter a valid operation name'
211                          ).format(GET_OPERATION_OUTPUT)))
212         return
213
214 def _find_node_template(self, node_template_name):
215     if node_template_name == TARGET:
216         if not isinstance(self.context.type_definition, RelationshipType):
217             ValidationIssueCollector.appendException(
218                 KeyError(_('"TARGET" keyword can only be used in context'
219                            ' to "Relationships" target node')))
220             return
221         return self.context.target
222     if node_template_name == SOURCE:
223         if not isinstance(self.context.type_definition, RelationshipType):
224             ValidationIssueCollector.appendException(
225                 KeyError(_('"SOURCE" keyword can only be used in context'
226                            ' to "Relationships" source node')))
227             return
228         return self.context.source
229     name = self.context.name \
230         if node_template_name == SELF and \
231         not isinstance(self.context, list) \
232         else node_template_name
233     for node_template in self.tosca_tpl.nodetemplates:
234         if node_template.name == name:
235             return node_template
236     ValidationIssueCollector.appendException(
237         KeyError(_(
238             'Node template "{0}" was not found.'
239             ).format(node_template_name)))
240
241 def result(self):
242     return self
243 */