2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.toscaparser.api.functions;
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;
33 import java.util.ArrayList;
36 public class GetOperationOutput extends Function {
38 public GetOperationOutput(TopologyTemplate ttpl, Object context, String name, ArrayList<Object> args) {
39 super(ttpl, context, name, args);
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));
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\""));
56 private String _findInterfaceName(String _interfaceName) {
57 boolean bFound = false;
58 for (String sect : InterfacesDef.SECTIONS) {
59 if (sect.equals(_interfaceName)) {
65 return _interfaceName;
67 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE160", String.format(
68 "ValueError: invalid interface name \"%s\" in \"get_operation_output\"",
74 private String _findOperationName(String interfaceName, String operationName) {
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)) {
88 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE161", String.format(
89 "ValueError: Invalid operation of Configure interface \"%s\" in \"get_operation_output\"",
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)) {
104 return operationName;
106 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE162", String.format(
107 "ValueError: Invalid operation of Configure interface \"%s\" in \"get_operation_output\"",
112 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE163", String.format(
113 "ValueError: Invalid interface name \"%s\" in \"get_operation_output\"",
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"));
127 return ((RelationshipTemplate) context).getTarget();
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"));
136 return ((RelationshipTemplate) context).getTarget();
139 if (nodeTemplateName.equals(SELF) && !(context instanceof ArrayList)) {
140 name = ((NodeTemplate) context).getName();
142 name = nodeTemplateName;
144 for (NodeTemplate nt : toscaTpl.getNodeTemplates()) {
145 if (nodeTemplateName.equals(name)) {
149 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE166", String.format(
150 "KeyError: Node template \"%s\" was not found", nodeTemplateName)));
155 public Object result() {
163 class GetOperationOutput(Function):
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])
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)))
177 def _find_interface_name(self, interface_name):
178 if interface_name in toscaparser.elements.interfaces.SECTIONS:
179 return interface_name
181 ValidationIssueCollector.appendException(
182 ValueError(_('Enter a valid interface name'
183 ).format(GET_OPERATION_OUTPUT)))
186 def _find_operation_name(self, interface_name, operation_name):
187 if(interface_name == 'Configure' or
188 interface_name == 'tosca.interfaces.node.relationship.Configure'):
191 interfaces_relationship_configure_operations):
192 return operation_name
194 ValidationIssueCollector.appendException(
195 ValueError(_('Enter an operation of Configure interface'
196 ).format(GET_OPERATION_OUTPUT)))
198 elif(interface_name == 'Standard' or
199 interface_name == 'tosca.interfaces.node.lifecycle.Standard'):
201 StatefulEntityType.interfaces_node_lifecycle_operations):
202 return operation_name
204 ValidationIssueCollector.appendException(
205 ValueError(_('Enter an operation of Standard interface'
206 ).format(GET_OPERATION_OUTPUT)))
209 ValidationIssueCollector.appendException(
210 ValueError(_('Enter a valid operation name'
211 ).format(GET_OPERATION_OUTPUT)))
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')))
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')))
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:
236 ValidationIssueCollector.appendException(
238 'Node template "{0}" was not found.'
239 ).format(node_template_name)))