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.SchemaPath;
29 import org.opendaylight.controller.yang.model.api.TypeDefinition;
30 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
32 final class TestUtils {
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);
45 for (int i = 0; i < fileList.length; i++) {
46 String fileName = fileList[i];
47 testFiles.add(new File(testDir, fileName));
49 return parser.parseYangModels(testFiles);
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) {
62 public static Module loadModule(final InputStream stream) throws
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));
69 return modules.iterator().next();
72 public static Module findModule(Set<Module> modules, String moduleName) {
74 for (Module module : modules) {
75 if (module.getName().equals(moduleName)) {
83 public static ModuleImport findImport(Set<ModuleImport> imports,
85 ModuleImport result = null;
86 for (ModuleImport moduleImport : imports) {
87 if (moduleImport.getPrefix().equals(prefix)) {
88 result = moduleImport;
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)) {
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));
113 return new SchemaPath(path, absolute);
116 public static Date createDate(String date) {
118 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
120 result = simpleDateFormat.parse(date);
121 } catch (ParseException e) {