5fcba5b0c9cfd16228c1e121a8cb292a310865e6
[ccsdk/cds.git] /
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.*
27 import kotlin.NoSuchElementException
28
29 /**
30  * ResourceConfigSnapshot managing service.
31  *
32  * @author Serge Simard
33  * @version 1.0
34  */
35 @Service
36 class ResourceConfigSnapshotService(private val repository: ResourceConfigSnapshotRepository) {
37
38     private val log = LoggerFactory.getLogger(ResourceConfigSnapshotService::class.toString())
39
40     suspend fun findByResourceIdAndResourceTypeAndStatus(resourceId: String, resourceType: String,
41                                                          status : ResourceConfigSnapshot.Status = RUNNING): String =
42             withContext(Dispatchers.IO) {
43                 repository.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType, status)
44                         ?.config_snapshot ?: Strings.EMPTY
45             }
46
47     suspend fun write(snapshot: String, resId: String, resType: String,
48                       status: ResourceConfigSnapshot.Status = RUNNING) : ResourceConfigSnapshot =
49             withContext(Dispatchers.IO) {
50
51                 val resourceConfigSnapshotEntry = ResourceConfigSnapshot()
52                 resourceConfigSnapshotEntry.id = UUID.randomUUID().toString()
53                 resourceConfigSnapshotEntry.resourceId = resId
54                 resourceConfigSnapshotEntry.resourceType = resType
55                 resourceConfigSnapshotEntry.status = status
56                 resourceConfigSnapshotEntry.config_snapshot = snapshot
57
58                 // Overwrite configuration snapshot entry of resId/resType
59                 if (resId.isNotEmpty() && resType.isNotEmpty()) {
60                     repository.findByResourceIdAndResourceTypeAndStatus(resId, resType, status)?.
61                         let {
62                             log.info("Overwriting configuration snapshot entry for resourceId=($resId), " +
63                                 "resourceType=($resType), status=($status)")
64                             repository.deleteByResourceIdAndResourceTypeAndStatus(resId, resType, status)
65                         }
66                 }
67                 var storedSnapshot: ResourceConfigSnapshot
68                 try {
69                     storedSnapshot = repository.saveAndFlush(resourceConfigSnapshotEntry)
70                     log.info("Stored configuration snapshot for resourceId=($resId), " +
71                             "resourceType=($resType), status=($status), " +
72                             "dated=(${storedSnapshot.createdDate})")
73                 } catch (ex: DataIntegrityViolationException) {
74                     throw BluePrintException("Failed to store configuration snapshot entry.", ex)
75                 }
76                 storedSnapshot
77             }
78 }