Yang-maven-plugin refactored + fixed bugs.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / parser / impl / TestUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.yang.parser.impl;
9
10 import java.io.File;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.URI;
15 import java.text.DateFormat;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.opendaylight.controller.yang.common.QName;
26 import org.opendaylight.controller.yang.model.api.Module;
27 import org.opendaylight.controller.yang.model.api.ModuleImport;
28 import org.opendaylight.controller.yang.model.api.SchemaPath;
29 import org.opendaylight.controller.yang.model.api.TypeDefinition;
30 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
31
32 final class TestUtils {
33
34     private TestUtils() {
35     }
36
37     public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
38         YangModelParser parser = new YangParserImpl();
39         final File testDir = new File(resourceDirectory);
40         final String[] fileList = testDir.list();
41         final List<File> testFiles = new ArrayList<File>();
42         if(fileList == null) {
43             throw new FileNotFoundException(resourceDirectory);
44         }
45         for (int i = 0; i < fileList.length; i++) {
46             String fileName = fileList[i];
47             testFiles.add(new File(testDir, fileName));
48         }
49         return parser.parseYangModels(testFiles);
50     }
51
52     public static Set<Module> loadModules(String... pathToYangFile) throws IOException {
53         YangModelParser parser = new YangParserImpl();
54         List<InputStream> input = new ArrayList<InputStream>();
55         for(String path : pathToYangFile) {
56             input.add(TestUtils.class.getResourceAsStream(path));
57         }
58         Set<Module> modules = new HashSet<Module>(
59                 parser.parseYangModelsFromStreams(input));
60         for(InputStream stream : input) {
61             stream.close();
62         }
63         return modules;
64     }
65
66     public static Module loadModule(String pathToYangFile) throws IOException {
67         YangModelParser parser = new YangParserImpl();
68         InputStream stream = TestUtils.class.getResourceAsStream(pathToYangFile);
69         List<InputStream> input = Collections.singletonList(stream);
70         Set<Module> modules = new HashSet<Module>(
71                 parser.parseYangModelsFromStreams(input));
72         stream.close();
73         return modules.iterator().next();
74     }
75
76     public static Module findModule(Set<Module> modules, String moduleName) {
77         Module result = null;
78         for (Module module : modules) {
79             if (module.getName().equals(moduleName)) {
80                 result = module;
81                 break;
82             }
83         }
84         return result;
85     }
86
87     public static ModuleImport findImport(Set<ModuleImport> imports,
88             String prefix) {
89         ModuleImport result = null;
90         for (ModuleImport moduleImport : imports) {
91             if (moduleImport.getPrefix().equals(prefix)) {
92                 result = moduleImport;
93                 break;
94             }
95         }
96         return result;
97     }
98
99     public static TypeDefinition<?> findTypedef(
100             Set<TypeDefinition<?>> typedefs, String name) {
101         TypeDefinition<?> result = null;
102         for (TypeDefinition<?> td : typedefs) {
103             if (td.getQName().getLocalName().equals(name)) {
104                 result = td;
105                 break;
106             }
107         }
108         return result;
109     }
110
111     public static SchemaPath createPath(boolean absolute, URI namespace,
112             Date revision, String prefix, String... names) {
113         List<QName> path = new ArrayList<QName>();
114         for (String name : names) {
115             path.add(new QName(namespace, revision, prefix, name));
116         }
117         return new SchemaPath(path, absolute);
118     }
119
120     public static Date createDate(String date) {
121         Date result;
122         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
123         try {
124             result = simpleDateFormat.parse(date);
125         } catch (ParseException e) {
126             result = null;
127         }
128         return result;
129     }
130
131 }