Don't use EELFLoggerDelegate.errorLogger in Async jobs
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / MacroServiceCommand.kt
1 package org.onap.vid.job.command
2
3 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate
4 import org.onap.vid.aai.ExceptionWithRequestInfo
5 import org.onap.vid.changeManagement.RequestDetailsWrapper
6 import org.onap.vid.exceptions.AbortingException
7 import org.onap.vid.exceptions.MaxRetriesException
8 import org.onap.vid.exceptions.TryAgainException
9 import org.onap.vid.job.Job
10 import org.onap.vid.job.JobAdapter
11 import org.onap.vid.job.JobCommand
12 import org.onap.vid.job.JobsBrokerService
13 import org.onap.vid.model.Action
14 import org.onap.vid.model.VidNotions.ModelCategory.*
15 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation
16 import org.onap.vid.mso.RestMsoImplementation
17 import org.onap.vid.services.AsyncInstantiationBusinessLogic
18 import org.onap.vid.services.AuditService
19 import org.springframework.beans.factory.annotation.Autowired
20 import org.springframework.beans.factory.config.ConfigurableBeanFactory
21 import org.springframework.context.annotation.Scope
22 import org.springframework.http.HttpMethod
23 import org.springframework.stereotype.Component
24 import java.util.*
25
26 @Component
27 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
28 class MacroServiceCommand @Autowired constructor(
29         inProgressStatusService: InProgressStatusService,
30         watchChildrenJobsBL: WatchChildrenJobsBL,
31         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
32         jobsBrokerService: JobsBrokerService,
33         private val msoRequestBuilder: MsoRequestBuilder,
34         msoResultHandlerService: MsoResultHandlerService,
35         jobAdapter: JobAdapter,
36         restMso: RestMsoImplementation,
37         auditService: AuditService
38 ) : RootServiceCommand(restMso, inProgressStatusService, msoResultHandlerService,
39         watchChildrenJobsBL, jobsBrokerService, jobAdapter, asyncInstantiationBL, auditService, msoRequestBuilder), JobCommand {
40
41
42     companion object {
43         private val Logger = EELFLoggerDelegate.getLogger(MacroServiceCommand::class.java)
44     }
45
46     override fun createChildren(): Job.JobStatus {
47         return Job.JobStatus.COMPLETED_WITH_NO_ACTION
48     }
49
50     private val pre1806Models = setOf(Transport, INFRASTRUCTURE_VPN, SERVICE_WITH_COLLECTION_RESOURCE);
51
52     override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
53         try {
54             val instantiatePath = asyncInstantiationBL.getServiceInstantiationPath(request as ServiceInstantiation)
55
56             val requestDetailsWrapper = generateRequest(sharedData.jobUuid, request, optimisticUniqueServiceInstanceName, userId)
57
58             val actionDescription = "create macro service instance"
59
60             return MsoRestCallPlan(HttpMethod.POST, instantiatePath, Optional.of(requestDetailsWrapper), Optional.empty(), actionDescription)
61         }
62
63         //Aai return bad response while checking names uniqueness
64         catch (exception : ExceptionWithRequestInfo) {
65             Logger.error("Failed to check name uniqueness in AAI. VID will try again later", exception)
66             throw TryAgainException(exception);
67         }
68
69         //Vid reached to max retries while trying to find unique name in AAI
70         catch (exception : MaxRetriesException) {
71             Logger.error("Failed to find unused name in AAI", exception)
72             throw AbortingException(exception);
73         }
74     }
75
76     private fun generateRequest(jobUuid: UUID?, request: ServiceInstantiation, optimisticUniqueServiceInstanceName: String, userId: String): RequestDetailsWrapper<out Any> {
77         // for transport or for infrastructure VPN - send the pre 1806 request
78         if (shouldUsePre1806Request(request)){
79             return msoRequestBuilder.generateMacroServicePre1806InstantiationRequest(request, userId)
80         }
81         return msoRequestBuilder.generateMacroServiceInstantiationRequest(jobUuid, request, optimisticUniqueServiceInstanceName, userId)
82     }
83
84     protected fun shouldUsePre1806Request(request: ServiceInstantiation): Boolean {
85         return (request.vidNotions != null && pre1806Models.contains(request.vidNotions.modelCategory))
86     }
87
88
89     override fun handleInProgressStatus(jobStatus: Job.JobStatus): Job.JobStatus {
90         asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
91         return if (jobStatus==Job.JobStatus.PAUSE) Job.JobStatus.IN_PROGRESS else jobStatus
92     }
93
94     override fun isDescendantHasAction(phase: Action): Boolean {
95         return false
96     }
97
98 }