forked from community/device-mgt-core
parent
21bc812281
commit
cc5d9012c1
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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.simple.policy.decision.point;
|
||||
|
||||
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.simple.policy.decision.point.internal.PolicyDecisionPointDataHolder;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SimpleEvaluationPoint implements PolicyEvaluationPoint {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SimpleEvaluationPoint.class);
|
||||
//TODO : to revove the stale reference
|
||||
private PolicyManagerService policyManagerService;
|
||||
private List<Policy> policyList;
|
||||
// public SimpleEvaluationPoint() {
|
||||
// policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
|
||||
// }
|
||||
|
||||
|
||||
@Override
|
||||
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
|
||||
PolicyAdministratorPoint policyAdministratorPoint;
|
||||
PolicyInformationPoint policyInformationPoint;
|
||||
policyManagerService = getPolicyManagerService();
|
||||
|
||||
try {
|
||||
if (policyManagerService != null) {
|
||||
|
||||
policyInformationPoint = policyManagerService.getPIP();
|
||||
PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier);
|
||||
policyList = policyInformationPoint.getRelatedPolicies(pipDevice);
|
||||
|
||||
if (!policyList.isEmpty()) {
|
||||
//policy = policyList.get(0);
|
||||
//policyList = new ArrayList<Policy>();
|
||||
Policy effectivePolicy = policyResolve(policyList);
|
||||
effectivePolicy.setActive(true);
|
||||
//TODO : UNCOMMENT THE FOLLOWING CASE
|
||||
policyAdministratorPoint = policyManagerService.getPAP();
|
||||
policyAdministratorPoint.setPolicyUsed(deviceIdentifier, effectivePolicy);
|
||||
return effectivePolicy.getProfile().getProfileFeaturesList();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
|
||||
Policy policy = new Policy();
|
||||
Profile profile = new Profile();
|
||||
profile.setProfileFeaturesList(getEffectiveFeatures(deviceIdentifier));
|
||||
policy.setProfile(profile);
|
||||
return policy;
|
||||
}
|
||||
|
||||
private Policy policyResolve(List<Policy> policyList) throws PolicyEvaluationException, PolicyManagementException {
|
||||
sortPolicies();
|
||||
|
||||
// Iterate through all policies
|
||||
Map<String, ProfileFeature> featureMap = new HashMap<>();
|
||||
Iterator<Policy> policyIterator = policyList.iterator();
|
||||
while (policyIterator.hasNext()) {
|
||||
Policy policy = policyIterator.next();
|
||||
List<ProfileFeature> profileFeaturesList = policy.getProfile().getProfileFeaturesList();
|
||||
if (profileFeaturesList != null) {
|
||||
Iterator<ProfileFeature> featureIterator = profileFeaturesList.iterator();
|
||||
while (featureIterator.hasNext()) {
|
||||
ProfileFeature feature = featureIterator.next();
|
||||
featureMap.put(feature.getFeatureCode(), feature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get prioritized features list
|
||||
List<ProfileFeature> newFeaturesList = new ArrayList<>(featureMap.values());
|
||||
Profile profile = new Profile();
|
||||
profile.setProfileFeaturesList(newFeaturesList);
|
||||
|
||||
Policy effectivePolicy = new Policy();
|
||||
effectivePolicy.setProfile(profile);
|
||||
|
||||
return effectivePolicy;
|
||||
}
|
||||
|
||||
public void sortPolicies() throws PolicyEvaluationException {
|
||||
//Collections.sort(policyList);
|
||||
Collections.sort(policyList, Collections.reverseOrder());
|
||||
}
|
||||
|
||||
private PolicyManagerService getPolicyManagerService() {
|
||||
return PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue