Introduce FeatureManager to ResourceCommand
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ResourceCommand.kt
index 0e9ab7b..9b4f8b9 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.vid.job.command
 
 
 import com.fasterxml.jackson.module.kotlin.convertValue
+import org.apache.commons.lang3.ObjectUtils.defaultIfNull
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate
 import org.onap.vid.changeManagement.RequestDetailsWrapper
 import org.onap.vid.exceptions.AbortingException
@@ -32,10 +33,14 @@ import org.onap.vid.job.impl.JobSharedData
 import org.onap.vid.model.Action
 import org.onap.vid.model.RequestReferencesContainer
 import org.onap.vid.model.serviceInstantiation.BaseResource
+import org.onap.vid.model.serviceInstantiation.BaseResource.PauseInstantiation.afterCompletion
 import org.onap.vid.mso.RestMsoImplementation
+import org.onap.vid.mso.model.ModelInfo
+import org.onap.vid.properties.Features
 import org.onap.vid.utils.JACKSON_OBJECT_MAPPER
 import org.onap.vid.utils.getEnumFromMapOfStrings
 import org.springframework.http.HttpMethod
+import org.togglz.core.manager.FeatureManager
 import java.util.*
 
 
@@ -74,7 +79,8 @@ abstract class ResourceCommand(
         protected val msoResultHandlerService: MsoResultHandlerService,
         protected val watchChildrenJobsBL: WatchChildrenJobsBL,
         private val jobsBrokerService: JobsBrokerService,
-        private val jobAdapter: JobAdapter
+        private val jobAdapter: JobAdapter,
+        private val featureManager: FeatureManager
         ) : CommandBase(), JobCommand {
 
     companion object {
@@ -110,7 +116,7 @@ abstract class ResourceCommand(
         jobStatus = comulateStatusAndUpdatePropertyIfFinal(jobStatus)
 
         try {
-            Logger.debug("job: ${this.javaClass.simpleName} ${sharedData.jobUuid} $actionPhase ${getActionType()} $internalState $jobStatus $childJobs")
+            Logger.debug(EELFLoggerDelegate.debugLogger, "job: ${this.javaClass.simpleName} ${sharedData.jobUuid} $actionPhase ${getActionType()} $internalState $jobStatus $childJobs")
         } catch (e:Exception) { /* do nothing. Just failed to log...*/}
 
         if (shallStopJob(jobStatus)) {
@@ -119,7 +125,7 @@ abstract class ResourceCommand(
         }
 
         val (nextActionPhase, nextInternalState) = calcNextInternalState(jobStatus, internalState, actionPhase)
-        Logger.debug("next state for job ${sharedData.jobUuid} is $nextInternalState")
+        Logger.debug(EELFLoggerDelegate.debugLogger, "next state for job ${sharedData.jobUuid} is $nextInternalState")
         actionPhase = nextActionPhase
         internalState = nextInternalState
 
@@ -129,7 +135,7 @@ abstract class ResourceCommand(
         }
 
         jobStatus = getExternalInProgressStatus()
-        Logger.debug("next status for job ${sharedData.jobUuid} is $jobStatus")
+        Logger.debug(EELFLoggerDelegate.debugLogger, "next status for job ${sharedData.jobUuid} is $jobStatus")
 //        if (internalState.immediate) return call() //shortcut instead of execute another command
         return NextCommand(jobStatus, this)
     }
@@ -226,7 +232,7 @@ abstract class ResourceCommand(
                 when {
                     jobStatus != JobStatus.COMPLETED -> InternalState.IN_PROGRESS
                     isDescendantHasAction(Action.Create) -> InternalState.CREATING_CHILDREN
-                    isDescendantHasAction(Action.Replace) -> InternalState.CREATING_CHILDREN
+                    isDescendantHasAction(Action.Upgrade) -> InternalState.CREATING_CHILDREN
                     else -> InternalState.TERMINAL
                 }
             }
@@ -284,7 +290,7 @@ abstract class ResourceCommand(
                     isNeedToResumeMySelf() -> InternalState.RESUME_MYSELF
                     isNeedToReplaceMySelf() -> InternalState.REPLACE_MYSELF
                     isDescendantHasAction(phase) -> InternalState.CREATING_CHILDREN
-                    isDescendantHasAction(Action.Replace) -> InternalState.CREATING_CHILDREN
+                    isDescendantHasAction(Action.Upgrade) -> InternalState.CREATING_CHILDREN
                     else -> InternalState.TERMINAL
                 }
                 else -> throw IllegalStateException("state $internalState is not supported yet")
@@ -328,13 +334,13 @@ abstract class ResourceCommand(
             handleInProgressStatus(jobStatus)
         } catch (e: javax.ws.rs.ProcessingException) {
             // Retry when we can't connect MSO during getStatus
-            Logger.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e)
+            Logger.error("Cannot get orchestration status for {}, will retry: {}", requestId, e, e)
             JobStatus.IN_PROGRESS;
         } catch (e: InProgressStatusService.BadResponseFromMso) {
             inProgressStatusService.handleFailedMsoResponse(sharedData.jobUuid, requestId, e.msoResponse)
             JobStatus.IN_PROGRESS
         } catch (e: RuntimeException) {
-            Logger.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e)
+            Logger.error("Cannot get orchestration status for {}, stopping: {}", requestId, e, e)
             JobStatus.STOPPED
         }
     }
@@ -403,6 +409,9 @@ abstract class ResourceCommand(
         return commandParentData.parentData
     }
 
+    protected fun serviceModelInfoFromRequest(): ModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
+    protected fun serviceInstanceIdFromRequest(): String = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
+
     protected open fun addMyselfToChildrenData(commandParentData: CommandParentData, request: BaseResource) {
         // Nothing by default
     }
@@ -426,14 +435,17 @@ abstract class ResourceCommand(
     protected fun pushChildrenJobsToBroker(children:Collection<BaseResource>,
                                            dataForChild: Map<String, Any>,
                                            jobType: JobType?=null): List<String> {
-        var counter = 0;
-        return  children
-                .map {Pair(it, counter++)}
+        return  setPositionWhereIsMissing(children)
                 .map { jobAdapter.createChildJob(jobType ?: it.first.jobType, it.first, sharedData, dataForChild, it.second) }
                 .map { jobsBrokerService.add(it) }
                 .map { it.toString() }
     }
 
+    protected fun setPositionWhereIsMissing(children: Collection<BaseResource>): List<Pair<BaseResource, Int>> {
+        var orderingPosition = children.map{ defaultIfNull(it.position, 0) }.max() ?: 0
+        return  children
+                .map {Pair(it, it.position ?: ++orderingPosition)}
+    }
 }