1 package org.onap.vid.job.command
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
20 typealias ToscaVfm = org.onap.vid.model.VfModule
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 {
37 private val LOGGER = EELFLoggerDelegate.getLogger(VfmoduleCommand::class.java)
40 override fun createChildren(): Job.JobStatus {
41 return Job.JobStatus.COMPLETED_WITH_NO_ACTION
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)
51 val instantiatePath = asyncInstantiationBL.getVfmoduleInstantiationPath(serviceInstanceId, vnfInstanceId)
53 val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
55 serviceModelInfo, serviceInstanceId, vnfModelInfo, vnfInstanceId, vgInstaceId, userId, testApi)
57 val actionDescription = "create vfmodule in $vnfInstanceId"
59 return MsoRestCallPlan(HttpMethod.POST, instantiatePath, Optional.of(requestDetailsWrapper), Optional.empty(), actionDescription)
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)
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")
73 override fun getRequest(): VfModule {
74 return sharedData.request as VfModule
77 override fun isDescendantHasAction(phase: Action): Boolean {
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)
88 amendModelInfoWithNewestModel(serviceModelInfo, vnfModelInfo, (request as VfModule).modelInfo)
90 val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
91 request as VfModule, serviceModelInfo, serviceInstanceId,vnfModelInfo, vnfInstanceId,null,userId, testApi)
93 val actionDescription = "replace vfmodule ${request.instanceId}"
95 return MsoRestCallPlan(HttpMethod.POST, replacePath, Optional.of(requestDetailsWrapper), Optional.of(userId), actionDescription)
98 private fun planReplaceMyselfRestCall(commandParentData: CommandParentData): MsoRestCallPlan {
100 val newestModel = fetchNewestServiceModel()
102 val serviceInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.SERVICE_INSTANCE_ID)
103 val vnfInstanceId = commandParentData.getInstanceId(CommandParentData.CommandDataKey.VNF_INSTANCE_ID)
105 val (serviceModelInfo, vnfModelInfo, vfmModelInfo) = newestSelector(newestModel, commandParentData);
107 val originalRequestWithNewestVfmModelInfo = getRequest().cloneWith(vfmModelInfo)
109 val requestDetailsWrapper = msoRequestBuilder.generateVfModuleInstantiationRequest(
110 originalRequestWithNewestVfmModelInfo, serviceModelInfo, serviceInstanceId,
111 vnfModelInfo, vnfInstanceId, null, sharedData.userId, sharedData.testApi)
114 val replacePath = asyncInstantiationBL.getVfModuleReplacePath(serviceInstanceId, vnfInstanceId, getRequest().instanceId)
116 return MsoRestCallPlan(HttpMethod.POST, replacePath, Optional.of(requestDetailsWrapper), Optional.of(sharedData.userId),
117 "replace vfmodule ${getRequest().instanceId}")
120 data class ModelsInfoTriplet(val serviceModelInfo: ModelInfo, val vnfModelInfo: ModelInfo, val vfmModelInfo: ModelInfo)
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)
127 val newestServiceModelInfo = newestServiceModelInfo(newestModel)
128 val newestVfmModelInfo = newestVfmModelInfo(newestModel)
129 val newestVnfModelInfo = newestVnfModelInfo(newestModel, commandParentData)
131 return if (newestServiceModelInfo == null || newestVfmModelInfo == null || newestVnfModelInfo == null) {
132 ModelsInfoTriplet(serviceModelInfo, vnfModelInfo, vfmModelInfo)
134 ModelsInfoTriplet(newestServiceModelInfo, newestVnfModelInfo, newestVfmModelInfo)
138 private fun newestServiceModelInfo(newestModel: ServiceModel) = toModelInfo(newestModel.service)
140 private fun newestVfmModelInfo(newestModel: ServiceModel): ModelInfo? {
141 val vfmModelInfo = getRequest().modelInfo
142 val newestVfm = selectVfm(newestModel, vfmModelInfo)
143 return toModelInfo(newestVfm)
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)
152 private fun selectVfm(newestModel: ServiceModel, modelInfo: ModelInfo) = newestModel.vfModules[modelInfo.modelCustomizationId]
154 private fun selectVnf(newestModel: ServiceModel, modelInfo: ModelInfo) = newestModel.vnfs[modelInfo.modelCustomizationId]
156 private fun toModelInfo(toBeConverted: VNF?): ModelInfo? = toBeConverted?.let { toModelInfo(it, "vnf") }
158 private fun toModelInfo(toBeConverted: ToscaVfm?): ModelInfo? = toBeConverted?.let { toModelInfo(it, "vfModule") }
160 private fun toModelInfo(toBeConverted: MinimalNode, modelType: String): ModelInfo {
161 val targetModelInfo = ModelInfo()
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
170 targetModelInfo.modelCustomizationId = when (toBeConverted) {
171 is VNF -> toBeConverted.customizationUuid
172 is ToscaVfm -> toBeConverted.customizationUuid
173 else -> throw IllegalArgumentException()
176 targetModelInfo.modelCustomizationName = when (toBeConverted) {
177 is VNF -> toBeConverted.modelCustomizationName
178 is ToscaVfm -> toBeConverted.modelCustomizationName
179 else -> throw IllegalArgumentException()
182 return targetModelInfo
185 private fun toModelInfo(toBeConverted: Service?): ModelInfo? {
187 if (toBeConverted == null)
190 val targetModelInfo = ModelInfo()
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
200 return targetModelInfo
203 private fun amendModelInfoWithNewestModel(serviceModelInfo: ModelInfo, vnfModelInfo: ModelInfo, vfmModelInfo: ModelInfo) {
204 val newestModel = fetchNewestServiceModel()
205 val newestService = newestModel.service
207 val newestVfm = newestModel.vfModules[vfmModelInfo.modelCustomizationId]
208 val newestVnf = newestModel.vnfs[vnfModelInfo.modelCustomizationId]
210 if (!(newestService == null || newestVnf == null || newestVfm == null)) {
212 serviceModelInfo.modelName = newestService.name
213 serviceModelInfo.modelVersionId = newestService.uuid
214 serviceModelInfo.modelVersion = newestService.version
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
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
231 override fun replaceMyself(): Job.JobStatus {
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
241 override fun isNeedToReplaceMySelf(): Boolean {
242 return getActionType() == Action.Upgrade
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);
250 return serviceNewestModel;