Fixed parsing of typedef statement. Refactored YangParserImpl to improve code readabi...
[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(List<InputStream> input) throws IOException {
53         final YangModelParser parser = new YangParserImpl();
54         final Set<Module> modules = new HashSet<Module>(
55                 parser.parseYangModelsFromStreams(input));
56         for(InputStream stream : input) {
57             stream.close();
58         }
59         return modules;
60     }
61
62     public static Module loadModule(final InputStream stream) throws
63             IOException {
64         final YangModelParser parser = new YangParserImpl();
65         final List<InputStream> input = Collections.singletonList(stream);
66         final Set<Module> modules = new HashSet<Module>(
67                 parser.parseYangModelsFromStreams(input));
68         stream.close();
69         return modules.iterator().next();
70     }
71
72     public static Module findModule(Set<Module> modules, String moduleName) {
73         Module result = null;
74         for (Module module : modules) {
75             if (module.getName().equals(moduleName)) {
76                 result = module;
77                 break;
78             }
79         }
80         return result;
81     }
82
83     public static ModuleImport findImport(Set<ModuleImport> imports,
84             String prefix) {
85         ModuleImport result = null;
86         for (ModuleImport moduleImport : imports) {
87             if (moduleImport.getPrefix().equals(prefix)) {
88                 result = moduleImport;
89                 break;
90             }
91         }
92         return result;
93     }
94
95     public static TypeDefinition<?> findTypedef(
96             Set<TypeDefinition<?>> typedefs, String name) {
97         TypeDefinition<?> result = null;
98         for (TypeDefinition<?> td : typedefs) {
99             if (td.getQName().getLocalName().equals(name)) {
100                 result = td;
101                 break;
102             }
103         }
104         return result;
105     }
106
107     public static SchemaPath createPath(boolean absolute, URI namespace,
108             Date revision, String prefix, String... names) {
109         List<QName> path = new ArrayList<QName>();
110         for (String name : names) {
111             path.add(new QName(namespace, revision, prefix, name));
112         }
113         return new SchemaPath(path, absolute);
114     }
115
116     public static Date createDate(String date) {
117         Date result;
118         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
119         try {
120             result = simpleDateFormat.parse(date);
121         } catch (ParseException e) {
122             result = null;
123         }
124         return result;
125     }
126
127 }