Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ALaCarteServiceCommand.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.job.command
22
23 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate
24 import org.onap.vid.job.Job
25 import org.onap.vid.job.JobAdapter
26 import org.onap.vid.job.JobCommand
27 import org.onap.vid.job.JobsBrokerService
28 import org.onap.vid.model.serviceInstantiation.BaseResource
29 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation
30 import org.onap.vid.mso.RestMsoImplementation
31 import org.onap.vid.properties.VidProperties
32 import org.onap.vid.services.AsyncInstantiationBusinessLogic
33 import org.onap.vid.services.AuditService
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory
36 import org.springframework.context.annotation.Scope
37 import org.springframework.http.HttpMethod
38 import org.springframework.stereotype.Component
39 import java.time.ZonedDateTime
40 import java.time.temporal.ChronoUnit
41 import java.util.*
42
43 const val UNIQUE_INSTANCE_NAME = "optimisticUniqueServiceInstanceName"
44
45 class ServiceExpiryChecker : ExpiryChecker {
46
47     override fun isExpired(jobStartTime: ZonedDateTime?): Boolean {
48         val now = ZonedDateTime.now()
49         val maxHoursInProgress = VidProperties.getLongProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS)
50         val hoursBetween = ChronoUnit.HOURS.between(jobStartTime, now)
51         return maxHoursInProgress in 1..hoursBetween
52     }
53 }
54
55 @Component
56 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
57 class ALaCarteServiceCommand @Autowired constructor(
58         inProgressStatusService: InProgressStatusService,
59         watchChildrenJobsBL: WatchChildrenJobsBL,
60         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
61         jobsBrokerService: JobsBrokerService,
62         private val msoRequestBuilder: MsoRequestBuilder,
63         msoResultHandlerService: MsoResultHandlerService,
64         jobAdapter: JobAdapter,
65         restMso: RestMsoImplementation,
66         auditService: AuditService
67 ) : RootServiceCommand(restMso, inProgressStatusService, msoResultHandlerService,
68         watchChildrenJobsBL, jobsBrokerService, jobAdapter, asyncInstantiationBL, auditService, msoRequestBuilder), JobCommand {
69
70     companion object {
71         private val LOGGER = EELFLoggerDelegate.getLogger(ALaCarteServiceCommand::class.java)
72     }
73
74     override fun getRequest(): ServiceInstantiation {
75         return msoResultHandlerService.getRequest(sharedData)
76     }
77
78     override fun createChildren(): Job.JobStatus {
79         val dataForChild = buildDataForChild(getRequest(), actionPhase)
80
81         childJobs = pushChildrenJobsToBroker(getRequest().children, dataForChild)
82
83         return Job.JobStatus.COMPLETED_WITH_NO_ACTION
84     }
85
86     override fun addMyselfToChildrenData(commandParentData: CommandParentData, request: BaseResource) {
87         val instanceId = getActualInstanceId(request)
88         commandParentData.addInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID, instanceId)
89         commandParentData.addModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO, request.modelInfo)
90     }
91
92     override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
93         val instantiatePath = asyncInstantiationBL.getServiceInstantiationPath(request as ServiceInstantiation)
94
95         val requestDetailsWrapper = msoRequestBuilder.generateALaCarteServiceInstantiationRequest(
96                 request, optimisticUniqueServiceInstanceName, userId)
97
98         val actionDescription = "create service instance"
99
100         return MsoRestCallPlan(HttpMethod.POST, instantiatePath, Optional.of(requestDetailsWrapper), Optional.empty(), actionDescription)
101     }
102 }