Added Method InstallRegisteredApps to CLI

Change-Id: Ib795c0c5ad4511ad138b32820038c6cf96aa926d
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationCommand.java
index 9b5e91f..67bd6ba 100644
--- a/cli/src/main/java/org/onosproject/cli/app/ApplicationCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationCommand.java
@@ -54,7 +54,7 @@
     protected VersionService versionService;
 
     @Argument(index = 0, name = "command",
-            description = "Command name (install|activate|deactivate|uninstall|download)",
+            description = "Command name (install|activate|deactivate|uninstall|download|installreg)",
             required = true)
     @Completion(ApplicationCommandCompleter.class)
     String command = null;
@@ -90,24 +90,31 @@
 
     // Installs the application from input of the specified URL
     private boolean installApp(ApplicationAdminService service, String url) {
+
         try {
             if ("-".equals(url)) {
                 service.install(System.in);
+            } else if (url.contains("oar")) {
+                service.install(new URL(url).openStream());
             } else {
                 Set<Application> app = get(ApplicationService.class)
-                        .getRegisteredApplications().stream()
-                        .filter(ra -> ra.id().toString().equals(url))
-                        .collect(Collectors.toSet());
+                        .getRegisteredApplications();
                 if (app.isEmpty()) {
-                    service.install(new URL(url).openStream());
+                    System.out.println("Could Not Install " + url);
+                    return false;
                 }
                 Iterator<Application> iterator = app.iterator();
+                Application recent = null;
                 while (iterator.hasNext()) {
                     Application application = iterator.next();
-                    if (application.version().toString().equals(versionService.version().toString())) {
-                        service.install(application.imageUrl().openStream());
+                    if (recent == null && application.id().name().equals(url)) {
+                        recent = application;
+                    } else if (application.version().compareTo(recent.version()) > 0 &&
+                            application.id().name().equals(url)) {
+                        recent = application;
                     }
                 }
+                service.install(recent.imageUrl().openStream());
             }
         } catch (IOException e) {
             error("Unable to get URL: %s", url);
diff --git a/core/net/src/main/java/org/onosproject/app/impl/ApplicationManager.java b/core/net/src/main/java/org/onosproject/app/impl/ApplicationManager.java
index 7ea40d4..c7e28bd 100644
--- a/core/net/src/main/java/org/onosproject/app/impl/ApplicationManager.java
+++ b/core/net/src/main/java/org/onosproject/app/impl/ApplicationManager.java
@@ -336,12 +336,13 @@
     public Set<Application> getRegisteredApplications() {
         ImmutableSet.Builder<Application> builder = ImmutableSet.builder();
         ObjectMapper mapper = new ObjectMapper();
-
+        String vers = versionService.version().toString();
+        vers = vers.substring(0, vers.lastIndexOf(".") + 2);
         // Get input stream from the URL
         try {
-            URL githubUrl = new URL(APP_REGISTRY_URL + "?onosVersion=" + versionService.version().toString());
-            HttpURLConnection githubHttp = (HttpURLConnection) githubUrl.openConnection();
-            InputStream githubStream = githubHttp.getInputStream();
+            URL serverUrl = new URL(APP_REGISTRY_URL + "?onosVersion=" + vers);
+            HttpURLConnection serverHttp = (HttpURLConnection) serverUrl.openConnection();
+            InputStream githubStream = serverHttp.getInputStream();
 
             // Read input stream into an ArrayNode
             ArrayNode rootTree = (ArrayNode) mapper.readTree(githubStream);
@@ -356,9 +357,9 @@
 
             return apps;
         } catch (MalformedURLException e) {
-            throw new IllegalStateException("Bad URL " + APP_REGISTRY_URL, e);
+            throw new IllegalStateException("Bad URL: " + APP_REGISTRY_URL + "?onosVersion=" + vers, e);
         } catch (IOException e) {
-            throw new IllegalStateException("Unable to fetch URL " + APP_REGISTRY_URL, e);
+            throw new IllegalStateException("Unable to fetch URL: " + APP_REGISTRY_URL + "?onosVersion=" + vers, e);
         }
     }