Don't use EELFLoggerDelegate.errorLogger in Async jobs
[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 java.util.*
17
18 abstract class RootServiceCommand @Autowired constructor(
19         restMso: RestMsoImplementation,
20         inProgressStatusService: InProgressStatusService,
21         msoResultHandlerService: MsoResultHandlerService,
22         watchChildrenJobsBL: WatchChildrenJobsBL,
23         jobsBrokerService: JobsBrokerService,
24         jobAdapter: JobAdapter,
25         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
26         private val auditService: AuditService,
27         private val msoRequestBuilder: MsoRequestBuilder
28 ) : ResourceCommand(restMso, inProgressStatusService, msoResultHandlerService,
29         watchChildrenJobsBL, jobsBrokerService, jobAdapter), JobCommand {
30
31     lateinit var optimisticUniqueServiceInstanceName: String
32
33     companion object {
34         private val LOGGER = EELFLoggerDelegate.getLogger(RootServiceCommand::class.java)
35     }
36
37     final override fun onInitial(phase: Action) {
38         if (phase== Action.Delete) {
39             asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, Job.JobStatus.IN_PROGRESS)
40         }
41     }
42
43     final override fun getExternalInProgressStatus() = Job.JobStatus.IN_PROGRESS
44
45     final override fun getData(): Map<String, Any?> {
46         return super.getData() + mapOf(UNIQUE_INSTANCE_NAME to optimisticUniqueServiceInstanceName)
47     }
48
49     final override fun onFinal(jobStatus: Job.JobStatus) {
50         asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
51         if (jobStatus.isFailure) {
52             asyncInstantiationBL.handleFailedInstantiation(sharedData.jobUuid)
53         }
54     }
55
56     final override fun init(sharedData: JobSharedData, commandData: Map<String, Any>): ResourceCommand {
57         optimisticUniqueServiceInstanceName = commandData.getOrDefault(UNIQUE_INSTANCE_NAME, "") as String
58         return super<ResourceCommand>.init(sharedData, commandData)
59     }
60
61     final override fun isServiceCommand(): Boolean = true
62
63     final override fun getExpiryChecker(): ExpiryChecker {
64         return ServiceExpiryChecker()
65     }
66
67     override fun resumeMyself(): Job.JobStatus {
68         val requestType = "createInstance"
69         val scope = "service"
70         val serviceInstanceId = getActualInstanceId(getRequest())
71         try {
72             val requests = auditService.retrieveRequestsFromMsoByServiceIdAndRequestTypeAndScope(serviceInstanceId, requestType, scope)
73             if (requests.isEmpty() || requests[0].requestId == null) {
74                 LOGGER.error("Failed to retrieve requestId with type: $type, scope: $scope for service instanceId $serviceInstanceId ")
75                 return Job.JobStatus.FAILED
76             }
77             val createMyselfCommand = planResumeMyselfRestCall(requests[0].requestId, sharedData.userId)
78             return executeAndHandleMsoInstanceRequest(createMyselfCommand)
79         } catch (exception: Exception) {
80             LOGGER.error("Failed to resume instanceId $serviceInstanceId ", exception)
81             return Job.JobStatus.FAILED
82         }
83     }
84
85     private fun planResumeMyselfRestCall(requestId: String, userId: String): MsoRestCallPlan {
86         val path = asyncInstantiationBL.getResumeRequestPath(requestId)
87         return MsoRestCallPlan(HttpMethod.POST, path, Optional.empty(), Optional.of(userId), "resume request $requestId")
88     }
89
90     override fun planDeleteMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
91         val requestDetailsWrapper = msoRequestBuilder.generateServiceDeletionRequest(
92                 request as ServiceInstantiation, userId
93         )
94         val path = asyncInstantiationBL.getServiceDeletionPath(request.instanceId)
95         return MsoRestCallPlan(HttpMethod.DELETE, path, Optional.of(requestDetailsWrapper), Optional.empty(),
96                 "delete instance with id ${request.instanceId}")
97     }
98 }