forked from community/device-mgt-core
Adding policy monitoring to core, this is only having the monitoring daos and manager class and exceptions
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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue