aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/functions/Function.java
blob: 102fbc005b55e12ea3b339df177043e2a66514c6 (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
package org.openecomp.sdc.toscaparser.api.functions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

import org.openecomp.sdc.toscaparser.api.TopologyTemplate;

public abstract class Function {

	protected static final String GET_PROPERTY = "get_property";
	protected static final String GET_ATTRIBUTE = "get_attribute";
	protected static final String GET_INPUT = "get_input";
	protected static final String GET_OPERATION_OUTPUT = "get_operation_output";
	protected static final String CONCAT = "concat";
	protected static final String TOKEN = "token";

	protected static final String SELF = "SELF";
	protected static final String HOST = "HOST";
	protected static final String TARGET = "TARGET";
	protected static final String SOURCE = "SOURCE";

	protected static final String HOSTED_ON = "tosca.relationships.HostedOn";
	
	protected static HashMap<String,String> functionMappings = _getFunctionMappings();
	
	private static HashMap<String,String> _getFunctionMappings() {
		HashMap<String,String> map = new HashMap<>();
	    map.put(GET_PROPERTY,"GetProperty");
	    map.put(GET_INPUT, "GetInput");
	    map.put(GET_ATTRIBUTE, "GetAttribute");
	    map.put(GET_OPERATION_OUTPUT, "GetOperationOutput");
	    map.put(CONCAT, "Concat");
	    map.put(TOKEN, "Token");
	    return map;
	}
	
	protected TopologyTemplate toscaTpl;
	protected Object context;
	protected String name;
	protected ArrayList<Object> args;

	
	public Function(TopologyTemplate _toscaTpl,Object _context,String _name,ArrayList<Object> _args) {
        toscaTpl = _toscaTpl;
        context = _context;
        name = _name;
        args = _args;
        validate();
		
	}
	
	abstract Object result();
	
	abstract void validate();

	@SuppressWarnings("unchecked")
	public static boolean isFunction(Object funcObj) {
	    // Returns True if the provided function is a Tosca intrinsic function.
		//
	    //Examples:
		//
	    //* "{ get_property: { SELF, port } }"
	    //* "{ get_input: db_name }"
	    //* Function instance

	    //:param function: Function as string or a Function instance.
	    //:return: True if function is a Tosca intrinsic function, otherwise False.
	    //
		
	    if(funcObj instanceof LinkedHashMap) {
	    	LinkedHashMap<String,Object> function = (LinkedHashMap<String,Object>)funcObj;
	    	if(function.size() == 1) {
		        String funcName = (new ArrayList<String>(function.keySet())).get(0);
		        return functionMappings.keySet().contains(funcName);
	    	}
	    }
	    return (funcObj instanceof Function);
	}
	
	@SuppressWarnings("unchecked")
	public static Object getFunction(TopologyTemplate ttpl,Object context,Object rawFunctionObj) {
	    // Gets a Function instance representing the provided template function.

	    // If the format provided raw_function format is not relevant for template
	    // functions or if the function name doesn't exist in function mapping the
	    // method returns the provided raw_function.
		//	
	    // :param tosca_tpl: The tosca template.
	    // :param node_template: The node template the function is specified for.
	    // :param raw_function: The raw function as dict.
	    // :return: Template function as Function instance or the raw_function if
	    //  parsing was unsuccessful.
		
	    if(isFunction(rawFunctionObj)) {
	        if(rawFunctionObj instanceof LinkedHashMap) {
	        	LinkedHashMap<String,Object> rawFunction = (LinkedHashMap<String,Object>)rawFunctionObj;
		        String funcName = (new ArrayList<String>(rawFunction.keySet())).get(0);
		        if(functionMappings.keySet().contains(funcName)) {
		        	String funcType = functionMappings.get(funcName);
		        	Object oargs = (new ArrayList<Object>(rawFunction.values())).get(0);
		        	ArrayList<Object> funcArgs;
		        	if(oargs instanceof ArrayList) {
		        		funcArgs = (ArrayList<Object>)oargs;
		        	}
		        	else {
		        		funcArgs = new ArrayList<>();
		        		funcArgs.add(oargs);
		        	}

		        	if(funcType.equals("GetInput")) {
		        		return new GetInput(ttpl,context,funcName,funcArgs);
		        	}
		        	else if(funcType.equals("GetAttribute")) {
		        		return new GetAttribute(ttpl,context,funcName,funcArgs);
		        	} 
		        	else if(funcType.equals("GetProperty")) {
		        		return new GetProperty(ttpl,context,funcName,funcArgs);
		        	} 
		        	else if(funcType.equals("GetOperationOutput")) {
		        		return new GetOperationOutput(ttpl,context,funcName,funcArgs);
		        	} 
		        	else if(funcType.equals("Concat")) {
		        		return new Concat(ttpl,context,funcName,funcArgs);
		        	} 
		        	else if(funcType.equals("Token")) {
		        		return new Token(ttpl,context,funcName,funcArgs);
		        	} 
		        }
	        }
	    }
	    return rawFunctionObj;
	}
}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import UnknownInputError
from toscaparser.dataentity import DataEntity
from toscaparser.elements.constraints import Schema
from toscaparser.elements.datatype import DataType
from toscaparser.elements.entity_type import EntityType
from toscaparser.elements.relationshiptype import RelationshipType
from toscaparser.elements.statefulentitytype import StatefulEntityType
from toscaparser.utils.gettextutils import _


GET_PROPERTY = 'get_property'
GET_ATTRIBUTE = 'get_attribute'
GET_INPUT = 'get_input'
GET_OPERATION_OUTPUT = 'get_operation_output'
CONCAT = 'concat'
TOKEN = 'token'

SELF = 'SELF'
HOST = 'HOST'
TARGET = 'TARGET'
SOURCE = 'SOURCE'

HOSTED_ON = 'tosca.relationships.HostedOn'


@six.add_metaclass(abc.ABCMeta)
class Function(object):
    """An abstract type for representing a Tosca template function."""

    def __init__(self, tosca_tpl, context, name, args):
        self.tosca_tpl = tosca_tpl
        self.context = context
        self.name = name
        self.args = args
        self.validate()

    @abc.abstractmethod
    def result(self):
        """Invokes the function and returns its result

        Some methods invocation may only be relevant on runtime (for example,
        getting runtime properties) and therefore its the responsibility of
        the orchestrator/translator to take care of such functions invocation.

        :return: Function invocation result.
        """
        return {self.name: self.args}

    @abc.abstractmethod
    def validate(self):
        """Validates function arguments."""
        pass
*/