Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / config-snapshots / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / config / snapshots / db / ResourceConfigSnapshotService.kt
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db
17
18 import kotlinx.coroutines.Dispatchers
19 import kotlinx.coroutines.withContext
20 import org.apache.logging.log4j.util.Strings
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshot.Status.RUNNING
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.slf4j.LoggerFactory
24 import org.springframework.dao.DataIntegrityViolationException
25 import org.springframework.stereotype.Service
26 import java.util.UUID
27
28 /**
29  * ResourceConfigSnapshot managing service.
30  *
31  * @author Serge Simard
32  * @version 1.0
33  */
34 @Service
35 open class ResourceConfigSnapshotService(private val resourceConfigSnapshotRepository: ResourceConfigSnapshotRepository) {
36
37     private val log = LoggerFactory.getLogger(ResourceConfigSnapshotService::class.toString())
38
39     suspend fun findAllByResourceIdForStatus(
40         resourceId: String,
41         status: ResourceConfigSnapshot.Status
42     ): List<ResourceConfigSnapshot>? =
43         withContext(Dispatchers.IO) {
44             resourceConfigSnapshotRepository.findByResourceIdAndStatusOrderByCreatedDateDesc(resourceId, status)
45         }
46
47     suspend fun findAllByResourceId(
48         resourceId: String
49     ): List<ResourceConfigSnapshot>? =
50         withContext(Dispatchers.IO) {
51             resourceConfigSnapshotRepository.findByResourceIdOrderByCreatedDateDesc(resourceId)
52         }
53
54     suspend fun findAllByResourceTypeForStatus(
55         resourceType: String,
56         status: ResourceConfigSnapshot.Status
57     ): List<ResourceConfigSnapshot>? =
58         withContext(Dispatchers.IO) {
59             resourceConfigSnapshotRepository.findByResourceTypeAndStatusOrderByCreatedDateDesc(resourceType, status)
60         }
61
62     suspend fun findAllByResourceType(
63         resourceType: String
64     ): List<ResourceConfigSnapshot>? =
65         withContext(Dispatchers.IO) {
66             resourceConfigSnapshotRepository.findByResourceTypeOrderByCreatedDateDesc(resourceType)
67         }
68
69     suspend fun findByResourceIdAndResourceTypeAndStatus(
70         resourceId: String,
71         resourceType: String,
72         status: ResourceConfigSnapshot.Status = RUNNING
73     ): String =
74         withContext(Dispatchers.IO) {
75             resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
76                 ?.config_snapshot ?: Strings.EMPTY
77         }
78
79     suspend fun write(
80         snapshot: String,
81         resId: String,
82         resType: String,
83         status: ResourceConfigSnapshot.Status = RUNNING
84     ): ResourceConfigSnapshot =
85         withContext(Dispatchers.IO) {
86
87             val resourceConfigSnapshotEntry = ResourceConfigSnapshot()
88             resourceConfigSnapshotEntry.id = UUID.randomUUID().toString()
89             resourceConfigSnapshotEntry.resourceId = resId
90             resourceConfigSnapshotEntry.resourceType = resType
91             resourceConfigSnapshotEntry.status = status
92             resourceConfigSnapshotEntry.config_snapshot = snapshot
93
94             // Overwrite configuration snapshot entry of resId/resType
95             if (resId.isNotEmpty() && resType.isNotEmpty()) {
96                 resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status)
97                     ?.let {
98                         log.info(
99                             "Overwriting configuration snapshot entry for resourceId=($resId), " +
100                                 "resourceType=($resType), status=($status)"
101                         )
102                         resourceConfigSnapshotRepository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status)
103                     }
104             }
105             var storedSnapshot: ResourceConfigSnapshot
106             try {
107                 storedSnapshot = resourceConfigSnapshotRepository.saveAndFlush(resourceConfigSnapshotEntry)
108                 log.info(
109                     "Stored configuration snapshot for resourceId=($resId), " +
110                         "resourceType=($resType), status=($status), " +
111                         "dated=(${storedSnapshot.createdDate})"
112                 )
113             } catch (ex: DataIntegrityViolationException) {
114                 throw BluePrintException("Failed to store configuration snapshot entry.", ex)
115             }
116             storedSnapshot
117         }
118
119     suspend fun deleteByResourceIdAndResourceTypeAndStatus(
120         resourceId: String,
121         resourceType: String,
122         status: ResourceConfigSnapshot.Status
123     ) {
124         resourceConfigSnapshotRepository.deleteByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
125         log.info(
126             "Deleted configuration snapshot for resourceId=($resourceId), " +
127                 "resourceType=($resourceType), status=($status)"
128         )
129     }
130 }