Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / MsoResultHandlerService.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.impl.JobSharedData
26 import org.onap.vid.model.RequestReferencesContainer
27 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation
28 import org.onap.vid.mso.RestObject
29 import org.onap.vid.services.AsyncInstantiationBusinessLogic
30 import org.onap.vid.services.AuditService
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.stereotype.Service
33 import java.util.*
34
35 @Service
36 class MsoResultHandlerService
37 @Autowired constructor(private val asyncInstantiationBL: AsyncInstantiationBusinessLogic, private val auditService: AuditService) {
38
39     companion object {
40         private val LOGGER = EELFLoggerDelegate.getLogger(MsoResultHandlerService::class.java)
41     }
42
43     fun getRequest(jobSharedData: JobSharedData): ServiceInstantiation {
44         return jobSharedData.request as ServiceInstantiation
45     }
46
47     fun handleRootResponse(sharedData: JobSharedData, msoResponse: RestObject<RequestReferencesContainer>): MsoResult {
48         val jobUUID:UUID = sharedData.jobUuid
49         return if (msoResponse.statusCode in 200..399) {
50             val jobStatus = Job.JobStatus.IN_PROGRESS
51             val msoResourceIds = MsoResourceIds(msoResponse.get().requestReferences.requestId, msoResponse.get().requestReferences.instanceId)
52             auditService.auditVidStatus(jobUUID, jobStatus)
53             setInitialRequestAuditStatusFromMso(jobUUID, msoResourceIds.requestId)
54             asyncInstantiationBL.updateServiceInfo(jobUUID) { x ->
55                 x.jobStatus = jobStatus
56                 x.serviceInstanceId = msoResourceIds.instanceId
57                 x.msoRequestId = UUID.fromString(msoResourceIds.requestId)
58             }
59             asyncInstantiationBL.addResourceInfo(sharedData, jobStatus, msoResourceIds.instanceId)
60             MsoResult(Job.JobStatus.COMPLETED_WITH_NO_ACTION, msoResourceIds)
61         } else {
62             auditService.setFailedAuditStatusFromMso(jobUUID, null, msoResponse.statusCode, msoResponse.raw)
63             asyncInstantiationBL.addFailedResourceInfo(sharedData, msoResponse)
64             return MsoResult(Job.JobStatus.FAILED)
65         }
66     }
67
68     fun handleResponse(sharedData: JobSharedData, msoResponse: RestObject<RequestReferencesContainer>, actionDescription: String): MsoResult {
69         return if (msoResponse.statusCode in 200..399) {
70             val msoResourceIds = MsoResourceIds(msoResponse.get().requestReferences.requestId, msoResponse.get().requestReferences.instanceId)
71             LOGGER.debug("Successfully sent $actionDescription. Request id: ${msoResourceIds.requestId}")
72             asyncInstantiationBL.addResourceInfo(sharedData, Job.JobStatus.IN_PROGRESS, msoResourceIds.instanceId)
73             MsoResult(Job.JobStatus.COMPLETED_WITH_NO_ACTION, msoResourceIds)
74         } else {
75             LOGGER.debug("Failed to $actionDescription. Details: ${msoResponse.raw}")
76             asyncInstantiationBL.addFailedResourceInfo(sharedData, msoResponse)
77             MsoResult(Job.JobStatus.FAILED)
78         }
79     }
80
81     private fun setInitialRequestAuditStatusFromMso(jobUUID: UUID, requestId: String) {
82         val initialMsoRequestStatus = "REQUESTED"
83         auditService.auditMsoStatus(jobUUID, initialMsoRequestStatus, requestId, null)
84     }
85 }