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;
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;
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;
28 final class TestUtils {
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);
41 for (int i = 0; i < fileList.length; i++) {
42 String fileName = fileList[i];
43 testFiles.add(new File(testDir, fileName));
45 return parser.parseYangModels(testFiles);
48 public static Module findModule(Set<Module> modules, String moduleName) {
50 for (Module module : modules) {
51 if (module.getName().equals(moduleName)) {
59 public static ModuleImport findImport(Set<ModuleImport> imports,
61 ModuleImport result = null;
62 for (ModuleImport moduleImport : imports) {
63 if (moduleImport.getPrefix().equals(prefix)) {
64 result = moduleImport;
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)) {
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));
89 return new SchemaPath(path, absolute);
92 public static Date createDate(String date) {
94 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
96 result = simpleDateFormat.parse(date);
97 } catch (ParseException e) {