Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintChainedService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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
17 package org.onap.ccsdk.cds.controllerblueprints.core.service
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
20 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
21 /**
22  *
23  *
24  * @author Brinda Santh
25  */
26 class BluePrintChainedService {
27     var bpc : BluePrintContext
28
29     constructor(bpc : BluePrintContext){
30         this.bpc = bpc
31     }
32
33     fun nodeTypeChained(nodeTypeName: String): NodeType {
34
35         val nodeType: NodeType = bpc.nodeTypeByName(nodeTypeName)
36         val attributes = hashMapOf<String, AttributeDefinition>()
37         val properties = hashMapOf<String, PropertyDefinition>()
38         val requirements = hashMapOf<String, RequirementDefinition>()
39         val capabilities = hashMapOf<String, CapabilityDefinition>()
40         val interfaces = hashMapOf<String, InterfaceDefinition>()
41         val artifacts = hashMapOf<String, ArtifactDefinition>()
42
43         recNodeTypesChained(nodeTypeName).forEach { nodeType ->
44
45             val subAttributes =  bpc.nodeTypeByName(nodeType.id!!).attributes
46             if (subAttributes != null) {
47                 attributes.putAll(subAttributes)
48             }
49
50             val subProperties =  bpc.nodeTypeByName(nodeType.id!!).properties
51             if (subProperties != null) {
52                 properties.putAll(subProperties)
53             }
54
55             val subRequirements =  bpc.nodeTypeByName(nodeType.id!!).requirements
56             if (subRequirements != null) {
57                 requirements.putAll(subRequirements)
58             }
59             val subCapabilities =  bpc.nodeTypeByName(nodeType.id!!).capabilities
60             if (subCapabilities != null) {
61                 capabilities.putAll(subCapabilities)
62             }
63             val subInterfaces =  bpc.nodeTypeByName(nodeType.id!!).interfaces
64             if (subInterfaces != null) {
65                 interfaces.putAll(subInterfaces)
66             }
67
68             val subArtifacts =  bpc.nodeTypeByName(nodeType.id!!).artifacts
69             if (subArtifacts != null) {
70                 artifacts.putAll(subArtifacts)
71             }
72         }
73         nodeType.attributes = attributes
74         nodeType.properties = properties
75         nodeType.requirements = requirements
76         nodeType.capabilities = capabilities
77         nodeType.interfaces = interfaces
78         nodeType.artifacts = artifacts
79         return nodeType
80     }
81
82     fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
83         val nodeType =  bpc.nodeTypeByName(nodeTypeName)
84         val properties = hashMapOf<String, PropertyDefinition>()
85
86         recNodeTypesChained(nodeTypeName).forEach { nodeType ->
87             val subProperties =  bpc.nodeTypeByName(nodeType.id!!).properties
88             if (subProperties != null) {
89                 properties.putAll(subProperties)
90             }
91         }
92         return properties
93     }
94
95     private fun recNodeTypesChained(nodeTypeName: String, nodeTypes: MutableList<NodeType>? = arrayListOf()): MutableList<NodeType> {
96         val nodeType: NodeType =  bpc.nodeTypeByName(nodeTypeName)
97         nodeType.id = nodeTypeName
98         val derivedFrom: String = nodeType.derivedFrom
99         if (!BluePrintTypes.rootNodeTypes().contains(derivedFrom)) {
100             recNodeTypesChained(derivedFrom, nodeTypes)
101         }
102         nodeTypes!!.add(nodeType)
103         return nodeTypes
104     }
105
106     private fun recDataTypesChained(dataTypeName: String, dataTypes: MutableList<DataType>? = arrayListOf()): MutableList<DataType> {
107         val dataType: DataType =  bpc.dataTypeByName(dataTypeName)!!
108         dataType.id = dataTypeName
109         val derivedFrom: String = dataType.derivedFrom
110         if (!BluePrintTypes.rootDataTypes().contains(derivedFrom)) {
111             recDataTypesChained(derivedFrom, dataTypes)
112         }
113         dataTypes!!.add(dataType)
114         return dataTypes
115     }
116
117 }