ONOS-1896 Modify Application Subsystem to support Security-Mode ONOS

Change-Id: Ie3686e0d5071f9f6e946bc48ed7562bb2f5ec413
diff --git a/core/common/src/main/java/org/onosproject/common/app/ApplicationArchive.java b/core/common/src/main/java/org/onosproject/common/app/ApplicationArchive.java
index f8359fd..2954d07 100644
--- a/core/common/src/main/java/org/onosproject/common/app/ApplicationArchive.java
+++ b/core/common/src/main/java/org/onosproject/common/app/ApplicationArchive.java
@@ -27,6 +27,8 @@
 import org.onosproject.app.ApplicationException;
 import org.onosproject.app.ApplicationStoreDelegate;
 import org.onosproject.app.DefaultApplicationDescription;
+import org.onosproject.core.ApplicationRole;
+import org.onosproject.core.DefaultPermission;
 import org.onosproject.core.Permission;
 import org.onosproject.core.Version;
 import org.onosproject.store.AbstractStore;
@@ -42,7 +44,9 @@
 import java.net.URI;
 import java.nio.charset.Charset;
 import java.nio.file.NoSuchFileException;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Set;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
@@ -74,6 +78,9 @@
     private static final String FEATURES = "[@features]";
     private static final String DESCRIPTION = "description";
 
+    private static final String ROLE = "security.role";
+    private static final String PERMISSIONS = "security.permissions.permission";
+
     private static final String OAR = ".oar";
     private static final String APP_XML = "app.xml";
     private static final String M2_PREFIX = "m2";
@@ -267,12 +274,13 @@
         Version version = Version.version(cfg.getString(VERSION));
         String desc = cfg.getString(DESCRIPTION);
         String origin = cfg.getString(ORIGIN);
-        Set<Permission> perms = ImmutableSet.of();
+        ApplicationRole role = getRole(cfg.getString(ROLE));
+        Set<Permission> perms = getPermissions(cfg);
         String featRepo = cfg.getString(FEATURES_REPO);
         URI featuresRepo = featRepo != null ? URI.create(featRepo) : null;
         List<String> features = ImmutableList.copyOf(cfg.getStringArray(FEATURES));
 
-        return new DefaultApplicationDescription(name, version, desc, origin,
+        return new DefaultApplicationDescription(name, version, desc, origin, role,
                                                  perms, featuresRepo, features);
     }
 
@@ -368,4 +376,34 @@
         return new File(new File(appsDir, appName), fileName);
     }
 
+    // Returns the set of Permissions specified in the app.xml file
+    private ImmutableSet<Permission> getPermissions(XMLConfiguration cfg) {
+        List<Permission> perms = new ArrayList();
+        for (Object o : cfg.getList(PERMISSIONS)) {
+            DefaultPermission perm = null;
+            if (o != null) {
+                String permStr = (String) o;
+                perm = new DefaultPermission(permStr);
+            }
+            if (perm != null) {
+                perms.add(perm);
+            }
+        }
+
+        return ImmutableSet.copyOf(perms);
+    }
+
+    // Returns application role type
+    public ApplicationRole getRole(String value) {
+        if (value == null) {
+            return ApplicationRole.UNSPECIFIED;
+        } else {
+            try {
+                return ApplicationRole.valueOf(value.toUpperCase(Locale.ENGLISH));
+            } catch (IllegalArgumentException e) {
+                log.debug("Unknown role value: %s", value);
+                return ApplicationRole.UNSPECIFIED;
+            }
+        }
+    }
 }
diff --git a/core/common/src/test/java/org/onosproject/common/app/ApplicationArchiveTest.java b/core/common/src/test/java/org/onosproject/common/app/ApplicationArchiveTest.java
index e1c7bfc..be10053 100644
--- a/core/common/src/test/java/org/onosproject/common/app/ApplicationArchiveTest.java
+++ b/core/common/src/test/java/org/onosproject/common/app/ApplicationArchiveTest.java
@@ -56,6 +56,7 @@
         assertEquals("incorrect name", APP_NAME, app.name());
         assertEquals("incorrect version", VER, app.version());
         assertEquals("incorrect origin", ORIGIN, app.origin());
+        assertEquals("incorrect role", ROLE, app.role());
 
         assertEquals("incorrect description", DESC, app.description());
         assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
diff --git a/core/common/src/test/resources/org/onosproject/common/app/app.xml b/core/common/src/test/resources/org/onosproject/common/app/app.xml
index 39f328b..4a8d0f7 100644
--- a/core/common/src/test/resources/org/onosproject/common/app/app.xml
+++ b/core/common/src/test/resources/org/onosproject/common/app/app.xml
@@ -18,4 +18,11 @@
         featuresRepo="mvn:org.foo-features/1.2a/xml/features"
         features="foo,bar">
     <description>Awesome application from Circus, Inc.</description>
+    <security>
+        <role>ADMIN</role>
+        <permissions>
+            <permission>FLOWRULE_WRITE</permission>
+            <permission>FLOWRULE_READ</permission>
+        </permissions>
+    </security>
 </app>
diff --git a/core/common/src/test/resources/org/onosproject/common/app/app.zip b/core/common/src/test/resources/org/onosproject/common/app/app.zip
index 07705e7..0313f50 100644
--- a/core/common/src/test/resources/org/onosproject/common/app/app.zip
+++ b/core/common/src/test/resources/org/onosproject/common/app/app.zip
Binary files differ