2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.parser.impl;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.InputStream;
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;
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;
31 final class TestUtils {
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);
44 for (int i = 0; i < fileList.length; i++) {
45 String fileName = fileList[i];
46 testFiles.add(new File(testDir, fileName));
48 return parser.parseYangModels(testFiles);
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));
57 Set<Module> modules = parser.parseYangModelsFromStreams(input);
58 for(InputStream stream : input) {
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);
70 return modules.iterator().next();
73 public static Module findModule(Set<Module> modules, String moduleName) {
75 for (Module module : modules) {
76 if (module.getName().equals(moduleName)) {
84 public static ModuleImport findImport(Set<ModuleImport> imports,
86 ModuleImport result = null;
87 for (ModuleImport moduleImport : imports) {
88 if (moduleImport.getPrefix().equals(prefix)) {
89 result = moduleImport;
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)) {
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));
114 return new SchemaPath(path, absolute);
117 public static Date createDate(String date) {
119 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
121 result = simpleDateFormat.parse(date);
122 } catch (ParseException e) {