Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ALaCarteServiceCommand.kt
1 package org.onap.vid.job.command
2
3 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate
4 import org.onap.vid.changeManagement.RequestDetailsWrapper
5 import org.onap.vid.job.*
6 import org.onap.vid.model.Action
7 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation
8 import org.onap.vid.mso.RestMsoImplementation
9 import org.onap.vid.mso.model.ServiceDeletionRequestDetails
10 import org.onap.vid.properties.VidProperties
11 import org.onap.vid.services.AsyncInstantiationBusinessLogic
12 import org.springframework.beans.factory.annotation.Autowired
13 import org.springframework.beans.factory.config.ConfigurableBeanFactory
14 import org.springframework.context.annotation.Scope
15 import org.springframework.http.HttpMethod
16 import org.springframework.stereotype.Component
17 import java.time.ZonedDateTime
18 import java.time.temporal.ChronoUnit
19 import java.util.*
20
21 class ServiceExpiryChecker : ExpiryChecker {
22
23     override fun isExpired(jobStartTime: ZonedDateTime?): Boolean {
24         val now = ZonedDateTime.now()
25         val maxHoursInProgress = VidProperties.getLongProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS)
26         val hoursBetween = ChronoUnit.HOURS.between(jobStartTime, now)
27         return maxHoursInProgress in 1..hoursBetween
28     }
29 }
30
31
32 @Component
33 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 class ALaCarteServiceCommand @Autowired constructor(
35         inProgressStatusService: InProgressStatusService,
36         watchChildrenJobsBL: WatchChildrenJobsBL,
37         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
38         private val jobsBrokerService: JobsBrokerService,
39         msoResultHandlerService: MsoResultHandlerService,
40         private val jobAdapter: JobAdapter,
41         restMso: RestMsoImplementation
42 ) : ResourceCommand(restMso, inProgressStatusService, msoResultHandlerService, watchChildrenJobsBL), JobCommand {
43
44     override fun getExpiryChecker(): ExpiryChecker {
45         return ServiceExpiryChecker();
46     }
47
48     companion object {
49         private val LOGGER = EELFLoggerDelegate.getLogger(ALaCarteServiceCommand::class.java)
50     }
51
52     override fun getRequest(): ServiceInstantiation {
53         return msoResultHandlerService.getRequest(sharedData)
54     }
55
56     override fun createChildren(): Job.JobStatus {
57         val dataForChild = buildDataForChild(getRequest())//.plus(ACTION_PHASE to actionPhase)
58
59         val childJobType = when (actionPhase) {
60             Action.Create -> JobType.InstanceGroupInstantiation
61             Action.Delete -> JobType.InstanceGroup
62             else -> return Job.JobStatus.COMPLETED
63         }
64
65         childJobs = getRequest().vnfGroups
66                 .map { jobAdapter.createChildJob(childJobType, Job.JobStatus.CREATING, it.value, sharedData, dataForChild) }
67                 .map { jobsBrokerService.add(it) }
68                 .map { it.toString() }
69
70         return Job.JobStatus.COMPLETED_WITH_NO_ACTION
71     }
72
73     private fun buildDataForChild(request: ServiceInstantiation): Map<String, Any> {
74         val commandParentData = CommandParentData()
75         commandParentData.addInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID, request.instanceId)
76         commandParentData.addModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO, request.modelInfo)
77         return commandParentData.parentData
78     }
79
80     override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
81         TODO("not implemented")
82     }
83
84     override fun planDeleteMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
85         val requestDetailsWrapper = generateServiceDeletionRequest()
86         val path = asyncInstantiationBL.getServiceDeletionPath(getRequest().instanceId)
87         return MsoRestCallPlan(HttpMethod.DELETE, path, Optional.of(requestDetailsWrapper), Optional.empty(),
88                 "delete instance with id ${getRequest().instanceId}")
89
90     }
91
92     override fun handleInProgressStatus(jobStatus: Job.JobStatus): Job.JobStatus {
93         if (jobStatus==Job.JobStatus.FAILED) {
94             asyncInstantiationBL.handleFailedInstantiation(sharedData.jobUuid)
95             return jobStatus
96         }
97
98         asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
99         return  if (jobStatus == Job.JobStatus.PAUSE) Job.JobStatus.IN_PROGRESS else jobStatus
100     }
101
102
103     private fun generateServiceDeletionRequest(): RequestDetailsWrapper<ServiceDeletionRequestDetails> {
104         return asyncInstantiationBL.generateALaCarteServiceDeletionRequest(
105                 sharedData.jobUuid, getRequest(), sharedData.userId
106         )
107     }
108
109     override fun getExternalInProgressStatus() = Job.JobStatus.IN_PROGRESS
110
111     override fun isServiceCommand(): Boolean = true
112
113     override fun onFinal(jobStatus: Job.JobStatus) {
114         asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
115     }
116
117     override fun onInitial(phase: Action) {
118         if (phase== Action.Delete) {
119             asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, Job.JobStatus.IN_PROGRESS)
120         }
121     }
122 }