Adding policy monitoring to core, this is only having the monitoring daos and manager class and exceptions

merge-requests/7/head
geethkokila 10 years ago
parent d30048d94d
commit e4d156a77e

@ -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<ComplianceFeature> 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<ComplianceFeature> getComplianceFeatures() {
return complianceFeatures;
}
public void setComplianceFeatures(List<ComplianceFeature> 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;
}
}

@ -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;
}
}

@ -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);
}
}

@ -140,9 +140,9 @@ public interface PolicyAdministratorPoint {
List<Profile> 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;

@ -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<ComplianceFeature> checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response)
throws PolicyComplianceException;
}

@ -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<ComplianceFeature> CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object
deviceResponse) throws PolicyComplianceException;
boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws PolicyComplianceException;
ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
}

@ -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<ProfileFeature> effectiveFeatures = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().
List<ProfileFeature> effectiveFeatures = PolicyManagementDataHolder.getInstance()
.getPolicyEvaluationPoint().
getEffectiveFeatures(deviceIdentifier);
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<DeviceIdentifier>();
@ -211,4 +209,27 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
public int getPolicyCount() throws PolicyManagementException {
return policyAdministratorPoint.getPolicyCount();
}
@Override
public List<ComplianceFeature> CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object
deviceResponse) throws PolicyComplianceException {
return monitoringManager.checkPolicyCompliance(deviceIdentifier, deviceResponse);
}
@Override
public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws
PolicyComplianceException {
List<ComplianceFeature> 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);
}
}

@ -27,11 +27,11 @@ import java.util.List;
public interface FeatureDAO {
Feature addFeature(Feature feature) throws FeatureManagerDAOException;
/* Feature addFeature(Feature feature) throws FeatureManagerDAOException;
List<Feature> addFeatures(List<Feature> feature) throws FeatureManagerDAOException;
Feature updateFeature(Feature feature) throws FeatureManagerDAOException;
Feature updateFeature(Feature feature) throws FeatureManagerDAOException;*/
ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException;

@ -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<ComplianceFeature> complianceFeatures)
throws MonitoringDAOException;
ComplianceData getCompliance(int deviceId) throws MonitoringDAOException;
List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException;
void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException;
}

@ -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);
}
}

@ -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;
}

@ -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
*

@ -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<Feature> addFeatures(List<Feature> 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 {

@ -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<ComplianceFeature>
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<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws
MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ComplianceFeature> complianceFeatures = new ArrayList<ComplianceFeature>();
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.");
}
}
}

@ -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<ProfileFeature> 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;
}
}

@ -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 {

@ -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<String, PolicyMonitoringService> 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);
}
}

@ -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("");
}
}

@ -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<Feature> addFeatures(List<Feature> features) throws FeatureManagementException;
Feature updateFeature(Feature feature) throws FeatureManagementException;
Feature updateFeature(Feature feature) throws FeatureManagementException;*/
boolean deleteFeature(Feature feature) throws FeatureManagementException;

@ -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<ComplianceFeature> checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Object deviceResponse)
throws PolicyComplianceException;
boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
}

@ -68,4 +68,6 @@ public interface PolicyManager {
boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
int getPolicyCount() throws PolicyManagementException;
Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
}

@ -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<Feature> addFeatures(List<Feature> 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 {

@ -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<ComplianceFeature> checkPolicyCompliance(DeviceIdentifier deviceIdentifier,
Object deviceResponse) throws PolicyComplianceException {
List<ComplianceFeature> 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<ProfileFeature> 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<ComplianceFeature> 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;
}
}

@ -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<Device> deviceList = new ArrayList<Device>();
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<Integer> policyIdList;
List<Policy> policies = new ArrayList<Policy>();
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
int tenantId = PolicyManagerUtil.getTenantId();
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId);
policyIdList = policyDAO.getPolicyIdsOfDevice(device);
List<Policy> tempPolicyList = this.getPolicies();
@ -632,7 +633,7 @@ public class PolicyManagerImpl implements PolicyManager {
List<Integer> 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<ProfileFeature> 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;
}
}

@ -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<ComplianceFeature> 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);
}
}

@ -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;
}
}

@ -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) {

@ -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');

@ -25,7 +25,7 @@
<test name="DAO Unit Tests" preserve-order="true">
<parameter name="dbType" value="H2"/>
<classes>
<!--class name="org.wso2.carbon.policy.mgt.core.PolicyDAOTestCase"/-->
<class name="org.wso2.carbon.policy.mgt.core.PolicyDAOTestCase"/>
</classes>
</test>
</suite>

@ -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);
}

Loading…
Cancel
Save