aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/functions/Function.java
blob: 711a7cae12332fd411e0cb9f3cb9b1dd100500a1 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.toscaparser.api.functions;


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

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

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, boolean resolveGetInput) {
        // 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.


        // iterate over leaves of the properties's tree and convert function leaves to function object,
        // support List and Map nested,
        // assuming that leaf value of function is always map type contains 1 item (e.g. my_leaf: {get_input: xxx}).

        if (rawFunctionObj instanceof LinkedHashMap) { // In map type case
            LinkedHashMap rawFunction = ((LinkedHashMap) rawFunctionObj);
            if (rawFunction.size() == 1 &&
                    !(rawFunction.values().iterator().next() instanceof LinkedHashMap)) { // End point
                return getFunctionForObjectItem(ttpl, context, rawFunction, resolveGetInput);
            } else {
                return getFunctionForMap(ttpl, context, rawFunction, resolveGetInput);
            }
        } else if (rawFunctionObj instanceof ArrayList) { // In list type case
            return getFunctionForList(ttpl, context, (ArrayList) rawFunctionObj, resolveGetInput);
        }

        return rawFunctionObj;
    }

    private static Object getFunctionForList(TopologyTemplate ttpl, Object context, ArrayList rawFunctionObj, boolean resolveGetInput) {
        // iterate over list properties in recursion, convert leaves to function,
        // and collect them in the same hierarchy as the original list.
        ArrayList<Object> rawFunctionObjList = new ArrayList<>();
        for (Object rawFunctionObjItem : rawFunctionObj) {
            rawFunctionObjList.add(getFunction(ttpl, context, rawFunctionObjItem, resolveGetInput));
        }
        return rawFunctionObjList;
    }

    private static Object getFunctionForMap(TopologyTemplate ttpl, Object context, LinkedHashMap rawFunction, boolean resolveGetInput) {
        // iterate over map nested properties in recursion, convert leaves to function,
        // and collect them in the same hierarchy as the original map.
        LinkedHashMap rawFunctionObjMap = new LinkedHashMap();
        for (Object rawFunctionObjItem : rawFunction.entrySet()) {
            Object itemValue = getFunction(ttpl, context, ((Map.Entry) rawFunctionObjItem).getValue(), resolveGetInput);
            rawFunctionObjMap.put(((Map.Entry) rawFunctionObjItem).getKey(), itemValue);
        }
        return rawFunctionObjMap;
    }

    private static Object getFunctionForObjectItem(TopologyTemplate ttpl, Object context, Object rawFunctionObjItem, boolean resolveGetInput) {
        if (isFunction(rawFunctionObjItem)) {
            LinkedHashMap<String, Object> rawFunction = (LinkedHashMap<String, Object>) rawFunctionObjItem;
            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);
                }

                switch (funcType) {
                    case "GetInput":
                        if (resolveGetInput) {
                            GetInput input = new GetInput(ttpl, context, funcName, funcArgs);
                            return input.result();
                        }
                        return new GetInput(ttpl, context, funcName, funcArgs);
                    case "GetAttribute":
                        return new GetAttribute(ttpl, context, funcName, funcArgs);
                    case "GetProperty":
                        return new GetProperty(ttpl, context, funcName, funcArgs);
                    case "GetOperationOutput":
                        return new GetOperationOutput(ttpl, context, funcName, funcArgs);
                    case "Concat":
                        return new Concat(ttpl, context, funcName, funcArgs);
                    case "Token":
                        return new Token(ttpl, context, funcName, funcArgs);
                }
            }
        }

        return rawFunctionObjItem;
    }

    @Override
    public String toString() {
        String argsStr = args.size() > 1 ? args.toString() : args.get(0).toString();
        return name + ":" + argsStr;
    }
}

/*python

from toscaparser.common.exception import ValidationIsshueCollector
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
*/