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

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.elements.TypeValidation;
import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder;
import org.openecomp.sdc.toscaparser.api.utils.UrlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.net.URL;
import java.nio.file.Paths;
import java.util.*;

public class ImportsLoader {

	private static Logger log = LoggerFactory.getLogger(ImportsLoader.class.getName());
	private static final String FILE = "file";
	private static final String REPOSITORY = "repository";
	private static final String NAMESPACE_URI = "namespace_uri";
	private static final String NAMESPACE_PREFIX = "namespace_prefix";
    private String IMPORTS_SECTION[] = {FILE, REPOSITORY, NAMESPACE_URI, NAMESPACE_PREFIX};
	
	private ArrayList<Object> importslist;
	private String path;
	private ArrayList<String> typeDefinitionList;
	
	private LinkedHashMap<String,Object> customDefs;
	private ArrayList<LinkedHashMap<String,Object>> nestedToscaTpls;
	private LinkedHashMap<String,Object> repositories;

	@SuppressWarnings("unchecked")
	public ImportsLoader(ArrayList<Object>_importslist,
						 String _path,
						 Object _typeDefinitionList,
						 LinkedHashMap<String,Object> tpl) {
		
	        this.importslist = _importslist;
	        customDefs = new LinkedHashMap<String,Object>();
	        nestedToscaTpls = new ArrayList<LinkedHashMap<String,Object>>();
	        if((_path == null || _path.isEmpty()) && tpl == null) {
	            //msg = _('Input tosca template is not provided.')
	            //log.warning(msg)
	            ThreadLocalsHolder.getCollector().appendException("ValidationError: Input tosca template is not provided");
	        }
	        
	        this.path = _path;
	        this.repositories = new LinkedHashMap<String,Object>();

	        if(tpl != null && tpl.get("repositories") != null) { 
	        	this.repositories = (LinkedHashMap<String,Object>)tpl.get("repositories");
	        }
	        this.typeDefinitionList = new ArrayList<String>();
	        if(_typeDefinitionList != null) {
	            if(_typeDefinitionList instanceof ArrayList) {
	                this.typeDefinitionList = (ArrayList<String>)_typeDefinitionList;
	            } 
	            else {
	                this.typeDefinitionList.add((String)_typeDefinitionList);
	            }
	        }
	        _validateAndLoadImports();
	}

	public LinkedHashMap<String,Object> getCustomDefs() {
	        return customDefs;
	}

    public ArrayList<LinkedHashMap<String,Object>> getNestedToscaTpls() {
	        return nestedToscaTpls;
    }
    
    @SuppressWarnings({ "unchecked", "unused" })
	public void _validateAndLoadImports() {
    	Set<String> importNames = new HashSet<String>();
    	
    	if(importslist == null) {
            //msg = _('"imports" keyname is defined without including templates.')
            //log.error(msg)
            ThreadLocalsHolder.getCollector().appendException(
            		"ValidationError: \"imports\" keyname is defined without including templates");
            return;
    	}
    	
    	for(Object importDef: importslist) {
    		String fullFileName = null;
    		LinkedHashMap<String,Object> customType = null;
    		if(importDef instanceof LinkedHashMap) {
    			for(Map.Entry<String,Object> me: ((LinkedHashMap<String,Object>)importDef).entrySet()) {
    				String importName = me.getKey();
    				Object importUri = me.getValue();
    				if(importNames.contains(importName)) {
                        //msg = (_('Duplicate import name "%s" was found.') % import_name)
                        //log.error(msg)
                        ThreadLocalsHolder.getCollector().appendException(String.format(
                        		"ValidationError: Duplicate import name \"%s\" was found",importName));
    				}
    				importNames.add(importName); //???

    				// _loadImportTemplate returns 2 objects
    				Object ffnct[] = _loadImportTemplate(importName, importUri);
    				fullFileName = (String)ffnct[0];
    				customType = (LinkedHashMap<String,Object>)ffnct[1];
    				String namespacePrefix = "";
    				if(importUri instanceof LinkedHashMap) {
    					namespacePrefix = (String)
    							((LinkedHashMap<String,Object>)importUri).get(NAMESPACE_PREFIX);
    				}
    				
    				if(customType != null) {
    					TypeValidation tv = new TypeValidation(customType, importDef);
                        _updateCustomDefs(customType, namespacePrefix);
					}
    			}
    		}
    		else { // old style of imports
				// _loadImportTemplate returns 2 objects
				Object ffnct[] = _loadImportTemplate(null,importDef);
				fullFileName = (String)ffnct[0];
				customType = (LinkedHashMap<String,Object>)ffnct[1];
                if(customType != null) {
                	TypeValidation tv = new TypeValidation(customType,importDef);
                    _updateCustomDefs(customType,null);
                }
    		}
            _updateNestedToscaTpls(fullFileName, customType);

    		
    	}
    }

    @SuppressWarnings("unchecked")
	private void _updateCustomDefs(LinkedHashMap<String,Object> customType, String namespacePrefix) {
    	LinkedHashMap<String,Object> outerCustomTypes;// = new LinkedHashMap<String,Object>();
    	for(String typeDef: typeDefinitionList) {
    		if(typeDef.equals("imports")) {
    			// imports are ArrayList...
    			customDefs.put("imports",(ArrayList<Object>)customType.get(typeDef));
    		}
    		else {
	    		outerCustomTypes = (LinkedHashMap<String,Object>)customType.get(typeDef);
	    		if(outerCustomTypes != null) {
    				if(namespacePrefix != null && !namespacePrefix.isEmpty()) {
    			    	LinkedHashMap<String,Object> prefixCustomTypes = new LinkedHashMap<String,Object>();
    	    			for(Map.Entry<String,Object> me: outerCustomTypes.entrySet()) {
    	    				String typeDefKey = me.getKey();
    	    				String nameSpacePrefixToKey = namespacePrefix + "." + typeDefKey;
    	    				prefixCustomTypes.put(nameSpacePrefixToKey, outerCustomTypes.get(typeDefKey));
    	    			}
    	    			customDefs.putAll(prefixCustomTypes);
    				}
    				else {
    	    			customDefs.putAll(outerCustomTypes);
    				}
	    		}
    		}
    	}
    }

    private void _updateNestedToscaTpls(String fullFileName,LinkedHashMap<String,Object> customTpl) {
    	if(fullFileName != null && customTpl != null) {
    		LinkedHashMap<String,Object> tt = new LinkedHashMap<String,Object>();
    		tt.put(fullFileName, customTpl);
    		nestedToscaTpls.add(tt);
    	}
    }

    private void _validateImportKeys(String importName, LinkedHashMap<String,Object> importUri) {
    	if(importUri.get(FILE) == null) {
            //log.warning(_('Missing keyname "file" in import "%(name)s".') % {'name': import_name})
    		ThreadLocalsHolder.getCollector().appendException(String.format(
    				"MissingRequiredFieldError: Import of template \"%s\" is missing field %s",importName,FILE));
    	}
    	for(String key: importUri.keySet()) {
    		boolean bFound = false;
    		for(String is: IMPORTS_SECTION) {
    			if(is.equals(key)) {
    				bFound = true;
    				break;
    			}
    		}
    		if(!bFound) {
                //log.warning(_('Unknown keyname "%(key)s" error in '
                //        'imported definition "%(def)s".')
                //      % {'key': key, 'def': import_name})
    			ThreadLocalsHolder.getCollector().appendException(String.format(
    					"UnknownFieldError: Import of template \"%s\" has unknown fiels %s",importName,key));
    		}
    	}
    }

    @SuppressWarnings("unchecked")
	private Object[] _loadImportTemplate(String importName, Object importUriDef) {
    	/*
        This method loads the custom type definitions referenced in "imports"
        section of the TOSCA YAML template by determining whether each import
        is specified via a file reference (by relative or absolute path) or a
        URL reference.

        Possibilities:
        +----------+--------+------------------------------+
        | template | import | comment                      |
        +----------+--------+------------------------------+
        | file     | file   | OK                           |
        | file     | URL    | OK                           |
        | preparsed| file   | file must be a full path     |
        | preparsed| URL    | OK                           |
        | URL      | file   | file must be a relative path |
        | URL      | URL    | OK                           |
        +----------+--------+------------------------------+
    	*/
    	Object al[] = new Object[2];

        boolean shortImportNotation = false;
        String fileName;
        String repository;
        if(importUriDef instanceof LinkedHashMap) {
            _validateImportKeys(importName, (LinkedHashMap<String,Object>)importUriDef);
            fileName = (String)((LinkedHashMap<String,Object>)importUriDef).get(FILE);
            repository = (String)((LinkedHashMap<String,Object>)importUriDef).get(REPOSITORY);
            if(repository != null) {
            	if(!repositories.keySet().contains(repository)) {
                    ThreadLocalsHolder.getCollector().appendException(String.format(
                    		"InvalidPropertyValueError: Repository \"%s\" not found in \"%s\"",
                    		repository,repositories.keySet().toString()));
            	}
            }
        }
        else {
            fileName = (String)importUriDef;
            repository = null;
            shortImportNotation = true;
        }

        if(fileName == null || fileName.isEmpty()) {
	        //msg = (_('A template file name is not provided with import '
	        //         'definition "%(import_name)s".')
	        //       % {'import_name': import_name})
	        //log.error(msg)
	        ThreadLocalsHolder.getCollector().appendException(String.format(
	        		"ValidationError: A template file name is not provided with import definition \"%s\"",importName));
	        al[0] = al[1] = null;
	        return al;
        }

        if(UrlUtils.validateUrl(fileName)) {
        	try {
	            al[0] = fileName;
        		InputStream input = new URL(fileName).openStream();
				Yaml yaml = new Yaml();
				al[1] = yaml.load(input);
	            return al;
        	}
        	catch(IOException e) {
    	        ThreadLocalsHolder.getCollector().appendException(String.format(
    	        		"ImportError: \"%s\" loading YAML import from \"%s\"",e.getClass().getSimpleName(),fileName));
    	        al[0] = al[1] = null;
    	        return al;
        	}
        }
        else if(repository == null || repository.isEmpty()) {
        	boolean aFile = false;
            String importTemplate = null;
            if(path != null && !path.isEmpty()) {
            	if(UrlUtils.validateUrl(path)) {
            		File fp = new File(path);
            		if(fp.isAbsolute()) {
            			String msg = String.format(
                            	"ImportError: Absolute file name \"%s\" cannot be used in the URL-based input template \"%s\"",
                            	fileName,path);
                        ThreadLocalsHolder.getCollector().appendException(msg);
                        al[0] = al[1] = null;
                        return al;
            		}
            		importTemplate = UrlUtils.joinUrl(path,fileName);
            		aFile = false;
            	}
            	else {
                
                    aFile = true;
            		File fp = new File(path);
            		if(fp.isFile()) {
                        File fn = new File(fileName);
            			if(fn.isFile()) {
                            importTemplate = fileName;
            			}
                        else {
                        	String fullPath = Paths.get(path).toAbsolutePath().getParent().toString() + File.separator + fileName;
                            File ffp = new File(fullPath);		
                            if(ffp.isFile()) {
                                importTemplate = fullPath;
                            }
                            else {
                            	String dirPath = Paths.get(path).toAbsolutePath().getParent().toString();
                            	String filePath;
                            	if(Paths.get(fileName).getParent() != null) {
                            		filePath = Paths.get(fileName).getParent().toString();
                            	}
                            	else {
                            		filePath = "";
                            	}
                            	if(!filePath.isEmpty() && dirPath.endsWith(filePath)) {
                            		String sFileName = Paths.get(fileName).getFileName().toString();
                            		importTemplate = dirPath + File.separator + sFileName; 
                            		File fit = new File(importTemplate);
                            		if(!fit.isFile()) {
                                        //msg = (_('"%(import_template)s" is'
                                        //        'not a valid file')
                                        //      % {'import_template':
                                        //         import_template})
                                        //log.error(msg)
                            			String msg = String.format(
                                        		"ValueError: \"%s\" is not a valid file",importTemplate);
                                        ThreadLocalsHolder.getCollector().appendException(msg);
                                        log.debug("ImportsLoader - _loadImportTemplate - {}", msg);
                            		}
                            	}
                            }
                        }
            		}
            	}	
            }
            else {  // template is pre-parsed
            	File fn = new File(fileName);
            	if(fn.isAbsolute() && fn.isFile()) {
            		aFile = true;
            		importTemplate = fileName;
            	}
            	else {
            		String msg = String.format(
            			"Relative file name \"%s\" cannot be used in a pre-parsed input template",fileName);
                   ThreadLocalsHolder.getCollector().appendException("ImportError: " + msg);
                   al[0] = al[1] = null;
                   return al;
            	}
        	}
            
            if(importTemplate == null || importTemplate.isEmpty()) {
                //log.error(_('Import "%(name)s" is not valid.') %
                //          {'name': import_uri_def})
                ThreadLocalsHolder.getCollector().appendException(String.format(
                		"ImportError: Import \"%s\" is not valid",importUriDef));
    	        al[0] = al[1] = null;
    	        return al;
            }
            
            // for now, this must be a file
            if(!aFile) {
            	log.error("ImportsLoader - _loadImportTemplate - Error!! Expected a file. importUriDef = {}, importTemplate = {}", importUriDef, importTemplate);
                ThreadLocalsHolder.getCollector().appendException(String.format(
                		"ImportError: Import \"%s\" is not a file",importName));
    	        al[0] = al[1] = null;
    	        return al;
            }
            try {
	            al[0] = importTemplate;
				InputStream input = new FileInputStream(new File(importTemplate));
				Yaml yaml = new Yaml();
				al[1] = yaml.load(input);
	            return al;
            }
            catch(FileNotFoundException e) {
                ThreadLocalsHolder.getCollector().appendException(String.format(
                		"ImportError: Failed to load YAML from \"%s\"",importName));
    	        al[0] = al[1] = null;
    	        return al;
            }
            catch(Exception e) {
                ThreadLocalsHolder.getCollector().appendException(String.format(
                		"ImportError: Exception from SnakeYAML file = \"%s\"",importName));
    	        al[0] = al[1] = null;
    	        return al;
            }
        }
            	
        if(shortImportNotation) {
            //log.error(_('Import "%(name)s" is not valid.') % import_uri_def)
            ThreadLocalsHolder.getCollector().appendException(String.format(
            		"ImportError: Import \"%s\" is not valid",importName));
	        al[0] = al[1] = null;
	        return al;
        }
        
        String fullUrl = "";
		String repoUrl = "";
        if(repository != null && !repository.isEmpty()) {
        	if(repositories != null) {
        		for(String repoName: repositories.keySet()) {
        			if(repoName.equals(repository)) {
	        			Object repoDef = repositories.get(repoName);
	        			if(repoDef instanceof String) {
	        				repoUrl = (String)repoDef;
	        			}
	        			else if(repoDef instanceof LinkedHashMap) {
	        				repoUrl = (String)((LinkedHashMap<String,Object>)repoDef).get("url");
	        			}
	                    // Remove leading, ending spaces and strip
	                    // the last character if "/"
	        			repoUrl = repoUrl.trim();
	        			if(repoUrl.endsWith("/")) {
	        				repoUrl = repoUrl.substring(0,repoUrl.length()-1);
	        			}
	        			fullUrl = repoUrl + "/" + fileName;
	        			break;
        			}
        		}
        	}
        	if(fullUrl.isEmpty()) {
                String msg = String.format(
                	"referenced repository \"%s\" in import definition \"%s\" not found",
                	repository,importName);
                ThreadLocalsHolder.getCollector().appendException("ImportError: " + msg);
   	        	al[0] = al[1] = null;
   	        	return al;
        	}
        }
        if(UrlUtils.validateUrl(fullUrl)) {
        	try {
	            al[0] = fullUrl;
        		InputStream input = new URL(fullUrl).openStream();
				Yaml yaml = new Yaml();
				al[1] = yaml.load(input);
	            return al;
        	}
        	catch(IOException e) {
    	        ThreadLocalsHolder.getCollector().appendException(String.format(
    	        		"ImportError: Exception loading YAML import from \"%s\"",fullUrl));
    	        al[0] = al[1] = null;
    	        return al;
        	}
        }
        else {
            String msg = String.format(
                	"repository URL \"%s\" in import definition \"%s\" is not valid",
                	repoUrl,importName);
               ThreadLocalsHolder.getCollector().appendException("ImportError: " + msg);
        }
          
        // if we got here something is wrong with the flow...
        log.error("ImportsLoader - _loadImportTemplate - got to dead end (importName {})", importName);
        ThreadLocalsHolder.getCollector().appendException(String.format(
        		"ImportError: _loadImportTemplate got to dead end (importName %s)\n",importName));
        al[0] = al[1] = null;
        return al;
    }

	@Override
	public String toString() {
		return "ImportsLoader{" +
				"IMPORTS_SECTION=" + Arrays.toString(IMPORTS_SECTION) +
				", importslist=" + importslist +
				", path='" + path + '\'' +
				", typeDefinitionList=" + typeDefinitionList +
				", customDefs=" + customDefs +
				", nestedToscaTpls=" + nestedToscaTpls +
				", repositories=" + repositories +
				'}';
	}
}

/*python

import logging
import os

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import InvalidPropertyValueError
from toscaparser.common.exception import MissingRequiredFieldError
from toscaparser.common.exception import UnknownFieldError
from toscaparser.common.exception import ValidationError
from toscaparser.elements.tosca_type_validation import TypeValidation
from toscaparser.utils.gettextutils import _
import org.openecomp.sdc.toscaparser.api.utils.urlutils
import org.openecomp.sdc.toscaparser.api.utils.yamlparser

YAML_LOADER = toscaparser.utils.yamlparser.load_yaml
log = logging.getLogger("tosca")


class ImportsLoader(object):

    IMPORTS_SECTION = (FILE, REPOSITORY, NAMESPACE_URI, NAMESPACE_PREFIX) = \
                      ('file', 'repository', 'namespace_uri',
                       'namespace_prefix')

    def __init__(self, importslist, path, type_definition_list=None,
                 tpl=None):
        self.importslist = importslist
        self.custom_defs = {}
        if not path and not tpl:
            msg = _('Input tosca template is not provided.')
            log.warning(msg)
            ExceptionCollector.appendException(ValidationError(message=msg))
        self.path = path
        self.repositories = {}
        if tpl and tpl.get('repositories'):
            self.repositories = tpl.get('repositories')
        self.type_definition_list = []
        if type_definition_list:
            if isinstance(type_definition_list, list):
                self.type_definition_list = type_definition_list
            else:
                self.type_definition_list.append(type_definition_list)
        self._validate_and_load_imports()

    def get_custom_defs(self):
        return self.custom_defs

    def _validate_and_load_imports(self):
        imports_names = set()

        if not self.importslist:
            msg = _('"imports" keyname is defined without including '
                    'templates.')
            log.error(msg)
            ExceptionCollector.appendException(ValidationError(message=msg))
            return

        for import_def in self.importslist:
            if isinstance(import_def, dict):
                for import_name, import_uri in import_def.items():
                    if import_name in imports_names:
                        msg = (_('Duplicate import name "%s" was found.') %
                               import_name)
                        log.error(msg)
                        ExceptionCollector.appendException(
                            ValidationError(message=msg))
                    imports_names.add(import_name)

                    custom_type = self._load_import_template(import_name,
                                                             import_uri)
                    namespace_prefix = None
                    if isinstance(import_uri, dict):
                        namespace_prefix = import_uri.get(
                            self.NAMESPACE_PREFIX)
                    if custom_type:
                        TypeValidation(custom_type, import_def)
                        self._update_custom_def(custom_type, namespace_prefix)
            else:  # old style of imports
                custom_type = self._load_import_template(None,
                                                         import_def)
                if custom_type:
                    TypeValidation(
                        custom_type, import_def)
                    self._update_custom_def(custom_type, None)

    def _update_custom_def(self, custom_type, namespace_prefix):
        outer_custom_types = {}
        for type_def in self.type_definition_list:
            outer_custom_types = custom_type.get(type_def)
            if outer_custom_types:
                if type_def == "imports":
                    self.custom_defs.update({'imports': outer_custom_types})
                else:
                    if namespace_prefix:
                        prefix_custom_types = {}
                        for type_def_key in outer_custom_types.keys():
                            namespace_prefix_to_key = (namespace_prefix +
                                                       "." + type_def_key)
                            prefix_custom_types[namespace_prefix_to_key] = \
                                outer_custom_types[type_def_key]
                        self.custom_defs.update(prefix_custom_types)
                    else:
                        self.custom_defs.update(outer_custom_types)

    def _validate_import_keys(self, import_name, import_uri_def):
        if self.FILE not in import_uri_def.keys():
            log.warning(_('Missing keyname "file" in import "%(name)s".')
                        % {'name': import_name})
            ExceptionCollector.appendException(
                MissingRequiredFieldError(
                    what='Import of template "%s"' % import_name,
                    required=self.FILE))
        for key in import_uri_def.keys():
            if key not in self.IMPORTS_SECTION:
                log.warning(_('Unknown keyname "%(key)s" error in '
                              'imported definition "%(def)s".')
                            % {'key': key, 'def': import_name})
                ExceptionCollector.appendException(
                    UnknownFieldError(
                        what='Import of template "%s"' % import_name,
                        field=key))

    def _load_import_template(self, import_name, import_uri_def):
        """Handle custom types defined in imported template files

        This method loads the custom type definitions referenced in "imports"
        section of the TOSCA YAML template by determining whether each import
        is specified via a file reference (by relative or absolute path) or a
        URL reference.

        Possibilities:
        +----------+--------+------------------------------+
        | template | import | comment                      |
        +----------+--------+------------------------------+
        | file     | file   | OK                           |
        | file     | URL    | OK                           |
        | preparsed| file   | file must be a full path     |
        | preparsed| URL    | OK                           |
        | URL      | file   | file must be a relative path |
        | URL      | URL    | OK                           |
        +----------+--------+------------------------------+
        """
        short_import_notation = False
        if isinstance(import_uri_def, dict):
            self._validate_import_keys(import_name, import_uri_def)
            file_name = import_uri_def.get(self.FILE)
            repository = import_uri_def.get(self.REPOSITORY)
            repos = self.repositories.keys()
            if repository is not None:
                if repository not in repos:
                    ExceptionCollector.appendException(
                        InvalidPropertyValueError(
                            what=_('Repository is not found in "%s"') % repos))
        else:
            file_name = import_uri_def
            repository = None
            short_import_notation = True

        if not file_name:
            msg = (_('A template file name is not provided with import '
                     'definition "%(import_name)s".')
                   % {'import_name': import_name})
            log.error(msg)
            ExceptionCollector.appendException(ValidationError(message=msg))
            return

        if toscaparser.utils.urlutils.UrlUtils.validate_url(file_name):
            return YAML_LOADER(file_name, False)
        elif not repository:
            import_template = None
            if self.path:
                if toscaparser.utils.urlutils.UrlUtils.validate_url(self.path):
                    if os.path.isabs(file_name):
                        msg = (_('Absolute file name "%(name)s" cannot be '
                                 'used in a URL-based input template '
                                 '"%(template)s".')
                               % {'name': file_name, 'template': self.path})
                        log.error(msg)
                        ExceptionCollector.appendException(ImportError(msg))
                        return
                    import_template = toscaparser.utils.urlutils.UrlUtils.\
                        join_url(self.path, file_name)
                    a_file = False
                else:
                    a_file = True
                    main_a_file = os.path.isfile(self.path)

                    if main_a_file:
                        if os.path.isfile(file_name):
                            import_template = file_name
                        else:
                            full_path = os.path.join(
                                os.path.dirname(os.path.abspath(self.path)),
                                file_name)
                            if os.path.isfile(full_path):
                                import_template = full_path
                            else:
                                file_path = file_name.rpartition("/")
                                dir_path = os.path.dirname(os.path.abspath(
                                    self.path))
                                if file_path[0] != '' and dir_path.endswith(
                                    file_path[0]):
                                        import_template = dir_path + "/" +\
                                            file_path[2]
                                        if not os.path.isfile(import_template):
                                            msg = (_('"%(import_template)s" is'
                                                     'not a valid file')
                                                   % {'import_template':
                                                      import_template})
                                            log.error(msg)
                                            ExceptionCollector.appendException
                                            (ValueError(msg))
            else:  # template is pre-parsed
                if os.path.isabs(file_name) and os.path.isfile(file_name):
                    a_file = True
                    import_template = file_name
                else:
                    msg = (_('Relative file name "%(name)s" cannot be used '
                             'in a pre-parsed input template.')
                           % {'name': file_name})
                    log.error(msg)
                    ExceptionCollector.appendException(ImportError(msg))
                    return

            if not import_template:
                log.error(_('Import "%(name)s" is not valid.') %
                          {'name': import_uri_def})
                ExceptionCollector.appendException(
                    ImportError(_('Import "%s" is not valid.') %
                                import_uri_def))
                return
            return YAML_LOADER(import_template, a_file)

        if short_import_notation:
            log.error(_('Import "%(name)s" is not valid.') % import_uri_def)
            ExceptionCollector.appendException(
                ImportError(_('Import "%s" is not valid.') % import_uri_def))
            return

        full_url = ""
        if repository:
            if self.repositories:
                for repo_name, repo_def in self.repositories.items():
                    if repo_name == repository:
                        # Remove leading, ending spaces and strip
                        # the last character if "/"
                        repo_url = ((repo_def['url']).strip()).rstrip("//")
                        full_url = repo_url + "/" + file_name

            if not full_url:
                msg = (_('referenced repository "%(n_uri)s" in import '
                         'definition "%(tpl)s" not found.')
                       % {'n_uri': repository, 'tpl': import_name})
                log.error(msg)
                ExceptionCollector.appendException(ImportError(msg))
                return

        if toscaparser.utils.urlutils.UrlUtils.validate_url(full_url):
            return YAML_LOADER(full_url, False)
        else:
            msg = (_('repository url "%(n_uri)s" is not valid in import '
                     'definition "%(tpl)s".')
                   % {'n_uri': repo_url, 'tpl': import_name})
            log.error(msg)
            ExceptionCollector.appendException(ImportError(msg))
*/