Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / VfmoduleCommand.kt
1 package org.onap.vid.job.command
2
3 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate
4 import org.onap.vid.job.Job
5 import org.onap.vid.job.JobAdapter
6 import org.onap.vid.job.JobCommand
7 import org.onap.vid.job.JobsBrokerService
8 import org.onap.vid.model.Action
9 import org.onap.vid.model.serviceInstantiation.VfModule
10 import org.onap.vid.mso.RestMsoImplementation
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.util.*
18
19 @Component
20 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
21 class VfmoduleCommand @Autowired constructor(
22         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
23         restMso: RestMsoImplementation,
24         private val msoRequestBuilder: MsoRequestBuilder,
25         msoResultHandlerService: MsoResultHandlerService,
26         inProgressStatusService:InProgressStatusService,
27         watchChildrenJobsBL: WatchChildrenJobsBL,
28         jobsBrokerService: JobsBrokerService,
29         jobAdapter: JobAdapter
30 ) : ResourceCommand(restMso, inProgressStatusService, msoResultHandlerService,
31         watchChildrenJobsBL, jobsBrokerService, jobAdapter), JobCommand {
32
33     companion object {
34         private val LOGGER = EELFLoggerDelegate.getLogger(VfmoduleCommand::class.java)
35     }
36
37     override fun createChildren(): Job.JobStatus {
38         return Job.JobStatus.COMPLETED_WITH_NO_ACTION
39     }
40
41     override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
42         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
43         val serviceModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
44         val vnfModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.VNF_MODEL_INFO)
45         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
46         val vgInstaceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VG_INSTANCE_ID)
47
48         val instantiatePath = asyncInstantiationBL.getVfmoduleInstantiationPath(serviceInstanceId, vnfInstanceId)
49
50         val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
51                 request as VfModule,
52                 serviceModelInfo, serviceInstanceId, vnfModelInfo, vnfInstanceId, vgInstaceId, userId, testApi)
53
54         val actionDescription = "create vfmodule in $vnfInstanceId"
55
56         return MsoRestCallPlan(HttpMethod.POST, instantiatePath, Optional.of(requestDetailsWrapper), Optional.empty(), actionDescription)
57
58     }
59
60     override fun planDeleteMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
61         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
62         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
63
64         val path = asyncInstantiationBL.getVfModuleDeletePath(serviceInstanceId, vnfInstanceId, getRequest().instanceId)
65         val requestDetailsWrapper = msoRequestBuilder.generateDeleteVfModuleRequest(getRequest(), userId)
66         return MsoRestCallPlan(HttpMethod.DELETE, path, Optional.of(requestDetailsWrapper), Optional.of(userId),
67                 "delete vfmodule ${getRequest().instanceId} from service instance $serviceInstanceId and vnf $vnfInstanceId")
68     }
69
70     override fun getRequest(): VfModule {
71         return sharedData.request as VfModule
72     }
73
74     override fun isDescendantHasAction(phase: Action): Boolean {
75         return false
76     }
77
78     private fun planReplaceMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
79         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
80         val serviceModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
81         val vnfModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.VNF_MODEL_INFO)
82         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
83         val replacePath = asyncInstantiationBL.getVfModuleReplacePath(serviceInstanceId, vnfInstanceId, getRequest().instanceId)
84
85         val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest( 
86                 request as VfModule, serviceModelInfo, serviceInstanceId,vnfModelInfo, vnfInstanceId,null,userId, testApi)
87
88         val actionDescription = "replace vfmodule ${request.instanceId}"
89
90         return MsoRestCallPlan(HttpMethod.POST, replacePath, Optional.of(requestDetailsWrapper), Optional.of(userId), actionDescription)
91     }
92
93     override fun replaceMyself(): Job.JobStatus {
94         try {
95             val replaceMyselfCommand = planReplaceMyselfRestCall(commandParentData, sharedData.request, sharedData.userId, sharedData.testApi )
96             return executeAndHandleMsoInstanceRequest(replaceMyselfCommand)
97         } catch (exception: Exception) {
98             LOGGER.error("Failed to replace instanceId ${getRequest().instanceId} ", exception)
99             return Job.JobStatus.FAILED
100         }
101     }
102
103     override fun isNeedToReplaceMySelf(): Boolean {
104         return getActionType() == Action.Replace
105     }
106 }