Merge "Semicolon at the end of the Statement and Remove trailing whitespaces at the...
[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.*
9 import org.onap.vid.model.serviceInstantiation.VfModule
10 import org.onap.vid.mso.RestMsoImplementation
11 import org.onap.vid.mso.model.ModelInfo
12 import org.onap.vid.services.AsyncInstantiationBusinessLogic
13 import org.springframework.beans.factory.annotation.Autowired
14 import org.springframework.beans.factory.config.ConfigurableBeanFactory
15 import org.springframework.context.annotation.Scope
16 import org.springframework.http.HttpMethod
17 import org.springframework.stereotype.Component
18 import java.util.*
19
20 typealias ToscaVfm = org.onap.vid.model.VfModule
21
22 @Component
23 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
24 class VfmoduleCommand @Autowired constructor(
25         private val asyncInstantiationBL: AsyncInstantiationBusinessLogic,
26         restMso: RestMsoImplementation,
27         private val msoRequestBuilder: MsoRequestBuilder,
28         msoResultHandlerService: MsoResultHandlerService,
29         inProgressStatusService:InProgressStatusService,
30         watchChildrenJobsBL: WatchChildrenJobsBL,
31         jobsBrokerService: JobsBrokerService,
32         jobAdapter: JobAdapter
33 ) : ResourceCommand(restMso, inProgressStatusService, msoResultHandlerService,
34         watchChildrenJobsBL, jobsBrokerService, jobAdapter), JobCommand {
35
36     companion object {
37         private val LOGGER = EELFLoggerDelegate.getLogger(VfmoduleCommand::class.java)
38     }
39
40     override fun createChildren(): Job.JobStatus {
41         return Job.JobStatus.COMPLETED_WITH_NO_ACTION
42     }
43
44     override fun planCreateMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
45         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
46         val serviceModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
47         val vnfModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.VNF_MODEL_INFO)
48         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
49         val vgInstaceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VG_INSTANCE_ID)
50
51          val instantiatePath = asyncInstantiationBL.getVfmoduleInstantiationPath(serviceInstanceId, vnfInstanceId)
52
53         val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
54                 request as VfModule,
55                 serviceModelInfo, serviceInstanceId, vnfModelInfo, vnfInstanceId, vgInstaceId, userId, testApi)
56
57         val actionDescription = "create vfmodule in $vnfInstanceId"
58
59         return MsoRestCallPlan(HttpMethod.POST, instantiatePath, Optional.of(requestDetailsWrapper), Optional.empty(), actionDescription)
60
61     }
62
63     override fun planDeleteMyselfRestCall(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String): MsoRestCallPlan {
64         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
65         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
66
67         val path = asyncInstantiationBL.getVfModuleDeletePath(serviceInstanceId, vnfInstanceId, getRequest().instanceId)
68         val requestDetailsWrapper = msoRequestBuilder.generateDeleteVfModuleRequest(getRequest(), userId)
69         return MsoRestCallPlan(HttpMethod.DELETE, path, Optional.of(requestDetailsWrapper), Optional.of(userId),
70                 "delete vfmodule ${getRequest().instanceId} from service instance $serviceInstanceId and vnf $vnfInstanceId")
71     }
72
73     override fun getRequest(): VfModule {
74         return sharedData.request as VfModule
75     }
76
77     override fun isDescendantHasAction(phase: Action): Boolean {
78         return false
79     }
80
81     private fun planReplaceMyselfRestCall3(commandParentData: CommandParentData, request: JobAdapter.AsyncJobRequest, userId: String, testApi: String?): MsoRestCallPlan {
82         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
83         val serviceModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
84         val vnfModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.VNF_MODEL_INFO)
85         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
86         val replacePath = asyncInstantiationBL.getVfModuleReplacePath(serviceInstanceId, vnfInstanceId, getRequest().instanceId)
87
88         amendModelInfoWithNewestModel(serviceModelInfo, vnfModelInfo, (request as VfModule).modelInfo)
89
90         val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
91                 request as VfModule, serviceModelInfo, serviceInstanceId,vnfModelInfo, vnfInstanceId,null,userId, testApi)
92
93         val actionDescription = "replace vfmodule ${request.instanceId}"
94
95         return MsoRestCallPlan(HttpMethod.POST, replacePath, Optional.of(requestDetailsWrapper), Optional.of(userId), actionDescription)
96     }
97
98     private fun planReplaceMyselfRestCall(commandParentData: CommandParentData): MsoRestCallPlan {
99
100         val newestModel = fetchNewestServiceModel()
101
102         val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
103         val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
104
105         val (serviceModelInfo, vnfModelInfo, vfmModelInfo) = newestSelector(newestModel, commandParentData);
106
107         val originalRequestWithNewestVfmModelInfo = getRequest().cloneWith(vfmModelInfo)
108
109         val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
110                 originalRequestWithNewestVfmModelInfo, serviceModelInfo, serviceInstanceId,
111                 vnfModelInfo, vnfInstanceId, null, sharedData.userId, sharedData.testApi)
112
113
114         val replacePath = asyncInstantiationBL.getVfModuleReplacePath(serviceInstanceId, vnfInstanceId, getRequest().instanceId)
115
116         return MsoRestCallPlan(HttpMethod.POST, replacePath, Optional.of(requestDetailsWrapper), Optional.of(sharedData.userId),
117                 "replace vfmodule ${getRequest().instanceId}")
118     }
119
120     data class ModelsInfoTriplet(val serviceModelInfo: ModelInfo, val vnfModelInfo: ModelInfo, val vfmModelInfo: ModelInfo)
121
122     private fun newestSelector(newestModel: ServiceModel, commandParentData: CommandParentData): ModelsInfoTriplet {
123         val serviceModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
124         val vfmModelInfo = getRequest().modelInfo
125         val vnfModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.VNF_MODEL_INFO)
126
127         val newestServiceModelInfo = newestServiceModelInfo(newestModel)
128         val newestVfmModelInfo = newestVfmModelInfo(newestModel)
129         val newestVnfModelInfo = newestVnfModelInfo(newestModel, commandParentData)
130
131         return if (newestServiceModelInfo == null || newestVfmModelInfo == null || newestVnfModelInfo == null) {
132             ModelsInfoTriplet(serviceModelInfo, vnfModelInfo, vfmModelInfo)
133         } else {
134             ModelsInfoTriplet(newestServiceModelInfo, newestVnfModelInfo, newestVfmModelInfo)
135         }
136     }
137
138     private fun newestServiceModelInfo(newestModel: ServiceModel) = toModelInfo(newestModel.service)
139
140     private fun newestVfmModelInfo(newestModel: ServiceModel): ModelInfo? {
141         val vfmModelInfo = getRequest().modelInfo
142         val newestVfm = selectVfm(newestModel, vfmModelInfo)
143         return toModelInfo(newestVfm)
144     }
145
146     private fun newestVnfModelInfo(newestModel: ServiceModel, commandParentData: CommandParentData): ModelInfo? {
147         val vnfModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.VNF_MODEL_INFO)
148         val newestVnf = selectVnf(newestModel, vnfModelInfo)
149         return toModelInfo(newestVnf)
150     }
151
152     private fun selectVfm(newestModel: ServiceModel, modelInfo: ModelInfo) = newestModel.vfModules[modelInfo.modelCustomizationId]
153
154     private fun selectVnf(newestModel: ServiceModel, modelInfo: ModelInfo) = newestModel.vnfs[modelInfo.modelCustomizationId]
155
156     private fun toModelInfo(toBeConverted: VNF?): ModelInfo? = toBeConverted?.let { toModelInfo(it, "vnf") }
157
158     private fun toModelInfo(toBeConverted: ToscaVfm?): ModelInfo? = toBeConverted?.let { toModelInfo(it, "vfModule") }
159
160     private fun toModelInfo(toBeConverted: MinimalNode, modelType: String): ModelInfo {
161         val targetModelInfo = ModelInfo()
162
163         targetModelInfo.modelType = modelType
164         targetModelInfo.modelName = toBeConverted.name
165         targetModelInfo.modelNameVersionId = null
166         targetModelInfo.modelVersion = toBeConverted.version
167         targetModelInfo.modelVersionId = toBeConverted.uuid
168         targetModelInfo.modelInvariantId = toBeConverted.invariantUuid
169
170         targetModelInfo.modelCustomizationId = when (toBeConverted) {
171             is VNF -> toBeConverted.customizationUuid
172             is ToscaVfm -> toBeConverted.customizationUuid
173             else -> throw IllegalArgumentException()
174         }
175
176         targetModelInfo.modelCustomizationName = when (toBeConverted) {
177             is VNF -> toBeConverted.modelCustomizationName
178             is ToscaVfm -> toBeConverted.modelCustomizationName
179             else -> throw IllegalArgumentException()
180         }
181
182         return targetModelInfo
183     }
184
185     private fun toModelInfo(toBeConverted: Service?): ModelInfo? {
186
187         if (toBeConverted == null)
188             return null
189
190         val targetModelInfo = ModelInfo()
191
192         targetModelInfo.modelVersionId = toBeConverted.uuid
193         targetModelInfo.modelInvariantId = toBeConverted.invariantUuid
194         targetModelInfo.modelVersion = toBeConverted.version
195         //targetModelInfo.modelCustomizationId = toBeConverted.customizationUuid
196         //targetModelInfo.modelCustomizationName = toBeConverted.modelCustomizationName
197         targetModelInfo.modelType = "service"
198         targetModelInfo.modelName = toBeConverted.name
199
200         return targetModelInfo
201     }
202
203     private fun amendModelInfoWithNewestModel(serviceModelInfo: ModelInfo, vnfModelInfo: ModelInfo, vfmModelInfo: ModelInfo) {
204         val newestModel = fetchNewestServiceModel()
205         val newestService = newestModel.service
206
207         val newestVfm = newestModel.vfModules[vfmModelInfo.modelCustomizationId]
208         val newestVnf = newestModel.vnfs[vnfModelInfo.modelCustomizationId]
209
210         if (!(newestService == null || newestVnf == null || newestVfm == null)) {
211
212             serviceModelInfo.modelName = newestService.name
213             serviceModelInfo.modelVersionId = newestService.uuid
214             serviceModelInfo.modelVersion = newestService.version
215
216             vnfModelInfo.modelName = newestVnf.name
217             vnfModelInfo.modelVersionId = newestVnf.uuid
218             vnfModelInfo.modelVersion = newestVnf.version
219             vnfModelInfo.modelCustomizationId = newestVnf.customizationUuid
220             vnfModelInfo.modelCustomizationName = newestVnf.modelCustomizationName
221
222             vfmModelInfo.modelName = newestVfm.name
223             vfmModelInfo.modelVersionId = newestVfm.uuid
224             vfmModelInfo.modelVersion = newestVfm.version
225             vfmModelInfo.modelCustomizationId = newestVfm.customizationUuid
226             vfmModelInfo.modelCustomizationName = newestVfm.modelCustomizationName
227         }
228     }
229
230
231     override fun replaceMyself(): Job.JobStatus {
232         try {
233             val replaceMyselfCommand = planReplaceMyselfRestCall(commandParentData)
234             return executeAndHandleMsoInstanceRequest(replaceMyselfCommand)
235         } catch (exception: Exception) {
236             LOGGER.error("Failed to replace instanceId ${getRequest().instanceId} ", exception)
237             return Job.JobStatus.FAILED
238         }
239     }
240
241     override fun isNeedToReplaceMySelf(): Boolean {
242         return getActionType() == Action.Upgrade
243     }
244
245     private fun fetchNewestServiceModel(): ServiceModel {
246         val serviceModelInfo = commandParentData.getModelInfo(CommandParentData.CommandDataKey.SERVICE_MODEL_INFO)
247         var modelNewestUuid = commandUtils.getNewestModelUuid(serviceModelInfo.modelInvariantId);
248         var serviceNewestModel = commandUtils.getServiceModel(modelNewestUuid);
249
250         return serviceNewestModel;
251     }
252 }