Merge "VNF's LCP regions found by Line-of-business (and owning-entity)"
[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     private data class Ids (val modelCustomizationId: String?, val modelVersionId: String)
30     data class Names (val modelCustomizationName: String?, val modelKey: String?)
31
32     fun toNamesByVersionId(model: ServiceModel): Map<String, Names> {
33         return toNamesByIds(model).mapKeys { it.key.modelVersionId }
34     }
35
36     fun toNamesByCustomizationId(model: ServiceModel): Map<String, Names> {
37         return toNamesByIds(model)
38                 .filterKeys { it.modelCustomizationId != null }
39                 .mapKeys { it.key.modelCustomizationId!! }
40     }
41
42     private fun toNamesByIds(model: ServiceModel): Map<Ids, Names> {
43         return emptyMap<Ids, Names>()
44                 .plus(inflate(model.networks))
45                 .plus(inflate(model.vnfs))
46                 .plus(inflate(model.vnfGroups))
47                 .plus(inflate(model.vrfs))
48                 .plus(inflate(model.collectionResources))
49     }
50
51     private fun inflate(instances: Map<String, *>): Map<Ids, Names> {
52         return instances.entries.map { inflate(it.key, it.value) }.fold(emptyMap()) { acc, it -> acc.plus(it) }
53     }
54
55     private fun inflate(modelKey: String, vnf: VNF): Map<Ids, Names> {
56         return mapOf(Ids(vnf.customizationUuid, vnf.uuid) to Names(vnf.modelCustomizationName, modelKey))
57                 .plus(inflate(vnf.vfModules))
58                 .plus(inflate(vnf.volumeGroups))
59     }
60
61     private fun inflate(modelKey: String, cr: CR): Map<Ids, Names> {
62         return mapOf(Ids(cr.customizationUuid, cr.uuid) to Names(null, modelKey))
63                 .plus(inflate(cr.networksCollection))
64     }
65
66     private fun inflate(modelKey: String, instance: Any?): Map<Ids, Names> {
67         return when (instance) {
68             is Network -> mapOf(Ids(instance.customizationUuid, instance.uuid) to Names(instance.modelCustomizationName, modelKey))
69             is VfModule -> mapOf(Ids(instance.customizationUuid, instance.uuid) to Names(instance.modelCustomizationName, modelKey))
70             is VolumeGroup -> mapOf(Ids(instance.customizationUuid, instance.uuid) to Names(instance.modelCustomizationName, modelKey))
71             is ResourceGroup -> mapOf(Ids(null, instance.uuid) to Names(instance.modelCustomizationName, modelKey))
72             is VNF -> inflate(modelKey, instance)
73             is CR -> inflate(modelKey, instance)
74             is NetworkCollection -> mapOf(Ids(null, instance.uuid) to Names(null, modelKey))
75             is Node -> mapOf(Ids(instance.customizationUuid, instance.uuid) to Names(null, modelKey))
76
77             else -> {
78                 // sink
79                 emptyMap()
80             }
81         }
82     }
83
84 }