Merge "- remove TLS/SSL support from netconf server and client - refactoring related...
authorEd Warnicke <eaw@cisco.com>
Tue, 3 Dec 2013 13:31:44 +0000 (13:31 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 3 Dec 2013 13:31:44 +0000 (13:31 +0000)
30 files changed:
opendaylight/config/pom.xml
opendaylight/config/yang-test-plugin/pom.xml [new file with mode: 0644]
opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/DeleteSources.java [new file with mode: 0644]
opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java [new file with mode: 0644]
opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/Util.java [new file with mode: 0644]
opendaylight/config/yang-test-plugin/src/main/resources/Header.txt [new file with mode: 0644]
opendaylight/config/yang-test/pom.xml
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModule.java
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModuleFactory.java
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModule.java
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleFactory.java
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleUtil.java
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModule.java
opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModuleFactory.java
opendaylight/md-sal/sal-rest-connector/pom.xml
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identity-module.yang [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identityref-module.yang [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/cont-augment-module.yang [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/main-module.yang [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml [new file with mode: 0644]

index c9ed19dacb2c0256f811025e2be01409b09e5bbc..675e5cf80da4366771cd52f12fd2ec06f7fc3e2f 100644 (file)
@@ -39,6 +39,7 @@
         <module>netty-event-executor-config</module>
         <module>netty-timer-config</module>
         <module>config-persister-directory-adapter</module>
+        <module>yang-test-plugin</module>
     </modules>
 
     <profiles>
diff --git a/opendaylight/config/yang-test-plugin/pom.xml b/opendaylight/config/yang-test-plugin/pom.xml
new file mode 100644 (file)
index 0000000..30ac855
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>org.opendaylight.controller</groupId>
+        <artifactId>config-plugin-parent</artifactId>
+        <version>0.2.3-SNAPSHOT</version>
+        <relativePath>../config-plugin-parent</relativePath>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <packaging>maven-plugin</packaging>
+    <artifactId>yang-test-plugin</artifactId>
+
+    <description>Remove generated source files, after new files generation, implementation is inserted.</description>
+    <name>${project.artifactId}</name>
+    <prerequisites>
+        <maven>3.0.4</maven>
+    </prerequisites>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-plugin-api</artifactId>
+            <version>3.0.5</version>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/DeleteSources.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/DeleteSources.java
new file mode 100644 (file)
index 0000000..4610662
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * 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.config.yang.test.plugin;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+import java.io.File;
+
+/**
+ * Delete all Module/ModuleFactory sources
+ *
+ * @goal delete-sources
+ *
+ * @phase initialize
+ */
+public class DeleteSources extends AbstractMojo{
+    /**
+     * @parameter expression="${project.build.sourceDirectory}"
+     * @readOnly
+     * @required
+     */
+    private File directory;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        if (directory == null || !directory.exists()) {
+            super.getLog().error("Directory does not exists.");
+        }
+        File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl"));
+        if (sourceDirectory == null || !sourceDirectory.exists()) {
+            super.getLog().error("Source directory does not exists " + sourceDirectory.getPath());
+        }
+        File[] sourceFiles = sourceDirectory.listFiles();
+        for (File sourceFile: sourceFiles) {
+            if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
+                super.getLog().debug("Source file deleted: " + sourceFile.getName());
+                sourceFile.delete();
+            }
+        }
+    }
+}
diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java
new file mode 100644 (file)
index 0000000..dbb9ddb
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * 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.config.yang.test.plugin;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+
+/**
+ * Add implementation code from stub.txt
+ *
+ * @goal process-sources
+ *
+ * @phase process-sources
+ *
+ */
+public class ProcessSources extends AbstractMojo{
+    /**
+     * @parameter expression="${project.build.sourceDirectory}"
+     * @readOnly
+     * @required
+     */
+    private File directory;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        if (directory == null || !directory.exists()) {
+            super.getLog().error("Directory does not exists.");
+        }
+        File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl"));
+        if (!sourceDirectory.exists()) {
+            super.getLog().error("Source directory does not exists " + sourceDirectory.getPath());
+        }
+        String header = "";
+        try {
+            header = Util.loadHeader();
+        } catch (IOException e) {
+           super.getLog().error("Header.txt not found.");
+        }
+        File[] sourceFiles = sourceDirectory.listFiles();
+        for (File sourceFile: sourceFiles) {
+            if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
+                File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt"));
+                String stubLines = null;
+                try {
+                    if (stubFile.exists()) {
+                        stubLines = Util.loadStubFile(stubFile.getPath());
+                    }
+
+                    InputStream javaIn = new FileInputStream(sourceFile.getPath());
+                    BufferedReader javaBuf = new BufferedReader(new InputStreamReader(javaIn));
+                    StringBuffer output = new StringBuffer();
+                    String line = javaBuf.readLine();
+                    boolean writeLine = false;
+                    while ((line = javaBuf.readLine()) != null) {
+                        if(!writeLine && line.contains("*/")) {
+                            line = header;
+                            writeLine = true;
+                        } else {
+                            if (line.contains("TODO")) {
+                                writeLine = false;
+                            } else {
+                                if (stubLines != null && line.contains("throw new")) {
+                                    line = stubLines.toString();
+                                    writeLine = true;
+                                }
+                            }
+                        }
+                        if(writeLine) {
+                            output.append(line).append(System.lineSeparator());
+                        }
+                    }
+                    javaBuf.close();
+
+                    OutputStream javaOut = new FileOutputStream(sourceFile.getPath());
+                    javaOut.write(output.toString().getBytes());
+                    javaOut.close();
+                } catch (IOException e) {
+                    getLog().error("Error while reading/writing to files.", e);
+                }
+
+            }
+        }
+    }
+}
diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/Util.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/Util.java
new file mode 100644 (file)
index 0000000..16a41d9
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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.config.yang.test.plugin;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.regex.Matcher;
+
+public class Util {
+
+    public static String replaceDots(String path) {
+        path = path.replace(".", Matcher.quoteReplacement(File.separator));
+        return path;
+    }
+
+    public static String loadHeader() throws IOException {
+        StringBuffer header = new StringBuffer();
+        InputStream headIn = Util.class.getClassLoader().getResourceAsStream("Header.txt");
+        BufferedReader headBuf = new BufferedReader(new InputStreamReader(headIn));
+        String line = null;
+        while ((line = headBuf.readLine()) != null) {
+            header.append(line).append(System.lineSeparator());
+        }
+        headBuf.close();
+        return header.toString();
+    }
+
+    public static String loadStubFile(String fileName) throws IOException {
+        InputStream stubIn = new FileInputStream(fileName);
+        BufferedReader stubBuf = new BufferedReader(new InputStreamReader(stubIn));
+
+        StringBuffer stubLines = new StringBuffer();
+        String stubLine = null;
+        while ((stubLine = stubBuf.readLine()) != null) {
+            stubLines.append(stubLine).append(System.lineSeparator());
+        }
+        stubBuf.close();
+        return stubLines.toString();
+    }
+}
diff --git a/opendaylight/config/yang-test-plugin/src/main/resources/Header.txt b/opendaylight/config/yang-test-plugin/src/main/resources/Header.txt
new file mode 100644 (file)
index 0000000..068fd26
--- /dev/null
@@ -0,0 +1,7 @@
+/*
+ * 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
+ */
\ No newline at end of file
index e3737c78f34d584e4771dc8df99b466d1897a828..149348df4b2234e7c54c49f59fd7f7118e9d0c91 100644 (file)
                     </dependency>
                 </dependencies>
             </plugin>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-test-plugin</artifactId>
+                <version>${config.version}</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>delete-sources</goal>
+                            <goal>process-sources</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 </project>
index 1122c1ffa46a16078623850314bb4cee3fd4aef0..c64f824b150f07c6f2179a6b8ab65909d9774454 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * 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.config.yang.test.impl;
 
 /**
@@ -28,5 +36,6 @@ public final class DepTestImplModule extends org.opendaylight.controller.config.
             public void close() throws Exception {
             }
         };
+
     }
 }
index 4152736768ca9ce12d089cb1fb039a536b712c47..c26c29ed60636f94b8dc73405cbaf7ecc948aa6b 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * 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.config.yang.test.impl;
 
 /**
index 7e1848dd6a7f20f6692d5dc5f5cfefd49b587190..3594ee03538f4f2bb097f319cba13a679bc39960 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * 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.config.yang.test.impl;
 
 /**
@@ -23,6 +31,7 @@ public final class NetconfTestImplModule extends org.opendaylight.controller.con
 
     @Override
     public java.lang.AutoCloseable createInstance() {
-        return NetconfTestImplModuleUtil.registerRuntimeBeans(this);
+return NetconfTestImplModuleUtil.registerRuntimeBeans(this);
+
     }
 }
index 7cab5288680b9f2cfe454d3630c1e92f3b29d3bc..accc1db76e72cf5292a8ece54cc31fb71644ead4 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * 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.config.yang.test.impl;
 
 /**
index 58943c9df2d8db2f2bc336a9ca2e335027894efa..4de980433753e4165f66dec8fe2eb7081073ddd5 100644 (file)
@@ -1,10 +1,9 @@
-/**
- * @author Tomas Olvecky
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
- * 11 2013
- *
- * Copyright (c) 2013 by Cisco Systems, Inc.
- * 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.config.yang.test.impl;
 
index 52a71620bf0062f929e2496dac32d094f66d7157..9ba1db4eedc7b690499704ff6da29cb7275438d5 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * 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.config.yang.test.impl;
 
 /**
@@ -28,5 +36,6 @@ public final class TestImplModule extends org.opendaylight.controller.config.yan
             public void close() throws Exception {
             }
         };
+
     }
 }
index ce9aa92b64ea0f4c61911b0f969c71ed048bac21..a6ce734f96ac83fe14c31d24b4b7e1e1b2094c5c 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * 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.config.yang.test.impl;
 
 /**
index bdcb0bdd2bce239d65eac72f83432c1cff036a0e..3e13a584d4b602a8f1a725ad06f491fb08c5897e 100644 (file)
       <version>2.4</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+       <groupId>org.opendaylight.yangtools</groupId>
+       <artifactId>yang-model-util</artifactId>
+       <version>0.5.9-SNAPSHOT</version>
+    </dependency>
   </dependencies>
 
   <build>
index 9608d65e41ca82a13e2dada78b5ce5dfeb0728e7..a42c468c2af27ea5f2cb84887757ed8057a4466f 100644 (file)
@@ -8,6 +8,7 @@ import java.util.*;
 import javax.activation.UnsupportedDataTypeException;
 
 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.*;
 import org.opendaylight.yangtools.yang.model.api.*;
 import org.opendaylight.yangtools.yang.model.api.type.*;
@@ -144,25 +145,43 @@ class JsonMapper {
 
         List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
         for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
-            writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
+            writeValueOfNodeByType(writer, nodeLeafList, schema.getType(), schema);
         }
-
         writer.endArray();
     }
 
     private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
         writeName(node, schema, writer);
-        writeValueOfNodeByType(writer, node, schema.getType());
+        writeValueOfNodeByType(writer, node, schema.getType(), schema);
     }
 
-    private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
-            throws IOException {
+    private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type,
+            DataSchemaNode schema) throws IOException {
 
         String value = String.valueOf(node.getValue());
-        // TODO check Leafref, InstanceIdentifierTypeDefinition,
-        // IdentityrefTypeDefinition, UnionTypeDefinition
         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
-        if (baseType instanceof InstanceIdentifierTypeDefinition) {
+
+        // TODO check InstanceIdentifierTypeDefinition,
+        // IdentityrefTypeDefinition
+        if (baseType instanceof IdentityrefTypeDefinition) {
+            if (node.getValue() instanceof QName) {
+                QName qName = (QName) node.getValue();
+
+                ControllerContext contContext = ControllerContext.getInstance();
+                String moduleName = contContext.findModuleByNamespace(qName.getNamespace());
+
+                writer.value(moduleName + ":" + qName.getLocalName());
+            }
+
+        } else if (baseType instanceof LeafrefTypeDefinition) {
+            ControllerContext contContext = ControllerContext.getInstance();
+            LeafSchemaNode lfSchemaNode = contContext.resolveTypeFromLeafref((LeafrefTypeDefinition) baseType, schema);
+            if (lfSchemaNode != null) {
+                writeValueOfNodeByType(writer, node, lfSchemaNode.getType(), lfSchemaNode);
+            } else {
+                writer.value(value);
+            }
+        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
             writer.value(((InstanceIdentifierTypeDefinition) baseType).getPathStatement().toString());
         } else if (baseType instanceof UnionTypeDefinition) {
             processTypeIsUnionType(writer, (UnionTypeDefinition) baseType, value);
index eca4bd257298b78706cbd10321e8484ea06cab87..470b4735a441ad7b5f3e8f421c53fea904d033d3 100644 (file)
@@ -27,6 +27,9 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
 import org.opendaylight.yangtools.yang.model.api.SchemaContext
+import org.opendaylight.yangtools.yang.model.api.SchemaNode
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil
 
 import static com.google.common.base.Preconditions.*
 
@@ -40,8 +43,7 @@ class ControllerContext implements SchemaServiceListener {
 
     private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
     private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
-    private val Map<QName,RpcDefinition> qnameToRpc = new ConcurrentHashMap();
-    
+    private val Map<QName, RpcDefinition> qnameToRpc = new ConcurrentHashMap();
 
     private new() {
         if (INSTANCE !== null) {
@@ -52,7 +54,7 @@ class ControllerContext implements SchemaServiceListener {
     static def getInstance() {
         return INSTANCE
     }
-    
+
     private def void checkPreconditions() {
         if (schemas === null) {
             throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
@@ -131,7 +133,7 @@ class ControllerContext implements SchemaServiceListener {
     private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
         throw new IllegalArgumentException("Conversion of generic path argument is not supported");
     }
-    
+
     def findModuleByNamespace(URI namespace) {
         checkPreconditions
         var module = uriToModuleName.get(namespace)
@@ -311,24 +313,55 @@ class ControllerContext implements SchemaServiceListener {
             return str;
         }
     }
-    
+
     private def QName toQName(String name) {
         val module = name.toModuleName;
         val node = name.toNodeName;
         val namespace = moduleNameToUri.get(module);
-        return new QName(namespace,null,node);
+        return new QName(namespace, null, node);
     }
-    
+
     def getRpcDefinition(String name) {
         return qnameToRpc.get(name.toQName)
     }
 
     override onGlobalContextUpdated(SchemaContext context) {
         this.schemas = context;
-        for(operation : context.operations) {
-            val qname = new QName(operation.QName.namespace,null,operation.QName.localName);
-            qnameToRpc.put(qname,operation);
+        for (operation : context.operations) {
+            val qname = new QName(operation.QName.namespace, null, operation.QName.localName);
+            qnameToRpc.put(qname, operation);
+        }
+    }
+
+    /**
+     * Resolve target type from leafref type.
+     * 
+     * According to RFC 6020 referenced element has to be leaf (chapter 9.9).
+     * Therefore if other element is referenced then null value is returned.
+     * 
+     * Currently only cases without path-predicate are supported.
+     * 
+     * @param leafRef
+     * @param schemaNode
+     *            data schema node which contains reference
+     * @return type if leaf is referenced and it is possible to find referenced
+     *         node in schema context. In other cases null value is returned
+     */
+    def LeafSchemaNode resolveTypeFromLeafref(LeafrefTypeDefinition leafRef, DataSchemaNode schemaNode) {
+        val xPath = leafRef.getPathStatement();
+        val module = SchemaContextUtil.findParentModule(schemas, schemaNode);
+
+        var SchemaNode foundSchemaNode
+        if (xPath.isAbsolute()) {
+            foundSchemaNode = SchemaContextUtil.findDataSchemaNode(schemas, module, xPath);
+        } else {
+            foundSchemaNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemas, module, schemaNode, xPath);
+        }
+
+        if (foundSchemaNode instanceof LeafSchemaNode) {
+            return foundSchemaNode as LeafSchemaNode;
         }
+
+        return null;
     }
-    
 }
index 89d24ad0579b9b78a77a68f121d8f13ca75ef399..61942e81a6e5918689cb4e77d1c9758b94b85911 100644 (file)
@@ -211,10 +211,12 @@ final class TestUtils {
                 null, byteArrayOS);
 
         jsonResult = byteArrayOS.toString();
-        try {
-            outputToFile(byteArrayOS, outputPath);
-        } catch (IOException e) {
-            System.out.println("Output file wasn't cloased sucessfuly.");
+        if (outputPath != null) {
+            try {            
+                outputToFile(byteArrayOS, outputPath);
+            } catch (IOException e) {
+                System.out.println("Output file wasn't cloased sucessfuly.");
+            }
         }
 
         return jsonResult;
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java
new file mode 100644 (file)
index 0000000..c9ac042
--- /dev/null
@@ -0,0 +1,61 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.regex.*;
+
+import javax.ws.rs.WebApplicationException;
+
+import org.junit.*;
+import org.opendaylight.yangtools.yang.data.api.*;
+import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
+import org.opendaylight.yangtools.yang.model.api.*;
+
+public class ToJsonIdentityrefTest {
+
+    private static Set<Module> modules;
+    private static DataSchemaNode dataSchemaNode;
+
+    @BeforeClass
+    public static void initialization() {
+        modules = TestUtils.resolveModules("/yang-to-json-conversion/identityref");
+        assertEquals(2, modules.size());
+        Module module = TestUtils.resolveModule("identityref-module", modules);
+        assertNotNull(module);
+        dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont");
+        assertNotNull(dataSchemaNode);
+
+    }
+
+    @Test
+    public void identityrefToJsonTest() {
+        String json = null;
+        try {
+            json = TestUtils
+                    .writeCompNodeWithSchemaContextToJson(prepareCompositeNode(), null, modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        Pattern ptrn = Pattern.compile(".*\"lf1\"\\p{Space}*:\\p{Space}*\"identityref-module:name_test\".*",
+                Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+
+        assertTrue(mtch.matches());
+    }
+
+    private CompositeNode prepareCompositeNode() {
+        MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
+                ModifyAction.CREATE, null);
+        MutableSimpleNode<?> lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1"), cont,
+                TestUtils.buildQName("name_test", "identityref:module", "2013-12-2"), ModifyAction.CREATE, null);
+        cont.getChildren().add(lf1);
+        cont.init();
+
+        return cont;
+    }
+
+}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java
new file mode 100644 (file)
index 0000000..a5cc02e
--- /dev/null
@@ -0,0 +1,158 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.regex.Matcher;
+
+import javax.ws.rs.WebApplicationException;
+
+import org.junit.*;
+import org.opendaylight.yangtools.yang.model.api.*;
+
+public class ToJsonLeafrefType {
+    private static Set<Module> modules;
+    private static DataSchemaNode dataSchemaNode;
+
+    @BeforeClass
+    public static void initialization() {
+        modules = TestUtils.resolveModules("/yang-to-json-conversion/leafref");
+        assertEquals(2, modules.size());
+        Module module = TestUtils.resolveModule("main-module", modules);
+        assertNotNull(module);
+        dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont");
+        assertNotNull(dataSchemaNode);
+
+    }
+
+    @Test
+    public void leafrefAbsolutePathToExistingLeafTest() {
+        String json = null;
+        try {
+            json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
+                    .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml"),
+                    "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf3\":\\p{Blank}*true.*",
+                java.util.regex.Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+        assertTrue(mtch.matches());
+    }
+
+    @Test
+    public void leafrefRelativePathToExistingLeafTest() {
+        String json = null;
+        try {
+            json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
+                    .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml"),
+                    "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf2\":\\p{Blank}*121.*",
+                java.util.regex.Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+        assertTrue(mtch.matches());
+    }
+
+    /**
+     * Tests case when reference to not existing element is present. In this
+     * case value from single node is printed as string.
+     */
+    @Test
+    public void leafrefToNonExistingLeafTest() {
+        String json = null;
+        try {
+            json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
+                    .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml"),
+                    "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf5\":\\p{Blank}*\"137\".*",
+                java.util.regex.Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+        assertTrue(mtch.matches());
+    }
+
+    /**
+     * Tests case when non leaf element is referenced. In this case value from
+     * single node is printed as string.
+     */
+    @Test
+    public void leafrefToNotLeafTest() {
+        String json = null;
+        try {
+            json = TestUtils.writeCompNodeWithSchemaContextToJson(
+                    TestUtils.loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml"),
+                    "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(
+                ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*",
+                java.util.regex.Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+        assertTrue(mtch.matches());
+    }
+
+    /**
+     * Tests case when leaflist element is refers to leaf.
+     */
+    @Test
+    public void leafrefFromLeafListToLeafTest() {
+        String json = null;
+        try {
+            json = TestUtils
+                    .writeCompNodeWithSchemaContextToJson(
+                            TestUtils
+                                    .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"),
+                            "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        java.util.regex.Pattern ptrn = java.util.regex.Pattern
+                .compile(
+                        ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*345,\\p{Space}*346,\\p{Space}*347.*",
+                        java.util.regex.Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+        assertTrue(mtch.matches());
+    }
+
+    /**
+     * Tests case when leaflist element is refers to leaf.
+     */
+    @Test
+    public void leafrefFromLeafrefToLeafrefTest() {
+        String json = null;
+        try {
+            json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
+                    .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml"),
+                    "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
+        } catch (WebApplicationException | IOException e) {
+            // shouldn't end here
+            assertTrue(false);
+        }
+        assertNotNull(json);
+        java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(
+                ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*200.*", java.util.regex.Pattern.DOTALL);
+        Matcher mtch = ptrn.matcher(json);
+        assertTrue(mtch.matches());
+    }
+
+}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identity-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identity-module.yang
new file mode 100644 (file)
index 0000000..30890bf
--- /dev/null
@@ -0,0 +1,10 @@
+module identity-module {
+  namespace "identity:module";  
+
+  prefix "idemod";
+  revision 2013-12-2 {    
+  }
+  
+       identity iden {         
+       }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identityref-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identityref-module.yang
new file mode 100644 (file)
index 0000000..273f5d7
--- /dev/null
@@ -0,0 +1,19 @@
+module identityref-module {
+  namespace "identityref:module";  
+
+  prefix "iderefmod";
+  
+  import identity-module {prefix idemo; revision-date 2013-12-2;}
+   
+  revision 2013-12-2 {    
+  }
+  
+       container cont {
+               leaf lf1 {
+                       type identityref {
+                               base "idemo:iden";
+                       }
+               }
+       }
+         
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/cont-augment-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/cont-augment-module.yang
new file mode 100644 (file)
index 0000000..afc23b7
--- /dev/null
@@ -0,0 +1,42 @@
+module cont-augment-module {
+  namespace "cont:augment:module";  
+
+  prefix "cntaugmod";
+  
+  import main-module {prefix mamo; revision-date 2013-12-2;}
+         
+  revision 2013-12-2 {
+  
+  }
+  
+  augment "/mamo:cont" {
+       leaf-list lflst1 {
+               type leafref {
+                       path "../lf1";
+               }
+       }       
+       
+       leaf lf4 {
+               type leafref {
+                       path "../lf1";
+               }
+       }
+       
+       /* reference to not leaf element */
+       leaf lf6 {
+               type leafref {
+                       path "../lflst1";
+               }
+       }
+       
+       leaf lf7 {
+               type leafref {
+                       path "../lf4";
+               }
+       }
+  }
+       
+
+         
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/main-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/main-module.yang
new file mode 100644 (file)
index 0000000..8c80392
--- /dev/null
@@ -0,0 +1,43 @@
+module main-module {
+  namespace "main:module";  
+
+  prefix "mainmod";
+  revision 2013-12-2 {    
+  }
+  
+       container cont {
+               leaf lf1 {
+                       type uint32;
+               }
+               
+               container cont1 {
+                       leaf lf11 {
+                               type boolean;
+                       }
+               }
+               
+               leaf lf2 {
+                       type leafref {
+                               path "../lf1";
+                       }
+               }
+               
+               leaf lf3 {
+                       type leafref {
+                               path "/cont/cont1/lf11";
+                       }
+               }
+               
+               /* reference to nonexisting leaf */
+               leaf lf5 {
+                       type leafref {
+                               path "/cont/lf";
+                       }
+               }
+               
+               
+       }
+       
+
+         
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml
new file mode 100644 (file)
index 0000000..bbebabe
--- /dev/null
@@ -0,0 +1,6 @@
+<cont>
+       <cont1>
+               <lf11>true</lf11>
+       </cont1>
+       <lf3>true</lf3>
+</cont>
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml
new file mode 100644 (file)
index 0000000..1bb9013
--- /dev/null
@@ -0,0 +1,3 @@
+<cont>
+       <lf7>200</lf7>
+</cont>
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml
new file mode 100644 (file)
index 0000000..dfea9c8
--- /dev/null
@@ -0,0 +1,3 @@
+<cont>
+       <lf5>137</lf5>
+</cont>
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml
new file mode 100644 (file)
index 0000000..7b05b0c
--- /dev/null
@@ -0,0 +1,3 @@
+<cont>
+       <lf6>44.33</lf6>
+</cont>
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml
new file mode 100644 (file)
index 0000000..0d8cf66
--- /dev/null
@@ -0,0 +1,5 @@
+<cont>
+       <lflst1>345</lflst1>
+       <lflst1>346</lflst1>
+       <lflst1>347</lflst1>
+</cont>
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml
new file mode 100644 (file)
index 0000000..4a73a8b
--- /dev/null
@@ -0,0 +1,4 @@
+<cont>
+       <lf1>121</lf1>
+       <lf2>121</lf2>
+</cont>
\ No newline at end of file