2 * Copyright (C) 2019 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db
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
29 * ResourceConfigSnapshot managing service.
31 * @author Serge Simard
35 open class ResourceConfigSnapshotService(private val resourceConfigSnapshotRepository: ResourceConfigSnapshotRepository) {
37 private val log = LoggerFactory.getLogger(ResourceConfigSnapshotService::class.toString())
39 suspend fun findAllByResourceIdForStatus(
41 status: ResourceConfigSnapshot.Status
42 ): List<ResourceConfigSnapshot>? =
43 withContext(Dispatchers.IO) {
44 resourceConfigSnapshotRepository.findByResourceIdAndStatusOrderByCreatedDateDesc(resourceId, status)
47 suspend fun findAllByResourceId(
49 ): List<ResourceConfigSnapshot>? =
50 withContext(Dispatchers.IO) {
51 resourceConfigSnapshotRepository.findByResourceIdOrderByCreatedDateDesc(resourceId)
54 suspend fun findAllByResourceTypeForStatus(
56 status: ResourceConfigSnapshot.Status
57 ): List<ResourceConfigSnapshot>? =
58 withContext(Dispatchers.IO) {
59 resourceConfigSnapshotRepository.findByResourceTypeAndStatusOrderByCreatedDateDesc(resourceType, status)
62 suspend fun findAllByResourceType(
64 ): List<ResourceConfigSnapshot>? =
65 withContext(Dispatchers.IO) {
66 resourceConfigSnapshotRepository.findByResourceTypeOrderByCreatedDateDesc(resourceType)
69 suspend fun findByResourceIdAndResourceTypeAndStatus(
72 status: ResourceConfigSnapshot.Status = RUNNING
74 withContext(Dispatchers.IO) {
75 resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
76 ?.config_snapshot ?: Strings.EMPTY
83 status: ResourceConfigSnapshot.Status = RUNNING
84 ): ResourceConfigSnapshot =
85 withContext(Dispatchers.IO) {
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
94 // Overwrite configuration snapshot entry of resId/resType
95 if (resId.isNotEmpty() && resType.isNotEmpty()) {
96 resourceConfigSnapshotRepository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status)
99 "Overwriting configuration snapshot entry for resourceId=($resId), " +
100 "resourceType=($resType), status=($status)"
102 resourceConfigSnapshotRepository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status)
105 var storedSnapshot: ResourceConfigSnapshot
107 storedSnapshot = resourceConfigSnapshotRepository.saveAndFlush(resourceConfigSnapshotEntry)
109 "Stored configuration snapshot for resourceId=($resId), " +
110 "resourceType=($resType), status=($status), " +
111 "dated=(${storedSnapshot.createdDate})"
113 } catch (ex: DataIntegrityViolationException) {
114 throw BluePrintException("Failed to store configuration snapshot entry.", ex)
119 suspend fun deleteByResourceIdAndResourceTypeAndStatus(
121 resourceType: String,
122 status: ResourceConfigSnapshot.Status
124 resourceConfigSnapshotRepository.deleteByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
126 "Deleted configuration snapshot for resourceId=($resourceId), " +
127 "resourceType=($resourceType), status=($status)"