aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/functions/GetAttribute.java
blob: 5536dc530cb4a20d01aff6c194d03f869acc53ad (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package org.openecomp.sdc.toscaparser.api.functions;

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

import org.openecomp.sdc.toscaparser.api.Capability;
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.AttributeDef;
import org.openecomp.sdc.toscaparser.api.elements.CapabilityTypeDef;
import org.openecomp.sdc.toscaparser.api.elements.DataType;
import org.openecomp.sdc.toscaparser.api.elements.EntityType;
import org.openecomp.sdc.toscaparser.api.elements.NodeType;
import org.openecomp.sdc.toscaparser.api.elements.PropertyDef;
import org.openecomp.sdc.toscaparser.api.elements.RelationshipType;
import org.openecomp.sdc.toscaparser.api.elements.StatefulEntityType;
import org.openecomp.sdc.toscaparser.api.elements.constraints.Schema;

public class GetAttribute extends Function {
	// Get an attribute value of an entity defined in the service template

	// Node template attributes values are set in runtime and therefore its the
	// responsibility of the Tosca engine to implement the evaluation of
	// get_attribute functions.

	// Arguments:

	// * Node template name | HOST.
	// * Attribute name.

	// If the HOST keyword is passed as the node template name argument the
	// function will search each node template along the HostedOn relationship
	// chain until a node which contains the attribute is found.

	// Examples:

	// * { get_attribute: [ server, private_address ] }
	// * { get_attribute: [ HOST, private_address ] }
	// * { get_attribute: [ HOST, private_address, 0 ] }
	// * { get_attribute: [ HOST, private_address, 0, some_prop] }

	public GetAttribute(TopologyTemplate ttpl,Object context,String name,ArrayList<Object> args) {
		super(ttpl,context,name,args);
	}
	
	@Override
	void validate() {
		if(args.size() < 2) {
	        ExceptionCollector.appendException(
			    "ValueError: Illegal arguments for function \"get_attribute\". Expected arguments: \"node-template-name\", \"req-or-cap\" (optional), \"property name.\"");
		    return;
		}
		else if(args.size() == 2) {
	        _findNodeTemplateContainingAttribute();
		}
	    else {
	        NodeTemplate nodeTpl = _findNodeTemplate((String)args.get(0));
	        if(nodeTpl == null) {
	            return;
	        }
	        int index = 2;
	        AttributeDef attr = nodeTpl.getTypeDefinition().getAttributeDefValue((String)args.get(1));
	        if(attr != null) {
	        	// found
	        }
	        else {
	            index = 3;
	            // then check the req or caps
	            attr = _findReqOrCapAttribute((String)args.get(1),(String)args.get(2));
	            if(attr == null) {
	            	return;
	            }
	        }
	        
	        String valueType = (String)attr.getSchema().get("type");
            if(args.size() > index) {
	        	for(Object elem: args.subList(index,args.size())) {
	        		if(valueType.equals("list")) {
                        if(!(elem instanceof Integer)) {
                            ExceptionCollector.appendException(String.format(
                                "ValueError: Illegal arguments for function \"get_attribute\" \"%s\". Expected positive integer argument",
                                elem.toString()));
                        } 
                        Object ob = attr.getSchema().get("entry_schema");
                        valueType = (String)
                        		((LinkedHashMap<String,Object>)ob).get("type");
	        		}
	        		else if(valueType.equals("map")) {
                        Object ob = attr.getSchema().get("entry_schema");
                        valueType = (String)
                        		((LinkedHashMap<String,Object>)ob).get("type");
	        		}
	        		else {
	        			boolean bFound = false;
	        			for(String p: Schema.PROPERTY_TYPES) {
	        				if(p.equals(valueType)) {
	        					bFound = true;
	        					break;
	        				}
	        			}
	        			if(bFound) {
	                        ExceptionCollector.appendException(String.format(
	                            "ValueError: 'Illegal arguments for function \"get_attribute\". Unexpected attribute/index value \"%d\"",
	                            elem));
	                        return;
	        			}
	                    else {  // It is a complex type
	                        DataType dataType = new DataType(valueType,null);
	                        LinkedHashMap<String,PropertyDef> props = 
	                        		dataType.getAllProperties();
	                        PropertyDef prop = props.get((String)elem);
	                        if(prop != null) {
	                            valueType = (String)prop.getSchema().get("type");
	                        }
	                        else {
	                            ExceptionCollector.appendException(String.format(
	                                "KeyError: Illegal arguments for function \"get_attribute\". Attribute name \"%s\" not found in \"%\"",
	                                elem,valueType));
	                        }
	                    }
	        		}
	        	}
            }
	    }
	}

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

	private NodeTemplate getReferencedNodeTemplate() {
	    // Gets the NodeTemplate instance the get_attribute function refers to

	    // If HOST keyword was used as the node template argument, the node
	    // template which contains the attribute along the HostedOn relationship
	    // chain will be returned.
	    
	    return _findNodeTemplateContainingAttribute();
		
	}

	// Attributes can be explicitly created as part of the type definition
	// or a property name can be implicitly used as an attribute name 
	private NodeTemplate _findNodeTemplateContainingAttribute() {
		NodeTemplate nodeTpl = _findNodeTemplate((String)args.get(0));
		if(nodeTpl != null &&
			!_attributeExistsInType(nodeTpl.getTypeDefinition()) &&
			!nodeTpl.getProperties().keySet().contains(getAttributeName())) {
	        ExceptionCollector.appendException(String.format(
	            "KeyError: Attribute \"%s\" was not found in node template \"%s\"",
	            getAttributeName(),nodeTpl.getName()));
		}
	    return nodeTpl;
	}

	private boolean _attributeExistsInType(StatefulEntityType typeDefinition) {
	    LinkedHashMap<String,AttributeDef> attrsDef = typeDefinition.getAttributesDef();
	    return attrsDef.get(getAttributeName()) != null;
	}

	private NodeTemplate _findHostContainingAttribute(String nodeTemplateName) {
		NodeTemplate nodeTemplate = _findNodeTemplate(nodeTemplateName);
		if(nodeTemplate != null) {
	        LinkedHashMap<String,Object> hostedOnRel = 
	        		(LinkedHashMap<String,Object>)EntityType.TOSCA_DEF.get(HOSTED_ON);
			for(Object ro: nodeTemplate.getRequirements()) {
				if(ro != null && ro instanceof LinkedHashMap) {
					LinkedHashMap<String,Object> r = (LinkedHashMap<String,Object>)ro;
					for(String requirement: r.keySet()) {
						String targetName = (String)r.get(requirement);
						NodeTemplate targetNode = _findNodeTemplate(targetName);
						NodeType targetType = (NodeType)targetNode.getTypeDefinition();
						for(CapabilityTypeDef capability: targetType.getCapabilitiesObjects()) {
//							if(((ArrayList<String>)hostedOnRel.get("valid_target_types")).contains(capability.getType())) {
							if(capability.inheritsFrom((ArrayList<String>)hostedOnRel.get("valid_target_types"))) {
								if(_attributeExistsInType(targetType)) {
									return targetNode;
								}
								return _findHostContainingAttribute(targetName);
							}
						}
					}
				}
			}
		}
		return null;
	}
	

	private NodeTemplate _findNodeTemplate(String nodeTemplateName) {
	    if(nodeTemplateName.equals(HOST)) {
	        // Currently this is the only way to tell whether the function
	        // is used within the outputs section of the TOSCA template.
	        if(context instanceof ArrayList) {
	            ExceptionCollector.appendException(
	                "ValueError: \"get_attribute: [ HOST, ... ]\" is not allowed in \"outputs\" section of the TOSCA template");
	            return null;
	        }
	        NodeTemplate nodeTpl = _findHostContainingAttribute(SELF);
	        if(nodeTpl == null) {
	            ExceptionCollector.appendException(String.format(
	                "ValueError: \"get_attribute: [ HOST, ... ]\" was used in " +
	                "node template \"%s\" but \"%s\" was not found in " +
	                "the relationship chain",((NodeTemplate)context).getName(),HOSTED_ON));
	            return null;
	        }
	        return nodeTpl;
	    }
	    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(nt.getName().equals(name)) {
	            return nt;
	        }
	    }
	    ExceptionCollector.appendException(String.format(
	        "KeyError: Node template \"%s\" was not found",nodeTemplateName));
    	return null;
    }
	
	public AttributeDef _findReqOrCapAttribute(String reqOrCap,String attrName) {

		NodeTemplate nodeTpl = _findNodeTemplate((String)args.get(0));
	    // Find attribute in node template's requirements
		for(Object ro: nodeTpl.getRequirements()) {
			if(ro != null && ro instanceof LinkedHashMap) {
				LinkedHashMap<String,Object> r = (LinkedHashMap<String,Object>)ro;
				for(String req: r.keySet()) {
					String nodeName = (String)r.get(req);
					if(req.equals(reqOrCap)) {
						NodeTemplate nodeTemplate = _findNodeTemplate(nodeName);
    	                return _getCapabilityAttribute(nodeTemplate,req,attrName);
					}
				}
			}
		}
	    // If requirement was not found, look in node template's capabilities
	    return _getCapabilityAttribute(nodeTpl,reqOrCap,attrName);
	}

	private AttributeDef _getCapabilityAttribute(NodeTemplate nodeTemplate,
										  String capabilityName,
										  String attrName) {
	    // Gets a node template capability attribute
	    LinkedHashMap<String,Capability> caps = nodeTemplate.getCapabilities();
	    if(caps != null && caps.keySet().contains(capabilityName)) {
	    	Capability cap = caps.get(capabilityName);
	    	AttributeDef attribute = null;
	    	LinkedHashMap<String,AttributeDef> attrs =
	    			cap.getDefinition().getAttributesDef();
	    	if(attrs != null && attrs.keySet().contains(attrName)) {
	    		attribute = attrs.get(attrName);
	    	}
	        if(attribute == null) {
	            ExceptionCollector.appendException(String.format(
	                "KeyError: Attribute \"%s\" was not found in capability \"%s\" of node template \"%s\" referenced from node template \"%s\"",
	                attrName,capabilityName,nodeTemplate.getName(),((NodeTemplate)context).getName()));
	        }
	        return attribute;
	    }
	    String msg = String.format(
	    	"Requirement/Capability \"%s\" referenced from node template \"%s\" was not found in node template \"%s\"",
	    	capabilityName,((NodeTemplate)context).getName(),nodeTemplate.getName());
	    ExceptionCollector.appendException("KeyError: " + msg);
		return null;									  
	}

	String getNodeTemplateName() {
		return (String)args.get(0);
	}

	String getAttributeName() {
		return (String)args.get(1);
	}

}

/*python

class GetAttribute(Function):
"""Get an attribute value of an entity defined in the service template

Node template attributes values are set in runtime and therefore its the
responsibility of the Tosca engine to implement the evaluation of
get_attribute functions.

Arguments:

* Node template name | HOST.
* Attribute name.

If the HOST keyword is passed as the node template name argument the
function will search each node template along the HostedOn relationship
chain until a node which contains the attribute is found.

Examples:

* { get_attribute: [ server, private_address ] }
* { get_attribute: [ HOST, private_address ] }
* { get_attribute: [ HOST, private_address, 0 ] }
* { get_attribute: [ HOST, private_address, 0, some_prop] }
"""

def validate(self):
    if len(self.args) < 2:
        ExceptionCollector.appendException(
            ValueError(_('Illegal arguments for function "{0}". Expected '
                         'arguments: "node-template-name", "req-or-cap"'
                         '(optional), "property name"'
                         ).format(GET_ATTRIBUTE)))
        return
    elif len(self.args) == 2:
        self._find_node_template_containing_attribute()
    else:
        node_tpl = self._find_node_template(self.args[0])
        if node_tpl is None:
            return
        index = 2
        attrs = node_tpl.type_definition.get_attributes_def()
        found = [attrs[self.args[1]]] if self.args[1] in attrs else []
        if found:
            attr = found[0]
        else:
            index = 3
            # then check the req or caps
            attr = self._find_req_or_cap_attribute(self.args[1],
                                                   self.args[2])

        value_type = attr.schema['type']
        if len(self.args) > index:
            for elem in self.args[index:]:
                if value_type == "list":
                    if not isinstance(elem, int):
                        ExceptionCollector.appendException(
                            ValueError(_('Illegal arguments for function'
                                         ' "{0}". "{1}" Expected positive'
                                         ' integer argument'
                                         ).format(GET_ATTRIBUTE, elem)))
                    value_type = attr.schema['entry_schema']['type']
                elif value_type == "map":
                    value_type = attr.schema['entry_schema']['type']
                elif value_type in Schema.PROPERTY_TYPES:
                    ExceptionCollector.appendException(
                        ValueError(_('Illegal arguments for function'
                                     ' "{0}". Unexpected attribute/'
                                     'index value "{1}"'
                                     ).format(GET_ATTRIBUTE, elem)))
                    return
                else:  # It is a complex type
                    data_type = DataType(value_type)
                    props = data_type.get_all_properties()
                    found = [props[elem]] if elem in props else []
                    if found:
                        prop = found[0]
                        value_type = prop.schema['type']
                    else:
                        ExceptionCollector.appendException(
                            KeyError(_('Illegal arguments for function'
                                       ' "{0}". Attribute name "{1}" not'
                                       ' found in "{2}"'
                                       ).format(GET_ATTRIBUTE,
                                                elem,
                                                value_type)))

def result(self):
    return self

def get_referenced_node_template(self):
    """Gets the NodeTemplate instance the get_attribute function refers to.

    If HOST keyword was used as the node template argument, the node
    template which contains the attribute along the HostedOn relationship
    chain will be returned.
    """
    return self._find_node_template_containing_attribute()

# Attributes can be explicitly created as part of the type definition
# or a property name can be implicitly used as an attribute name
def _find_node_template_containing_attribute(self):
    node_tpl = self._find_node_template(self.args[0])
    if node_tpl and \
            not self._attribute_exists_in_type(node_tpl.type_definition) \
            and self.attribute_name not in node_tpl.get_properties():
        ExceptionCollector.appendException(
            KeyError(_('Attribute "%(att)s" was not found in node '
                       'template "%(ntpl)s".') %
                     {'att': self.attribute_name,
                      'ntpl': node_tpl.name}))
    return node_tpl

def _attribute_exists_in_type(self, type_definition):
    attrs_def = type_definition.get_attributes_def()
    found = [attrs_def[self.attribute_name]] \
        if self.attribute_name in attrs_def else []
    return len(found) == 1

def _find_host_containing_attribute(self, node_template_name=SELF):
    node_template = self._find_node_template(node_template_name)
    if node_template:
        hosted_on_rel = EntityType.TOSCA_DEF[HOSTED_ON]
        for r in node_template.requirements:
            for requirement, target_name in r.items():
                target_node = self._find_node_template(target_name)
                target_type = target_node.type_definition
                for capability in target_type.get_capabilities_objects():
                    if capability.type in \
                            hosted_on_rel['valid_target_types']:
                        if self._attribute_exists_in_type(target_type):
                            return target_node
                        return self._find_host_containing_attribute(
                            target_name)

def _find_node_template(self, node_template_name):
    if node_template_name == HOST:
        # Currently this is the only way to tell whether the function
        # is used within the outputs section of the TOSCA template.
        if isinstance(self.context, list):
            ExceptionCollector.appendException(
                ValueError(_(
                    '"get_attribute: [ HOST, ... ]" is not allowed in '
                    '"outputs" section of the TOSCA template.')))
            return
        node_tpl = self._find_host_containing_attribute()
        if not node_tpl:
            ExceptionCollector.appendException(
                ValueError(_(
                    '"get_attribute: [ HOST, ... ]" was used in node '
                    'template "{0}" but "{1}" was not found in '
                    'the relationship chain.').format(self.context.name,
                                                      HOSTED_ON)))
            return
        return node_tpl
    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 _find_req_or_cap_attribute(self, req_or_cap, attr_name):
    node_tpl = self._find_node_template(self.args[0])
    # Find attribute in node template's requirements
    for r in node_tpl.requirements:
        for req, node_name in r.items():
            if req == req_or_cap:
                node_template = self._find_node_template(node_name)
                return self._get_capability_attribute(
                    node_template,
                    req,
                    attr_name)
    # If requirement was not found, look in node template's capabilities
    return self._get_capability_attribute(node_tpl,
                                          req_or_cap,
                                          attr_name)

def _get_capability_attribute(self,
                              node_template,
                              capability_name,
                              attr_name):
    """Gets a node template capability attribute."""
    caps = node_template.get_capabilities()
    if caps and capability_name in caps.keys():
        cap = caps[capability_name]
        attribute = None
        attrs = cap.definition.get_attributes_def()
        if attrs and attr_name in attrs.keys():
            attribute = attrs[attr_name]
        if not attribute:
            ExceptionCollector.appendException(
                KeyError(_('Attribute "%(attr)s" was not found in '
                           'capability "%(cap)s" of node template '
                           '"%(ntpl1)s" referenced from node template '
                           '"%(ntpl2)s".') % {'attr': attr_name,
                                              'cap': capability_name,
                                              'ntpl1': node_template.name,
                                              'ntpl2': self.context.name}))
        return attribute
    msg = _('Requirement/Capability "{0}" referenced from node template '
            '"{1}" was not found in node template "{2}".').format(
                capability_name,
                self.context.name,
                node_template.name)
    ExceptionCollector.appendException(KeyError(msg))

@property
def node_template_name(self):
    return self.args[0]

@property
def attribute_name(self):
    return self.args[1]
*/