babd0d06f6a7bb217bf12de36b863be569921c56
[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.net.URI;
13 import java.text.DateFormat;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.opendaylight.controller.yang.common.QName;
22 import org.opendaylight.controller.yang.model.api.Module;
23 import org.opendaylight.controller.yang.model.api.ModuleImport;
24 import org.opendaylight.controller.yang.model.api.SchemaPath;
25 import org.opendaylight.controller.yang.model.api.TypeDefinition;
26 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
27
28 final class TestUtils {
29
30     private TestUtils() {
31     }
32
33     public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
34         YangModelParser parser = new YangParserImpl();
35         final File testDir = new File(resourceDirectory);
36         final String[] fileList = testDir.list();
37         final List<File> testFiles = new ArrayList<File>();
38         if(fileList == null) {
39             throw new FileNotFoundException(resourceDirectory);
40         }
41         for (int i = 0; i < fileList.length; i++) {
42             String fileName = fileList[i];
43             testFiles.add(new File(testDir, fileName));
44         }
45         return parser.parseYangModels(testFiles);
46     }
47
48     public static Module findModule(Set<Module> modules, String moduleName) {
49         Module result = null;
50         for (Module module : modules) {
51             if (module.getName().equals(moduleName)) {
52                 result = module;
53                 break;
54             }
55         }
56         return result;
57     }
58
59     public static ModuleImport findImport(Set<ModuleImport> imports,
60             String prefix) {
61         ModuleImport result = null;
62         for (ModuleImport moduleImport : imports) {
63             if (moduleImport.getPrefix().equals(prefix)) {
64                 result = moduleImport;
65                 break;
66             }
67         }
68         return result;
69     }
70
71     public static TypeDefinition<?> findTypedef(
72             Set<TypeDefinition<?>> typedefs, String name) {
73         TypeDefinition<?> result = null;
74         for (TypeDefinition<?> td : typedefs) {
75             if (td.getQName().getLocalName().equals(name)) {
76                 result = td;
77                 break;
78             }
79         }
80         return result;
81     }
82
83     public static SchemaPath createPath(boolean absolute, URI namespace,
84             Date revision, String prefix, String... names) {
85         List<QName> path = new ArrayList<QName>();
86         for (String name : names) {
87             path.add(new QName(namespace, revision, prefix, name));
88         }
89         return new SchemaPath(path, absolute);
90     }
91
92     public static Date createDate(String date) {
93         Date result;
94         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
95         try {
96             result = simpleDateFormat.parse(date);
97         } catch (ParseException e) {
98             result = null;
99         }
100         return result;
101     }
102
103 }