aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/EntityTemplate.java
blob: 3d9c470dcd86e1220af72dc737e5b7aeff49a2d8 (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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
package org.openecomp.sdc.toscaparser.api;

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

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.elements.*;

public abstract class EntityTemplate {
    // Base class for TOSCA templates

	protected static final String DERIVED_FROM = "derived_from";
	protected static final String PROPERTIES = "properties";
	protected static final String REQUIREMENTS = "requirements";
	protected static final String INTERFACES = "interfaces";
	protected static final String CAPABILITIES = "capabilities";
	protected static final String TYPE = "type";
	protected static final String DESCRIPTION = "description";
	protected static final String DIRECTIVES = "directives";
	protected static final String ATTRIBUTES = "attributes";
	protected static final String ARTIFACTS = "artifacts";
	protected static final String NODE_FILTER = "node_filter";
	protected static final String COPY = "copy";
	
	protected static final String SECTIONS[] = {
			DERIVED_FROM, PROPERTIES, REQUIREMENTS,INTERFACES, 
			CAPABILITIES, TYPE, DESCRIPTION, DIRECTIVES,
			ATTRIBUTES, ARTIFACTS, NODE_FILTER, COPY};

	private static final String NODE = "node";
	private static final String CAPABILITY = "capability";
	private static final String RELATIONSHIP = "relationship";
	private static final String OCCURRENCES = "occurrences";
	
	protected static final String REQUIREMENTS_SECTION[] = {
				NODE, CAPABILITY, RELATIONSHIP, OCCURRENCES, NODE_FILTER};
    
	//# Special key names
	private static final String METADATA = "metadata";
	protected static final String SPECIAL_SECTIONS[] = {METADATA};
	
	protected String name;
	protected LinkedHashMap<String,Object> entityTpl;
	protected LinkedHashMap<String,Object> customDef; 
	protected StatefulEntityType typeDefinition;
	private ArrayList<Property> _properties;
	private ArrayList<InterfacesDef> _interfaces; 
	private ArrayList<Object> _requirements;
	private ArrayList<Capability> _capabilities;

	// dummy constructor for subclasses that don't want super
	public EntityTemplate() {
		return;
	}

    @SuppressWarnings("unchecked")
	public EntityTemplate(String _name, 
    					  LinkedHashMap<String,Object> _template, 
    					  String _entityName, 
    					  LinkedHashMap<String,Object> _customDef) {
        name = _name;
        entityTpl = _template;
        customDef = _customDef;
        _validateField(entityTpl);
        String type = (String)entityTpl.get("type");
       	UnsupportedType.validateType(type);
        if(_entityName.equals("node_type")) {
        	if(type != null) {
        		typeDefinition = new NodeType(type, customDef);
        	}
        	else {
        		typeDefinition = null;
        	}
        }
        if(_entityName.equals("relationship_type")) {
			Object relationship = _template.get("relationship");
            type = null;
            if(relationship != null && relationship instanceof LinkedHashMap) {
                type = (String)((LinkedHashMap<String,Object>)relationship).get("type");
            }
            else if(relationship instanceof String) {
                type = (String)entityTpl.get("relationship");
            }
            else {
                type = (String)entityTpl.get("type");
            }
            UnsupportedType.validateType(type);
            typeDefinition = new RelationshipType(type,null, customDef);
        }
        if(_entityName.equals("policy_type")) {
            if(type == null) {
                //msg = (_('Policy definition of "%(pname)s" must have'
                //       ' a "type" ''attribute.') % dict(pname=name))
                ExceptionCollector.appendException(String.format(
                		"ValidationError: Policy definition of \"%s\" must have a \"type\" attribute",name));
            }
            typeDefinition = new PolicyType(type, customDef);
        }
        if(_entityName.equals("group_type")) {
        	if(type != null) {
        		typeDefinition = new GroupType(type, customDef);
        	}
        	else { 
                typeDefinition = null;
        	}
        }
        _properties = null;
        _interfaces = null;
        _requirements = null;
        _capabilities = null;
    }

    public String getType() {
    	if(typeDefinition != null) {
    		String clType = typeDefinition.getClass().getSimpleName();
    		if(clType.equals("NodeType")) {
    			return (String)((NodeType)typeDefinition).getType();
    		}
    		else if(clType.equals("PolicyType")) {
    			return (String)((PolicyType)typeDefinition).getType();
    		}
    		else if(clType.equals("GroupType")) {
    			return (String)((GroupType)typeDefinition).getType();
    		}
    		else if(clType.equals("RelationshipType")) {
    			return (String)((RelationshipType)typeDefinition).getType();
    		}
    	}
    	return null;
    }

    public Object getParentType() {
    	if(typeDefinition != null) {
    		String clType = typeDefinition.getClass().getSimpleName();
    		if(clType.equals("NodeType")) {
    			return ((NodeType)typeDefinition).getParentType();
    		}
    		else if(clType.equals("PolicyType")) {
    			return ((PolicyType)typeDefinition).getParentType();
    		}
    		else if(clType.equals("GroupType")) {
    			return ((GroupType)typeDefinition).getParentType();
    		}
    		else if(clType.equals("RelationshipType")) {
    			return ((RelationshipType)typeDefinition).getParentType();
    		}
    	}
    	return null;
    }
    
     @SuppressWarnings("unchecked")
	public ArrayList<Object> getRequirements() {
    	if(_requirements == null) {
    		_requirements = new ArrayList<Object>();
    		Object ob = ((EntityType)typeDefinition).getValue(REQUIREMENTS,entityTpl,false);
    		if(ob != null) {
    			_requirements.addAll((ArrayList<Object>)ob);
    		}
    				
    	}
    	return _requirements;
    }

    public ArrayList<Property> getPropertiesObjects() {
        // Return properties objects for this template
        if(_properties ==null) {
            _properties = _createProperties();
        }
    	return _properties;
    }
    
    public LinkedHashMap<String,Property> getProperties() {
    	LinkedHashMap<String,Property> props = new LinkedHashMap<>();
    	for(Property po: getPropertiesObjects()) {
    		props.put(((Property)po).getName(),po);
    	}
    	return props;
    }
    
    public Object getPropertyValue(String name) {
    	LinkedHashMap<String,Property> props = getProperties();
    	Property p = (Property)props.get(name);
    	return p != null ? p.getValue() : null;
    }

    public ArrayList<InterfacesDef> getInterfaces() {
    	if(_interfaces == null) {
    		_interfaces = _createInterfaces();
    	}
    	return _interfaces;
    }
    
    public ArrayList<Capability> getCapabilitiesObjects() {
        // Return capabilities objects for this template
    	if(_capabilities == null) {
    		_capabilities = _createCapabilities();
    	}
    	return _capabilities;
   	
    }
    
    public LinkedHashMap<String,Capability> getCapabilities() {
    	LinkedHashMap<String,Capability> caps = new LinkedHashMap<String,Capability>();
    	for(Capability cap: getCapabilitiesObjects()) {
    		caps.put(cap.getName(),cap);
    	}
    	return caps;
    }

    public boolean isDerivedFrom(String typeStr) {
    	// Returns true if this object is derived from 'type_str'.
        // False otherwise
    	
    	if(getType() == null) {
    		return false;
    	}
    	else if(getType().equals(typeStr)) {
    		return true;
    	}
    	else if(getParentType() != null) {
    		return ((EntityType)getParentType()).isDerivedFrom(typeStr);
    	}
   		return false;
    }
    
    @SuppressWarnings("unchecked")
	private ArrayList<Capability> _createCapabilities() {
    	ArrayList<Capability> capability = new ArrayList<Capability>();
		LinkedHashMap<String,Object> caps = (LinkedHashMap<String,Object>)
							((EntityType)typeDefinition).getValue(CAPABILITIES,entityTpl,true);
		if(caps != null) {
			//?!? getCapabilities defined only for NodeType...
			LinkedHashMap<String,CapabilityTypeDef> capabilities = ((NodeType)typeDefinition).getCapabilities();
			for(Map.Entry<String,Object> me: caps.entrySet()) {
				String name = me. getKey();
				LinkedHashMap<String,Object> props = (LinkedHashMap<String,Object>)me.getValue();
				if(capabilities.get(name) != null) {
					CapabilityTypeDef c = capabilities.get(name);  // a CapabilityTypeDef
					LinkedHashMap<String,Object> properties = new LinkedHashMap<String,Object>();
					// first use the definition default value
					LinkedHashMap<String,Object> cprops = c.getProperties();
					if(cprops != null) {
						for(Map.Entry<String,Object> cpe: cprops.entrySet()) {
							String propertyName = cpe.getKey();
							LinkedHashMap<String,Object> propertyDef = (LinkedHashMap<String,Object>)cpe.getValue();
							Object dob = propertyDef.get("default");
							if(dob != null) {
								properties.put(propertyName, dob);
								
							}
						}
					}
                    // then update (if available) with the node properties
					LinkedHashMap<String,Object> pp = (LinkedHashMap<String,Object>)props.get("properties");
					if(pp != null) {
						properties.putAll(pp);
					}
                    Capability cap = new Capability(name, properties, c);
                    capability.add(cap);
				}
			}
		}
		return capability;
     }

    protected void _validateProperties(LinkedHashMap<String,Object> template,StatefulEntityType entityType) {
    	@SuppressWarnings("unchecked")
		LinkedHashMap<String,Object> properties = (LinkedHashMap<String,Object>)entityType.getValue(PROPERTIES,template,false);
    	_commonValidateProperties(entityType,properties);
    }

    protected void _validateCapabilities() {
    	//BUG??? getCapabilities only defined in NodeType...
    	LinkedHashMap<String,CapabilityTypeDef> typeCapabilities = ((NodeType)typeDefinition).getCapabilities();
    	ArrayList<String> allowedCaps = new ArrayList<String>();
    	if(typeCapabilities != null) {
    		allowedCaps.addAll(typeCapabilities.keySet());
    	}
    	@SuppressWarnings("unchecked")
		LinkedHashMap<String,Object> capabilities = (LinkedHashMap<String,Object>)
    			((EntityType)typeDefinition).getValue(CAPABILITIES, entityTpl, false);
    	if(capabilities != null) {
            _commonValidateField(capabilities, allowedCaps, "capabilities");
            _validateCapabilitiesProperties(capabilities);
    	}
    }
    
	@SuppressWarnings("unchecked")
    private void _validateCapabilitiesProperties(LinkedHashMap<String,Object> capabilities) {
    	for(Map.Entry<String,Object> me: capabilities.entrySet()) {
    		String cap = me.getKey();
			LinkedHashMap<String,Object> props = (LinkedHashMap<String,Object>)me.getValue();
    		Capability capability = getCapability(cap);
    		if(capability == null) {
    			continue;
    		}
    		CapabilityTypeDef capabilitydef = capability.getDefinition();
    		_commonValidateProperties(capabilitydef,(LinkedHashMap<String,Object>)props.get(PROPERTIES));
    		
            // validating capability properties values
    		for(Property prop: getCapability(cap).getPropertiesObjects()) {
                prop.validate();
    		               
                if(cap.equals("scalable") && prop.getName().equals("default_instances")) {
					LinkedHashMap<String,Object> propDict = (LinkedHashMap<String,Object>)props.get(PROPERTIES);
                    int minInstances = (int)propDict.get("min_instances");
                    int maxInstances = (int)propDict.get("max_instances");
                    int defaultInstances = (int)propDict.get("default_instances");
                    if(defaultInstances < minInstances || defaultInstances > maxInstances) {
                        //err_msg = ('"properties" of template "%s": '
                        //           '"default_instances" value is not between '
                        //           '"min_instances" and "max_instances".' %
                        //           self.name)
                        ExceptionCollector.appendException(String.format(
                            "ValidationError: \"properties\" of template \"%s\": \"default_instances\" value is not between \"min_instances\" and \"max_instances\"",
                            name));
                    }
                }
    		}
    	}
    }

	private void _commonValidateProperties(StatefulEntityType entityType,LinkedHashMap<String,Object> properties) {
    	ArrayList<String> allowedProps = new ArrayList<String>();
    	ArrayList<String> requiredProps = new ArrayList<String>();
    	for(PropertyDef p: entityType.getPropertiesDefObjects()) {
    		allowedProps.add(p.getName());
    		// If property is 'required' and has no 'default' value then record
    		if(p.isRequired() && p.getDefault() == null) {
    			requiredProps.add(p.getName());
    		}
    	}
        // validate all required properties have values
        if(properties != null) {
            ArrayList<String> reqPropsNoValueOrDefault = new ArrayList<String>();
            _commonValidateField(properties, allowedProps, "properties");
            // make sure it's not missing any property required by a tosca type
            for(String r: requiredProps) {
            	if(properties.get(r) == null) {
            		reqPropsNoValueOrDefault.add(r);
            	}
            }
            // Required properties found without value or a default value
            if(!reqPropsNoValueOrDefault.isEmpty()) {
                ExceptionCollector.appendException(String.format(
                    "MissingRequiredFieldError: properties of template \"%s\" are missing field(s): %s",
                    name,reqPropsNoValueOrDefault.toString()));
            }
        }
        else {
            // Required properties in schema, but not in template
            if(!requiredProps.isEmpty()) {
                ExceptionCollector.appendException(String.format(
                        "MissingRequiredFieldError2: properties of template \"%s\" are missing field(s): %s",
                        name,requiredProps.toString()));
            }
        }
    }
    
    @SuppressWarnings("unchecked")
	private void _validateField(LinkedHashMap<String,Object> template) {
        if(!(template instanceof LinkedHashMap)) {
            ExceptionCollector.appendException(String.format(
            		"MissingRequiredFieldError: Template \"%s\" is missing required field \"%s\"",name,TYPE));
            return;//???
        }
        boolean bBad = false;
        Object relationship = ((LinkedHashMap<String,Object>)template).get("relationship");
        if(relationship != null) {
        	if(!(relationship instanceof String)) {
        		bBad = (((LinkedHashMap<String,Object>)relationship).get(TYPE) == null);
        	}
        	else if(relationship instanceof String) {
        		bBad = (template.get("relationship") == null);
        	}
        }
        else {
       		bBad = (template.get(TYPE) == null);
        }
        if(bBad) {
        	ExceptionCollector.appendException(String.format(
            		"MissingRequiredFieldError: Template \"%s\" is missing required field \"%s\"",name,TYPE));
        }
    }
    
    protected void _commonValidateField(LinkedHashMap<String,Object> schema, ArrayList<String> allowedList,String section) {
    	for(String sname: schema.keySet()) {
    		boolean bFound = false;
    		for(String allowed: allowedList) {
    			if(sname.equals(allowed)) {
    				bFound = true;
    				break;
    			}
    		}
    		if(!bFound) {
                ExceptionCollector.appendException(String.format(
                        "UnknownFieldError: Section \"%s\" of template \"%s\" contains unknown field \"%s\"",section,name,sname));
    		}
    	}
    	
    }
    
    @SuppressWarnings("unchecked")
	private ArrayList<Property> _createProperties() {
    	ArrayList<Property> props = new ArrayList<Property>();
    	LinkedHashMap<String,Object> properties = (LinkedHashMap<String,Object>)
				((EntityType)typeDefinition).getValue(PROPERTIES,entityTpl,false);
    	if(properties == null) {
    		properties = new LinkedHashMap<String,Object>();
    	}
	   	for(Map.Entry<String,Object> me: properties.entrySet()) {
	   		String pname = me.getKey();
	   		Object pvalue = me.getValue();
	   		LinkedHashMap<String,PropertyDef> propsDef = ((StatefulEntityType)typeDefinition).getPropertiesDef();
	   		if(propsDef != null && propsDef.get(pname) != null) {
	   			PropertyDef pd = (PropertyDef)propsDef.get(pname);
	   			Property prop = new Property(pname,pvalue,pd.getSchema(),customDef);
	   			props.add(prop);
	   		}
	   	}
	   	ArrayList<PropertyDef> pds = ((StatefulEntityType)typeDefinition).getPropertiesDefObjects();
	   	for(Object pdo: pds) {
	   		PropertyDef pd = (PropertyDef)pdo;
	   		if(pd.getDefault() != null && properties.get(pd.getName()) == null) {
	   			Property prop = new Property(pd.getName(),pd.getDefault(),pd.getSchema(),customDef);
	   			props.add(prop);
	   		}
	   	}
	   	return props;
    }

    @SuppressWarnings("unchecked")
	private ArrayList<InterfacesDef> _createInterfaces() {
    	ArrayList<InterfacesDef> interfaces = new ArrayList<>();
    	LinkedHashMap<String,Object> typeInterfaces = new LinkedHashMap<String,Object>();
    	if(typeDefinition instanceof RelationshipType) {
    		if(entityTpl instanceof LinkedHashMap) {
    			typeInterfaces = (LinkedHashMap<String,Object>)entityTpl.get(INTERFACES);
    			if(typeInterfaces == null) {
        		   	for(String relName: entityTpl.keySet()) {
        		   		Object relValue = entityTpl.get(relName);
        		   		if(!relName.equals("type")) {
        		   			Object relDef = relValue;
        		   			LinkedHashMap<String,Object> rel = null;
        		   			if(relDef instanceof LinkedHashMap) {
        		   				Object relob = ((LinkedHashMap<String,Object>)relDef).get("relationship");
        		   				if(relob instanceof LinkedHashMap) {
        		   					rel = (LinkedHashMap<String,Object>)relob;
        		   				}
        		   			}
        		   			if(rel != null) {
        		   				if(rel.get(INTERFACES) != null) {
        		   					typeInterfaces = (LinkedHashMap<String,Object>)rel.get(INTERFACES);
        		   					break;
        		   				}
        		   			}
        		   		}
        		   	}
    			}
    		}
    	}
    	else {
    		typeInterfaces = (LinkedHashMap<String,Object>)
					((EntityType)typeDefinition).getValue(INTERFACES,entityTpl,false);
    	}
    	if(typeInterfaces != null) {
    		for(Map.Entry<String,Object> me: typeInterfaces.entrySet()) {
    			String interfaceType = me.getKey();
    		   	LinkedHashMap<String,Object> value = (LinkedHashMap<String,Object>)me.getValue();
    		   	for(Map.Entry<String,Object> ve: value.entrySet()) {
    		   		String op = ve.getKey();
    		   		Object opDef = ve.getValue();
    		   		InterfacesDef iface = new InterfacesDef((EntityType)typeDefinition,
    		   												interfaceType,
    		   												this,
    		   												op,
    		   												opDef);
    		   		interfaces.add(iface);
    		   	}
    			
    		}
    	}
    	return interfaces;
    }
    
	public Capability getCapability(String name) {
        // Provide named capability
    	// :param name: name of capability
        // :return: capability object if found, None otherwise
    	LinkedHashMap<String,Capability> caps = getCapabilities();
    	if(caps != null) {
    		return caps.get(name);
    	}
    	return null;
    }
    
	// getter
   public String getName() {
    	return name;
    }
   
    public StatefulEntityType getTypeDefinition() {
    	return typeDefinition;
    }
   
	public LinkedHashMap<String,Object> getCustomDef() {
		return customDef;
	}

	@Override
	public String toString() {
		return "EntityTemplate{" +
				"name='" + name + '\'' +
				", entityTpl=" + entityTpl +
				", customDef=" + customDef +
				", typeDefinition=" + typeDefinition +
				", _properties=" + _properties +
				", _interfaces=" + _interfaces +
				", _requirements=" + _requirements +
				", _capabilities=" + _capabilities +
				'}';
	}
}

/*python

class EntityTemplate(object):
    '''Base class for TOSCA templates.'''

    SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS,
                INTERFACES, CAPABILITIES, TYPE, DESCRIPTION, DIRECTIVES,
                ATTRIBUTES, ARTIFACTS, NODE_FILTER, COPY) = \
               ('derived_from', 'properties', 'requirements', 'interfaces',
                'capabilities', 'type', 'description', 'directives',
                'attributes', 'artifacts', 'node_filter', 'copy')
    REQUIREMENTS_SECTION = (NODE, CAPABILITY, RELATIONSHIP, OCCURRENCES, NODE_FILTER) = \
                           ('node', 'capability', 'relationship',
                            'occurrences', 'node_filter')
    # Special key names
    SPECIAL_SECTIONS = (METADATA) = ('metadata')

    def __init__(self, name, template, entity_name, custom_def=None):
        self.name = name
        self.entity_tpl = template
        self.custom_def = custom_def
        self._validate_field(self.entity_tpl)
        type = self.entity_tpl.get('type')
        UnsupportedType.validate_type(type)
        if entity_name == 'node_type':
            self.type_definition = NodeType(type, custom_def) \
                if type is not None else None
        if entity_name == 'relationship_type':
            relationship = template.get('relationship')
            type = None
            if relationship and isinstance(relationship, dict):
                type = relationship.get('type')
            elif isinstance(relationship, str):
                type = self.entity_tpl['relationship']
            else:
                type = self.entity_tpl['type']
            UnsupportedType.validate_type(type)
            self.type_definition = RelationshipType(type,
                                                    None, custom_def)
        if entity_name == 'policy_type':
            if not type:
                msg = (_('Policy definition of "%(pname)s" must have'
                       ' a "type" ''attribute.') % dict(pname=name))
                ExceptionCollector.appendException(
                    ValidationError(msg))

            self.type_definition = PolicyType(type, custom_def)
        if entity_name == 'group_type':
            self.type_definition = GroupType(type, custom_def) \
                if type is not None else None
        self._properties = None
        self._interfaces = None
        self._requirements = None
        self._capabilities = None

    @property
    def type(self):
        if self.type_definition:
            return self.type_definition.type

    @property
    def parent_type(self):
        if self.type_definition:
            return self.type_definition.parent_type

    @property
    def requirements(self):
        if self._requirements is None:
            self._requirements = self.type_definition.get_value(
                self.REQUIREMENTS,
                self.entity_tpl) or []
        return self._requirements

    def get_properties_objects(self):
        '''Return properties objects for this template.'''
        if self._properties is None:
            self._properties = self._create_properties()
        return self._properties

    def get_properties(self):
        '''Return a dictionary of property name-object pairs.'''
        return {prop.name: prop
                for prop in self.get_properties_objects()}

    def get_property_value(self, name):
        '''Return the value of a given property name.'''
        props = self.get_properties()
        if props and name in props.keys():
            return props[name].value

    @property
    def interfaces(self):
        if self._interfaces is None:
            self._interfaces = self._create_interfaces()
        return self._interfaces

    def get_capabilities_objects(self):
        '''Return capabilities objects for this template.'''
        if not self._capabilities:
            self._capabilities = self._create_capabilities()
        return self._capabilities

    def get_capabilities(self):
        '''Return a dictionary of capability name-object pairs.'''
        return {cap.name: cap
                for cap in self.get_capabilities_objects()}

    def is_derived_from(self, type_str):
        '''Check if object inherits from the given type.

        Returns true if this object is derived from 'type_str'.
        False otherwise.
        '''
        if not self.type:
            return False
        elif self.type == type_str:
            return True
        elif self.parent_type:
            return self.parent_type.is_derived_from(type_str)
        else:
            return False

    def _create_capabilities(self):
        capability = []
        caps = self.type_definition.get_value(self.CAPABILITIES,
                                              self.entity_tpl, True)
        if caps:
            for name, props in caps.items():
                capabilities = self.type_definition.get_capabilities()
                if name in capabilities.keys():
                    c = capabilities[name]
                    properties = {}
                    # first use the definition default value
                    if c.properties:
                        for property_name in c.properties.keys():
                            prop_def = c.properties[property_name]
                            if 'default' in prop_def:
                                properties[property_name] = prop_def['default']
                    # then update (if available) with the node properties
                    if 'properties' in props and props['properties']:
                        properties.update(props['properties'])

                    cap = Capability(name, properties, c)
                    capability.append(cap)
        return capability

    def _validate_properties(self, template, entitytype):
        properties = entitytype.get_value(self.PROPERTIES, template)
        self._common_validate_properties(entitytype, properties)

    def _validate_capabilities(self):
        type_capabilities = self.type_definition.get_capabilities()
        allowed_caps = \
            type_capabilities.keys() if type_capabilities else []
        capabilities = self.type_definition.get_value(self.CAPABILITIES,
                                                      self.entity_tpl)
        if capabilities:
            self._common_validate_field(capabilities, allowed_caps,
                                        'capabilities')
            self._validate_capabilities_properties(capabilities)

    def _validate_capabilities_properties(self, capabilities):
        for cap, props in capabilities.items():
            capability = self.get_capability(cap)
            if not capability:
                continue
            capabilitydef = capability.definition
            self._common_validate_properties(capabilitydef,
                                             props[self.PROPERTIES])

            # validating capability properties values
            for prop in self.get_capability(cap).get_properties_objects():
                prop.validate()

                # tODO(srinivas_tadepalli): temporary work around to validate
                # default_instances until standardized in specification
                if cap == "scalable" and prop.name == "default_instances":
                    prop_dict = props[self.PROPERTIES]
                    min_instances = prop_dict.get("min_instances")
                    max_instances = prop_dict.get("max_instances")
                    default_instances = prop_dict.get("default_instances")
                    if not (min_instances <= default_instances
                            <= max_instances):
                        err_msg = ('"properties" of template "%s": '
                                   '"default_instances" value is not between '
                                   '"min_instances" and "max_instances".' %
                                   self.name)
                        ExceptionCollector.appendException(
                            ValidationError(message=err_msg))

    def _common_validate_properties(self, entitytype, properties):
        allowed_props = []
        required_props = []
        for p in entitytype.get_properties_def_objects():
            allowed_props.append(p.name)
            # If property is 'required' and has no 'default' value then record
            if p.required and p.default is None:
                required_props.append(p.name)
        # validate all required properties have values
        if properties:
            req_props_no_value_or_default = []
            self._common_validate_field(properties, allowed_props,
                                        'properties')
            # make sure it's not missing any property required by a tosca type
            for r in required_props:
                if r not in properties.keys():
                    req_props_no_value_or_default.append(r)
            # Required properties found without value or a default value
            if req_props_no_value_or_default:
                ExceptionCollector.appendException(
                    MissingRequiredFieldError(
                        what='"properties" of template "%s"' % self.name,
                        required=req_props_no_value_or_default))
        else:
            # Required properties in schema, but not in template
            if required_props:
                ExceptionCollector.appendException(
                    MissingRequiredFieldError(
                        what='"properties" of template "%s"' % self.name,
                        required=required_props))

    def _validate_field(self, template):
        if not isinstance(template, dict):
            ExceptionCollector.appendException(
                MissingRequiredFieldError(
                    what='Template "%s"' % self.name, required=self.TYPE))
        try:
            relationship = template.get('relationship')
            if relationship and not isinstance(relationship, str):
                relationship[self.TYPE]
            elif isinstance(relationship, str):
                template['relationship']
            else:
                template[self.TYPE]
        except KeyError:
            ExceptionCollector.appendException(
                MissingRequiredFieldError(
                    what='Template "%s"' % self.name, required=self.TYPE))

    def _common_validate_field(self, schema, allowedlist, section):
        for name in schema:
            if name not in allowedlist:
                ExceptionCollector.appendException(
                    UnknownFieldError(
                        what=('"%(section)s" of template "%(nodename)s"'
                              % {'section': section, 'nodename': self.name}),
                        field=name))

    def _create_properties(self):
        props = []
        properties = self.type_definition.get_value(self.PROPERTIES,
                                                    self.entity_tpl) or {}
        for name, value in properties.items():
            props_def = self.type_definition.get_properties_def()
            if props_def and name in props_def:
                prop = Property(name, value,
                                props_def[name].schema, self.custom_def)
                props.append(prop)
        for p in self.type_definition.get_properties_def_objects():
            if p.default is not None and p.name not in properties.keys():
                prop = Property(p.name, p.default, p.schema, self.custom_def)
                props.append(prop)
        return props

    def _create_interfaces(self):
        interfaces = []
        type_interfaces = None
        if isinstance(self.type_definition, RelationshipType):
            if isinstance(self.entity_tpl, dict):
                if self.INTERFACES in self.entity_tpl:
                    type_interfaces = self.entity_tpl[self.INTERFACES]
                else:
                    for rel_def, value in self.entity_tpl.items():
                        if rel_def != 'type':
                            rel_def = self.entity_tpl.get(rel_def)
                            rel = None
                            if isinstance(rel_def, dict):
                                rel = rel_def.get('relationship')
                            if rel:
                                if self.INTERFACES in rel:
                                    type_interfaces = rel[self.INTERFACES]
                                    break
        else:
            type_interfaces = self.type_definition.get_value(self.INTERFACES,
                                                             self.entity_tpl)
        if type_interfaces:
            for interface_type, value in type_interfaces.items():
                for op, op_def in value.items():
                    iface = InterfacesDef(self.type_definition,
                                          interfacetype=interface_type,
                                          node_template=self,
                                          name=op,
                                          value=op_def)
                    interfaces.append(iface)
        return interfaces

    def get_capability(self, name):
        """Provide named capability

        :param name: name of capability
        :return: capability object if found, None otherwise
        """
        caps = self.get_capabilities()
        if caps and name in caps.keys():
            return caps[name]
*/