aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/functions/GetOperationOutput.java
blob: f2231a146d9e0d6324ee016bb7f1495e6c837da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package org.openecomp.sdc.toscaparser.api.functions;

import java.util.ArrayList;

import org.openecomp.sdc.toscaparser.api.EntityTemplate;
import org.openecomp.sdc.toscaparser.api.NodeTemplate;
import org.openecomp.sdc.toscaparser.api.RelationshipTemplate;
import org.openecomp.sdc.toscaparser.api.TopologyTemplate;
import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.elements.InterfacesDef;
import org.openecomp.sdc.toscaparser.api.elements.RelationshipType;
import org.openecomp.sdc.toscaparser.api.elements.StatefulEntityType;

public class GetOperationOutput extends Function {

	public GetOperationOutput(TopologyTemplate ttpl,Object context,String name,ArrayList<Object> args) {
		super(ttpl,context,name,args);
	}

	@Override
	public void validate() {
	    if(args.size() == 4) {
	        _findNodeTemplate((String)args.get(0));
	        String interfaceName = _findInterfaceName((String)args.get(1));
	        _findOperationName(interfaceName,(String)args.get(2));
	    }
	    else {
	        ExceptionCollector.appendException(
	            "ValueError: Illegal arguments for function \"get_operation_output\". " +
	            "Expected arguments: \"template_name\",\"interface_name\"," +
	            "\"operation_name\",\"output_variable_name\"");
	    }
	}
	
	private String _findInterfaceName(String _interfaceName) {
		boolean bFound = false;
		for(String sect: InterfacesDef.SECTIONS) {
			if(sect.equals(_interfaceName)) {
				bFound = true;
				break;
			}
		}
	    if(bFound) {
	        return _interfaceName;
	    }
	    else {
	        ExceptionCollector.appendException(String.format(
	        	"ValueError: invalid interface name \"%s\" in \"get_operation_output\"",
	        	_interfaceName));
	        return null;
	    }
	}

	private String _findOperationName(String interfaceName,String operationName) {
		
	    if(interfaceName.equals("Configure") ||
	       interfaceName.equals("tosca.interfaces.node.relationship.Configure")) {
			boolean bFound = false;
			for(String sect: StatefulEntityType.interfacesRelationshipConfigureOperations) {
				if(sect.equals(operationName)) {
					bFound = true;
					break;
				}
			}
		    if(bFound) {
	 	        return operationName;
		    }
 	        else {
 	            ExceptionCollector.appendException(String.format(
 	            	"ValueError: Invalid operation of Configure interface \"%s\" in \"get_operation_output\"",
 	            	operationName));
 	            return null;
 	        }
	    }
	    if(interfaceName.equals("Standard") ||
 	       interfaceName.equals("tosca.interfaces.node.lifecycle.Standard")) {
 			boolean bFound = false;
 			for(String sect: StatefulEntityType.interfacesNodeLifecycleOperations) {
 				if(sect.equals(operationName)) {
 					bFound = true;
 					break;
 				}
 			}
 		    if(bFound) {
 	 	        return operationName;
 		    }
  	        else {
  	            ExceptionCollector.appendException(String.format(
 	            	"ValueError: Invalid operation of Configure interface \"%s\" in \"get_operation_output\"",
 	            	operationName));
 	            return null;
 	        }
	    }
	    else {
	        ExceptionCollector.appendException(String.format(
	            	"ValueError: Invalid interface name \"%s\" in \"get_operation_output\"",
 	            	interfaceName));
	        return null;
	    }
	}
	
	private NodeTemplate _findNodeTemplate(String nodeTemplateName) {
	    if(nodeTemplateName.equals(TARGET)) {
	    	if(!(((EntityTemplate)context).getTypeDefinition() instanceof RelationshipType)) {
	            ExceptionCollector.appendException(
	                "KeyError: \"TARGET\" keyword can only be used in context " +
	                           " to \"Relationships\" target node");
	            return null;
	    	}
	        return ((RelationshipTemplate)context).getTarget();
	    }
	    if(nodeTemplateName.equals(SOURCE)) {
	    	if(!(((EntityTemplate)context).getTypeDefinition() instanceof RelationshipType)) {
	            ExceptionCollector.appendException(
	                "KeyError: \"SOURCE\" keyword can only be used in context " +
	                           " to \"Relationships\" source node");
	            return null;
	    	}
	        return ((RelationshipTemplate)context).getTarget();
	    }
	    String name;
	    if(nodeTemplateName.equals(SELF) && !(context instanceof ArrayList)) {
	    	name = ((NodeTemplate)context).getName();
	    }
	    else {
	    	name = nodeTemplateName;
	    }
	    for(NodeTemplate nt: toscaTpl.getNodeTemplates()) {
	        if(nodeTemplateName.equals(name)) {
	            return nt;
	        }
	    }
	    ExceptionCollector.appendException(String.format(
	        "KeyError: Node template \"%s\" was not found",nodeTemplateName));
    	return null;
    }

	@Override
	public Object result() {
		return this;
	}

}

/*python 

class GetOperationOutput(Function):
def validate(self):
    if len(self.args) == 4:
        self._find_node_template(self.args[0])
        interface_name = self._find_interface_name(self.args[1])
        self._find_operation_name(interface_name, self.args[2])
    else:
        ExceptionCollector.appendException(
            ValueError(_('Illegal arguments for function "{0}". Expected '
                         'arguments: "template_name","interface_name",'
                         '"operation_name","output_variable_name"'
                         ).format(GET_OPERATION_OUTPUT)))
        return

def _find_interface_name(self, interface_name):
    if interface_name in toscaparser.elements.interfaces.SECTIONS:
        return interface_name
    else:
        ExceptionCollector.appendException(
            ValueError(_('Enter a valid interface name'
                         ).format(GET_OPERATION_OUTPUT)))
        return

def _find_operation_name(self, interface_name, operation_name):
    if(interface_name == 'Configure' or
       interface_name == 'tosca.interfaces.node.relationship.Configure'):
        if(operation_name in
           StatefulEntityType.
           interfaces_relationship_configure_operations):
            return operation_name
        else:
            ExceptionCollector.appendException(
                ValueError(_('Enter an operation of Configure interface'
                             ).format(GET_OPERATION_OUTPUT)))
            return
    elif(interface_name == 'Standard' or
         interface_name == 'tosca.interfaces.node.lifecycle.Standard'):
        if(operation_name in
           StatefulEntityType.interfaces_node_lifecycle_operations):
            return operation_name
        else:
            ExceptionCollector.appendException(
                ValueError(_('Enter an operation of Standard interface'
                             ).format(GET_OPERATION_OUTPUT)))
            return
    else:
        ExceptionCollector.appendException(
            ValueError(_('Enter a valid operation name'
                         ).format(GET_OPERATION_OUTPUT)))
        return

def _find_node_template(self, node_template_name):
    if node_template_name == TARGET:
        if not isinstance(self.context.type_definition, RelationshipType):
            ExceptionCollector.appendException(
                KeyError(_('"TARGET" keyword can only be used in context'
                           ' to "Relationships" target node')))
            return
        return self.context.target
    if node_template_name == SOURCE:
        if not isinstance(self.context.type_definition, RelationshipType):
            ExceptionCollector.appendException(
                KeyError(_('"SOURCE" keyword can only be used in context'
                           ' to "Relationships" source node')))
            return
        return self.context.source
    name = self.context.name \
        if node_template_name == SELF and \
        not isinstance(self.context, list) \
        else node_template_name
    for node_template in self.tosca_tpl.nodetemplates:
        if node_template.name == name:
            return node_template
    ExceptionCollector.appendException(
        KeyError(_(
            'Node template "{0}" was not found.'
            ).format(node_template_name)))

def result(self):
    return self
*/