From e4d156a77e0a7dd5468f0bacce4ccd4c8ac2c8f4 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 3 Jul 2015 13:03:27 +0530 Subject: [PATCH] Adding policy monitoring to core, this is only having the monitoring daos and manager class and exceptions --- .../mgt/common/Monitor/ComplianceData.java | 80 ++++++ .../mgt/common/Monitor/ComplianceFeature.java | 63 +++++ .../Monitor/PolicyComplianceException.java | 56 +++++ .../mgt/common/PolicyAdministratorPoint.java | 6 +- .../common/spi/PolicyMonitoringService.java | 33 +++ .../policy/mgt/core/PolicyManagerService.java | 12 + .../mgt/core/PolicyManagerServiceImpl.java | 43 +++- .../policy/mgt/core/dao/FeatureDAO.java | 4 +- .../policy/mgt/core/dao/MonitoringDAO.java | 42 ++++ .../mgt/core/dao/MonitoringDAOException.java | 57 +++++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 5 + .../core/dao/PolicyManagementDAOFactory.java | 5 + .../mgt/core/dao/impl/FeatureDAOImpl.java | 12 +- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 235 ++++++++++++++++++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 106 +++++++- .../impl/PolicyAdministratorPointImpl.java | 4 +- .../internal/PolicyManagementDataHolder.java | 17 ++ .../PolicyManagementServiceComponent.java | 24 ++ .../policy/mgt/core/mgt/FeatureManager.java | 4 +- .../mgt/core/mgt/MonitoringManager.java | 39 +++ .../policy/mgt/core/mgt/PolicyManager.java | 2 + .../mgt/core/mgt/impl/FeatureManagerImpl.java | 12 +- .../core/mgt/impl/MonitoringManagerImpl.java | 183 ++++++++++++++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 37 ++- .../core/service/PolicyManagementService.java | 28 ++- .../mgt/core/util/PolicyManagerUtil.java | 15 +- .../policy/mgt/core/PolicyDAOTestCase.java | 2 + .../src/test/resources/sql/CreateH2TestDB.sql | 204 ++++++++------- .../src/test/resources/testng.xml | 2 +- .../decision/point/SimpleEvaluationImpl.java | 4 +- 30 files changed, 1184 insertions(+), 152 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java new file mode 100644 index 0000000000..85234fff61 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java @@ -0,0 +1,80 @@ +/* + * 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.Monitor; + +import java.util.List; + +public class ComplianceData { + + private int id; + private int deviceId; + private int policyId; + List complianceFeatures; + private boolean status; + private String message; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getDeviceId() { + return deviceId; + } + + public void setDeviceId(int deviceId) { + this.deviceId = deviceId; + } + + public int getPolicyId() { + return policyId; + } + + public void setPolicyId(int policyId) { + this.policyId = policyId; + } + + public List getComplianceFeatures() { + return complianceFeatures; + } + + public void setComplianceFeatures(List complianceFeatures) { + this.complianceFeatures = complianceFeatures; + } + + public boolean isStatus() { + return status; + } + + public void setStatus(boolean status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java new file mode 100644 index 0000000000..7c4589a8ab --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java @@ -0,0 +1,63 @@ +/* + * 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.Monitor; + +import org.wso2.carbon.policy.mgt.common.ProfileFeature; + +public class ComplianceFeature { + + private ProfileFeature feature; + private String featureCode; + private boolean compliance; + private String message; + + + public ProfileFeature getFeature() { + return feature; + } + + public void setFeature(ProfileFeature feature) { + this.feature = feature; + } + + public String getFeatureCode() { + return featureCode; + } + + public void setFeatureCode(String featureCode) { + this.featureCode = featureCode; + } + + public boolean isCompliance() { + return compliance; + } + + public void setCompliance(boolean compliance) { + this.compliance = compliance; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java new file mode 100644 index 0000000000..d579546dd9 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java @@ -0,0 +1,56 @@ +/* + * 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.Monitor; + +public class PolicyComplianceException extends Exception { + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public PolicyComplianceException(String message) { + super(message); + setErrorMessage(message); + } + + public PolicyComplianceException(String message, Exception ex) { + super(message, ex); + setErrorMessage(message); + } + + public PolicyComplianceException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public PolicyComplianceException() { + super(); + } + + public PolicyComplianceException(Throwable cause) { + super(cause); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 1910ee4615..f59e722d5f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -140,9 +140,9 @@ public interface PolicyAdministratorPoint { List getProfiles() throws PolicyManagementException; - Feature addFeature(Feature feature) throws FeatureManagementException; - - Feature updateFeature(Feature feature) throws FeatureManagementException; +// Feature addFeature(Feature feature) throws FeatureManagementException; +// +// Feature updateFeature(Feature feature) throws FeatureManagementException; boolean deleteFeature(int featureId) throws FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java new file mode 100644 index 0000000000..4d5e34b368 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java @@ -0,0 +1,33 @@ +/* + * 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.spi; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + +public interface PolicyMonitoringService { + + List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response) + throws PolicyComplianceException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index 0214e34448..3a5a48ab52 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -22,6 +22,9 @@ package org.wso2.carbon.policy.mgt.core; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; @@ -34,9 +37,11 @@ import java.util.List; public interface PolicyManagerService { +/* Feature addFeature(Feature feature) throws FeatureManagementException; Feature updateFeature(Feature feature) throws FeatureManagementException; +*/ Profile addProfile(Profile profile) throws PolicyManagementException; @@ -65,4 +70,11 @@ public interface PolicyManagerService { PolicyEvaluationPoint getPEP() throws PolicyManagementException; int getPolicyCount() throws PolicyManagementException; + + List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object + deviceResponse) throws PolicyComplianceException; + + boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws PolicyComplianceException; + + ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index c6da23346d..c4fcd2a4e8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -27,9 +27,14 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.impl.PolicyInformationPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; +import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import java.util.ArrayList; @@ -41,19 +46,11 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class); PolicyAdministratorPointImpl policyAdministratorPoint; + MonitoringManager monitoringManager; public PolicyManagerServiceImpl() { policyAdministratorPoint = new PolicyAdministratorPointImpl(); - } - - @Override - public Feature addFeature(Feature feature) throws FeatureManagementException { - return policyAdministratorPoint.addFeature(feature); - } - - @Override - public Feature updateFeature(Feature feature) throws FeatureManagementException { - return policyAdministratorPoint.updateFeature(feature); + monitoringManager = new MonitoringManagerImpl(); } @Override @@ -144,7 +141,8 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { FeatureManagementException { try { - List effectiveFeatures = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). + List effectiveFeatures = PolicyManagementDataHolder.getInstance() + .getPolicyEvaluationPoint(). getEffectiveFeatures(deviceIdentifier); List deviceIdentifiers = new ArrayList(); @@ -211,4 +209,27 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { public int getPolicyCount() throws PolicyManagementException { return policyAdministratorPoint.getPolicyCount(); } + + @Override + public List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object + deviceResponse) throws PolicyComplianceException { + return monitoringManager.checkPolicyCompliance(deviceIdentifier, deviceResponse); + } + + @Override + public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws + PolicyComplianceException { + + List complianceFeatures = + monitoringManager.checkPolicyCompliance(deviceIdentifier, response); + if (complianceFeatures == null || complianceFeatures.isEmpty()) { + return false; + } + return true; + } + + @Override + public ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + return monitoringManager.getDevicePolicyCompliance(deviceIdentifier); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index f35e5070ea..1c08a18789 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -27,11 +27,11 @@ import java.util.List; public interface FeatureDAO { - Feature addFeature(Feature feature) throws FeatureManagerDAOException; +/* Feature addFeature(Feature feature) throws FeatureManagerDAOException; List addFeatures(List feature) throws FeatureManagerDAOException; - Feature updateFeature(Feature feature) throws FeatureManagerDAOException; + Feature updateFeature(Feature feature) throws FeatureManagerDAOException;*/ ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java new file mode 100644 index 0000000000..e97501dc05 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.dao; + +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; + +import java.util.List; +import java.util.Map; + +public interface MonitoringDAO { + + int setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException; + + void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; + + void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) + throws MonitoringDAOException; + + ComplianceData getCompliance(int deviceId) throws MonitoringDAOException; + + List getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException; + + void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java new file mode 100644 index 0000000000..5ab28f187f --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.dao; + +public class MonitoringDAOException extends Exception{ + + private String monitoringDAOErrorMessage; + + public String getMonitoringDAOErrorMessage() { + return monitoringDAOErrorMessage; + } + + public void setMonitoringDAOErrorMessage(String monitoringDAOErrorMessage) { + this.monitoringDAOErrorMessage = monitoringDAOErrorMessage; + } + + public MonitoringDAOException(String message) { + super(message); + setMonitoringDAOErrorMessage(message); + } + + public MonitoringDAOException(String message, Exception ex) { + super(message, ex); + setMonitoringDAOErrorMessage(message); + } + + public MonitoringDAOException(String message, Throwable cause) { + super(message, cause); + setMonitoringDAOErrorMessage(message); + } + + public MonitoringDAOException() { + super(); + } + + public MonitoringDAOException(Throwable cause) { + super(cause); + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index b8c3fb32d5..92940de313 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -19,6 +19,7 @@ package org.wso2.carbon.policy.mgt.core.dao; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyCriterion; @@ -99,4 +100,8 @@ public interface PolicyDAO { boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; int getPolicyCount() throws PolicyManagerDAOException; + + int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException; + + Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index ab0c5e3697..d7d74daf50 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOE import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.MonitoringDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; @@ -67,6 +68,10 @@ public class PolicyManagementDAOFactory { return new FeatureDAOImpl(); } + public static MonitoringDAO getMonitoringDAO() { + return new MonitoringDAOImpl(); + } + /** * Resolve data source from the data source definition * diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 9324d3ba45..1c4b67a564 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -42,7 +42,7 @@ public class FeatureDAOImpl implements FeatureDAO { private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); - @Override +/* @Override public Feature addFeature(Feature feature) throws FeatureManagerDAOException { Connection conn; @@ -75,9 +75,9 @@ public class FeatureDAOImpl implements FeatureDAO { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } return feature; - } + }*/ - @Override + /* @Override public List addFeatures(List features) throws FeatureManagerDAOException { Connection conn; @@ -119,10 +119,10 @@ public class FeatureDAOImpl implements FeatureDAO { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } return featureList; - } + }*/ - @Override + /* @Override public Feature updateFeature(Feature feature) throws FeatureManagerDAOException { Connection conn; @@ -147,7 +147,7 @@ public class FeatureDAOImpl implements FeatureDAO { } return feature; - } + }*/ @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java new file mode 100644 index 0000000000..e34f4eab84 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException; +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.util.PolicyManagementDAOUtil; + +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 MonitoringDAOImpl implements MonitoringDAO { + + private static final Log log = LogFactory.getLog(MonitoringDAOImpl.class); + + @Override + public int setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS) VALUES" + + " (?, ?, ?) "; + stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt.setInt(1, deviceId); + stmt.setInt(2, policyId); + stmt.setInt(3, 0); + stmt.executeUpdate(); + + generatedKeys = stmt.getGeneratedKeys(); + if (generatedKeys.next()) { + return generatedKeys.getInt(1); + } else { + return 0; + } + + } catch (SQLException e) { + String msg = "Error occurred while adding the none compliance to the database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); + } + + } + + @Override + public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Error occurred while deleting the none compliance to the database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + complianceFeatures) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS) " + + "VALUES" + + " (?, ?, ?) "; + stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + for (ComplianceFeature feature : complianceFeatures) { + stmt.setInt(1, policyComplianceStatusId); + stmt.setString(2, feature.getFeatureCode()); + stmt.setString(3, String.valueOf(feature.isCompliance())); + stmt.addBatch(); + } + stmt.executeBatch(); + + } catch (SQLException e) { + String msg = "Error occurred while adding the none compliance features to the database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public ComplianceData getCompliance(int deviceId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + ComplianceData complianceData = null; + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + complianceData.setId(resultSet.getInt("ID")); + complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); + complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); + complianceData.setStatus(resultSet.getBoolean("STATUS")); + } + return complianceData; + + } catch (SQLException e) { + String msg = "Unable to retrieve compliance data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + } + + @Override + public List getNoneComplianceFeatures(int policyComplianceStatusId) throws + MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List complianceFeatures = new ArrayList(); + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, policyComplianceStatusId); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + ComplianceFeature feature = new ComplianceFeature(); + feature.setFeatureCode(resultSet.getString("FEATURE_CODE")); + feature.setMessage(resultSet.getString("STATUS")); + complianceFeatures.add(feature); + } + return complianceFeatures; + + } catch (SQLException e) { + String msg = "Unable to retrieve compliance features data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + + } + + @Override + public void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Unable to delete compliance data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + + } + + + private Connection getConnection() throws MonitoringDAOException { + try { + return PolicyManagementDAOFactory.getConnection(); + } catch (PolicyManagerDAOException e) { + throw new MonitoringDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", e); + } + } + + + private void closeConnection() { + try { + PolicyManagementDAOFactory.closeConnection(); + } catch (PolicyManagerDAOException e) { + log.warn("Unable to close the database connection."); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 551243e318..cf82d26ce9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -21,16 +21,21 @@ 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.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyCriterion; import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; 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.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.*; import java.util.ArrayList; import java.util.Calendar; @@ -429,7 +434,8 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, CONTENT) VALUES (?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + + "CONTENT) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(query); for (PolicyCriterion criterion : policyCriteria) { @@ -517,7 +523,8 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ? WHERE ID = ?"; + String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + + " WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getTenantId()); @@ -740,7 +747,6 @@ public class PolicyDAOImpl implements PolicyDAO { } - @Override public void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) throws PolicyManagerDAOException { @@ -1060,7 +1066,8 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE) VALUES (?, ?, ?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE)" + + " VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); @@ -1191,4 +1198,95 @@ public class PolicyDAOImpl implements PolicyDAO { } } + @Override + public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + return resultSet.getInt("POLICY_ID"); + } + + } catch (SQLException e) { + String msg = "Error occurred while getting the applied policy id."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + + return 0; + } + + @Override + public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + Policy policy = null; + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + resultSet = stmt.executeQuery(); + + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + byte[] contentBytes; + try { + contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + policy = (Policy) ois.readObject(); + } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } + } + + } catch (SQLException e) { + String msg = "Error occurred while getting the applied policy."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Unable to read the byte stream for content"; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } catch (ClassNotFoundException e) { + String msg = "Class not found while converting the object"; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + return policy; + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 2041d99713..052199f4e0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -188,7 +188,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { } } - @Override + /* @Override public Feature addFeature(Feature feature) throws FeatureManagementException { return featureManager.addFeature(feature); } @@ -197,7 +197,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { public Feature updateFeature(Feature feature) throws FeatureManagementException { return featureManager.updateFeature(feature); - } + }*/ @Override public boolean deleteFeature(int featureId) throws FeatureManagementException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java index 506488f762..4f2d664494 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java @@ -21,9 +21,12 @@ package org.wso2.carbon.policy.mgt.core.internal; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; +import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; +import java.util.Map; + public class PolicyManagementDataHolder { private RealmService realmService; @@ -31,6 +34,8 @@ public class PolicyManagementDataHolder { private PolicyEvaluationPoint policyEvaluationPoint; private PolicyInformationPoint policyInformationPoint; private DeviceManagementProviderService deviceManagementService; + private Map policyMonitoringServiceMap; + private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); private PolicyManagementDataHolder() {} @@ -82,4 +87,16 @@ public class PolicyManagementDataHolder { public void setDeviceManagementService(DeviceManagementProviderService deviceManagementService) { this.deviceManagementService = deviceManagementService; } + + public PolicyMonitoringService getPolicyMonitoringService(String deviceType) { + return policyMonitoringServiceMap.get(deviceType); + } + + public void setPolicyMonitoringService(String deviceType, PolicyMonitoringService policyMonitoringService) { + this.policyMonitoringServiceMap.put(deviceType, policyMonitoringService); + } + + public void unsetPolicyMonitoringService(String deviceType) { + this.policyMonitoringServiceMap.remove(deviceType); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index c0f8450ac1..d6f72dba74 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; +import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; @@ -51,6 +52,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setDeviceManagementService" * unbind="unsetDeviceManagementService" + * @scr.reference name="org.wso2.carbon.policy.mgt.common.policy.monitor" + * interface="org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService" + * cardinality="0..n" + * policy="dynamic" + * bind="setPolicyMonitoringService" + * unbind="unsetPolicyMonitoringService" */ @SuppressWarnings("unused") public class PolicyManagementServiceComponent { @@ -143,4 +150,21 @@ public class PolicyManagementServiceComponent { PolicyManagementDataHolder.getInstance().setDeviceManagementService(null); } + + protected void setPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { + if (log.isDebugEnabled()) { + log.debug("Setting Policy Monitoring Service"); + } + // TODO: FIX THE device type by taking from properties + PolicyManagementDataHolder.getInstance().setPolicyMonitoringService("", policyMonitoringService); + } + + protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { + if (log.isDebugEnabled()) { + log.debug("Setting Policy Monitoring Service"); + } + // TODO: FIX THE device type by taking from properties + PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(""); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java index 5af6049f5b..8ccab296e5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java @@ -29,11 +29,11 @@ import java.util.List; public interface FeatureManager { - Feature addFeature(Feature feature) throws FeatureManagementException; + /*Feature addFeature(Feature feature) throws FeatureManagementException; public List addFeatures(List features) throws FeatureManagementException; - Feature updateFeature(Feature feature) throws FeatureManagementException; + Feature updateFeature(Feature feature) throws FeatureManagementException;*/ boolean deleteFeature(Feature feature) throws FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java new file mode 100644 index 0000000000..f0ca764d8c --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.mgt; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; + +import java.util.List; + +public interface MonitoringManager { + + List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Object deviceResponse) + throws PolicyComplianceException; + + + boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + + ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index 924a57116b..0aa8c11ac7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -68,4 +68,6 @@ public interface PolicyManager { boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; int getPolicyCount() throws PolicyManagementException; + + Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java index 7b9f9aa801..94ee440a10 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java @@ -41,7 +41,7 @@ public class FeatureManagerImpl implements FeatureManager { featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); } - @Override + /*@Override public Feature addFeature(Feature feature) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -68,9 +68,9 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException(msg, e); } return feature; - } + }*/ - @Override + /*@Override public List addFeatures(List features) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -98,9 +98,9 @@ public class FeatureManagerImpl implements FeatureManager { } return features; - } + }*/ - @Override + /* @Override public Feature updateFeature(Feature feature) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -127,7 +127,7 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException(msg, e); } return feature; - } + }*/ @Override public boolean deleteFeature(Feature feature) throws FeatureManagementException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java new file mode 100644 index 0000000000..56c4f593b8 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.mgt.impl; + +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.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException; +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.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import java.util.List; + +public class MonitoringManagerImpl implements MonitoringManager { + + private PolicyDAO policyDAO; + private DeviceDAO deviceDAO; + private MonitoringDAO monitoringDAO; + + private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); + + public MonitoringManagerImpl() { + this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO(); + } + + @Override + public List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, + Object deviceResponse) throws PolicyComplianceException { + + List complianceFeatures; + try { + PolicyManagementDAOFactory.beginTransaction(); + int tenantId = PolicyManagerUtil.getTenantId(); + + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + Policy policy = policyDAO.getAppliedPolicy(device.getId()); + PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance(). + getPolicyMonitoringService(deviceIdentifier.getType()); + + complianceFeatures = monitoringService.checkPolicyCompliance(deviceIdentifier, + policy, deviceResponse); + + if (!complianceFeatures.isEmpty()) { + int complianceId = monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); + monitoringDAO.addNoneComplianceFeatures(complianceId, device.getId(), complianceFeatures); + + List profileFeatures = policy.getProfile().getProfileFeaturesList(); + for (ComplianceFeature compFeature : complianceFeatures) { + for (ProfileFeature profFeature : profileFeatures) { + if (profFeature.getFeatureCode().equalsIgnoreCase(compFeature.getFeatureCode())) { + compFeature.setFeature(profFeature); + } + } + } + + } else { + monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); + } + PolicyManagementDAOFactory.commitTransaction(); + + } catch (DeviceManagementDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Unable tor retrieve device data from DB for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } catch (PolicyManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Unable tor retrieve policy data from DB for device " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } catch (MonitoringDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Unable to add the none compliance features to database for device " + deviceIdentifier. + getId() + " - " + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + return complianceFeatures; + } + + + @Override + public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + ComplianceData complianceData = monitoringDAO.getCompliance(device.getId()); + if (complianceData == null || !complianceData.isStatus()) { + return false; + } + + } catch (DeviceManagementDAOException e) { + String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + + } catch (MonitoringDAOException e) { + String msg = "Unable to retrieve compliance status for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + return true; + } + + @Override + public ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws + PolicyComplianceException { + + ComplianceData complianceData; + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + complianceData = monitoringDAO.getCompliance(device.getId()); + List complianceFeatures = + monitoringDAO.getNoneComplianceFeatures(complianceData.getId()); + complianceData.setComplianceFeatures(complianceFeatures); + + } catch (DeviceManagementDAOException e) { + String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + + } catch (MonitoringDAOException e) { + String msg = "Unable to retrieve compliance data for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + return complianceData; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 0426a0fad2..15013de7f0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -30,6 +30,7 @@ import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.Timestamp; import java.util.ArrayList; @@ -288,7 +289,7 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicy(policy); } List deviceList = new ArrayList(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { deviceList.add(deviceDAO.getDevice(deviceIdentifier, tenantId)); } @@ -517,7 +518,7 @@ public class PolicyManagerImpl implements PolicyManager { List policyIdList; List policies = new ArrayList(); try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); policyIdList = policyDAO.getPolicyIdsOfDevice(device); List tempPolicyList = this.getPolicies(); @@ -632,7 +633,7 @@ public class PolicyManagerImpl implements PolicyManager { List deviceIds; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); for (int deviceId : deviceIds) { //TODO FIX ME @@ -654,9 +655,10 @@ public class PolicyManagerImpl implements PolicyManager { @Override public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List profileFeatures) throws PolicyManagementException { + int deviceId = -1; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); deviceId = device.getId(); boolean exist = policyDAO.checkPolicyAvailable(deviceId); @@ -690,7 +692,7 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); exist = policyDAO.checkPolicyAvailable(device.getId()); } catch (PolicyManagerDAOException e) { @@ -709,7 +711,7 @@ public class PolicyManagerImpl implements PolicyManager { public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); policyDAO.setPolicyApplied(device.getId()); return true; @@ -727,6 +729,7 @@ public class PolicyManagerImpl implements PolicyManager { @Override public int getPolicyCount() throws PolicyManagementException { + int policyCount = 0; try { policyCount = policyDAO.getPolicyCount(); @@ -737,4 +740,26 @@ public class PolicyManagerImpl implements PolicyManager { throw new PolicyManagementException(msg, e); } } + + @Override + public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + + Policy policy; + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + int policyId = policyDAO.getAppliedPolicyId(device.getId()); + policy = policyDAO.getPolicy(policyId); + + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting device id."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while getting policy id or policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return policy; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java index 46b51c7352..a6cebdd59c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -21,6 +21,9 @@ package org.wso2.carbon.policy.mgt.core.service; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; @@ -42,15 +45,6 @@ public class PolicyManagementService implements PolicyManagerService { policyManagerService = new PolicyManagerServiceImpl(); } - @Override - public Feature addFeature(Feature feature) throws FeatureManagementException { - return policyManagerService.addFeature(feature); - } - - @Override - public Feature updateFeature(Feature feature) throws FeatureManagementException { - return policyManagerService.updateFeature(feature); - } @Override public Profile addProfile(Profile profile) throws PolicyManagementException { @@ -122,4 +116,20 @@ public class PolicyManagementService implements PolicyManagerService { public int getPolicyCount() throws PolicyManagementException { return policyManagerService.getPolicyCount(); } + + @Override + public List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object + deviceResponse) throws PolicyComplianceException { + return policyManagerService.CheckPolicyCompliance(deviceIdentifier, deviceResponse); + } + + @Override + public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws PolicyComplianceException { + return policyManagerService.checkCompliance(deviceIdentifier, response); + } + + @Override + public ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + return policyManagerService.getDeviceCompliance(deviceIdentifier); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 08acd03c79..8e2ca941d0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -90,17 +90,12 @@ public class PolicyManagerUtil { //TODO: Get the tenant id proper way. This is has to be fix for test to run. int tenantId; - tenantId = MultitenantConstants.SUPER_TENANT_ID; -/* try { - if (PrivilegedCarbonContext.getThreadLocalCarbonContext() != null) { - tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - } else { - tenantId = MultitenantConstants.SUPER_TENANT_ID; - } - } catch (Exception e) { - - }*/ + if ("Super".equalsIgnoreCase(System.getProperty("GetTenantIDForTest"))) { + tenantId = MultitenantConstants.SUPER_TENANT_ID; + } else { + tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + } return tenantId; } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index a3138da535..f3fc808127 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -77,6 +77,8 @@ public class PolicyDAOTestCase { TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); PoolProperties properties = new PoolProperties(); + System.setProperty("GetTenantIDForTest","Super"); + log.info("Database Type : " + dbTypeStr); switch (dbType) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 7f291415de..b4bfe487a2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -1,49 +1,105 @@ +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( + ID INT auto_increment NOT NULL, + NAME VARCHAR(300) NULL DEFAULT NULL, + PRIMARY KEY (ID) +); --- ----------------------------------------------------- --- Table DM_DEVICE_TYPE --- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS DM_DEVICE ( + ID INTEGER auto_increment NOT NULL, + DESCRIPTION TEXT NULL DEFAULT NULL, + NAME VARCHAR(100) NULL DEFAULT NULL, + DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(15) NULL DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, + OWNER VARCHAR(45) NULL DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); +CREATE TABLE IF NOT EXISTS DM_OPERATION ( + ID INTEGER AUTO_INCREMENT NOT NULL, + TYPE VARCHAR(50) NOT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + RECEIVED_TIMESTAMP TIMESTAMP NULL, + OPERATION_CODE VARCHAR(1000) NOT NULL, + PRIMARY KEY (ID) +); -CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( - ID INT(11) AUTO_INCREMENT , - NAME VARCHAR(300) NULL DEFAULT NULL , - PRIMARY KEY (ID) ) +CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + OPERATION_CONFIG BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -; +CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); ---INSERT INTO DM_DEVICE_TYPE (NAME) VALUES ('ANDROID'); ---INSERT INTO DM_DEVICE_TYPE (NAME) VALUES ('IOS'); +CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_DEVICE --- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -CREATE TABLE IF NOT EXISTS DM_DEVICE ( - ID INT(11) AUTO_INCREMENT NOT NULL , - DESCRIPTION TEXT NULL DEFAULT NULL , - NAME VARCHAR(100) NULL DEFAULT NULL , - DATE_OF_ENROLLMENT BIGINT(20) NULL DEFAULT NULL , - DATE_OF_LAST_UPDATE BIGINT(20) NULL DEFAULT NULL , - OWNERSHIP VARCHAR(45) NULL DEFAULT NULL , - STATUS VARCHAR(15) NULL DEFAULT NULL , - DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL , - DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL , - OWNER VARCHAR(45) NULL DEFAULT NULL , - TENANT_ID INT(11) NULL DEFAULT '0' , - PRIMARY KEY (ID) , - CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 - FOREIGN KEY (DEVICE_TYPE_ID ) - REFERENCES DM_DEVICE_TYPE (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + OPERATION_RESPONSE BLOB DEFAULT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_response_operation FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -; +CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATIONS ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + APPLICATIONS BLOB DEFAULT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_applications_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, +); + +--- POLICY RELATED TABLES ---- --- ----------------------------------------------------- --- Table DM_PROFILE --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_PROFILE ( @@ -58,13 +114,11 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE ( FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) -; + ON UPDATE NO ACTION +); + --- ----------------------------------------------------- --- Table DM_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_POLICY ( @@ -80,14 +134,10 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( FOREIGN KEY (PROFILE_ID ) REFERENCES DM_PROFILE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_DEVICE_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( @@ -104,14 +154,10 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( FOREIGN KEY (DEVICE_ID ) REFERENCES DM_DEVICE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_DEVICE_TYPE_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( @@ -128,15 +174,11 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_PROFILE_FEATURES --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( @@ -150,14 +192,10 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( FOREIGN KEY (PROFILE_ID) REFERENCES DM_PROFILE (ID) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_ROLE_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( @@ -169,15 +207,11 @@ CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table .DM_USER_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( ID INT NOT NULL AUTO_INCREMENT , @@ -188,10 +222,10 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - ; - - + ON UPDATE NO ACTION +); + + CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( ID INT NOT NULL AUTO_INCREMENT , DEVICE_ID INT NOT NULL , @@ -211,14 +245,10 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_CRITERIA --- ----------------------------------------------------- -DROP TABLE IF EXISTS DM_CRITERIA ; CREATE TABLE IF NOT EXISTS DM_CRITERIA ( ID INT NOT NULL AUTO_INCREMENT, @@ -228,10 +258,6 @@ CREATE TABLE IF NOT EXISTS DM_CRITERIA ( ); --- ----------------------------------------------------- --- Table DM_POLICY_CRITERIA --- ----------------------------------------------------- -DROP TABLE IF EXISTS DM_POLICY_CRITERIA ; CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( ID INT NOT NULL AUTO_INCREMENT, @@ -251,10 +277,6 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( ); --- ----------------------------------------------------- --- Table DM_POLICY_CRITERIA_PROPERTIES --- ----------------------------------------------------- -DROP TABLE IF EXISTS DM_POLICY_CRITERIA_PROPERTIES ; CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ID INT NOT NULL AUTO_INCREMENT, @@ -271,3 +293,9 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ); +-- POLICY RELATED TABLES FINISHED -- + + +-- 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'); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 474d352e7a..02107984d5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -25,7 +25,7 @@ - + \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java index 188a742f3d..f7b7135eb5 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -61,8 +61,8 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { return null; } //TODO : UNCOMMENT THE FOLLOWING CASE -// policyAdministratorPoint = policyManagerService.getPAP(); -// policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); + policyAdministratorPoint = policyManagerService.getPAP(); + policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); }