Adding CLI command to list models and enhancing YANG smoke scenario
to validate that expected model is registered.

Change-Id: I4db10fcdb0fe5ad922b0312b10ba1e23387b7792
diff --git a/apps/yang/BUCK b/apps/yang/BUCK
index 6bc5f06..06a5eb7 100644
--- a/apps/yang/BUCK
+++ b/apps/yang/BUCK
@@ -1,5 +1,10 @@
 COMPILE_DEPS = [
   '//lib:CORE_DEPS',
+  '//core/api:onos-api',
+  '//core/common:onos-core-common',
+  '//cli:onos-cli',
+  '//lib:org.apache.karaf.shell.console',
+  '//lib:jackson-core',
   '//lib:onos-yang-model',
   '//lib:onos-yang-compiler-api',
   '//lib:onos-yang-runtime',
diff --git a/apps/yang/src/main/java/org/onosproject/yang/YangModelsListCommand.java b/apps/yang/src/main/java/org/onosproject/yang/YangModelsListCommand.java
new file mode 100644
index 0000000..297b5b0
--- /dev/null
+++ b/apps/yang/src/main/java/org/onosproject/yang/YangModelsListCommand.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.yang;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.yang.model.YangModel;
+import org.onosproject.yang.model.YangModule;
+import org.onosproject.yang.runtime.YangModelRegistry;
+
+/**
+ * Lists registered YANG models.
+ */
+@Command(scope = "onos", name = "models",
+         description = "Lists registered YANG models")
+public class YangModelsListCommand extends AbstractShellCommand {
+
+
+    private static final String FORMAT = "moduleName=%s moduleRevision=%s modelId=%s";
+    private static final String MODULE_NAME = "moduleName";
+    private static final String MODULE_REVISION = "moduleRevision";
+
+    @Override
+    protected void execute() {
+        YangModelRegistry service = get(YangModelRegistry.class);
+
+        if (outputJson()) {
+            print("%s", json(service));
+        } else {
+            for (YangModel model: service.getModels()) {
+                for (YangModule module : model.getYangModules()) {
+                    print(FORMAT, module.getYangModuleId().moduleName(),
+                          module.getYangModuleId().revision(),
+                          modelId(model));
+                }
+            }
+        }
+    }
+
+    // Produces JSON structure.
+    private JsonNode json(YangModelRegistry service) {
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode result = mapper.createObjectNode();
+        for (YangModel model: service.getModels()) {
+            ArrayNode modelNode = mapper.createArrayNode();
+            result.set(modelId(model), modelNode);
+            for (YangModule module : model.getYangModules()) {
+                ObjectNode moduleNode = mapper.createObjectNode();
+                modelNode.add(moduleNode);
+                moduleNode.put(MODULE_NAME, module.getYangModuleId().moduleName());
+                moduleNode.put(MODULE_REVISION, module.getYangModuleId().revision());
+            }
+        }
+        return result;
+    }
+
+    private String modelId(YangModel m) {
+        return "YM" + Math.abs(m.hashCode());
+    }
+}
diff --git a/apps/yang/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/yang/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..2127c91
--- /dev/null
+++ b/apps/yang/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,22 @@
+<!--
+  ~ Copyright 2017-present Open Networking Laboratory
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+        <command>
+            <action class="org.onosproject.yang.YangModelsListCommand"/>
+        </command>
+    </command-bundle>
+</blueprint>
diff --git a/tools/test/scenarios/yang-smoke.xml b/tools/test/scenarios/yang-smoke.xml
index c55a0ba..b44646d 100644
--- a/tools/test/scenarios/yang-smoke.xml
+++ b/tools/test/scenarios/yang-smoke.xml
@@ -24,5 +24,7 @@
                       exec="onos-check-apps ${#} drivers,yang,yang-gui,models.openroadm includes"/>
             </parallel>
         </group>
+        <step name="Verify-Models" exec="onos ${OC1} models | grep org-openroadm-interfaces"
+              requires="Check-YANG-Apps-Activated"/>
     </group>
 </scenario>