Optimize spring data JPA UT.
[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 findByResourceIdAndResourceTypeAndStatus(
40         resourceId: String,
41         resourceType: String,
42         status: ResourceConfigSnapshot.Status = RUNNING
43     ): String =
44         withContext(Dispatchers.IO) {
45             resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
46                 ?.config_snapshot ?: Strings.EMPTY
47         }
48
49     suspend fun write(
50         snapshot: String,
51         resId: String,
52         resType: String,
53         status: ResourceConfigSnapshot.Status = RUNNING
54     ): ResourceConfigSnapshot =
55         withContext(Dispatchers.IO) {
56
57             val resourceConfigSnapshotEntry = ResourceConfigSnapshot()
58             resourceConfigSnapshotEntry.id = UUID.randomUUID().toString()
59             resourceConfigSnapshotEntry.resourceId = resId
60             resourceConfigSnapshotEntry.resourceType = resType
61             resourceConfigSnapshotEntry.status = status
62             resourceConfigSnapshotEntry.config_snapshot = snapshot
63
64             // Overwrite configuration snapshot entry of resId/resType
65             if (resId.isNotEmpty() && resType.isNotEmpty()) {
66                 resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status)
67                     ?.let {
68                         log.info(
69                             "Overwriting configuration snapshot entry for resourceId=($resId), " +
70                                     "resourceType=($resType), status=($status)"
71                         )
72                         resourceConfigSnapshotRepository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status)
73                     }
74             }
75             var storedSnapshot: ResourceConfigSnapshot
76             try {
77                 storedSnapshot = resourceConfigSnapshotRepository.saveAndFlush(resourceConfigSnapshotEntry)
78                 log.info(
79                     "Stored configuration snapshot for resourceId=($resId), " +
80                             "resourceType=($resType), status=($status), " +
81                             "dated=(${storedSnapshot.createdDate})"
82                 )
83             } catch (ex: DataIntegrityViolationException) {
84                 throw BluePrintException("Failed to store configuration snapshot entry.", ex)
85             }
86             storedSnapshot
87         }
88 }