Changhoon Yoon
Committed by Gerrit Code Review

ONOS-1896 : enum Permission added, DefaultPermission removed

Change-Id: Ie75313acc9adeaee9c5a55978b5ec8e8fb5bf9b6
/*
* Copyright 2015 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.core;
import java.security.BasicPermission;
/**
* Default implementation of ONOS application permissions for API-level access control.
*/
public class DefaultPermission extends BasicPermission implements Permission {
public enum Type {
APP_READ,
APP_EVENT,
CONFIG_READ,
CONFIG_WRITE,
CLUSTER_READ,
CLUSTER_WRITE,
CLUSTER_EVENT,
DEVICE_READ,
DEVICE_EVENT,
DRIVER_READ,
DRIVER_WRITE,
FLOWRULE_READ,
FLOWRULE_WRITE,
FLOWRULE_EVENT,
GROUP_READ,
GROUP_WRITE,
GROUP_EVENT,
HOST_READ,
HOST_WRITE,
HOST_EVENT,
INTENT_READ,
INTENT_WRITE,
INTENT_EVENT,
LINK_READ,
LINK_WRITE,
LINK_EVENT,
PACKET_READ,
PACKET_WRITE,
PACKET_EVENT,
STATISTIC_READ,
TOPOLOGY_READ,
TOPOLOGY_EVENT,
TUNNEL_READ,
TUNNEL_WRITE,
TUNNEL_EVENT,
STORAGE_WRITE
}
/**
* Creates a new DefaultPermission.
* @param name name of the permission
* @param actions optional action field
*/
public DefaultPermission(String name, String actions) {
super(name, actions);
}
/**
* Creates a new DefaultPermission.
* @param name name of the permission
*/
public DefaultPermission(String name) {
super(name, "");
}
public DefaultPermission(Type permtype) {
super(permtype.name(), "");
}
@Override
public String name() {
return super.getName();
}
@Override
public String actions() {
return super.getActions();
}
}
......@@ -18,17 +18,41 @@ package org.onosproject.core;
/**
* Representation of an application permission.
*/
public interface Permission {
/**
* Returns the name of the permission.
* @return a string value
*/
String name();
/**
* Returns the actions string of the permission if specified.
* @return a string value
*/
String actions();
public enum Permission {
APP_READ,
APP_EVENT,
CONFIG_READ,
CONFIG_WRITE,
CLUSTER_READ,
CLUSTER_WRITE,
CLUSTER_EVENT,
DEVICE_READ,
DEVICE_EVENT,
DRIVER_READ,
DRIVER_WRITE,
FLOWRULE_READ,
FLOWRULE_WRITE,
FLOWRULE_EVENT,
GROUP_READ,
GROUP_WRITE,
GROUP_EVENT,
HOST_READ,
HOST_WRITE,
HOST_EVENT,
INTENT_READ,
INTENT_WRITE,
INTENT_EVENT,
LINK_READ,
LINK_WRITE,
LINK_EVENT,
PACKET_READ,
PACKET_WRITE,
PACKET_EVENT,
STATISTIC_READ,
TOPOLOGY_READ,
TOPOLOGY_EVENT,
TUNNEL_READ,
TUNNEL_WRITE,
TUNNEL_EVENT,
STORAGE_WRITE
}
......
......@@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.onosproject.core.ApplicationRole;
import org.onosproject.core.DefaultPermission;
import org.onosproject.core.Permission;
import org.onosproject.core.Version;
......@@ -29,8 +28,6 @@ import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.onosproject.core.DefaultPermission.Type.FLOWRULE_WRITE;
import static org.onosproject.core.DefaultPermission.Type.FLOWRULE_READ;
/**
......@@ -43,8 +40,7 @@ public class DefaultApplicationDescriptionTest {
public static final String DESC = "Awesome application from Circus";
public static final String ORIGIN = "Circus";
public static final ApplicationRole ROLE = ApplicationRole.ADMIN;
public static final Set<Permission> PERMS = ImmutableSet.of(new DefaultPermission(FLOWRULE_WRITE),
new DefaultPermission(FLOWRULE_READ));
public static final Set<Permission> PERMS = ImmutableSet.of(Permission.FLOWRULE_WRITE, Permission.FLOWRULE_READ);
public static final URI FURL = URI.create("mvn:org.foo-features/1.2a/xml/features");
public static final List<String> FEATURES = ImmutableList.of("foo", "bar");
......
......@@ -28,7 +28,6 @@ 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;
......@@ -378,21 +377,20 @@ public class ApplicationArchive
// Returns the set of Permissions specified in the app.xml file
private ImmutableSet<Permission> getPermissions(XMLConfiguration cfg) {
List<Permission> perms = new ArrayList();
List<Permission> permissionList = 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);
String name = (String) o;
try {
Permission perm = Permission.valueOf(name);
permissionList.add(perm);
} catch (IllegalArgumentException e) {
log.debug("Unknown permission specified: %s", name);
}
}
return ImmutableSet.copyOf(perms);
return ImmutableSet.copyOf(permissionList);
}
//
// Returns application role type
public ApplicationRole getRole(String value) {
if (value == null) {
......
......@@ -25,11 +25,9 @@ import org.onosproject.common.app.ApplicationArchive;
import org.onosproject.core.Application;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.Permission;
import org.onosproject.core.DefaultPermission;
import org.onosproject.core.ApplicationIdStoreAdapter;
import org.onosproject.core.DefaultApplicationId;
import static org.onosproject.core.DefaultPermission.Type.FLOWRULE_WRITE;
import static org.junit.Assert.assertEquals;
import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
......@@ -106,7 +104,7 @@ public class SimpleApplicationStoreTest {
@Test
public void permissions() {
Application app = createTestApp();
ImmutableSet<Permission> permissions = ImmutableSet.of(new DefaultPermission(FLOWRULE_WRITE));
ImmutableSet<Permission> permissions = ImmutableSet.of(Permission.FLOWRULE_WRITE);
store.setPermissions(app.id(), permissions);
assertEquals("incorrect app perms", 1, store.getPermissions(app.id()).size());
assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
......