Merge "Custom Node Type jaxb string to id"
[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.List;
22 import java.util.Set;
23
24 import org.opendaylight.controller.yang.common.QName;
25 import org.opendaylight.controller.yang.model.api.Module;
26 import org.opendaylight.controller.yang.model.api.ModuleImport;
27 import org.opendaylight.controller.yang.model.api.SchemaPath;
28 import org.opendaylight.controller.yang.model.api.TypeDefinition;
29 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
30
31 final class TestUtils {
32
33     private TestUtils() {
34     }
35
36     public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
37         YangModelParser parser = new YangParserImpl();
38         final File testDir = new File(resourceDirectory);
39         final String[] fileList = testDir.list();
40         final List<File> testFiles = new ArrayList<File>();
41         if(fileList == null) {
42             throw new FileNotFoundException(resourceDirectory);
43         }
44         for (int i = 0; i < fileList.length; i++) {
45             String fileName = fileList[i];
46             testFiles.add(new File(testDir, fileName));
47         }
48         return parser.parseYangModels(testFiles);
49     }
50
51     public static Set<Module> loadModules(String... pathToYangFile) throws IOException {
52         YangModelParser parser = new YangParserImpl();
53         List<InputStream> input = new ArrayList<InputStream>();
54         for(String path : pathToYangFile) {
55             input.add(TestUtils.class.getResourceAsStream(path));
56         }
57         Set<Module> modules = parser.parseYangModelsFromStreams(input);
58         for(InputStream stream : input) {
59             stream.close();
60         }
61         return modules;
62     }
63
64     public static Module loadModule(String pathToYangFile) throws IOException {
65         YangModelParser parser = new YangParserImpl();
66         InputStream stream = TestUtils.class.getResourceAsStream(pathToYangFile);
67         List<InputStream> input = Collections.singletonList(stream);
68         Set<Module> modules = parser.parseYangModelsFromStreams(input);
69         stream.close();
70         return modules.iterator().next();
71     }
72
73     public static Module findModule(Set<Module> modules, String moduleName) {
74         Module result = null;
75         for (Module module : modules) {
76             if (module.getName().equals(moduleName)) {
77                 result = module;
78                 break;
79             }
80         }
81         return result;
82     }
83
84     public static ModuleImport findImport(Set<ModuleImport> imports,
85             String prefix) {
86         ModuleImport result = null;
87         for (ModuleImport moduleImport : imports) {
88             if (moduleImport.getPrefix().equals(prefix)) {
89                 result = moduleImport;
90                 break;
91             }
92         }
93         return result;
94     }
95
96     public static TypeDefinition<?> findTypedef(
97             Set<TypeDefinition<?>> typedefs, String name) {
98         TypeDefinition<?> result = null;
99         for (TypeDefinition<?> td : typedefs) {
100             if (td.getQName().getLocalName().equals(name)) {
101                 result = td;
102                 break;
103             }
104         }
105         return result;
106     }
107
108     public static SchemaPath createPath(boolean absolute, URI namespace,
109             Date revision, String prefix, String... names) {
110         List<QName> path = new ArrayList<QName>();
111         for (String name : names) {
112             path.add(new QName(namespace, revision, prefix, name));
113         }
114         return new SchemaPath(path, absolute);
115     }
116
117     public static Date createDate(String date) {
118         Date result;
119         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
120         try {
121             result = simpleDateFormat.parse(date);
122         } catch (ParseException e) {
123             result = null;
124         }
125         return result;
126     }
127
128 }