manoj 10 years ago
commit 3fc11654e7

@ -21,29 +21,30 @@ package org.wso2.carbon.policy.evaluator;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.List;
import java.util.Map;
public interface FeatureFilter {
List<Feature> evaluate(List<Policy> policyList, List<FeatureRules> featureRulesList);
List<ProfileFeature> evaluate(List<Policy> policyList, List<FeatureRules> featureRulesList);
List<Feature> extractFeatures(List<Policy> policyList);
List<ProfileFeature> extractFeatures(List<Policy> policyList);
List<Feature> evaluateFeatures(List<Feature> featureList, List<FeatureRules> featureRulesList);
List<ProfileFeature> evaluateFeatures(List<ProfileFeature> featureList, List<FeatureRules> featureRulesList);
void getDenyOverridesFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getDenyOverridesFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
void getPermitOverridesFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getPermitOverridesFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
void getFirstApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getFirstApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
void getLastApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getLastApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
void getAllApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getAllApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
void getHighestApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getHighestApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
void getLowestApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getLowestApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList);
}

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.evaluator;
import org.wso2.carbon.policy.evaluator.utils.Constants;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.ArrayList;
import java.util.List;
@ -39,7 +40,7 @@ public class FeatureFilterImpl implements FeatureFilter {
* @return
*/
@Override
public List<Feature> evaluate(List<Policy> policyList, List<FeatureRules> featureRulesList) {
public List<ProfileFeature> evaluate(List<Policy> policyList, List<FeatureRules> featureRulesList) {
return evaluateFeatures(extractFeatures(policyList), featureRulesList);
}
@ -48,10 +49,10 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param policyList
* @return
*/
public List<Feature> extractFeatures(List<Policy> policyList) {
List<Feature> featureList = new ArrayList<Feature>();
public List<ProfileFeature> extractFeatures(List<Policy> policyList) {
List<ProfileFeature> featureList = new ArrayList<ProfileFeature>();
for (Policy policy : policyList) {
featureList.addAll(policy.getProfile().getFeaturesList());
featureList.addAll(policy.getProfile().getProfileFeaturesList());
}
return featureList;
}
@ -62,8 +63,8 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureRulesList
* @return
*/
public List<Feature> evaluateFeatures(List<Feature> featureList, List<FeatureRules> featureRulesList) {
List<Feature> effectiveFeatureList = new ArrayList<Feature>();
public List<ProfileFeature> evaluateFeatures(List<ProfileFeature> featureList, List<FeatureRules> featureRulesList) {
List<ProfileFeature> effectiveFeatureList = new ArrayList<ProfileFeature>();
for (FeatureRules rule : featureRulesList) {
String ruleName = rule.getEvaluationCriteria();
String featureName = rule.getName();
@ -100,11 +101,11 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getDenyOverridesFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
Feature evaluatedFeature = null;
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
if (feature.getRuleValue().equalsIgnoreCase("Deny")) {
public void getDenyOverridesFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
ProfileFeature evaluatedFeature = null;
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
if (feature.getFeature().getRuleValue().equalsIgnoreCase("Deny")) {
evaluatedFeature = feature;
effectiveFeatureList.add(evaluatedFeature);
return;
@ -127,11 +128,11 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getPermitOverridesFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
Feature evaluatedFeature = null;
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
if (feature.getRuleValue().equalsIgnoreCase("Permit")) {
public void getPermitOverridesFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
ProfileFeature evaluatedFeature = null;
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
if (feature.getFeature().getRuleValue().equalsIgnoreCase("Permit")) {
evaluatedFeature = feature;
effectiveFeatureList.add(evaluatedFeature);
return;
@ -154,9 +155,9 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getFirstApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
public void getFirstApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
effectiveFeatureList.add(feature);
return;
@ -172,10 +173,10 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getLastApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
Feature evaluatedFeature = null;
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
public void getLastApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
ProfileFeature evaluatedFeature = null;
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
evaluatedFeature = feature;
}
}
@ -192,9 +193,9 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getAllApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
public void getAllApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
effectiveFeatureList.add(feature);
}
}
@ -208,13 +209,13 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getHighestApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
Feature evaluatedFeature = null;
public void getHighestApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
ProfileFeature evaluatedFeature = null;
int intValve = 0;
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
if (Integer.parseInt(feature.getRuleValue()) > intValve) {
intValve = Integer.parseInt(feature.getRuleValue());
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
if (Integer.parseInt(feature.getFeature().getRuleValue()) > intValve) {
intValve = Integer.parseInt(feature.getFeature().getRuleValue());
evaluatedFeature = feature;
}
}
@ -232,13 +233,13 @@ public class FeatureFilterImpl implements FeatureFilter {
* @param featureList
* @param effectiveFeatureList
*/
public void getLowestApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList) {
Feature evaluatedFeature = null;
public void getLowestApplicableFeatures(String featureName, List<ProfileFeature> featureList, List<ProfileFeature> effectiveFeatureList) {
ProfileFeature evaluatedFeature = null;
int intValve = 0;
for (Feature feature : featureList) {
if (feature.getName().equalsIgnoreCase(featureName)) {
if (Integer.parseInt(feature.getRuleValue()) < intValve) {
intValve = Integer.parseInt(feature.getRuleValue());
for (ProfileFeature feature : featureList) {
if (feature.getFeature().getName().equalsIgnoreCase(featureName)) {
if (Integer.parseInt(feature.getFeature().getRuleValue()) < intValve) {
intValve = Integer.parseInt(feature.getFeature().getRuleValue());
evaluatedFeature = feature;
}
}

@ -20,7 +20,7 @@ package org.wso2.carbon.policy.information.point;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.PIPDeviceData;
import org.wso2.carbon.policy.mgt.common.PIPDevice;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
@ -28,12 +28,12 @@ import java.util.List;
public class PolicyInformationServiceImpl implements PolicyInformationPoint {
@Override
public PIPDeviceData getDeviceData(DeviceIdentifier deviceIdentifier) {
public PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) {
return null;
}
@Override
public List<Policy> getRelatedPolicies(PIPDeviceData pipDeviceData) {
public List<Policy> getRelatedPolicies(PIPDevice pipDevice) {
return null;
}

@ -24,8 +24,9 @@ public class Feature {
private String code;
private String name;
private String description;
private Object attribute;
// private Object attribute;
private String ruleValue;
private int deviceTypeId;
public String getDescription() {
return description;
@ -67,11 +68,19 @@ public class Feature {
this.name = name;
}
public Object getAttribute() {
/* public Object getAttribute() {
return attribute;
}
public void setAttribute(Object attribute) {
this.attribute = attribute;
}*/
public int getDeviceTypeId() {
return deviceTypeId;
}
public void setDeviceTypeId(int deviceTypeId) {
this.deviceTypeId = deviceTypeId;
}
}

@ -19,6 +19,7 @@
package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -26,13 +27,14 @@ import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
public class PIPDeviceData {
public class PIPDevice {
Device device;
DeviceType deviceType;
DeviceIdentifier deviceIdentifier;
String ownershipType;
List<String> userIds;
List<String> roles;
String roles [];
String altitude;
String longitude;
Timestamp timestamp;
@ -72,11 +74,11 @@ public class PIPDeviceData {
this.userIds = userIds;
}
public List<String> getRoles() {
public String[] getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
public void setRoles(String roles[]) {
this.roles = roles;
}
@ -111,4 +113,12 @@ public class PIPDeviceData {
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public DeviceIdentifier getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
}

@ -18,14 +18,19 @@
package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.core.dto.Device;
import java.sql.Date;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* This class will be the used to create policy object with relevant information for evaluating.
*/
public class Policy {
public class Policy implements Comparable<Policy> {
private int id; // Identifier of the policy.
private int priorityId; // Priority of the policies. This will be used only for simple evaluation.
private Profile profile; // Profile id
@ -33,7 +38,7 @@ public class Policy {
private boolean generic; // If true, this should be applied to all related device.
private List<String> roleList; // Roles which this policy should be applied.
private String ownershipType; // Ownership type (COPE, BYOD, CPE)
private List<String> DeviceList; // Individual devices this policy should be applied
private List<Device> DeviceList; // Individual devices this policy should be applied
/*Dynamic policy attributes*/
@ -47,9 +52,11 @@ public class Policy {
/*These are related to location based policies*/
private String altitude; // Altitude
private String latitude; // Latitude
private String longitude; // Longitude
private int tenantId;
/*This will be used to record attributes which will be used by customer extended PDPs and PIPs*/
private Map<String, Object> attributes;
@ -110,11 +117,11 @@ public class Policy {
this.ownershipType = ownershipType;
}
public List<String> getDeviceList() {
public List<Device> getDeviceList() {
return DeviceList;
}
public void setDeviceList(List<String> deviceList) {
public void setDeviceList(List<Device> deviceList) {
DeviceList = deviceList;
}
@ -150,12 +157,12 @@ public class Policy {
this.endDate = endDate;
}
public String getAltitude() {
return altitude;
public String getLatitude() {
return latitude;
}
public void setAltitude(String altitude) {
this.altitude = altitude;
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
@ -173,4 +180,35 @@ public class Policy {
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public int getTenantId() {
return tenantId;
}
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
/* static final Comparator<Policy> PRIORITY_ORDER =
new Comparator<Policy>() {
public int compare(Policy p1, Policy p2) {
int dateCmp = new Integer(p2.getId()).compareTo(new Integer(p1.getId()));
if (dateCmp != 0)
return dateCmp;
return (p1.getId() < p2.getId() ? -1 :
(p1.getId() == p2.getId() ? 0 : 1));
}
};*/
@Override
public int compareTo(Policy o) {
if (this.priorityId == o.priorityId)
return 0;
else if ((this.priorityId) > o.priorityId)
return 1;
else
return -1;
}
}

@ -18,6 +18,10 @@
package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import java.util.List;
/**
* This interface defines the policy management which should be implemented by the plugins
*/
@ -39,13 +43,12 @@ public interface PolicyAdministratorPoint {
/**
* This method adds a policy per device which should be implemented by the related plugins.
*
* @param deviceId
* @param deviceType
* @param deviceIdentifierr
* @param policy
* @return primary key (generated key)
*/
Policy addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException;
Policy addPolicyToDevice(DeviceIdentifier deviceIdentifierr, Policy policy) throws FeatureManagementException, PolicyManagementException;
/**
* This method adds the policy to specific role.
@ -62,7 +65,7 @@ public interface PolicyAdministratorPoint {
* @return
*/
Policy getPolicy();
List<Policy> getPolicies() throws PolicyManagementException;
/**
* This method gives the device specific policy.
@ -72,7 +75,7 @@ public interface PolicyAdministratorPoint {
* @return Policy
*/
Policy getPolicyOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException;
List<Policy> getPoliciesOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException;
/**
* This method returns the device type specific policy.
@ -81,7 +84,7 @@ public interface PolicyAdministratorPoint {
* @return Policy
*/
Policy getPolicyOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException;
List<Policy> getPoliciesOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException;
/**
* This method returns the role specific policy.
@ -90,38 +93,35 @@ public interface PolicyAdministratorPoint {
* @return
*/
Policy getPolicyOfRole(String roleName) throws FeatureManagementException, PolicyManagementException;
List<Policy> getPoliciesOfRole(String roleName) throws FeatureManagementException, PolicyManagementException;
/**
* This method checks weather a policy is available for a device.
*
* @param deviceId
* @param deviceType
* @param deviceIdentifier
* @return
* @throws PolicyManagementException
*/
boolean isPolicyAvailableForDevice(String deviceId, String deviceType) throws PolicyManagementException;
boolean isPolicyAvailableForDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
/**
* This method checks weather a policy is used by a particular device.
*
* @param deviceId
* @param deviceType
* @param deviceIdentifier
* @return
* @throws PolicyManagementException
*/
boolean isPolicyApplied(String deviceId, String deviceType) throws PolicyManagementException;
boolean isPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
/**
* @param deviceId
* @param deviceType
* @param deviceIdentifier
* @param policy
* @throws PolicyManagementException
*/
void setPolicyUsed(String deviceId, String deviceType, Policy policy) throws PolicyManagementException;
void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;
/**
* This method will add the profile to database,

@ -34,7 +34,7 @@ public interface PolicyEvaluationPoint {
* @param deviceIdentifier device information.
* @return returns the effective policy.
*/
Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier);
Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException;
/**
@ -42,5 +42,5 @@ public interface PolicyEvaluationPoint {
* @param deviceIdentifier device information.
* @return returns the effective feature set.
*/
List<Feature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier);
List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException ;
}

@ -20,7 +20,6 @@
package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device;
import java.util.List;
@ -33,16 +32,16 @@ public interface PolicyInformationPoint {
* This method will return the data related Device, Some of the device data will provided in the initial pipDeviceData object such as
* device id, device time and location, Other data such as roles, owned users and ownership type will be filled by this method.
* @param deviceIdentifier device data.
* @return PIPDeviceData
* @return PIPDevice
*/
PIPDeviceData getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
/**
* This method will retrieve the policies related given device Data.
* @param pipDeviceData
* @param pipDevice
* @return
*/
List<Policy> getRelatedPolicies(PIPDeviceData pipDeviceData) throws PolicyManagementException;
List<Policy> getRelatedPolicies(PIPDevice pipDevice) throws PolicyManagementException;
/**
* This is will retrieve the features related to device type. This feature list will be used for dynamically

@ -33,7 +33,8 @@ public class Profile {
private DeviceType deviceType;
private Timestamp createdDate;
private Timestamp updatedDate;
private List<Feature> featuresList; // Features included in the policies.
// private List<Feature> featuresList; // Features included in the policies.
private List<ProfileFeature> profileFeaturesList; // Features included in the policies.
public DeviceType getDeviceType() {
return deviceType;
@ -51,13 +52,13 @@ public class Profile {
this.tenantId = tenantId;
}
public List<Feature> getFeaturesList() {
/* public List<Feature> getFeaturesList() {
return featuresList;
}
public void setFeaturesList(List<Feature> featuresList) {
this.featuresList = featuresList;
}
}*/
public int getProfileId() {
return profileId;
@ -90,4 +91,12 @@ public class Profile {
public void setUpdatedDate(Timestamp updatedDate) {
this.updatedDate = updatedDate;
}
public List<ProfileFeature> getProfileFeaturesList() {
return profileFeaturesList;
}
public void setProfileFeaturesList(List<ProfileFeature> profileFeaturesList) {
this.profileFeaturesList = profileFeaturesList;
}
}

@ -0,0 +1,59 @@
/*
* 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.common;
public class ProfileFeature {
private int id;
private Feature feature;
private int profileId;
private Object content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Feature getFeature() {
return feature;
}
public void setFeature(Feature feature) {
this.feature = feature;
}
public int getProfileId() {
return profileId;
}
public void setProfileId(int profileId) {
this.profileId = profileId;
}
public Object getContent() {
return content;
}
public void setContent(Object content) {
this.content = content;
}
}

@ -34,7 +34,7 @@ public class PolicyCreator {
Feature feature = new Feature();
feature.setName("Camera");
feature.setCode("502A");
feature.setAttribute("disable");
// feature.setAttribute("disable");
List<Feature> featureList = new ArrayList<Feature>();
featureList.add(feature);
@ -45,7 +45,7 @@ public class PolicyCreator {
profile.setTenantId(-1234);
policy.setProfile(profile);
profile.setFeaturesList(featureList);
//profile.setFeaturesList(featureList);
policy.setPolicyName("Camera_related_policy");

@ -68,6 +68,8 @@
org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.user.core.*,
org.wso2.carbon.utils.*,
org.wso2.carbon.context.*,
org.wso2.carbon.user.api.*,
org.wso2.carbon.device.mgt.core.*,
org.wso2.carbon.device.mgt.common.*
</Import-Package>

@ -47,4 +47,7 @@ public interface PolicyManager {
List<Feature> getFeatures() throws FeatureManagementException;
PolicyAdministratorPoint getPAP() throws PolicyManagementException;
PolicyInformationPoint getPIP() throws PolicyManagementException;
}

@ -22,12 +22,9 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl;
import org.wso2.carbon.policy.mgt.core.impl.PolicyInformationPointImpl;
import java.util.List;
@ -90,4 +87,14 @@ public class PolicyManagerImpl implements PolicyManager {
public List<Feature> getFeatures() throws FeatureManagementException {
return null;
}
@Override
public PolicyAdministratorPoint getPAP() throws PolicyManagementException {
return new PolicyAdministratorPointImpl();
}
@Override
public PolicyInformationPoint getPIP() throws PolicyManagementException {
return new PolicyInformationPointImpl();
}
}

@ -22,6 +22,7 @@ package org.wso2.carbon.policy.mgt.core.dao;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.List;
@ -31,12 +32,22 @@ public interface FeatureDAO {
Feature updateFeature(Feature feature) throws FeatureManagerDAOException;
ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException;
ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException;
List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException;
List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException;
List<Feature> getAllFeatures() throws FeatureManagerDAOException;
List<Feature> getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException;
List<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException;
List<ProfileFeature> getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException;
void deleteFeature(int featureId) throws FeatureManagerDAOException;
boolean deleteFeature(int featureId) throws FeatureManagerDAOException;
void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException;
boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException;
}

@ -18,6 +18,7 @@
package org.wso2.carbon.policy.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Policy;
@ -33,46 +34,22 @@ public interface PolicyDAO {
Policy addPolicyToRole(String roleName, Policy policy) throws PolicyManagerDAOException;
Policy addPolicy(String deviceID, String deviceType, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagerDAOException;
Policy updatePolicy(Policy policy) throws PolicyManagerDAOException;
Policy getPolicy() throws PolicyManagerDAOException;
Policy getPolicy(int policyId) throws PolicyManagerDAOException;
Policy getPolicy(String deviceType) throws PolicyManagerDAOException;
Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException;
Policy getPolicy(String deviceID, String deviceType) throws PolicyManagerDAOException;
List<Policy> getPolicy() throws PolicyManagerDAOException;
void deletePolicy(Policy policy) throws PolicyManagerDAOException;
/* Profile addProfile(Profile profile) throws PolicyManagerDAOException;
Profile updateProfile(Profile profile) throws PolicyManagerDAOException;
List<Policy> getPolicy(String deviceType) throws PolicyManagerDAOException;
void deleteProfile(Profile profile) throws PolicyManagerDAOException;
List<Policy> getPolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagerDAOException;
List<Profile> getAllProfiles() throws PolicyManagerDAOException;
List<Policy> getPolicyOfRole(String roleName) throws PolicyManagerDAOException;
List<Profile> getProfilesOfDeviceType(String deviceType) throws PolicyManagerDAOException;
List<Feature> getAllFeatures() throws PolicyManagerDAOException;
List<Feature> getFeaturesForProfile(int ProfileId) throws PolicyManagerDAOException;
void deleteFeature(int featureId) throws PolicyManagerDAOException;
void deleteFeaturesOfProfile(Profile profile) throws PolicyManagerDAOException;
Feature addFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException;
void deletePolicy(Policy policy) throws PolicyManagerDAOException;
Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException;*/
}

@ -31,6 +31,8 @@ public interface ProfileDAO {
void deleteProfile(Profile profile) throws ProfileManagerDAOException;
Profile getProfiles(int profileId) throws ProfileManagerDAOException;
List<Profile> getAllProfiles() throws ProfileManagerDAOException;
List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException;

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
@ -75,11 +76,106 @@ public class FeatureDAOImpl implements FeatureDAO {
@Override
public Feature updateFeature(Feature feature) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
conn = this.getConnection();
String query = "UPDATE DM_FEATURES SET NAME = ?, CODE = ?, DESCRIPTION = ?, EVALUVATION_RULE = ? WHERE ID = ?";
stmt = conn.prepareStatement(query);
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
stmt.setString(3, feature.getDescription());
stmt.setString(4, feature.getRuleValue());
stmt.setInt(5, feature.getId());
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Error occurred while updating feature " + feature.getName() + " (Feature Name) to the database.";
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
return feature;
}
@Override
public void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException {
public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException {
return null;
}
@Override
public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException {
return null;
}
@Override
public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_ID, CONTENT) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
for (ProfileFeature feature : features) {
stmt.setInt(1, profileId);
stmt.setInt(2, feature.getId());
stmt.setObject(3, feature.getContent());
stmt.addBatch();
//Not adding the logic to check the size of the stmt and execute if the size records added is over 1000
}
stmt.executeBatch();
generatedKeys = stmt.getGeneratedKeys();
int i = 0;
while (generatedKeys.next()) {
features.get(i).setId(generatedKeys.getInt(i));
i++;
}
} catch (SQLException e) {
String msg = "Error occurred while adding the feature list to the database.";
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
return features;
}
@Override
public List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? , FEATURE_ID = ? ";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
for (ProfileFeature feature : features) {
stmt.setObject(1, feature.getContent());
stmt.setInt(2, profileId);
stmt.setInt(3, feature.getId());
stmt.addBatch();
//Not adding the logic to check the size of the stmt and execute if the size records added is over 1000
}
stmt.executeBatch();
} catch (SQLException e) {
String msg = "Error occurred while adding the feature list to the database.";
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return features;
}
@Override
public boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
@ -89,6 +185,7 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt = conn.prepareStatement(query);
stmt.setInt(1, profile.getProfileId());
stmt.executeUpdate();
return true;
} catch (SQLException e) {
String msg = "Error occurred while deleting the feature related to a profile.";
log.error(msg);
@ -109,7 +206,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "SELECT ID, NAME, CODE, EVALUVATION_RULE FROM DM_FEATURES";
String query = "SELECT ID, NAME, CODE, DEVICE_TYPE_ID, EVALUVATION_RULE FROM DM_FEATURES";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
@ -118,6 +215,7 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setId(resultSet.getInt("ID"));
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
feature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID"));
feature.setRuleValue(resultSet.getString("EVALUVATION_RULE"));
featureList.add(feature);
}
@ -133,16 +231,52 @@ public class FeatureDAOImpl implements FeatureDAO {
}
@Override
public List<Feature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException {
public List<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
try {
conn = this.getConnection();
String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE_ID DEVICE_TYPE_ID," +
" f.EVALUVATION_RULE EVALUVATION_RULE FROM DM_FEATURES f INNER JOIN DM_DEVICE_TYPE d " +
"ON d.ID=f.DEVICE_TYPE_ID WHERE d.NAME = ?";
stmt = conn.prepareStatement(query);
stmt.setString(1, deviceType);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
Feature feature = new Feature();
feature.setId(resultSet.getInt("ID"));
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
feature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID"));
feature.setRuleValue(resultSet.getString("EVALUVATION_RULE"));
featureList.add(feature);
}
return featureList;
} catch (SQLException e) {
String msg = "Unable to get the list of the features related device type from database.";
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
@Override
public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ProfileFeature> featureList = new ArrayList<ProfileFeature>();
try {
conn = this.getConnection();
String query = "SELECT PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " +
String query = "SELECT PF.ID AS ID, PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " +
"F.EVALUVATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " +
"JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID WHERE PROFILE_ID=?";
stmt = conn.prepareStatement(query);
@ -150,13 +284,18 @@ public class FeatureDAOImpl implements FeatureDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
ProfileFeature profileFeature = new ProfileFeature();
Feature feature = new Feature();
feature.setId(resultSet.getInt("FEATURE_ID"));
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
feature.setAttribute(resultSet.getObject("CONTENT"));
// feature.setAttribute(resultSet.getObject("CONTENT"));
feature.setRuleValue(resultSet.getString("RULE"));
featureList.add(feature);
profileFeature.setFeature(feature);
profileFeature.setId(resultSet.getInt("ID"));
profileFeature.setContent(resultSet.getObject("CONTENT"));
featureList.add(profileFeature);
}
} catch (SQLException e) {
@ -170,18 +309,24 @@ public class FeatureDAOImpl implements FeatureDAO {
}
@Override
public void deleteFeature(int featureId) throws FeatureManagerDAOException {
public boolean deleteFeature(int featureId) throws FeatureManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
conn = this.getConnection();
String query = "";
String query = "DELETE FROM DM_FEATURES WHERE ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, featureId);
stmt.executeUpdate();
return true;
} catch (SQLException e) {
String msg = "Unable to delete the feature " + featureId + " (Feature ID) from database.";
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, null);
}
}

@ -20,31 +20,33 @@ package org.wso2.carbon.policy.mgt.core.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class PolicyDAOImpl implements PolicyDAO {
private static final Log log = LogFactory.getLog(PolicyDAOImpl.class);
DeviceDAOImpl deviceDAO = new DeviceDAOImpl(PolicyManagementDAOFactory.getDataSource());
DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAOImpl(PolicyManagementDAOFactory.getDataSource());
ProfileDAO profileDAO = new ProfileDAOImpl();
@Override
public Policy addPolicy(Policy policy) throws PolicyManagerDAOException {
persistPolicy(policy);
return policy;
public Policy addPolicy(Policy policy) throws PolicyManagerDAOException {
return persistPolicy(policy);
}
@Override
@ -79,11 +81,34 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override
public Policy addPolicyToRole(String roleName, Policy policy) throws PolicyManagerDAOException {
return null;
// First persist the policy to the data base.
persistPolicy(policy);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)";
stmt = conn.prepareStatement(query);
stmt.setString(1, roleName);
stmt.setInt(2, policy.getId());
stmt.executeQuery();
policy.getRoleList().add(roleName);
} catch (SQLException e) {
String msg = "Error occurred while adding the role name with policy to database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
return policy;
}
@Override
public Policy addPolicy(String deviceID, String deviceType, Policy policy) throws PolicyManagerDAOException {
public Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagerDAOException {
// First persist the policy to the data base.
persistPolicy(policy);
@ -92,11 +117,23 @@ public class PolicyDAOImpl implements PolicyDAO {
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
try {
Device device = deviceDAO.getDevice(deviceIdentifier);
conn = this.getConnection();
String query = "";
String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)";
stmt = conn.prepareStatement(query);
} catch (SQLException e) {
stmt.setInt(1, device.getId());
stmt.setInt(2, policy.getId());
stmt.executeUpdate();
policy.getDeviceList().add(device);
} catch (SQLException e) {
String msg = "Error occurred while adding the device ids with policy to database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while reading the device data from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
@ -112,10 +149,18 @@ public class PolicyDAOImpl implements PolicyDAO {
ResultSet generatedKeys = null;
try {
conn = this.getConnection();
String query = "";
String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ? WHERE ID = ?";
stmt = conn.prepareStatement(query);
} catch (Exception e) {
stmt.setString(1, policy.getPolicyName());
stmt.setInt(2, policy.getTenantId());
stmt.setInt(3, policy.getProfile().getProfileId());
stmt.setInt(4, policy.getId());
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Error occurred while updating policy " + policy.getPolicyName() + " (Policy Name) in database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
@ -124,60 +169,430 @@ public class PolicyDAOImpl implements PolicyDAO {
}
@Override
public Policy getPolicy() throws PolicyManagerDAOException {
public Policy getPolicy(int policyId) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
ResultSet resultSet = null;
Policy policy = new Policy();
try {
conn = this.getConnection();
String query = "";
String query = "SELECT * FROM DM_POLICY WHERE ID= ?";
stmt = conn.prepareStatement(query);
} catch (Exception e) {
stmt.setInt(1, policyId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
Profile profile = profileDAO.getProfiles(resultSet.getInt("PROFILE_ID"));
List<Device> deviceList = getPolicyAppliedDevices(policyId);
List<String> roleNames = getPolicyAppliedRoles(policyId);
policy.setId(policyId);
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
policy.setProfile(profile);
policy.setDeviceList(deviceList);
policy.setRoleList(roleNames);
setDatesOfPolicy(policy);
setTimesOfPolicy(policy);
setLocationsOfPolicy(policy);
}
return policy;
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profiles.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
@Override
public Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException {
return null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
Policy policy = new Policy();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_POLICY WHERE PROFILE_ID= ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, profileId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
int policyId = resultSet.getInt("ID");
Profile profile = profileDAO.getProfiles(profileId);
List<Device> deviceList = getPolicyAppliedDevices(policyId);
List<String> roleNames = getPolicyAppliedRoles(policyId);
policy.setId(policyId);
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
policy.setProfile(profile);
policy.setDeviceList(deviceList);
policy.setRoleList(roleNames);
setDatesOfPolicy(policy);
setTimesOfPolicy(policy);
setLocationsOfPolicy(policy);
}
return policy;
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profiles.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
@Override
public Policy getPolicy(String deviceType) throws PolicyManagerDAOException {
public List<Policy> getPolicy() throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Policy> policies = new ArrayList<Policy>();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_POLICY";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
Policy policy = new Policy();
int policyId = resultSet.getInt("ID");
Profile profile = profileDAO.getProfiles(resultSet.getInt("PROFILE_ID"));
List<Device> deviceList = getPolicyAppliedDevices(policyId);
List<String> roleNames = getPolicyAppliedRoles(policyId);
policy.setId(policyId);
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
policy.setProfile(profile);
policy.setDeviceList(deviceList);
policy.setRoleList(roleNames);
setDatesOfPolicy(policy);
setTimesOfPolicy(policy);
setLocationsOfPolicy(policy);
policies.add(policy);
}
return policies;
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profiles.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
@Override
public List<Policy> getPolicy(String deviceTypeName) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Policy> policies = new ArrayList<Policy>();
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
// This gives the profiles related to given device types.
List<Profile> profileList = profileDAO.getProfilesOfDeviceType(deviceTypeName);
for (Profile profile : profileList) {
policies.add(getPolicyByProfileID(profile.getProfileId()));
}
/* conn = this.getConnection();
// TODO : Change the query for device type
String query = "SELECT dp.ID PID, dp.NAME PNAME, dp.TENANT_ID PTD, dp.PROFILE_ID PPID FROM DM_POLICY dp " +
"INNER JOIN DM_PROFILE dpr ON dpr.ID = dp.PROFILE_ID WHERE dpr.ID = ?";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
//ID NAME TENANT_ID PROFILE_ID
while (resultSet.next()) {
Policy policy = new Policy();
int policyId = resultSet.getInt("PID");
Profile profile = profileDAO.getProfiles(resultSet.getInt("PID"));
List<Device> deviceList = getPolicyAppliedDevices(policyId);
List<String> roleNames = getPolicyAppliedRoles(policyId);
policy.setId(policyId);
policy.setPolicyName(resultSet.getString("PNAME"));
policy.setTenantId(resultSet.getInt("PTD"));
policy.setProfile(profile);
policy.setDeviceList(deviceList);
policy.setRoleList(roleNames);
setDatesOfPolicy(policy);
setTimesOfPolicy(policy);
setLocationsOfPolicy(policy);
policies.add(policy);
}*/
return policies;
/* } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);*/
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profiles.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
// TODO :
private List<Device> getPolicyAppliedDevices(int policyId) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Device> deviceList = new ArrayList<Device>();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
deviceList.add(deviceDAO.getDevice(resultSet.getInt("DEVICE_ID")));
}
return deviceList;
} catch (SQLException e) {
String msg = "Error occurred while getting the device related to policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device data.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
// TODO :
private List<String> getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<String> roleNames = new ArrayList<String>();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_ROLE_POLICY WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
roleNames.add(resultSet.getString("ROLE_NAME"));
}
return roleNames;
} catch (SQLException e) {
String msg = "Error occurred while getting the roles related to policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
// TODO :
private void setTimesOfPolicy(Policy policy) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = this.getConnection();
String query = "SELECT STARTING_TIME, ENDING_TIME FROM DM_TIME WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policy.getId());
resultSet = stmt.executeQuery();
while (resultSet.next()) {
policy.setStartTime(resultSet.getInt("STARTING_TIME"));
policy.setEndTime(resultSet.getInt("ENDING_TIME"));
}
} catch (SQLException e) {
String msg = "Error occurred while getting the start time and end time related to policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
// TODO :
private void setDatesOfPolicy(Policy policy) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = this.getConnection();
String query = "SELECT START_DATE, END_DATE FROM DM_DATE WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policy.getId());
resultSet = stmt.executeQuery();
while (resultSet.next()) {
policy.setStartDate(resultSet.getDate("START_DATE"));
policy.setEndDate(resultSet.getDate("END_DATE"));
}
} catch (SQLException e) {
String msg = "Error occurred while getting the start date and end date related to policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
// TODO:
private void setLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = this.getConnection();
String query = "SELECT LAT, LONG FROM DM_LOCATION WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policy.getId());
resultSet = stmt.executeQuery();
while (resultSet.next()) {
policy.setLatitude(resultSet.getString("LAT"));
policy.setLongitude(resultSet.getString("LONG"));
}
} catch (SQLException e) {
String msg = "Error occurred while getting the start time and end time related to policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
@Override
public List<Policy> getPolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
List<Policy> policies = new ArrayList<Policy>();
try {
conn = this.getConnection();
String query = "";
String query = "SELECT * FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ? ";
stmt = conn.prepareStatement(query);
} catch (Exception e) {
stmt.setInt(1, deviceDAO.getDevice(deviceIdentifier).getId().intValue());
generatedKeys = stmt.executeQuery();
while (generatedKeys.next()){
policies.add(getPolicy(generatedKeys.getInt("POLICY_ID")));
}
return policies;
} catch (SQLException e) {
String msg = "Error occurred while reading the device policy table.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device data from the device identifier.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
return null;
}
@Override
public Policy getPolicy(String deviceID, String deviceType) throws PolicyManagerDAOException {
public List<Policy> getPolicyOfRole(String roleName) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
List<Policy> policies = new ArrayList<Policy>();
try {
conn = this.getConnection();
String query = "";
String query = "SELECT * FROM DM_ROLE_POLICY WHERE ROLE_NAME = ? ";
stmt = conn.prepareStatement(query);
} catch (Exception e) {
stmt.setString(1, roleName);
generatedKeys = stmt.executeQuery();
while (generatedKeys.next()){
policies.add(getPolicy(generatedKeys.getInt("POLICY_ID")));
}
return policies;
} catch (SQLException e) {
String msg = "Error occurred while reading the role policy table.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
return null;
}
@Override
@ -196,7 +611,7 @@ public class PolicyDAOImpl implements PolicyDAO {
if (log.isDebugEnabled()) {
log.debug("Policy (" + policy.getPolicyName() + ") delete from database.");
}
} catch (Exception e) {
} catch (SQLException e) {
String msg = "Unable to delete the policy (" + policy.getPolicyName() + ") from database.";
log.error(msg);
throw new PolicyManagerDAOException(msg, e);
@ -216,7 +631,7 @@ public class PolicyDAOImpl implements PolicyDAO {
}
private void persistPolicy(Policy policy) throws PolicyManagerDAOException {
private Policy persistPolicy(Policy policy) throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
@ -226,12 +641,13 @@ public class PolicyDAOImpl implements PolicyDAO {
int tenantId = -1234;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID) VALUES (?, ?, ?)";
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
stmt.setString(1, policy.getPolicyName());
stmt.setInt(2, policy.getProfile().getProfileId());
stmt.setInt(3, tenantId);
stmt.setInt(4, readHighestPriorityOfPolicies());
int affectedRows = stmt.executeUpdate();
@ -248,22 +664,24 @@ public class PolicyDAOImpl implements PolicyDAO {
if (policy.getId() == 0) {
throw new RuntimeException("No rows were inserted, policy id cannot be null.");
}
profileDAO.addProfile(policy.getProfile());
return policy;
} catch (SQLException e) {
String msg = "Error occurred while adding policy to the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while adding profile to the database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
}
/**
* This method returns the device type id when supplied with device type name.
*
@ -298,4 +716,30 @@ public class PolicyDAOImpl implements PolicyDAO {
return deviceTypeId;
}
private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = this.getConnection();
String query = "SELECT MAX(PRIORITY) PRIORITY FROM DM_POLICY;";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
return resultSet.getInt("PRIORITY");
}
} catch (SQLException e) {
String msg = "Error occurred while reading the highest priority of the policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
return 0;
}
}

@ -20,29 +20,37 @@ package org.wso2.carbon.policy.mgt.core.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProfileDAOImpl implements ProfileDAO {
private static final Log log = LogFactory.getLog(ProfileDAOImpl.class);
FeatureDAOImpl featureDAO = new FeatureDAOImpl();
DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAOImpl(PolicyManagementDAOFactory.getDataSource());
public Profile addProfile(Profile profile) throws ProfileManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
// TODO : find a way to get the tenant Id.
int tenantId = -1234;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE (PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?)";
@ -70,12 +78,17 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new RuntimeException("Profile id is 0, this could be an issue.");
}
persistFeatures(profile);
featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
// persistFeatures(profile);
} catch (SQLException e) {
String msg = "Error occurred while adding the profile to database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while adding the features to database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
@ -85,6 +98,56 @@ public class ProfileDAOImpl implements ProfileDAO {
public Profile updateProfile(Profile profile) throws ProfileManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
try {
conn = this.getConnection();
String query = "UPDATE DM_PROFILE SET PROFILE_NAME = ? ,TENANT_ID = ?, DEVICE_TYPE_ID = ? , UPDATED_TIME = ? " +
"WHERE ID = ?";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
stmt.setString(1, profile.getProfileName());
stmt.setInt(2, tenantId);
stmt.setLong(3, profile.getDeviceType().getId());
stmt.setTimestamp(4, profile.getUpdatedDate());
stmt.setInt(5, profile.getProfileId());
int affectedRows = stmt.executeUpdate();
if (affectedRows == 0 && log.isDebugEnabled()) {
String msg = "No rows are updated on the profile table.";
log.debug(msg);
}
generatedKeys = stmt.getGeneratedKeys();
if (generatedKeys.next()) {
profile.setProfileId(generatedKeys.getInt(1));
}
// Checking the profile id here, because profile id could have been passed from the calling method.
if (profile.getProfileId() == 0) {
throw new RuntimeException("Profile id is 0, this could be an issue.");
}
// TODO : Check to update the features.
featureDAO.updateProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
//persistFeatures(profile);
} catch (SQLException e) {
String msg = "Error occurred while updating the profile ("+profile.getProfileName()+") in database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while updating the profile in database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys);
}
return profile;
}
@ -92,7 +155,7 @@ public class ProfileDAOImpl implements ProfileDAO {
public void deleteProfile(Profile profile) throws ProfileManagerDAOException {
// First delete the features related to the profile
FeatureDAOImpl featureDAO = new FeatureDAOImpl();
try {
featureDAO.deleteFeaturesOfProfile(profile);
} catch (FeatureManagerDAOException e) {
@ -101,7 +164,6 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new ProfileManagerDAOException(msg, e);
}
Connection conn = null;
PreparedStatement stmt = null;
@ -120,7 +182,54 @@ public class ProfileDAOImpl implements ProfileDAO {
}
}
@Override
public Profile getProfiles(int profileId) throws ProfileManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
Profile profile = new Profile();
try {
List<ProfileFeature> featureList = featureDAO.getFeaturesForProfile(profileId);
conn = this.getConnection();
String query = "SELECT * FROM DM_PROFILE WHERE ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, profileId);
resultSet = stmt.executeQuery();
//ID PROFILE_NAME TENANT_ID DEVICE_TYPE_ID CREATED_TIME UPDATED_TIME
while (resultSet.next()) {
profile.setProfileFeaturesList(featureList);
profile.setProfileId(profileId);
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(deviceTypeDAO.getDeviceType(resultSet.getInt("DEVICE_TYPE_ID")));
profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME"));
profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME"));
}
return profile;
} catch (SQLException e) {
String msg = "Error occurred while reading the profile from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred when getting the device type name by device type id.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred when getting the features related to a profile.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
}
private void persistFeatures(Profile profile) throws ProfileManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
try {
@ -128,10 +237,10 @@ public class ProfileDAOImpl implements ProfileDAO {
String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_ID, CONTENT) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(query);
for (Feature feature : profile.getFeaturesList()) {
for (ProfileFeature feature : profile.getProfileFeaturesList()) {
stmt.setInt(1, profile.getProfileId());
stmt.setInt(2, feature.getId());
stmt.setObject(3, feature.getAttribute());
stmt.setObject(3, feature.getContent());
stmt.addBatch();
//Not adding the logic to check the size of the stmt and execute if the size records added is over 1000
}
@ -148,38 +257,95 @@ public class ProfileDAOImpl implements ProfileDAO {
@Override
public List<Profile> getAllProfiles() throws ProfileManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
ResultSet resultSet = null;
List<Profile> profileList = new ArrayList<Profile>();
try {
conn = this.getConnection();
String query = "";
String query = "SELECT * FROM DM_PROFILE";
stmt = conn.prepareStatement(query);
} catch (SQLException e) {
resultSet = stmt.executeQuery();
//ID PROFILE_NAME TENANT_ID DEVICE_TYPE_ID CREATED_TIME UPDATED_TIME
while (resultSet.next()) {
Profile profile = new Profile();
int profileId = resultSet.getInt("ID");
List<ProfileFeature> featureList = featureDAO.getFeaturesForProfile(profileId);
profile.setProfileFeaturesList(featureList);
profile.setProfileId(profileId);
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(deviceTypeDAO.getDeviceType(resultSet.getInt("DEVICE_TYPE_ID")));
profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME"));
profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME"));
profileList.add(profile);
}
return profileList;
} catch (SQLException e) {
String msg = "Error occurred while reading the profile list from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features related to a profile.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
}
return null;
}
@Override
public List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException {
public List<Profile> getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
ResultSet resultSet = null;
List<Profile> profileList = new ArrayList<Profile>();
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
conn = this.getConnection();
String query = "";
String query = "SELECT * FROM DM_PROFILE WHERE DEVICE_TYPE_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceType.getId());
resultSet = stmt.executeQuery();
//ID PROFILE_NAME TENANT_ID DEVICE_TYPE_ID CREATED_TIME UPDATED_TIME
while (resultSet.next()) {
Profile profile = new Profile();
int profileId = resultSet.getInt("ID");
List<ProfileFeature> featureList = featureDAO.getFeaturesForProfile(profileId);
profile.setProfileFeaturesList(featureList);
profile.setProfileId(profileId);
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(deviceType);
profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME"));
profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME"));
profileList.add(profile);
}
return profileList;
} catch (SQLException e) {
String msg = "Error occurred while reading the profile list from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features related to a profile.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
}
return null;
}

@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.impl;
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.Feature;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Policy;
@ -33,6 +34,8 @@ import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
import java.util.List;
public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class);
@ -71,11 +74,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
}
@Override
public Policy addPolicyToDevice(String deviceId, String deviceType, Policy policy)
public Policy addPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
throws FeatureManagementException, PolicyManagementException {
try {
policy = policyDAO.addPolicy(deviceId, deviceType, policy);
policy = policyDAO.addPolicyToDevice(deviceIdentifier, policy);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while persisting the policy.";
log.error(msg, e);
@ -98,39 +101,57 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
}
@Override
public Policy getPolicy() {
return null;
public List<Policy> getPolicies() throws PolicyManagementException {
try {
return policyDAO.getPolicy();
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override
public Policy getPolicyOfDevice(String deviceId, String deviceType)
public List<Policy> getPoliciesOfDevice(String deviceId, String deviceType)
throws FeatureManagementException, PolicyManagementException {
return null;
}
@Override
public Policy getPolicyOfDeviceType(String deviceType)
public List<Policy> getPoliciesOfDeviceType(String deviceType)
throws FeatureManagementException, PolicyManagementException {
return null;
try {
return policyDAO.getPolicy(deviceType);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to device type.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override
public Policy getPolicyOfRole(String roleName) throws FeatureManagementException, PolicyManagementException {
return null;
public List<Policy> getPoliciesOfRole(String roleName) throws FeatureManagementException, PolicyManagementException {
try {
return policyDAO.getPolicyOfRole(roleName);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to role name.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override
public boolean isPolicyAvailableForDevice(String deviceId, String deviceType) throws PolicyManagementException {
public boolean isPolicyAvailableForDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return false;
}
@Override
public boolean isPolicyApplied(String deviceId, String deviceType) throws PolicyManagementException {
public boolean isPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return false;
}
@Override
public void setPolicyUsed(String deviceId, String deviceType, Policy policy) throws PolicyManagementException {
public void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException {
}
@ -192,6 +213,12 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public void deleteFeature(int featureId) throws FeatureManagementException {
try {
featureDAO.deleteFeature(featureId);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred while deleting the feature.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
}
}

@ -20,48 +20,124 @@ package org.wso2.carbon.policy.mgt.core.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.user.api.UserStoreException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PolicyInformationPointImpl implements PolicyInformationPoint {
private static final Log log = LogFactory.getLog(PolicyInformationPointImpl.class);
DeviceDAOImpl deviceDAO;
DeviceDAOImpl deviceDAO = new DeviceDAOImpl(PolicyManagementDAOFactory.getDataSource());
DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAOImpl(PolicyManagementDAOFactory.getDataSource());
FeatureDAO featureDAO = new FeatureDAOImpl();
PolicyDAO policyDAO = new PolicyDAOImpl();
public PolicyInformationPointImpl() {
deviceDAO = new DeviceDAOImpl(DeviceManagementDAOFactory.getDataSource());
}
@Override
public PIPDeviceData getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
PIPDeviceData pipDeviceData = new PIPDeviceData();
public PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
PIPDevice pipDevice = new PIPDevice();
Device device;
try {
device = deviceDAO.getDevice(deviceIdentifier);
pipDeviceData.setDevice(device);
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceIdentifier.getType());
pipDevice.setDevice(device);
pipDevice.setRoles(getRoleOfDevice(device));
pipDevice.setDeviceType(deviceType);
pipDevice.setDeviceIdentifier(deviceIdentifier);
// TODO : Find a way to retrieve the timestamp and location (lat, long) of the device
// pipDevice.setLongitude();
// pipDevice.setAltitude();
// pipDevice.setTimestamp();
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred when retrieving the data related to device from the database.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return pipDeviceData;
return pipDevice;
}
@Override
public List<Policy> getRelatedPolicies(PIPDeviceData pipDeviceData) throws PolicyManagementException {
return null;
public List<Policy> getRelatedPolicies(PIPDevice pipDevice) throws PolicyManagementException {
List<List<Policy>> policies = new ArrayList<List<Policy>>();
try {
// Get the device type related policies
policies.add(policyDAO.getPolicy(pipDevice.getDeviceType().getName()));
// Get the roles related policies
for (String role : pipDevice.getRoles()) {
policies.add(policyDAO.getPolicyOfRole(role));
}
// Get policy related to the device
policies.add(policyDAO.getPolicy(pipDevice.getDeviceIdentifier()));
return removeDuplicatePolicies(policies);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred when retrieving related to given device " +
pipDevice.getDeviceIdentifier().getId() + " " + pipDevice.getDeviceIdentifier().getType() + ".";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override
public List<Feature> getRelatedFeatures(String deviceType) throws FeatureManagementException {
return null;
try {
return featureDAO.getAllFeatures(deviceType);
} catch (FeatureManagerDAOException e) {
String msg = "Error occurred when retrieving features related to device type.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
}
private String[] getRoleOfDevice(Device device) throws PolicyManagementException {
try {
return CarbonContext.getThreadLocalCarbonContext().getUserRealm().
getUserStoreManager().getRoleListOfUser(device.getOwnerId());
} catch (UserStoreException e) {
String msg = "Error occurred when retrieving roles related to user name.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
private List<Policy> removeDuplicatePolicies(List<List<Policy>> policies) {
Map<Integer, Policy> map = new HashMap<Integer, Policy>();
List<Policy> finalPolicies = new ArrayList<Policy>();
for (List<Policy> policyList : policies) {
for (Policy policy : policyList) {
if (!map.containsKey(policy.getId())) {
map.put(policy.getId(), policy);
finalPolicies.add(policy);
}
}
}
return finalPolicies;
}
}

@ -18,6 +18,8 @@
package org.wso2.carbon.policy.mgt.core.internal;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager;
@ -25,6 +27,8 @@ public class PolicyManagementDataHolder {
private RealmService realmService;
private TenantManager tenantManager;
private PolicyEvaluationPoint policyEvaluationPoint;
private PolicyInformationPoint policyInformationPoint;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();
private PolicyManagementDataHolder() {}
@ -53,5 +57,19 @@ public class PolicyManagementDataHolder {
return tenantManager;
}
public PolicyEvaluationPoint getPolicyEvaluationPoint() {
return policyEvaluationPoint;
}
public void setPolicyEvaluationPoint(PolicyEvaluationPoint policyEvaluationPoint) {
this.policyEvaluationPoint = policyEvaluationPoint;
}
public PolicyInformationPoint getPolicyInformationPoint() {
return policyInformationPoint;
}
public void setPolicyInformationPoint(PolicyInformationPoint policyInformationPoint) {
this.policyInformationPoint = policyInformationPoint;
}
}

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager;
@ -44,6 +45,12 @@ import org.wso2.carbon.user.core.service.RealmService;
* policy="dynamic"
* bind="setPIPService"
* unbind="unsetPIPService"
* @scr.reference name="org.wso2.carbon.devicemgt.simple.policy.evaluation.manager"
* interface="org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint"
* cardinality="1..1"
* policy="dynamic"
* bind="setPEPService"
* unbind="unsetPEPService"
*/
public class PolicyManagementServiceComponent {
@ -94,16 +101,33 @@ public class PolicyManagementServiceComponent {
}
protected void setPIPService(PolicyInformationPoint policyInformationService) {
protected void setPIPService(PolicyInformationPoint pipService) {
if (log.isDebugEnabled()) {
log.debug("Setting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(pipService);
}
protected void unsetPIPService(PolicyInformationPoint pipService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(null);
}
protected void setPEPService(PolicyEvaluationPoint pepService) {
if (log.isDebugEnabled()) {
log.debug("Setting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(pepService);
}
protected void unsetPIPService(PolicyInformationPoint policyInformationService) {
protected void unsetPEPService(PolicyEvaluationPoint pepService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null);
}
}

@ -84,4 +84,14 @@ public class PolicyManagementService implements PolicyManager {
public List<Feature> getFeatures() throws FeatureManagementException {
return policyManager.getFeatures();
}
@Override
public PolicyAdministratorPoint getPAP() throws PolicyManagementException {
return policyManager.getPAP();
}
@Override
public PolicyInformationPoint getPIP() throws PolicyManagementException {
return policyManager.getPIP();
}
}

@ -79,6 +79,7 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` (
`NAME` VARCHAR(45) NULL DEFAULT NULL ,
`TENANT_ID` INT(11) NOT NULL ,
`PROFILE_ID` INT(11) NOT NULL ,
`PRIORITY` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC) ,
CONSTRAINT `FK_DM_PROFILE_DM_POLICY`
@ -151,9 +152,16 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`NAME` VARCHAR(256) NOT NULL ,
`CODE` VARCHAR(45) NULL DEFAULT NULL ,
`DEVICE_TYPE_ID` INT NOT NULL ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`EVALUVATION_RULE` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`ID`) )
PRIMARY KEY (`ID`) ,
INDEX `DM_FEATURES_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) ,
CONSTRAINT `DM_FEATURES_DEVICE_TYPE`
FOREIGN KEY (`DEVICE_TYPE_ID` )
REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
@ -241,6 +249,24 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_TIME` (
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WSO2CDM`.`DM_DATE`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `WSO2CDM`.`DM_DATE` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DATE` (
`START_DATE` DATE NOT NULL ,
`END_DATE` DATE NOT NULL ,
`POLICY_ID` INT NOT NULL ,
INDEX `DM_DATE_POLICY` (`POLICY_ID` ASC) ,
CONSTRAINT `DM_DATE_POLICY`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

@ -34,6 +34,15 @@
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
<Bundle-Description>Simple Policy Decision Point Bundle</Bundle-Description>
<Private-Package>org.wso2.carbon.simple.policy.decision.point.internal</Private-Package>
<Import-Package>
org.osgi.framework,
org.osgi.service.component,
org.apache.commons.logging,
org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.policy.mgt.core.*,
org.wso2.carbon.user.core.*,
org.wso2.carbon.device.mgt.common.*
</Import-Package>
<Export-Package>
org.wso2.carbon.simple.policy.decision.point.*
</Export-Package>

@ -19,21 +19,28 @@
package org.wso2.carbon.simple.policy.decision.point;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.PIPDeviceData;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.List;
public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint {
private SimpleEvaluationImpl evaluation;
public PolicyEvaluationServiceImpl() {
evaluation = new SimpleEvaluationImpl();
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) {
return null;
public Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
return evaluation.getEffectivePolicy(deviceIdentifier);
}
@Override
public List<Feature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) {
return null;
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
return evaluation.getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList();
}
}

@ -19,13 +19,14 @@
package org.wso2.carbon.simple.policy.decision.point;
import org.wso2.carbon.policy.mgt.common.PIPDeviceData;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
public interface SimpleEvaluation {
void sortPolicy(Policy policy) throws PolicyEvaluationException;
void sortPolicy() throws PolicyEvaluationException;
Policy getEffectivePolicy(PIPDeviceData pipDeviceData) throws PolicyEvaluationException;
Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException;
}

@ -20,28 +20,48 @@ package org.wso2.carbon.simple.policy.decision.point;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.policy.mgt.common.PIPDeviceData;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.PIPDevice;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.simple.policy.decision.point.internal.PolicyDecisionPointDataHolder;
import java.util.Collections;
import java.util.List;
public class SimpleEvaluationImpl implements SimpleEvaluation {
private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class);
PolicyDAOImpl policyDAO;
private PolicyManager policyManager;
private List<Policy> policyList;
public SimpleEvaluationImpl() {
policyDAO = new PolicyDAOImpl();
policyManager = PolicyDecisionPointDataHolder.getInstance().getPolicyManager();
}
@Override
public void sortPolicy(Policy policy) throws PolicyEvaluationException {
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
try {
if (policyManager == null && policyList == null) {
PIPDevice pipDevice = policyManager.getPIP().getDeviceData(deviceIdentifier);
policyList = policyManager.getPIP().getRelatedPolicies(pipDevice);
}
sortPolicy();
} 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 policyList.get(0);
}
@Override
public Policy getEffectivePolicy(PIPDeviceData pipDeviceData) throws PolicyEvaluationException {
return null;
public void sortPolicy() throws PolicyEvaluationException {
Collections.sort(policyList);
}
}

@ -0,0 +1,53 @@
/*
* 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.internal;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.user.core.service.RealmService;
public class PolicyDecisionPointDataHolder {
private RealmService realmService;
private PolicyManager policyManager;
private static PolicyDecisionPointDataHolder dataHolder = new PolicyDecisionPointDataHolder();
private PolicyDecisionPointDataHolder() {
}
public static PolicyDecisionPointDataHolder getInstance() {
return dataHolder;
}
public RealmService getRealmService() {
return realmService;
}
public void setRealmService(RealmService realmService) {
this.realmService = realmService;
}
public PolicyManager getPolicyManager() {
return policyManager;
}
public void setPolicyManager(PolicyManager policyManager) {
this.policyManager = policyManager;
}
}

@ -0,0 +1,109 @@
/*
* 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.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.simple.policy.decision.point.PolicyEvaluationServiceImpl;
import org.wso2.carbon.user.core.service.RealmService;
/**
* @scr.component name="org.wso2.carbon.simple.policy.decision.PolicyEvaluationServiceComponent" immediate="true"
* @scr.reference name="user.realmservice.default"
* interface="org.wso2.carbon.user.core.service.RealmService"
* cardinality="1..1"
* policy="dynamic"
* bind="setRealmService"
* unbind="unsetRealmService"
* @scr.reference name="org.wso2.carbon.devicemgt.policy.manager"
* interface="org.wso2.carbon.policy.mgt.core.PolicyManager"
* cardinality="0..1"
* policy="dynamic"
* bind="setPolicyManagerService"
* unbind="unsetPolicyManagerService"
*/
public class PolicyEvaluationServiceComponent {
private static Log log = LogFactory.getLog(PolicyEvaluationServiceComponent.class);
protected void activate(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("Activating the simple policy evaluation bundle.");
}
try {
componentContext.getBundleContext().registerService(PolicyEvaluationPoint.class.getName(),
new PolicyEvaluationServiceImpl(), null);
} catch (Throwable t) {
log.error("Error occurred while initializing the simple policy evaluation bundle");
}
}
protected void deactivate(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("De-activating the simple policy evaluation bundle.");
}
}
/**
* Sets Realm Service
*
* @param realmService An instance of RealmService
*/
protected void setRealmService(RealmService realmService) {
if (log.isDebugEnabled()) {
log.debug("Setting Realm Service");
}
PolicyDecisionPointDataHolder.getInstance().setRealmService(realmService);
}
/**
* Unsets Realm Service
*
* @param realmService An instance of RealmService
*/
protected void unsetRealmService(RealmService realmService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Realm Service");
}
PolicyDecisionPointDataHolder.getInstance().setRealmService(null);
}
protected void setPolicyManagerService(PolicyManager policyManagerService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting PolicyManager Service");
}
PolicyDecisionPointDataHolder.getInstance().setPolicyManager(policyManagerService);
}
protected void unsetPolicyManagerService(PolicyManager policyManagerService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting PolicyManager Service");
}
PolicyDecisionPointDataHolder.getInstance().setPolicyManager(null);
}
}

@ -68,3 +68,4 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
-- TO:DO - Remove this INSERT sql statement.
Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android');
Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (2, 'ios');

@ -47,7 +47,7 @@
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.complex.policy.decision.point</artifactId>
<artifactId>org.wso2.carbon.simple.policy.decision.point</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
@ -110,7 +110,7 @@
org.wso2.carbon.devicemgt:org.wso2.carbon.policy.mgt.common:${carbon.device.mgt.version}
</bundleDef>
<bundleDef>
org.wso2.carbon.devicemgt:org.wso2.carbon.complex.policy.decision.point:${carbon.device.mgt.version}
org.wso2.carbon.devicemgt:org.wso2.carbon.simple.policy.decision.point:${carbon.device.mgt.version}
</bundleDef>
<bundleDef>
org.wso2.carbon.devicemgt:org.wso2.carbon.policy.information.point:${carbon.device.mgt.version}

@ -141,6 +141,11 @@
<artifactId>org.wso2.carbon.complex.policy.decision.point</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.simple.policy.decision.point</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.information.point</artifactId>

Loading…
Cancel
Save