Introduce FeatureManager to ResourceCommand
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / RootServiceCommand.kt
1 package org.onap.vid.job.command
2
3 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate
4 import org.onap.vid.job.Job
5 import org.onap.vid.job.JobAdapter
6 import org.onap.vid.job.JobCommand
7 import org.onap.vid.job.JobsBrokerService
8 import org.onap.vid.job.impl.JobSharedData
9 import org.onap.vid.model.Action
10 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation
11 import org.onap.vid.mso.RestMsoImplementation
12 import org.onap.vid.services.AsyncInstantiationBusinessLogic
13 import org.onap.vid.services.AuditService
14 import org.springframework.beans.factory.annotation.Autowired
15 import org.springframework.http.HttpMethod
16 import org.togglz.core.manager.FeatureManager
17 import java.util.*
18
19 abstract class RootServiceCommand @Autowired constructor(
20         restMso: RestMsoImplementation,
21         inProgressStatusService: InProgressStatusService,
22         msoResultHandlerService: MsoResultHandlerService,
23         watchChildrenJobsBL: WatchChildrenJobsBL,
24         jobsBrokerService: JobsBrokerService,
25         jobAdapter: JobAdapter,
26         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
27         private val auditService: AuditService,
28         private val msoRequestBuilder: MsoRequestBuilder,
29         featureManager: FeatureManager
30 ) : ResourceCommand(restMso, inProgressStatusService, msoResultHandlerService,
31         watchChildrenJobsBL, jobsBrokerService, jobAdapter, featureManager), JobCommand {
32
33     lateinit var optimisticUniqueServiceInstanceName: String
34
35     companion object {
36         private val LOGGER = EELFLoggerDelegate.getLogger(RootServiceCommand::class.java)
37     }
38
39     final override fun onInitial(phase: Action) {
40         if (phase== Action.Delete) {
41             asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, Job.JobStatus.IN_PROGRESS)
42         }
43     }
44
45     final override fun getExternalInProgressStatus() = Job.JobStatus.IN_PROGRESS
46
47     final override fun getData(): Map<String, Any?> {
48         return super.getData() + mapOf(UNIQUE_INSTANCE_NAME to optimisticUniqueServiceInstanceName)
49     }
50
51     final override fun onFinal(jobStatus: Job.JobStatus) {
52         asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
53         if (jobStatus.isFailure) {
54             asyncInstantiationBL.handleFailedInstantiation(sharedData.jobUuid)
55         }
56     }
57
58     final override fun init(sharedData: JobSharedData, commandData: Map<String, Any>): ResourceCommand {
59         optimisticUniqueServiceInstanceName = commandData.getOrDefault(UNIQUE_INSTANCE_NAME, "") as String
60         return super<ResourceCommand>.init(sharedData, commandData)
61     }
62
63     final override fun isServiceCommand(): Boolean = true
64
65     final override fun getExpiryChecker(): ExpiryChecker {
66         return ServiceExpiryChecker()
67     }
68
69     override fun resumeMyself(): Job.JobStatus {
70         val requestType = "createInstance"
71         val scope = "service"
72         val serviceInstanceId = getActualInstanceId(getRequest())
73         try {
74             val requests = auditService.retrieveRequestsFromMsoByServiceIdAndRequestTypeAndScope(serviceInstanceId, requestType, scope)
75             if (requests.isEmpty() || requests[0].requestId == null) {
76                 LOGGER.error("Failed to retrieve requestId with type: $type, scope: $scope for service instanceId $serviceInstanceId ")
77                 return Job.JobStatus.FAILED
78             }
79             val createMyselfCommand = planResumeMyselfRestCall(requests[0].requestId, sharedData.userId)
80             return executeAndHandleMsoInstanceRequest(createMyselfCommand)
81         } catch (exception: Exception) {
82             LOGGER.error("Failed to resume instanceId $serviceInstanceId ", exception)
83             return Job.JobStatus.FAILED
84         }
85     }
86
87     private fun planResumeMyselfRestCall(requestId: String, userId: String): MsoRestCallPlan {
88         val path = asyncInstantiationBL.getResumeRequestPath(requestId)
89         return MsoRestCallPlan(HttpMethod.POST, path, Optional.empty(), Optional.of(userId), "resume request $requestId")
90     }
91
92     override fun planDeleteMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
93         val requestDetailsWrapper = msoRequestBuilder.generateServiceDeletionRequest(
94                 request as ServiceInstantiation, userId
95         )
96         val path = asyncInstantiationBL.getServiceDeletionPath(request.instanceId)
97         return MsoRestCallPlan(HttpMethod.DELETE, path, Optional.of(requestDetailsWrapper), Optional.empty(),
98                 "delete instance with id ${request.instanceId}")
99     }
100 }