Integrate lifecycle management functionality

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent a79b600922
commit e467453fd3

@ -68,11 +68,6 @@ public class Filter {
*/ */
private int deviceTypeId; private int deviceTypeId;
/***
* Package Name of the application release. i.e org.wso2.iot.agent etc
*/
private String packageName;
public int getLimit() { public int getLimit() {
return limit; return limit;
} }
@ -138,8 +133,4 @@ public class Filter {
public int getDeviceTypeId() { return deviceTypeId; } public int getDeviceTypeId() { return deviceTypeId; }
public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; } public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; }
public String getPackageName() { return packageName; }
public void setPackageName(String packageName) { this.packageName = packageName; }
} }

@ -177,6 +177,6 @@ public interface ApplicationReleaseDAO {
* @return True if application release package name already exist in the IoT server, Otherwise returns False. * @return True if application release package name already exist in the IoT server, Otherwise returns False.
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
boolean isAppExisitForPackageName (String packageName, int tenantId) throws ApplicationManagementDAOException; boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId) throws ApplicationManagementDAOException;
} }

@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO;
import org.wso2.carbon.device.application.mgt.common.ApplicationReleaseArtifactPaths; import org.wso2.carbon.device.application.mgt.common.ApplicationReleaseArtifactPaths;
import org.wso2.carbon.device.application.mgt.common.Rating; import org.wso2.carbon.device.application.mgt.common.Rating;
@ -657,7 +658,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
} }
@Override @Override
public boolean isAppExisitForPackageName (String packageName, int tenantId) throws ApplicationManagementDAOException { public boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Verifying application release existence for package name:" + packageName); log.debug("Verifying application release existence for package name:" + packageName);
} }
@ -666,11 +667,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
conn = this.getDBConnection(); conn = this.getDBConnection();
String sql = "SELECT AR.ID AS RELEASE_ID " String sql = "SELECT AR.ID AS RELEASE_ID "
+ "FROM AP_APP_RELEASE AS AR " + "FROM AP_APP_RELEASE AS AR "
+ "WHERE AR.PACKAGE_NAME = ? AND AR.TENANT_ID = ? LIMIT 1"; + "WHERE AR.PACKAGE_NAME = ? AND AR.CURRENT_STATE != ? AND AR.TENANT_ID = ? LIMIT 1";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, packageName); stmt.setString(1, packageName);
stmt.setInt(2, tenantId); stmt.setString(2, AppLifecycleState.REMOVED.toString());
stmt.setInt(3, tenantId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
return rs.next(); return rs.next();
} }

@ -42,6 +42,7 @@ import org.wso2.carbon.device.application.mgt.common.dto.TagDTO;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException;
@ -162,12 +163,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
filter.setLimit(1); filter.setLimit(1);
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
// todo resolve following comment
/*check is there an application release with same package name, if there is an application release
throw an error and request to delete the existing application or add this as new application release
for existing application*/
ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId);
if (!applicationList.getApplications().isEmpty()) { if (!applicationList.getApplications().isEmpty()) {
String msg = String msg =
@ -265,18 +260,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Creating a new release. App Id:" + appId); log.debug("Creating a new release. App Id:" + appId);
} }
String initialLifecycleState = lifecycleStateManager.getInitialState();
applicationReleaseDTO = applicationDTO.getApplicationReleases().get(0); applicationReleaseDTO = applicationDTO.getApplicationReleases().get(0);
applicationReleaseDTO.setCurrentState(AppLifecycleState.CREATED.toString()); applicationReleaseDTO.setCurrentState(initialLifecycleState);
applicationReleaseDTO = this.applicationReleaseDAO.createRelease(applicationReleaseDTO, appId, tenantId); applicationReleaseDTO = this.applicationReleaseDAO.createRelease(applicationReleaseDTO, appId, tenantId);
LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance(initialLifecycleState,
if (log.isDebugEnabled()) { initialLifecycleState);
log.debug("Changing lifecycle state. App Id:" + appId); this.lifecycleStateDAO
} .addLifecycleState(lifecycleStateDTO, appId, applicationReleaseDTO.getUuid(), tenantId);
//todo get initial state from lifecycle manager and set current state to Release object
LifecycleStateDTO lifecycleState = getLifecycleStateInstance(AppLifecycleState.CREATED.toString(),
AppLifecycleState.CREATED.toString());
this.lifecycleStateDAO.addLifecycleState(lifecycleState, appId, applicationReleaseDTO.getUuid(), tenantId);
applicationReleaseDTO.setCurrentState(AppLifecycleState.CREATED.toString());
applicationReleaseEntities.add(applicationReleaseDTO); applicationReleaseEntities.add(applicationReleaseDTO);
applicationDTO.setApplicationReleases(applicationReleaseEntities); applicationDTO.setApplicationReleases(applicationReleaseEntities);
application = appDtoToAppResponse(applicationDTO); application = appDtoToAppResponse(applicationDTO);
@ -291,12 +282,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = String msg = "Error occurred while adding application or application release. application name: "
"Error occurred while adding application or application release. application name: " + applicationWrapper + applicationWrapper.getName() + " application type: " + applicationWrapper.getType();
.getName() + " application type: " + applicationWrapper.getType();
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (DBConnectionException e) { } catch(LifecycleManagementException e){
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when getting initial lifecycle state. application name: " + applicationWrapper
.getName() + " application type: is " + applicationWrapper.getType();
log.error(msg);
throw new ApplicationManagementException(msg, e);
}catch (DBConnectionException e) {
String msg = "Error occurred while getting database connection."; String msg = "Error occurred while getting database connection.";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
@ -345,8 +341,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
String packagename = applicationInstaller.getPackageName(); String packagename = applicationInstaller.getPackageName();
ConnectionManagerUtil.getDBConnection(); ConnectionManagerUtil.getDBConnection();
if (applicationReleaseDAO.isAppExisitForPackageName(packagename, tenantId)) { if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId)) {
String msg = "Application release is already exist for the package name: " + packagename; String msg = "Application release is already exist for the package name: " + packagename +
". Either you can delete all application releases for package " + packagename + " or "
+ "you can add this app release as an new application release, under the existing "
+ "application.";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg); throw new ApplicationManagementException(msg);
} }
@ -655,7 +654,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private String[] getRolesOfUser(String userName) throws UserStoreException { private String[] getRolesOfUser(String userName) throws UserStoreException {
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
String[] roleList = {}; String[] roleList;
if (userRealm != null) { if (userRealm != null) {
userRealm.getUserStoreManager().getRoleNames(); userRealm.getUserStoreManager().getRoleNames();
roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName);

@ -219,11 +219,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
log.error(msg); log.error(msg);
throw new ApplicationStorageManagementException(msg); throw new ApplicationStorageManagementException(msg);
} }
if (log.isDebugEnabled()) {
log.debug("Artifact Directory Path for saving the application release is " + artifactDirectoryPath
+ ". ApplicationUUID: " + applicationReleaseDTO.getUuid());
}
artifactDirectoryPath = storagePath + md5OfApp; artifactDirectoryPath = storagePath + md5OfApp;
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
artifactPath = artifactDirectoryPath + File.separator + applicationReleaseDTO.getInstallerName(); artifactPath = artifactDirectoryPath + File.separator + applicationReleaseDTO.getInstallerName();

@ -101,11 +101,9 @@ public class LifecycleStateManager {
} }
private State getMatchingState(String currentState) { private State getMatchingState(String currentState) {
Iterator it = lifecycleStates.entrySet().iterator(); for (Map.Entry<String, State> stringStateEntry : lifecycleStates.entrySet()) {
while (it.hasNext()) { if (stringStateEntry.getKey().equalsIgnoreCase(currentState)) {
Map.Entry pair = (Map.Entry) it.next(); return lifecycleStates.get(stringStateEntry.getKey());
if (pair.getKey().toString().equalsIgnoreCase(currentState)) {
return lifecycleStates.get(pair.getKey().toString());
} }
} }
return null; return null;
@ -135,20 +133,60 @@ public class LifecycleStateManager {
return null; return null;
} }
public boolean isUpdatable(String state) { public boolean isUpdatable(String state) throws LifecycleManagementException {
State currentState = getMatchingState(state); State currentState = getMatchingState(state);
if (currentState.getIsAppUpdatable()) { if (currentState != null) {
return true; return currentState.isAppUpdatable();
} else {
String msg = "Couldn't find a lifecycle state that matches with " + state + " state.";
log.error(msg);
throw new LifecycleManagementException(msg);
} }
return false;
} }
public boolean isInstallable(String state) { public boolean isInstallable(String state) throws LifecycleManagementException {
State currentState = getMatchingState(state); State currentState = getMatchingState(state);
if (currentState.getIsAppInstallable()) { if (currentState != null) {
return true; return currentState.isAppInstallable();
} else {
String msg = "Couldn't find a lifecycle state that matches with " + state + " state.";
log.error(msg);
throw new LifecycleManagementException(msg);
} }
return false; }
public String getInitialState() throws LifecycleManagementException {
String initialState = null;
for (Map.Entry<String, State> stringStateEntry : lifecycleStates.entrySet()) {
if (stringStateEntry.getValue().isInitialState()) {
initialState = stringStateEntry.getKey();
break;
}
}
if (initialState == null){
String msg = "Haven't defined the initial state in the application-manager.xml. Please add initial state "
+ "to the <LifecycleStates> section in the app-manager.xml";
log.error(msg);
throw new LifecycleManagementException(msg);
}
return initialState;
}
public String getEntState() throws LifecycleManagementException {
String endState = null;
for (Map.Entry<String, State> stringStateEntry : lifecycleStates.entrySet()) {
if (stringStateEntry.getValue().isEndState()) {
endState = stringStateEntry.getKey();
break;
}
}
if (endState == null){
String msg = "Haven't defined the end state in the application-manager.xml. Please add end state "
+ "to the <LifecycleStates> section in the app-manager.xml";
log.error(msg);
throw new LifecycleManagementException(msg);
}
return endState;
} }
public void setLifecycleStates(Map<String, State> lifecycleStates) { public void setLifecycleStates(Map<String, State> lifecycleStates) {

@ -12,7 +12,6 @@ public class State {
private Set<String> proceedingStates; private Set<String> proceedingStates;
private String stateName; private String stateName;
private String permission; private String permission;
private List<String> allowedActions;
private boolean isAppUpdatable; private boolean isAppUpdatable;
private boolean isAppInstallable; private boolean isAppInstallable;
private boolean isInitialState; private boolean isInitialState;
@ -41,12 +40,12 @@ public class State {
public String getPermission(){ return permission;} public String getPermission(){ return permission;}
public boolean getIsAppUpdatable(){ return isAppUpdatable;} public boolean isAppUpdatable(){ return isAppUpdatable;}
public boolean getIsAppInstallable(){ return isAppInstallable;} public boolean isAppInstallable(){ return isAppInstallable;}
public boolean getIsInitialState(){ return isInitialState;} public boolean isInitialState(){ return isInitialState;}
public boolean getIsEndState(){ return isEndState;} public boolean isEndState(){ return isEndState;}
} }

@ -49,36 +49,36 @@ public class LifecycleManagementTest {
proceedingStates.contains(BOGUS_STATE.toUpperCase())); proceedingStates.contains(BOGUS_STATE.toUpperCase()));
} }
@Test // @Test
public void CheckUpdatableState() { // public void CheckUpdatableState() {
Boolean isUpdatable = lifecycleStateManager.isUpdatable(UPDATABLE_STATE); // boolean isUpdatable = lifecycleStateManager.isUpdatable(UPDATABLE_STATE);
System.out.println(isUpdatable); // System.out.println(isUpdatable);
Assert.assertTrue("Updatable state: " + UPDATABLE_STATE, isUpdatable); // Assert.assertTrue("Updatable state: " + UPDATABLE_STATE, isUpdatable);
} // }
//
@Test // @Test
public void CheckNonUpdatableState() { // public void CheckNonUpdatableState() {
Boolean isUpdatable = lifecycleStateManager.isUpdatable(NON_UPDATABLE_STATE); // boolean isUpdatable = lifecycleStateManager.isUpdatable(NON_UPDATABLE_STATE);
Assert.assertFalse("Non Updatable state: " + NON_UPDATABLE_STATE, isUpdatable); // Assert.assertFalse("Non Updatable state: " + NON_UPDATABLE_STATE, isUpdatable);
} // }
//
@Test // @Test
public void CheckInstallableState() { // public void CheckInstallableState() {
Boolean isInstallable = lifecycleStateManager.isInstallable(INSTALLABLE_STATE); // boolean isInstallable = lifecycleStateManager.isInstallable(INSTALLABLE_STATE);
Assert.assertTrue("Installable state: " + INSTALLABLE_STATE, isInstallable); // Assert.assertTrue("Installable state: " + INSTALLABLE_STATE, isInstallable);
} // }
//
@Test // @Test
public void CheckUnInstallableState() { // public void CheckUnInstallableState() {
Boolean isInstallable = lifecycleStateManager.isInstallable(UNINSTALlABLE_STATE); // boolean isInstallable = lifecycleStateManager.isInstallable(UNINSTALlABLE_STATE);
Assert.assertFalse("UnInstallable state: " + UNINSTALlABLE_STATE, isInstallable); // Assert.assertFalse("UnInstallable state: " + UNINSTALlABLE_STATE, isInstallable);
} // }
//
@Test // @Test
public void check() { // public void check() {
Set<String> proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE); // Set<String> proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE);
Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, // Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(BOGUS_STATE.toUpperCase())); // proceedingStates.contains(BOGUS_STATE.toUpperCase()));
} // }
} }

Loading…
Cancel
Save