/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.yang.parser.impl; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.ModuleImport; import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; final class TestUtils { private TestUtils() { } public static Set loadModules(String resourceDirectory) throws FileNotFoundException { YangModelParser parser = new YangParserImpl(); final File testDir = new File(resourceDirectory); final String[] fileList = testDir.list(); final List testFiles = new ArrayList(); if(fileList == null) { throw new FileNotFoundException(resourceDirectory); } for (int i = 0; i < fileList.length; i++) { String fileName = fileList[i]; testFiles.add(new File(testDir, fileName)); } return parser.parseYangModels(testFiles); } public static Set loadModules(List input) throws IOException { final YangModelParser parser = new YangParserImpl(); final Set modules = new HashSet( parser.parseYangModelsFromStreams(input)); for(InputStream stream : input) { stream.close(); } return modules; } public static Module loadModule(final InputStream stream) throws IOException { final YangModelParser parser = new YangParserImpl(); final List input = Collections.singletonList(stream); final Set modules = new HashSet( parser.parseYangModelsFromStreams(input)); stream.close(); return modules.iterator().next(); } public static Module findModule(Set modules, String moduleName) { Module result = null; for (Module module : modules) { if (module.getName().equals(moduleName)) { result = module; break; } } return result; } public static ModuleImport findImport(Set imports, String prefix) { ModuleImport result = null; for (ModuleImport moduleImport : imports) { if (moduleImport.getPrefix().equals(prefix)) { result = moduleImport; break; } } return result; } public static TypeDefinition findTypedef( Set> typedefs, String name) { TypeDefinition result = null; for (TypeDefinition td : typedefs) { if (td.getQName().getLocalName().equals(name)) { result = td; break; } } return result; } public static SchemaPath createPath(boolean absolute, URI namespace, Date revision, String prefix, String... names) { List path = new ArrayList(); for (String name : names) { path.add(new QName(namespace, revision, prefix, name)); } return new SchemaPath(path, absolute); } public static Date createDate(String date) { Date result; final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { result = simpleDateFormat.parse(date); } catch (ParseException e) { result = null; } return result; } }