From cc5d9012c1a278db8634bfd36ab5738011a2d92a Mon Sep 17 00:00:00 2001 From: Supun94 Date: Tue, 11 Oct 2016 16:13:30 +0530 Subject: [PATCH] Created simple evaluation point --- .../decision/point/SimpleEvaluationPoint.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java new file mode 100644 index 0000000000..b2b81076ed --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java @@ -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 policyList; +// public SimpleEvaluationPoint() { +// policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); +// } + + + @Override + public List 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 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 policyList) throws PolicyEvaluationException, PolicyManagementException { + sortPolicies(); + + // Iterate through all policies + Map featureMap = new HashMap<>(); + Iterator policyIterator = policyList.iterator(); + while (policyIterator.hasNext()) { + Policy policy = policyIterator.next(); + List profileFeaturesList = policy.getProfile().getProfileFeaturesList(); + if (profileFeaturesList != null) { + Iterator featureIterator = profileFeaturesList.iterator(); + while (featureIterator.hasNext()) { + ProfileFeature feature = featureIterator.next(); + featureMap.put(feature.getFeatureCode(), feature); + } + } + } + + // Get prioritized features list + List 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(); + } +}