Introduce FeatureManager to ResourceCommand
[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 org.togglz.core.manager.FeatureManager
25 import java.util.*
26
27 @Component
28 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
29 class MacroServiceCommand @Autowired constructor(
30         inProgressStatusService: InProgressStatusService,
31         watchChildrenJobsBL: WatchChildrenJobsBL,
32         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
33         jobsBrokerService: JobsBrokerService,
34         private val msoRequestBuilder: MsoRequestBuilder,
35         msoResultHandlerService: MsoResultHandlerService,
36         jobAdapter: JobAdapter,
37         restMso: RestMsoImplementation,
38         auditService: AuditService,
39         featureManager: FeatureManager
40 ) : RootServiceCommand(restMso, inProgressStatusService, msoResultHandlerService,
41         watchChildrenJobsBL, jobsBrokerService, jobAdapter, asyncInstantiationBL, auditService, msoRequestBuilder, featureManager), JobCommand {
42
43
44     companion object {
45         private val Logger = EELFLoggerDelegate.getLogger(MacroServiceCommand::class.java)
46     }
47
48     override fun createChildren(): Job.JobStatus {
49         return Job.JobStatus.COMPLETED_WITH_NO_ACTION
50     }
51
52     private val pre1806Models = setOf(Transport, INFRASTRUCTURE_VPN, SERVICE_WITH_COLLECTION_RESOURCE);
53
54     override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
55         try {
56             val instantiatePath = asyncInstantiationBL.getServiceInstantiationPath(request as ServiceInstantiation)
57
58             val requestDetailsWrapper = generateRequest(sharedData.jobUuid, request, optimisticUniqueServiceInstanceName, userId)
59
60             val actionDescription = "create macro service instance"
61
62             return MsoRestCallPlan(HttpMethod.POST, instantiatePath, Optional.of(requestDetailsWrapper), Optional.empty(), actionDescription)
63         }
64
65         //Aai return bad response while checking names uniqueness
66         catch (exception : ExceptionWithRequestInfo) {
67             Logger.error("Failed to check name uniqueness in AAI. VID will try again later", exception)
68             throw TryAgainException(exception);
69         }
70
71         //Vid reached to max retries while trying to find unique name in AAI
72         catch (exception : MaxRetriesException) {
73             Logger.error("Failed to find unused name in AAI", exception)
74             throw AbortingException(exception);
75         }
76     }
77
78     private fun generateRequest(jobUuid: UUID?, request: ServiceInstantiation, optimisticUniqueServiceInstanceName: String, userId: String): RequestDetailsWrapper<out Any> {
79         // for transport or for infrastructure VPN - send the pre 1806 request
80         if (shouldUsePre1806Request(request)){
81             return msoRequestBuilder.generateMacroServicePre1806InstantiationRequest(request, userId)
82         }
83         return msoRequestBuilder.generateMacroServiceInstantiationRequest(jobUuid, request, optimisticUniqueServiceInstanceName, userId)
84     }
85
86     protected fun shouldUsePre1806Request(request: ServiceInstantiation): Boolean {
87         return (request.vidNotions != null && pre1806Models.contains(request.vidNotions.modelCategory))
88     }
89
90
91     override fun handleInProgressStatus(jobStatus: Job.JobStatus): Job.JobStatus {
92         asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
93         return if (jobStatus==Job.JobStatus.PAUSE) Job.JobStatus.IN_PROGRESS else jobStatus
94     }
95
96     override fun isDescendantHasAction(phase: Action): Boolean {
97         return false
98     }
99
100 }