cdd2550a8f08e8b592cb6dcffb7c018a4b80a76c
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / parser / ServiceModelInflator.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.asdc.parser
22
23 import org.onap.vid.model.*
24 import org.springframework.stereotype.Component
25
26 @Component
27 class ServiceModelInflator {
28
29     data class Names (val modelCustomizationName: String?, val modelKey: String?)
30
31     fun toNamesByVersionId(model: ServiceModel): Map<String, Names> {
32         return emptyMap<String, Names>()
33                 .plus(inflate(model.networks))
34                 .plus(inflate(model.vnfs))
35                 .plus(inflate(model.vnfGroups))
36                 .plus(inflate(model.vrfs))
37                 .plus(inflate(model.collectionResources))
38     }
39
40     private fun inflate(instances: Map<String, *>): Map<String, Names> {
41         return instances.entries.map { inflate(it.key, it.value) }.fold(emptyMap()) { acc, it -> acc.plus(it) }
42     }
43
44     private fun inflate(modelKey: String, vnf: VNF): Map<String, Names> {
45         return mapOf(vnf.uuid to Names(vnf.modelCustomizationName, modelKey))
46                 .plus(inflate(vnf.vfModules))
47                 .plus(inflate(vnf.volumeGroups))
48     }
49
50     private fun inflate(modelKey: String, cr: CR): Map<String, Names> {
51         return mapOf(cr.uuid to Names(null, modelKey))
52                 .plus(inflate(cr.networksCollection))
53     }
54
55     private fun inflate(modelKey: String, instance: Any?): Map<String, Names> {
56         return when (instance) {
57             is Network -> mapOf(instance.uuid to Names(instance.modelCustomizationName, modelKey))
58             is VfModule -> mapOf(instance.uuid to Names(instance.modelCustomizationName, modelKey))
59             is VolumeGroup -> mapOf(instance.uuid to Names(instance.modelCustomizationName, modelKey))
60             is ResourceGroup -> mapOf(instance.uuid to Names(instance.modelCustomizationName, modelKey))
61             is VNF -> inflate(modelKey, instance)
62             is CR -> inflate(modelKey, instance)
63             is NetworkCollection -> mapOf(instance.uuid to Names(null, modelKey))
64             is Node -> mapOf(instance.uuid to Names(null, modelKey))
65
66             else -> {
67                 // sink
68                 emptyMap()
69             }
70         }
71     }
72
73 }