Merge "BUG 2805 - Fixed Restconf RPC calls and POST to create data"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / md / sal / rest / common / TestRestconfUtils.java
1 /**
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.md.sal.rest.common;
10
11 import com.google.common.base.Preconditions;
12 import java.io.File;
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;
24
25 /**
26  * sal-rest-connector
27  * org.opendaylight.controller.md.sal.rest.common
28  *
29  *
30  *
31  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
32  *
33  * Created: Mar 7, 2015
34  */
35 public class TestRestconfUtils {
36
37     private static final Logger LOG = LoggerFactory.getLogger(TestRestconfUtils.class);
38
39     private final static YangContextParser parser = new YangParserImpl();
40
41     private TestRestconfUtils () {
42         throw new UnsupportedOperationException("Test utility class");
43     }
44
45     public static SchemaContext loadSchemaContext(final String yangPath, final SchemaContext schemaContext) {
46         try {
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);
51             } else {
52                 return addSchemaContext(yangPath, schemaContext);
53             }
54         }
55         catch (final Exception e) {
56             LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
57         }
58         return schemaContext;
59     }
60
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);
68         }
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));
73             }
74         }
75         return testFiles;
76     }
77
78     private static SchemaContext loadSchemaContext(final String resourceDirectory) throws IOException {
79         final Collection<File> testFiles = loadFiles(resourceDirectory);
80         return parser.parseFiles(testFiles);
81     }
82
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);
87     }
88 }