aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/service/BluePrintRepoServiceImpl.kt
blob: 420a9fcd7a5a10b430597ebf05f26a5dbfc9858d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2019 IBM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service

import com.fasterxml.jackson.databind.JsonNode
import com.google.common.base.Preconditions
import org.apache.commons.lang3.StringUtils
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ModelTypeRepository
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityDefinition
import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
import org.springframework.stereotype.Service

interface ResourceDefinitionRepoService : BluePrintRepoService {

    @Throws(BluePrintException::class)
    fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
}

@Service
open class BluePrintRepoFileService(
    private val modelTypeRepository: ModelTypeRepository,
    private val resourceDictionaryRepository: ResourceDictionaryRepository
) : ResourceDefinitionRepoService {

    @Throws(BluePrintException::class)
    override fun getNodeType(nodeTypeName: String): NodeType {
        return getModelType(nodeTypeName, NodeType::class.java)
            ?: throw BluePrintException("couldn't get NodeType($nodeTypeName)")
    }

    @Throws(BluePrintException::class)
    override fun getDataType(dataTypeName: String): DataType {
        return getModelType(dataTypeName, DataType::class.java)
            ?: throw BluePrintException("couldn't get DataType($dataTypeName)")
    }

    @Throws(BluePrintException::class)
    override fun getArtifactType(artifactTypeName: String): ArtifactType {
        return getModelType(artifactTypeName, ArtifactType::class.java)
            ?: throw BluePrintException("couldn't get ArtifactType($artifactTypeName)")
    }

    @Throws(BluePrintException::class)
    override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
        return getModelType(relationshipTypeName, RelationshipType::class.java)
            ?: throw BluePrintException("couldn't get RelationshipType($relationshipTypeName)")
    }

    @Throws(BluePrintException::class)
    override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
        return getModelType(capabilityDefinitionName, CapabilityDefinition::class.java)
            ?: throw BluePrintException("couldn't get CapabilityDefinition($capabilityDefinitionName)")
    }

    @Throws(BluePrintException::class)
    override fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition {
        val dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName)
        return if (dbResourceDictionary != null) {
            dbResourceDictionary.definition
        } else {
            throw BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
        }
    }

    @Throws(BluePrintException::class)
    private fun <T> getModelType(modelName: String, valueClass: Class<T>): T? {
        Preconditions.checkArgument(
            StringUtils.isNotBlank(modelName),
            "Failed to get model from repo, model name is missing"
        )

        val modelDefinition = getModelDefinition(modelName)
        Preconditions.checkNotNull(
            modelDefinition,
            String.format("Failed to get model content for model name (%s)", modelName)
        )

        return JacksonUtils.readValue(modelDefinition, valueClass)
    }

    @Throws(BluePrintException::class)
    private fun getModelDefinition(modelName: String): JsonNode {
        val modelDefinition: JsonNode
        val modelTypeDb = modelTypeRepository.findByModelName(modelName)
        if (modelTypeDb != null) {
            modelDefinition = modelTypeDb.definition
        } else {
            throw BluePrintException(String.format("failed to get model definition (%s) from repo", modelName))
        }
        return modelDefinition
    }
}