Extending operation management functionality to support Profile operations

revert-70aa11f8
prabathabey 10 years ago
commit 14e7be6146

@ -25,9 +25,9 @@ import java.util.Properties;
@XmlRootElement
public class Operation {
public enum Type {
CONFIG, MESSAGE, INFO, COMMAND
}
public enum Type {
CONFIG, MESSAGE, INFO, COMMAND
}
public enum Status {
IN_PROGRES, PENDING, COMPLETED, ERROR

@ -47,16 +47,18 @@ public class OperationManagerImpl implements OperationManager {
private OperationDAO commandOperationDAO;
private OperationDAO configOperationDAO;
private OperationDAO simpleOperationDAO;
private OperationDAO profileOperationDAO;
private OperationMappingDAO operationMappingDAO;
private DeviceDAO deviceDAO;
private OperationDAO operationDAO;
public OperationManagerImpl() {
commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO();
configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO();
simpleOperationDAO = OperationManagementDAOFactory.getSimpleOperationDAO();
profileOperationDAO = OperationManagementDAOFactory.getProfileOperationDAO();
operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
operationDAO = OperationManagementDAOFactory.getOperationDAO();
}
@Override
@ -100,7 +102,21 @@ public class OperationManagerImpl implements OperationManager {
@Override
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
return null;
try {
OperationManagementDAOFactory.beginTransaction();
operationDAO.getNextOperation(deviceId);
OperationManagementDAOFactory.commitTransaction();
return null;
} catch (OperationManagementDAOException e) {
try {
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e1);
}
throw new OperationManagementException("Error occurred while adding operation", e);
}
}
@Override
@ -115,7 +131,7 @@ public class OperationManagerImpl implements OperationManager {
} else if (operation instanceof ConfigOperation) {
return configOperationDAO;
} else {
return simpleOperationDAO;
return profileOperationDAO;
}
}

@ -18,7 +18,18 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import java.io.Serializable;
public class ProfileOperation extends ConfigOperation implements Serializable {
private Object payload;
public Object getPayload() {
return payload;
}
public void setPayload(Object payload) {
this.payload = payload;
}
public class SimpleOperation extends Operation {
}

@ -18,6 +18,7 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import java.util.List;
@ -28,12 +29,14 @@ public interface OperationDAO {
int updateOperation(Operation operation) throws OperationManagementDAOException;
int deleteOperation(int id) throws OperationManagementDAOException;
int deleteOperation(int operationId) throws OperationManagementDAOException;
Operation getOperation(int id) throws OperationManagementDAOException;
Operation getOperation(int operationId) throws OperationManagementDAOException;
List<Operation> getOperations() throws OperationManagementDAOException;
List<Operation> getOperations(String status) throws OperationManagementDAOException;
Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException;
}

@ -23,10 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.CommandOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.ConfigOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.OperationMappingDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.SimpleOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.*;
import javax.sql.DataSource;
import java.sql.Connection;
@ -48,14 +45,18 @@ public class OperationManagementDAOFactory {
return new ConfigOperationDAOImpl();
}
public static OperationDAO getSimpleOperationDAO() {
return new SimpleOperationDAOImpl();
public static OperationDAO getProfileOperationDAO() {
return new ProfileOperationDAOImpl();
}
public static OperationMappingDAO getOperationMappingDAO() {
return new OperationMappingDAOImpl();
}
public static OperationDAO getOperationDAO() {
return new OperationDAOImpl();
}
public static void init(DataSource dtSource) {
dataSource = dtSource;
}

@ -18,20 +18,20 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class CommandOperationDAOImpl extends AbstractOperationDAO {
public class CommandOperationDAOImpl extends OperationDAOImpl {
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {
@ -79,4 +79,9 @@ public class CommandOperationDAOImpl extends AbstractOperationDAO {
return null;
}
@Override
public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException {
return null;
}
}

@ -18,14 +18,13 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import javax.sql.DataSource;
import java.util.List;
public class ConfigOperationDAOImpl extends AbstractOperationDAO {
public class ConfigOperationDAOImpl extends OperationDAOImpl {
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {
@ -57,4 +56,9 @@ public class ConfigOperationDAOImpl extends AbstractOperationDAO {
return null;
}
@Override
public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException {
return null;
}
}

@ -18,6 +18,7 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.*;
@ -28,8 +29,9 @@ import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;
import java.sql.SQLException;
import java.util.List;
public abstract class AbstractOperationDAO implements OperationDAO {
public class OperationDAOImpl implements OperationDAO {
public int addOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
@ -57,4 +59,33 @@ public abstract class AbstractOperationDAO implements OperationDAO {
}
}
@Override
public int updateOperation(Operation operation) throws OperationManagementDAOException {
return 0;
}
@Override
public int deleteOperation(int id) throws OperationManagementDAOException {
return 0;
}
public Operation getOperation(int id) throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations() throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations(String status) throws OperationManagementDAOException {
return null;
}
@Override
public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException {
return null;
}
}

@ -0,0 +1,155 @@
/*
* 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.device.mgt.core.operation.mgt.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class ProfileOperationDAOImpl extends OperationDAOImpl {
private static final Log log = LogFactory.getLog(ProfileOperationDAOImpl.class);
public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId = super.addOperation(operation);
ProfileOperation profileOp = (ProfileOperation) operation;
Connection conn = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try {
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(profileOp);
stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, PAYLOAD) VALUES(?, ?)");
stmt.setInt(1, operationId);
stmt.setBytes(2, bao.toByteArray());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding profile operation", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationId;
}
@Override
public int updateOperation(Operation operation) throws OperationManagementDAOException {
return 0;
}
@Override
public int deleteOperation(int id) throws OperationManagementDAOException {
return 0;
}
@Override
public Operation getOperation(int operationId) throws OperationManagementDAOException {
Connection conn = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
stmt = conn.prepareStatement("SELECT PAYLOAD FROM DM_PROFILE_OPERATION WHERE ID = ?");
stmt.setInt(1, operationId);
rs = stmt.executeQuery();
byte[] payload = new byte[0];
if (rs.next()) {
payload = rs.getBytes("PAYLOAD");
}
bais = new ByteArrayInputStream(payload);
ois = new ObjectInputStream(bais);
return (ProfileOperation) ois.readObject();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding profile operation", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} catch (ClassNotFoundException e) {
throw new OperationManagementDAOException("Error occurred while casting retrieved payload as a " +
"ProfileOperation object", e);
} 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);
}
}
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<Operation> getOperations() throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations(String status) throws OperationManagementDAOException {
return null;
}
@Override
public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException {
return null;
}
}

@ -1,54 +0,0 @@
/*
* 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.device.mgt.core.operation.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import javax.sql.DataSource;
import java.util.List;
public class SimpleOperationDAOImpl extends AbstractOperationDAO {
@Override
public int updateOperation(Operation operation) throws OperationManagementDAOException {
return 0;
}
@Override
public int deleteOperation(int id) throws OperationManagementDAOException {
return 0;
}
@Override
public Operation getOperation(int id) throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations() throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations(String status) throws OperationManagementDAOException {
return null;
}
}

@ -83,7 +83,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
public FeatureManager getFeatureManager(String type) throws DeviceManagementException {
return null;
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getFeatureManager(type);
}
@Override

@ -89,7 +89,7 @@ public class DeviceManagementBaseTest {
try {
conn = this.getDataSource().getConnection();
stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
} catch(Exception e){
log.error(e);
throw e;

@ -108,7 +108,7 @@ public class DeviceManagementDAOTests {
try {
conn = this.getDataSource().getConnection();
stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}

@ -45,6 +45,14 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION (
OPERATION_ID INTEGER NOT NULL,
PAYLOAD BLOB NOT 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,

@ -20,11 +20,11 @@ package org.wso2.carbon.policy.evaluator.utils;
public class Constants {
public static final String DENY_OVERRIDES="deny_overrides";
public static final String PERMIT_OVERRIDES="permit_overrides";
public static final String FIRST_APPLICABLE="first_applicable";
public static final String LAST_APPLICABLE="last_applicable";
public static final String ALL_APPLICABLE="all_applicable";
public static final String HIGHEST_APPLICABLE="highest_applicable";
public static final String LOWEST_APPLICABLE="lowest_applicable";
public static final String DENY_OVERRIDES = "deny_overrides";
public static final String PERMIT_OVERRIDES = "permit_overrides";
public static final String FIRST_APPLICABLE = "first_applicable";
public static final String LAST_APPLICABLE = "last_applicable";
public static final String ALL_APPLICABLE = "all_applicable";
public static final String HIGHEST_APPLICABLE = "highest_applicable";
public static final String LOWEST_APPLICABLE = "lowest_applicable";
}

Loading…
Cancel
Save