1 package org.onap.vid.job.command
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
21 class ServiceExpiryChecker : ExpiryChecker {
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
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 {
44 override fun getExpiryChecker(): ExpiryChecker {
45 return ServiceExpiryChecker();
49 private val LOGGER = EELFLoggerDelegate.getLogger(ALaCarteServiceCommand::class.java)
52 override fun getRequest(): ServiceInstantiation {
53 return msoResultHandlerService.getRequest(sharedData)
56 override fun createChildren(): Job.JobStatus {
57 val dataForChild = buildDataForChild(getRequest())//.plus(ACTION_PHASE to actionPhase)
59 val childJobType = when (actionPhase) {
60 Action.Create -> JobType.InstanceGroupInstantiation
61 Action.Delete -> JobType.InstanceGroup
62 else -> return Job.JobStatus.COMPLETED
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() }
70 return Job.JobStatus.COMPLETED_WITH_NO_ACTION
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
80 override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
81 TODO("not implemented")
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}")
92 override fun handleInProgressStatus(jobStatus: Job.JobStatus): Job.JobStatus {
93 if (jobStatus==Job.JobStatus.FAILED) {
94 asyncInstantiationBL.handleFailedInstantiation(sharedData.jobUuid)
98 asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
99 return if (jobStatus == Job.JobStatus.PAUSE) Job.JobStatus.IN_PROGRESS else jobStatus
103 private fun generateServiceDeletionRequest(): RequestDetailsWrapper<ServiceDeletionRequestDetails> {
104 return asyncInstantiationBL.generateALaCarteServiceDeletionRequest(
105 sharedData.jobUuid, getRequest(), sharedData.userId
109 override fun getExternalInProgressStatus() = Job.JobStatus.IN_PROGRESS
111 override fun isServiceCommand(): Boolean = true
113 override fun onFinal(jobStatus: Job.JobStatus) {
114 asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, jobStatus)
117 override fun onInitial(phase: Action) {
118 if (phase== Action.Delete) {
119 asyncInstantiationBL.updateServiceInfoAndAuditStatus(sharedData.jobUuid, Job.JobStatus.IN_PROGRESS)