e1e9b139760443c892dbd9ea1c7e6c416100506c
[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(jobUUID: UUID, msoResponse: RestObject<RequestReferencesContainer>): MsoResult {
48         return if (msoResponse.statusCode in 200..399) {
49             val jobStatus = Job.JobStatus.IN_PROGRESS
50             val msoResourceIds = MsoResourceIds(msoResponse.get().requestReferences.requestId, msoResponse.get().requestReferences.instanceId)
51             asyncInstantiationBL.auditVidStatus(jobUUID, jobStatus)
52             setInitialRequestAuditStatusFromMso(jobUUID, msoResourceIds.requestId)
53             asyncInstantiationBL.updateServiceInfo(jobUUID) { x ->
54                 x.jobStatus = jobStatus
55                 x.serviceInstanceId = msoResourceIds.instanceId
56                 x.msoRequestId = UUID.fromString(msoResourceIds.requestId)
57             }
58             MsoResult(jobStatus, msoResourceIds)
59         } else {
60             auditService.setFailedAuditStatusFromMso(jobUUID, null, msoResponse.statusCode, msoResponse.raw)
61             handleRootCommandFailed(jobUUID)
62         }
63     }
64
65     fun handleResponse(msoResponse: RestObject<RequestReferencesContainer>, actionDescription: String): MsoResult {
66         return if (msoResponse.statusCode in 200..399) {
67             val msoResourceIds = MsoResourceIds(msoResponse.get().requestReferences.requestId, msoResponse.get().requestReferences.instanceId)
68             LOGGER.debug("Successfully sent $actionDescription. Request id: ${msoResourceIds.requestId}")
69             MsoResult(Job.JobStatus.COMPLETED_WITH_NO_ACTION, msoResourceIds)
70         } else {
71             LOGGER.debug("Failed to $actionDescription. Details: ${msoResponse.raw}")
72             MsoResult(Job.JobStatus.FAILED)
73         }
74     }
75
76
77     fun handleRootCommandFailed(jobUUID: UUID): MsoResult {
78         asyncInstantiationBL.handleFailedInstantiation(jobUUID)
79         return MsoResult(Job.JobStatus.FAILED)
80     }
81
82     private fun setInitialRequestAuditStatusFromMso(jobUUID: UUID, requestId: String) {
83         val initialMsoRequestStatus = "REQUESTED"
84         asyncInstantiationBL.auditMsoStatus(jobUUID, initialMsoRequestStatus, requestId, null)
85     }
86 }