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.HashSet;
22 import java.util.List;
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.SchemaContext;
29 import org.opendaylight.controller.yang.model.api.SchemaPath;
30 import org.opendaylight.controller.yang.model.api.TypeDefinition;
31 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
33 final class TestUtils {
38 public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
39 YangModelParser parser = new YangParserImpl();
40 final File testDir = new File(resourceDirectory);
41 final String[] fileList = testDir.list();
42 final List<File> testFiles = new ArrayList<File>();
43 if(fileList == null) {
44 throw new FileNotFoundException(resourceDirectory);
46 for (int i = 0; i < fileList.length; i++) {
47 String fileName = fileList[i];
48 testFiles.add(new File(testDir, fileName));
50 return parser.parseYangModels(testFiles);
53 public static Set<Module> loadModules(List<InputStream> input) throws IOException {
54 final YangModelParser parser = new YangParserImpl();
55 final Set<Module> modules = new HashSet<Module>(
56 parser.parseYangModelsFromStreams(input));
57 for(InputStream stream : input) {
63 public static Module loadModule(final InputStream stream) throws
65 final YangModelParser parser = new YangParserImpl();
66 final List<InputStream> input = Collections.singletonList(stream);
67 final Set<Module> modules = new HashSet<Module>(
68 parser.parseYangModelsFromStreams(input));
70 return modules.iterator().next();
73 public static Module loadModuleWithContext(final InputStream stream, final SchemaContext context) throws IOException {
74 final YangModelParser parser = new YangParserImpl();
75 final List<InputStream> input = Collections.singletonList(stream);
76 final Set<Module> modules = new HashSet<Module>(parser.parseYangModelsFromStreams(input, context));
78 return modules.iterator().next();
81 public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context) throws IOException {
82 final YangModelParser parser = new YangParserImpl();
83 final Set<Module> modules = new HashSet<Module>(parser.parseYangModelsFromStreams(input, context));
84 for(InputStream is : input) {
92 public static Module findModule(Set<Module> modules, String moduleName) {
94 for (Module module : modules) {
95 if (module.getName().equals(moduleName)) {
103 public static ModuleImport findImport(Set<ModuleImport> imports,
105 ModuleImport result = null;
106 for (ModuleImport moduleImport : imports) {
107 if (moduleImport.getPrefix().equals(prefix)) {
108 result = moduleImport;
115 public static TypeDefinition<?> findTypedef(
116 Set<TypeDefinition<?>> typedefs, String name) {
117 TypeDefinition<?> result = null;
118 for (TypeDefinition<?> td : typedefs) {
119 if (td.getQName().getLocalName().equals(name)) {
127 public static SchemaPath createPath(boolean absolute, URI namespace,
128 Date revision, String prefix, String... names) {
129 List<QName> path = new ArrayList<QName>();
130 for (String name : names) {
131 path.add(new QName(namespace, revision, prefix, name));
133 return new SchemaPath(path, absolute);
136 public static Date createDate(String date) {
138 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
140 result = simpleDateFormat.parse(date);
141 } catch (ParseException e) {