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 class ResourceConfigSnapshotService(private val repository: ResourceConfigSnapshotRepository) {
37 private val log = LoggerFactory.getLogger(ResourceConfigSnapshotService::class.toString())
39 suspend fun findByResourceIdAndResourceTypeAndStatus(resourceId: String, resourceType: String,
40 status : ResourceConfigSnapshot.Status = RUNNING): String =
41 withContext(Dispatchers.IO) {
42 repository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
43 ?.config_snapshot ?: Strings.EMPTY
46 suspend fun write(snapshot: String, resId: String, resType: String,
47 status: ResourceConfigSnapshot.Status = RUNNING) : ResourceConfigSnapshot =
48 withContext(Dispatchers.IO) {
50 val resourceConfigSnapshotEntry = ResourceConfigSnapshot()
51 resourceConfigSnapshotEntry.id = UUID.randomUUID().toString()
52 resourceConfigSnapshotEntry.resourceId = resId
53 resourceConfigSnapshotEntry.resourceType = resType
54 resourceConfigSnapshotEntry.status = status
55 resourceConfigSnapshotEntry.config_snapshot = snapshot
57 // Overwrite configuration snapshot entry of resId/resType
58 if (resId.isNotEmpty() && resType.isNotEmpty()) {
59 repository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status)?.
61 log.info("Overwriting configuration snapshot entry for resourceId=($resId), " +
62 "resourceType=($resType), status=($status)")
63 repository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status)
66 var storedSnapshot: ResourceConfigSnapshot
68 storedSnapshot = repository.saveAndFlush(resourceConfigSnapshotEntry)
69 log.info("Stored configuration snapshot for resourceId=($resId), " +
70 "resourceType=($resType), status=($status), " +
71 "dated=(${storedSnapshot.createdDate})")
72 } catch (ex: DataIntegrityViolationException) {
73 throw BluePrintException("Failed to store configuration snapshot entry.", ex)