When a node is going down, remove edges in both directions associated with the node.
[controller.git] / opendaylight / sal / yang-prototype / yang / 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.io.IOException;
13 import java.io.InputStream;
14 import java.net.URI;
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;
23 import java.util.Set;
24
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;
32
33 final class TestUtils {
34
35     private TestUtils() {
36     }
37
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);
45         }
46         for (int i = 0; i < fileList.length; i++) {
47             String fileName = fileList[i];
48             testFiles.add(new File(testDir, fileName));
49         }
50         return parser.parseYangModels(testFiles);
51     }
52
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) {
58             stream.close();
59         }
60         return modules;
61     }
62
63     public static Module loadModule(final InputStream stream) throws
64             IOException {
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));
69         stream.close();
70         return modules.iterator().next();
71     }
72
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));
77         stream.close();
78         return modules.iterator().next();
79     }
80
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) {
85             if(is != null) {
86                 is.close();
87             }
88         }
89         return modules;
90     }
91
92     public static Module findModule(Set<Module> modules, String moduleName) {
93         Module result = null;
94         for (Module module : modules) {
95             if (module.getName().equals(moduleName)) {
96                 result = module;
97                 break;
98             }
99         }
100         return result;
101     }
102
103     public static ModuleImport findImport(Set<ModuleImport> imports,
104             String prefix) {
105         ModuleImport result = null;
106         for (ModuleImport moduleImport : imports) {
107             if (moduleImport.getPrefix().equals(prefix)) {
108                 result = moduleImport;
109                 break;
110             }
111         }
112         return result;
113     }
114
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)) {
120                 result = td;
121                 break;
122             }
123         }
124         return result;
125     }
126
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));
132         }
133         return new SchemaPath(path, absolute);
134     }
135
136     public static Date createDate(String date) {
137         Date result;
138         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
139         try {
140             result = simpleDateFormat.parse(date);
141         } catch (ParseException e) {
142             result = null;
143         }
144         return result;
145     }
146
147 }