2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.md.sal.rest.common;
11 import com.google.common.base.Preconditions;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
21 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * org.opendaylight.controller.md.sal.rest.common
31 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
33 * Created: Mar 7, 2015
35 public class TestRestconfUtils {
37 private static final Logger LOG = LoggerFactory.getLogger(TestRestconfUtils.class);
39 private final static YangContextParser parser = new YangParserImpl();
41 private TestRestconfUtils () {
42 throw new UnsupportedOperationException("Test utility class");
45 public static SchemaContext loadSchemaContext(final String yangPath, final SchemaContext schemaContext) {
47 Preconditions.checkArgument(yangPath != null, "Path can not be null.");
48 Preconditions.checkArgument(( ! yangPath.isEmpty()), "Path can not be empty.");
49 if (schemaContext == null) {
50 return loadSchemaContext(yangPath);
52 return addSchemaContext(yangPath, schemaContext);
55 catch (final Exception e) {
56 LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
61 private static Collection<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
62 final String path = TestRestconfUtils.class.getResource(resourceDirectory).getPath();
63 final File testDir = new File(path);
64 final String[] fileList = testDir.list();
65 final List<File> testFiles = new ArrayList<File>();
66 if (fileList == null) {
67 throw new FileNotFoundException(resourceDirectory);
69 for (int i = 0; i < fileList.length; i++) {
70 final String fileName = fileList[i];
71 if (new File(testDir, fileName).isDirectory() == false) {
72 testFiles.add(new File(testDir, fileName));
78 private static SchemaContext loadSchemaContext(final String resourceDirectory) throws IOException {
79 final Collection<File> testFiles = loadFiles(resourceDirectory);
80 return parser.parseFiles(testFiles);
83 private static SchemaContext addSchemaContext(final String resourceDirectory,
84 final SchemaContext schemaContext) throws IOException, YangSyntaxErrorException {
85 final Collection<File> testFiles = loadFiles(resourceDirectory);
86 return parser.parseFiles(testFiles, schemaContext);