forked from community/device-mgt-core
parent
12a2e1f11f
commit
44b496d350
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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.wso2.carbon.policy.mgt.core;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
|
||||
import org.wso2.carbon.policy.mgt.common.*;
|
||||
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
|
||||
import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest {
|
||||
|
||||
private static final String ANDROID = "android";
|
||||
private static final Log log = LogFactory.getLog(PolicyEvaluationTestCase.class);
|
||||
|
||||
|
||||
@BeforeClass
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
|
||||
PolicyEvaluationPoint evaluationPoint = new SimplePolicyEvaluationTest();
|
||||
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(evaluationPoint);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void activatePolicies() throws PolicyManagementException {
|
||||
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
|
||||
PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP();
|
||||
List<Policy> policies = policyManagerService.getPolicies(ANDROID);
|
||||
|
||||
for (Policy policy : policies) {
|
||||
log.debug("Policy status : " + policy.getPolicyName() + " - " + policy.isActive() + " - " + policy
|
||||
.isUpdated());
|
||||
|
||||
if (!policy.isActive()) {
|
||||
administratorPoint.activatePolicy(policy.getId());
|
||||
}
|
||||
}
|
||||
administratorPoint.publishChanges();
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("activatePolicies"))
|
||||
public void getEffectivePolicy() throws DeviceManagementException, PolicyEvaluationException {
|
||||
|
||||
log.debug("Getting effective policy for device started ..........");
|
||||
|
||||
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
|
||||
List<Device> devices = service.getAllDevices(ANDROID);
|
||||
|
||||
PolicyEvaluationPoint evaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint();
|
||||
|
||||
for (Device device : devices) {
|
||||
DeviceIdentifier identifier = new DeviceIdentifier();
|
||||
identifier.setType(device.getType());
|
||||
identifier.setId(device.getDeviceIdentifier());
|
||||
Policy policy = evaluationPoint.getEffectivePolicy(identifier);
|
||||
|
||||
if (policy != null) {
|
||||
log.debug("Name of the policy applied to device is " + policy.getPolicyName());
|
||||
} else {
|
||||
log.debug("No policy is applied to device.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test(dependsOnMethods = ("getEffectivePolicy"))
|
||||
public void updatePriorities() throws PolicyManagementException {
|
||||
|
||||
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
|
||||
PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP();
|
||||
|
||||
List<Policy> policies = administratorPoint.getPolicies();
|
||||
|
||||
log.debug("Re-enforcing policy started....");
|
||||
|
||||
int sixe = policies.size();
|
||||
|
||||
sortPolicies(policies);
|
||||
int x = 0;
|
||||
for (Policy policy : policies) {
|
||||
policy.setPriorityId(sixe - x);
|
||||
x++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
administratorPoint.updatePolicyPriorities(policies);
|
||||
administratorPoint.publishChanges();
|
||||
}
|
||||
|
||||
public void sortPolicies(List<Policy> policyList) {
|
||||
Collections.sort(policyList);
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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.wso2.carbon.policy.mgt.core.services;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.policy.mgt.common.*;
|
||||
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
|
||||
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SimplePolicyEvaluationTest.class);
|
||||
|
||||
@Override
|
||||
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
|
||||
Policy policy = new Policy();
|
||||
|
||||
List<Policy> policyList;
|
||||
PolicyAdministratorPoint policyAdministratorPoint;
|
||||
PolicyInformationPoint policyInformationPoint;
|
||||
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
|
||||
|
||||
try {
|
||||
if (policyManagerService != null) {
|
||||
|
||||
policyInformationPoint = policyManagerService.getPIP();
|
||||
PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier);
|
||||
policyList = policyInformationPoint.getRelatedPolicies(pipDevice);
|
||||
|
||||
for(Policy pol : policyList) {
|
||||
log.debug("Policy used in evaluation - Name : " + pol.getPolicyName() );
|
||||
}
|
||||
|
||||
sortPolicies(policyList);
|
||||
if(!policyList.isEmpty()) {
|
||||
policy = policyList.get(0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
policyAdministratorPoint = policyManagerService.getPAP();
|
||||
policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy);
|
||||
|
||||
}
|
||||
|
||||
} catch (PolicyManagementException e) {
|
||||
String msg = "Error occurred when retrieving the policy related data from policy management service.";
|
||||
log.error(msg, e);
|
||||
throw new PolicyEvaluationException(msg, e);
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void sortPolicies(List<Policy> policyList) throws PolicyEvaluationException {
|
||||
Collections.sort(policyList);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue