aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/TopologyTemplate.java
blob: 25f118b18293c73be38ab24b774aee356df6bdd5 (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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package org.openecomp.sdc.toscaparser.api;

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

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.elements.InterfacesDef;
import org.openecomp.sdc.toscaparser.api.elements.NodeType;
import org.openecomp.sdc.toscaparser.api.elements.RelationshipType;
import org.openecomp.sdc.toscaparser.api.functions.Function;
import org.openecomp.sdc.toscaparser.api.functions.GetAttribute;
import org.openecomp.sdc.toscaparser.api.functions.GetInput;
import org.openecomp.sdc.toscaparser.api.parameters.Input;
import org.openecomp.sdc.toscaparser.api.parameters.Output;
import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder;

public class TopologyTemplate {

	private static final String DESCRIPTION = "description";
	private static final String INPUTS = "inputs";
	private static final String NODE_TEMPLATES = "node_templates";
	private static final String RELATIONSHIP_TEMPLATES = "relationship_templates";
	private static final String OUTPUTS = "outputs";
	private static final String GROUPS = "groups";
	private static final String SUBSTITUTION_MAPPINGS = "substitution_mappings";
	private static final String POLICIES = "policies";
	private static final String METADATA = "metadata";
	
	private static String SECTIONS[] = {
				DESCRIPTION, INPUTS, NODE_TEMPLATES, RELATIONSHIP_TEMPLATES, 
				OUTPUTS, GROUPS,  SUBSTITUTION_MAPPINGS, POLICIES, METADATA
			};

    private LinkedHashMap<String,Object> tpl;
	LinkedHashMap<String,Object> metaData;
    private ArrayList<Input> inputs;
    private ArrayList<Output> outputs;
    private ArrayList<RelationshipTemplate> relationshipTemplates;
    private ArrayList<NodeTemplate> nodeTemplates;
	private LinkedHashMap<String,Object> customDefs;
	private LinkedHashMap<String,Object> relTypes;//TYPE
    private NodeTemplate subMappedNodeTemplate;
    private ArrayList<Group> groups;
    private ArrayList<Policy> policies;
    private LinkedHashMap<String,Object> parsedParams = null;//TYPE
    private String description;
    private ToscaGraph graph;
    private SubstitutionMappings substitutionMappings;
	
	public TopologyTemplate(
			LinkedHashMap<String,Object> _template, 
			LinkedHashMap<String,Object> _customDefs,
			LinkedHashMap<String,Object> _relTypes,//TYPE
            LinkedHashMap<String, Object> _parsedParams,
            NodeTemplate  _subMappedNodeTemplate) {

		tpl = _template;
		if(tpl != null) {
			subMappedNodeTemplate = _subMappedNodeTemplate;
			metaData = _metaData();
			customDefs = _customDefs;
			relTypes = _relTypes;
			parsedParams = _parsedParams;
			_validateField();
			description = _tplDescription();
			inputs = _inputs();
	        relationshipTemplates =_relationshipTemplates();
	        nodeTemplates = _nodeTemplates();
	        outputs = _outputs();
	        if(nodeTemplates != null) {
	        	graph = new ToscaGraph(nodeTemplates);
	        }
	        groups = _groups();
	        policies = _policies();
	        _processIntrinsicFunctions();
            substitutionMappings = _substitutionMappings();
		}
	}

	@SuppressWarnings("unchecked")
	private ArrayList<Input> _inputs() {
		//DumpUtils.dumpYaml(customDefs,0);
		ArrayList<Input> alInputs = new ArrayList<>();
		for(String name: _tplInputs().keySet()) {
			Object attrs = _tplInputs().get(name);
			Input input = new Input(name,(LinkedHashMap<String,Object>)attrs,customDefs);
            if(parsedParams != null && parsedParams.get(name) != null) {
            	input.validate(parsedParams.get(name));
            }
            else {
                Object _default = input.getDefault();
                if(_default != null) {
                    input.validate(_default);
                }
            }
            if((parsedParams != null && parsedParams.get(input.getName()) == null || parsedParams == null)
            		 && input.isRequired() && input.getDefault() == null) {
            	System.out.format("Log warning: The required parameter \"%s\" is not provided\n",input.getName());
            }
            alInputs.add(input);
		}
        return alInputs;
		
	}

	private LinkedHashMap<String,Object> _metaData() {
        if(tpl.get(METADATA) != null) {
        	return (LinkedHashMap<String,Object>)tpl.get(METADATA);
        }
        else {
        	return new LinkedHashMap<String,Object>();
        }
	
	}

	private ArrayList<NodeTemplate> _nodeTemplates() {
		ArrayList<NodeTemplate> alNodeTemplates = new ArrayList<>();
		LinkedHashMap<String,Object> tpls = _tplNodeTemplates();
		if(tpls != null) {
			for(String name: tpls.keySet()) {
				NodeTemplate tpl = new NodeTemplate(name,
													tpls,
													customDefs,
													relationshipTemplates,
													relTypes);
				if(tpl.getTypeDefinition() != null) {
					boolean b = NodeType.TOSCA_DEF.get(tpl.getType()) != null;
					if(b || (tpl.getCustomDef() != null && !tpl.getCustomDef().isEmpty())) {
						tpl.validate();
						alNodeTemplates.add(tpl);
					}
				}
			}
		}
		return alNodeTemplates;
	}
	
	@SuppressWarnings("unchecked")
	private ArrayList<RelationshipTemplate> _relationshipTemplates() {
		ArrayList<RelationshipTemplate> alRelationshipTemplates = new ArrayList<>();
		LinkedHashMap<String,Object> tpls = _tplRelationshipTemplates();
		if(tpls != null) {
			for(String name: tpls.keySet()) {
				RelationshipTemplate tpl = new RelationshipTemplate(
						(LinkedHashMap<String,Object>)tpls.get(name),name,customDefs,null,null);
						
				alRelationshipTemplates.add(tpl);
			}
		}
		return alRelationshipTemplates;
	}
	
	private ArrayList<Output> _outputs() {
		ArrayList<Output> alOutputs = new ArrayList<>();
		for(Map.Entry<String,Object> me: _tplOutputs().entrySet()) {
			String oname = me.getKey();
			LinkedHashMap<String,Object> oattrs = (LinkedHashMap<String,Object>)me.getValue(); 
			Output o = new Output(oname,oattrs);
			o.validate();
			alOutputs.add(o);
		}
		return alOutputs;
	}
	
	private SubstitutionMappings _substitutionMappings() {
		LinkedHashMap<String,Object> tplSubstitutionMapping = (LinkedHashMap<String,Object>) _tplSubstitutionMappings();
		
		//*** the commenting-out below and the weaker condition are in the Python source
		// #if tpl_substitution_mapping and self.sub_mapped_node_template:
		if(tplSubstitutionMapping != null && tplSubstitutionMapping.size() > 0) {
			return new SubstitutionMappings(tplSubstitutionMapping,
					                    nodeTemplates,
					                    inputs,
					                    outputs,
					                    groups,
					                    subMappedNodeTemplate,
					                    customDefs);
		}
		return null;
		
	}
	
	@SuppressWarnings("unchecked")
	private ArrayList<Policy> _policies() {
		ArrayList<Policy> alPolicies = new ArrayList<>();
		for(Object po: _tplPolicies()) {
			LinkedHashMap<String,Object> policy = (LinkedHashMap<String,Object>)po;
			for(Map.Entry<String,Object> me: policy.entrySet()) {
				String policyName = me.getKey();
				LinkedHashMap<String,Object> policyTpl = (LinkedHashMap<String,Object>)me.getValue();
				ArrayList<String> targetList = (ArrayList<String>)policyTpl.get("targets");
				//ArrayList<Object> targetObjects = new ArrayList<>();
				ArrayList<NodeTemplate> targetNodes = new ArrayList<>();
				ArrayList<Object> targetObjects = new ArrayList<>();
				ArrayList<Group> targetGroups = new ArrayList<>();
				String targetsType = "groups"; 
				if(targetList != null && targetList.size() >= 1) {
                    targetGroups = _getPolicyGroups(targetList);
                    if(targetGroups == null) {
                    	targetsType = "node_templates";
                        targetNodes = _getGroupMembers(targetList);
                        for(NodeTemplate nt: targetNodes) {
                        	targetObjects.add(nt);
                        }
                    }
                    else {
                    	for(Group gr: targetGroups) {
                    		targetObjects.add(gr);
                    	}
                    }
				}
                Policy policyObj = new Policy(policyName, 
                							  policyTpl,
		                                      targetObjects, 
		                                      targetsType,
		                                      customDefs);
                alPolicies.add(policyObj);
			}
		}
        return alPolicies;
	}
	
	private ArrayList<Group> _groups() {
		ArrayList<Group> groups = new ArrayList<>();
		ArrayList<NodeTemplate> memberNodes  = null;
		for(Map.Entry<String,Object> me: _tplGroups().entrySet()) {
			String groupName = me.getKey();
			LinkedHashMap<String,Object> groupTpl = (LinkedHashMap<String,Object>)me.getValue();
			ArrayList<String> memberNames = (ArrayList<String>)groupTpl.get("members");
			if(memberNames != null) {
                DataEntity.validateDatatype("list", memberNames,null,null,null);
				if(memberNames.size() < 1 || 
				       (new HashSet<String>(memberNames)).size() != memberNames.size()) {
                    ThreadLocalsHolder.getCollector().appendWarning(String.format(
                            "InvalidGroupTargetException: Member nodes \"%s\" should be >= 1 and not repeated",
                            memberNames.toString()));
				}
				else {
					memberNodes = _getGroupMembers(memberNames);
				}
			}
            Group group = new Group(groupName,
            						groupTpl,
            						memberNodes,
            						customDefs);
            groups.add(group);
		}
		return groups;
	}
	
	private ArrayList<NodeTemplate> _getGroupMembers(ArrayList<String> memberNames) {
		ArrayList<NodeTemplate> memberNodes = new ArrayList<>();
		_validateGroupMembers(memberNames);
		for(String member: memberNames) {
			for(NodeTemplate node: nodeTemplates) {
				if(member.equals(node.getName())) {
					memberNodes.add(node);
				}
			}
		}
		return memberNodes;
	}
	
	private ArrayList<Group> _getPolicyGroups(ArrayList<String> memberNames) {
		ArrayList<Group> memberGroups = new ArrayList<>();
		for(String member: memberNames) {
			for(Group group: groups) {
				if(member.equals(group.getName())) {
					memberGroups.add(group);
				}
			}
		}
		return memberGroups;
	}
	
	private void _validateGroupMembers(ArrayList<String> members) {
		ArrayList<String> nodeNames = new ArrayList<>();
		for(NodeTemplate node: nodeTemplates) {
			nodeNames.add(node.getName());
		}
		for(String member: members) {
			if(!nodeNames.contains(member)) {
                ThreadLocalsHolder.getCollector().appendException(String.format(
                        "InvalidGroupTargetException: Target member \"%s\" is not found in \"nodeTemplates\"",member));
			}
		}
	}
	
	// topology template can act like node template
	// it is exposed by substitution_mappings.

	public String nodetype() {
		return substitutionMappings.getNodeType();
	}
	
	public LinkedHashMap<String,Object> capabilities() {
		return substitutionMappings.getCapabilities();
	}
	
	public LinkedHashMap<String,Object> requirements() {
		return substitutionMappings.getRequirements();
	}

	private String _tplDescription() {
        return (String)tpl.get(DESCRIPTION);
        //if description:
        //	return description.rstrip()
	}

	@SuppressWarnings("unchecked")
	private LinkedHashMap<String,Object> _tplInputs() {
        if(tpl.get(INPUTS) != null) {
        	return (LinkedHashMap<String,Object>)tpl.get(INPUTS);
        }
        else {
        	return new LinkedHashMap<String,Object>();
        }
    }

    @SuppressWarnings("unchecked")
	private LinkedHashMap<String,Object> _tplNodeTemplates() {
        return (LinkedHashMap<String,Object>)tpl.get(NODE_TEMPLATES);
    }

    @SuppressWarnings("unchecked")
	private LinkedHashMap<String,Object> _tplRelationshipTemplates() {
        if(tpl.get(RELATIONSHIP_TEMPLATES) != null) {
        	return (LinkedHashMap<String,Object>)tpl.get(RELATIONSHIP_TEMPLATES);
        }
        else {
        	return new LinkedHashMap<String,Object>();
        }
    }

    @SuppressWarnings("unchecked")
 	private LinkedHashMap<String,Object> _tplOutputs() {
         if(tpl.get(OUTPUTS) != null) {
         	return (LinkedHashMap<String,Object>)tpl.get(OUTPUTS);
         }
         else {
         	return new LinkedHashMap<String,Object>();
         }
     }

    @SuppressWarnings("unchecked")
    private LinkedHashMap<String,Object> _tplSubstitutionMappings() {
        if(tpl.get(SUBSTITUTION_MAPPINGS) != null) {
        	return (LinkedHashMap<String,Object>)tpl.get(SUBSTITUTION_MAPPINGS);
        }
        else {
        	return new LinkedHashMap<String,Object>();
        }
    }

    @SuppressWarnings("unchecked")
    private LinkedHashMap<String,Object> _tplGroups() {
        if(tpl.get(GROUPS) != null) {
        	return (LinkedHashMap<String,Object>)tpl.get(GROUPS);
        }
        else {
        	return new LinkedHashMap<String,Object>();
        }
    }

    @SuppressWarnings("unchecked")
    private ArrayList<Object> _tplPolicies() {
        if(tpl.get(POLICIES) != null) {
        	return (ArrayList<Object>)tpl.get(POLICIES);
        }
        else {
        	return new ArrayList<Object>();
        }
    }

    private void _validateField() {
    	for(String name: tpl.keySet()) {
    		boolean bFound = false;
    		for(String section: SECTIONS) {
    			if(name.equals(section)) {
    				bFound = true;
    				break;
    			}
    		}
    		if(!bFound) {
                ThreadLocalsHolder.getCollector().appendException(String.format(
                		"UnknownFieldError: TopologyTemplate contains unknown field \"%s\"",name));
    		}
    	}
    }

    @SuppressWarnings("unchecked")
	private void _processIntrinsicFunctions() {
        // Process intrinsic functions

        // Current implementation processes functions within node template
        // properties, requirements, interfaces inputs and template outputs.
    	
    	if(nodeTemplates != null) {
    		for(NodeTemplate nt: nodeTemplates) {
    			for(Property prop: nt.getPropertiesObjects()) {
    				prop.setValue(Function.getFunction(this,nt,prop.getValue()));
    			}
    			for(InterfacesDef ifd: nt.getInterfaces()) {
    				LinkedHashMap<String,Object> ifin = ifd.getInputs();
    				if(ifin != null) {
    					for(Map.Entry<String,Object> me: ifin.entrySet()) {
    						String name = me.getKey();
    						Object value = Function.getFunction(this,nt,me.getValue());
    						ifd.setInput(name,value);
    					}
    				}
    			}
    			if(nt.getRequirements() != null &&
    					nt.getRequirements() instanceof ArrayList) {
    				for(Object oreq: nt.getRequirements()) {
    					LinkedHashMap<String,Object> req = (LinkedHashMap<String,Object>)oreq;
    					LinkedHashMap<String,Object> rel = req;
    					for(String reqName: req.keySet()) {
    						Object reqItem = req.get(reqName);
    						if(reqItem instanceof LinkedHashMap) {
    							Object t = ((LinkedHashMap<String,Object>)reqItem).get("relationship");
    							// it can be a string or a LHM...
    							if(t instanceof LinkedHashMap) {
    								rel = (LinkedHashMap<String,Object>)t;
    							}
    							else {
    								// we set it to null to fail the next test
    								// and avoid the get("proprties")
    								rel = null;
    							}
    							break;
    						}
    					}
    					if(rel != null && rel.get("properties") != null) {
    						LinkedHashMap<String,Object> relprops = 
    								(LinkedHashMap<String,Object>)rel.get("properties");
    						for(String key: relprops.keySet()) {
    							Object value = relprops.get(key);
    							Object func = Function.getFunction(this,req,value);
    							relprops.put(key,func);
    						}
    					}
    				}
    			}
    			if(nt.getCapabilitiesObjects() != null) {
    				for(Capability cap: nt.getCapabilitiesObjects()) {
    					if(cap.getPropertiesObjects() != null) {
    						for(Property prop: cap.getPropertiesObjects()) {
    							Object propvalue = Function.getFunction(this,nt,prop.getValue());
    							if(propvalue instanceof GetInput) {
    								propvalue = ((GetInput)propvalue).result();
    								for(String p: cap.getProperties().keySet()) {
    									//Object v = cap.getProperties().get(p);
    									if(p.equals(prop.getName())) {
    										cap.setProperty(p,propvalue);
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    			for(RelationshipType rel: nt.getRelationships().keySet()) {
    				NodeTemplate node = nt.getRelationships().get(rel);
    				ArrayList<RelationshipTemplate> relTpls = node.getRelationshipTemplate();
    				if(relTpls != null) {
    					for(RelationshipTemplate relTpl: relTpls) {
    			    		// TT 5
    						for(InterfacesDef iface: relTpl.getInterfaces()) {
    							if(iface.getInputs() != null) {
    								for(String name: iface.getInputs().keySet()) {
    									Object value = iface.getInputs().get(name);
    									Object func = Function.getFunction(
    											this,
    											relTpl,
    											value);
    									iface.setInput(name,func);
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	for(Output output: outputs) {
    		Object func = Function.getFunction(this,outputs,output.getValue());
    		if(func instanceof GetAttribute) {
    			output.setAttr(Output.VALUE,func);
    		}
    	}
    }
    
    public static String getSubMappingNodeType(LinkedHashMap<String,Object> topologyTpl) {
    	if(topologyTpl != null && topologyTpl instanceof LinkedHashMap) { 
    		Object submapTpl = topologyTpl.get(SUBSTITUTION_MAPPINGS);
    		return SubstitutionMappings.stGetNodeType((LinkedHashMap<String,Object>)submapTpl);
    	}
    	return null;
    }
    
    // getters
    
	public LinkedHashMap<String,Object> getTpl() {
		return tpl;
	}
	
	public LinkedHashMap<String,Object> getMetadata() {
		return metaData;
	}
	
	public ArrayList<Input> getInputs() {
		return inputs;
	}
	
	public ArrayList<Output> getOutputs() {
		return outputs;
	}
	
	public ArrayList<Policy> getPolicies() {
		return policies;
	}
	
	public ArrayList<RelationshipTemplate> getRelationshipTemplates() {
		return relationshipTemplates;
	}

	public ArrayList<NodeTemplate> getNodeTemplates() {
		return nodeTemplates;
	}
	
	public ArrayList<Group> getGroups() {
		return groups;
	}
	
	public SubstitutionMappings getSubstitutionMappings() {
		return substitutionMappings;
	}

	public LinkedHashMap<String,Object> getParsedParams() {
		return parsedParams;
	}
}

/*python

#    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.


import logging

from toscaparser.common import exception
from toscaparser.dataentity import DataEntity
from toscaparser import functions
from toscaparser.groups import Group
from toscaparser.nodetemplate import NodeTemplate
from toscaparser.parameters import Input
from toscaparser.parameters import Output
from toscaparser.policy import Policy
from toscaparser.relationship_template import RelationshipTemplate
from toscaparser.substitution_mappings import SubstitutionMappings
from toscaparser.tpl_relationship_graph import ToscaGraph
from toscaparser.utils.gettextutils import _


# Topology template key names
SECTIONS = (DESCRIPTION, INPUTS, NODE_TEMPLATES,
            RELATIONSHIP_TEMPLATES, OUTPUTS, GROUPS,
            SUBSTITUION_MAPPINGS, POLICIES) = \
           ('description', 'inputs', 'node_templates',
            'relationship_templates', 'outputs', 'groups',
            'substitution_mappings', 'policies')

log = logging.getLogger("tosca.model")


class TopologyTemplate(object):

    '''Load the template data.'''
    def __init__(self, template, custom_defs,
                 rel_types=None, parsed_params=None,
                 sub_mapped_node_template=None):
        self.tpl = template
        self.sub_mapped_node_template = sub_mapped_node_template
        if self.tpl:
            self.custom_defs = custom_defs
            self.rel_types = rel_types
            self.parsed_params = parsed_params
            self._validate_field()
            self.description = self._tpl_description()
            self.inputs = self._inputs()
            self.relationship_templates = self._relationship_templates()
            self.nodetemplates = self._nodetemplates()
            self.outputs = self._outputs()
            if hasattr(self, 'nodetemplates'):
                self.graph = ToscaGraph(self.nodetemplates)
            self.groups = self._groups()
            self.policies = self._policies()
            self._process_intrinsic_functions()
            self.substitution_mappings = self._substitution_mappings()

    def _inputs(self):
        inputs = []
        for name, attrs in self._tpl_inputs().items():
            input = Input(name, attrs)
            if self.parsed_params and name in self.parsed_params:
                input.validate(self.parsed_params[name])
            else:
                default = input.default
                if default:
                    input.validate(default)
            if (self.parsed_params and input.name not in self.parsed_params
                or self.parsed_params is None) and input.required \
                    and input.default is None:
                log.warning(_('The required parameter %s '
                              'is not provided') % input.name)

            inputs.append(input)
        return inputs

    def _nodetemplates(self):
        nodetemplates = []
        tpls = self._tpl_nodetemplates()
        if tpls:
            for name in tpls:
                tpl = NodeTemplate(name, tpls, self.custom_defs,
                                   self.relationship_templates,
                                   self.rel_types)
                if (tpl.type_definition and
                    (tpl.type in tpl.type_definition.TOSCA_DEF or
                     (tpl.type not in tpl.type_definition.TOSCA_DEF and
                      bool(tpl.custom_def)))):
                    tpl.validate(self)
                    nodetemplates.append(tpl)
        return nodetemplates

    def _relationship_templates(self):
        rel_templates = []
        tpls = self._tpl_relationship_templates()
        for name in tpls:
            tpl = RelationshipTemplate(tpls[name], name, self.custom_defs)
            rel_templates.append(tpl)
        return rel_templates

    def _outputs(self):
        outputs = []
        for name, attrs in self._tpl_outputs().items():
            output = Output(name, attrs)
            output.validate()
            outputs.append(output)
        return outputs

    def _substitution_mappings(self):
        tpl_substitution_mapping = self._tpl_substitution_mappings()
        # if tpl_substitution_mapping and self.sub_mapped_node_template:
        if tpl_substitution_mapping:
            return SubstitutionMappings(tpl_substitution_mapping,
                                        self.nodetemplates,
                                        self.inputs,
                                        self.outputs,
                                        self.sub_mapped_node_template,
                                        self.custom_defs)

    def _policies(self):
        policies = []
        for policy in self._tpl_policies():
            for policy_name, policy_tpl in policy.items():
                target_list = policy_tpl.get('targets')
                if target_list and len(target_list) >= 1:
                    target_objects = []
                    targets_type = "groups"
                    target_objects = self._get_policy_groups(target_list)
                    if not target_objects:
                        targets_type = "node_templates"
                        target_objects = self._get_group_members(target_list)
                    policyObj = Policy(policy_name, policy_tpl,
                                       target_objects, targets_type,
                                       self.custom_defs)
                    policies.append(policyObj)
        return policies

    def _groups(self):
        groups = []
        member_nodes = None
        for group_name, group_tpl in self._tpl_groups().items():
            member_names = group_tpl.get('members')
            if member_names is not None:
                DataEntity.validate_datatype('list', member_names)
                if len(member_names) < 1 or \
                        len(member_names) != len(set(member_names)):
                    exception.ExceptionCollector.appendException(
                        exception.InvalidGroupTargetException(
                            message=_('Member nodes "%s" should be >= 1 '
                                      'and not repeated') % member_names))
                else:
                    member_nodes = self._get_group_members(member_names)
            group = Group(group_name, group_tpl,
                          member_nodes,
                          self.custom_defs)
            groups.append(group)
        return groups

    def _get_group_members(self, member_names):
        member_nodes = []
        self._validate_group_members(member_names)
        for member in member_names:
            for node in self.nodetemplates:
                if node.name == member:
                    member_nodes.append(node)
        return member_nodes

    def _get_policy_groups(self, member_names):
        member_groups = []
        for member in member_names:
            for group in self.groups:
                if group.name == member:
                    member_groups.append(group)
        return member_groups

    def _validate_group_members(self, members):
        node_names = []
        for node in self.nodetemplates:
            node_names.append(node.name)
        for member in members:
            if member not in node_names:
                exception.ExceptionCollector.appendException(
                    exception.InvalidGroupTargetException(
                        message=_('Target member "%s" is not found in '
                                  'node_templates') % member))

    # topology template can act like node template
    # it is exposed by substitution_mappings.
    def nodetype(self):
        return self.substitution_mappings.node_type \
            if self.substitution_mappings else None

    def capabilities(self):
        return self.substitution_mappings.capabilities \
            if self.substitution_mappings else None

    def requirements(self):
        return self.substitution_mappings.requirements \
            if self.substitution_mappings else None

    def _tpl_description(self):
        description = self.tpl.get(DESCRIPTION)
        if description:
            return description.rstrip()

    def _tpl_inputs(self):
        return self.tpl.get(INPUTS) or {}

    def _tpl_nodetemplates(self):
        return self.tpl.get(NODE_TEMPLATES)

    def _tpl_relationship_templates(self):
        return self.tpl.get(RELATIONSHIP_TEMPLATES) or {}

    def _tpl_outputs(self):
        return self.tpl.get(OUTPUTS) or {}

    def _tpl_substitution_mappings(self):
        return self.tpl.get(SUBSTITUION_MAPPINGS) or {}

    def _tpl_groups(self):
        return self.tpl.get(GROUPS) or {}

    def _tpl_policies(self):
        return self.tpl.get(POLICIES) or {}

    def _validate_field(self):
        for name in self.tpl:
            if name not in SECTIONS:
                exception.ExceptionCollector.appendException(
                    exception.UnknownFieldError(what='Template', field=name))

    def _process_intrinsic_functions(self):
        """Process intrinsic functions

        Current implementation processes functions within node template
        properties, requirements, interfaces inputs and template outputs.
        """
        if hasattr(self, 'nodetemplates'):
            for node_template in self.nodetemplates:
                for prop in node_template.get_properties_objects():
                    prop.value = functions.get_function(self,
                                                        node_template,
                                                        prop.value)
                for interface in node_template.interfaces:
                    if interface.inputs:
                        for name, value in interface.inputs.items():
                            interface.inputs[name] = functions.get_function(
                                self,
                                node_template,
                                value)
                if node_template.requirements and \
                   isinstance(node_template.requirements, list):
                    for req in node_template.requirements:
                        rel = req
                        for req_name, req_item in req.items():
                            if isinstance(req_item, dict):
                                rel = req_item.get('relationship')
                                break
                        if rel and 'properties' in rel:
                            for key, value in rel['properties'].items():
                                rel['properties'][key] = \
                                    functions.get_function(self,
                                                           req,
                                                           value)
                if node_template.get_capabilities_objects():
                    for cap in node_template.get_capabilities_objects():
                        if cap.get_properties_objects():
                            for prop in cap.get_properties_objects():
                                propvalue = functions.get_function(
                                    self,
                                    node_template,
                                    prop.value)
                                if isinstance(propvalue, functions.GetInput):
                                    propvalue = propvalue.result()
                                    for p, v in cap._properties.items():
                                        if p == prop.name:
                                            cap._properties[p] = propvalue
                for rel, node in node_template.relationships.items():
                    rel_tpls = node.relationship_tpl
                    if rel_tpls:
                        for rel_tpl in rel_tpls:
                            for interface in rel_tpl.interfaces:
                                if interface.inputs:
                                    for name, value in \
                                            interface.inputs.items():
                                        interface.inputs[name] = \
                                            functions.get_function(self,
                                                                   rel_tpl,
                                                                   value)
        for output in self.outputs:
            func = functions.get_function(self, self.outputs, output.value)
            if isinstance(func, functions.GetAttribute):
                output.attrs[output.VALUE] = func

    @classmethod
    def get_sub_mapping_node_type(cls, topology_tpl):
        if topology_tpl and isinstance(topology_tpl, dict):
            submap_tpl = topology_tpl.get(SUBSTITUION_MAPPINGS)
            return SubstitutionMappings.get_node_type(submap_tpl)
*/