5be26a0ecd7cf9925b5fbc1bc13b212c8f782276
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / AAITreeConverter.java
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.aai.util;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.vid.model.aaiTree.*;
25 import org.onap.vid.mso.model.ModelInfo;
26 import org.onap.vid.services.AAITreeNodeBuilder;
27 import org.springframework.stereotype.Component;
28
29 import java.util.Objects;
30
31 import static java.util.function.Function.identity;
32 import static java.util.stream.Collectors.counting;
33 import static java.util.stream.Collectors.groupingBy;
34 import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
35
36 @Component
37 public class AAITreeConverter {
38
39     public static final String VNF_TYPE = "vnf-type";
40     public static final String NETWORK_TYPE = "network-type";
41
42     public static final String IS_BASE_VF_MODULE = "is-base-vf-module";
43
44     public enum ModelType {
45         service,
46         vnf,
47         network,
48         instanceGroup,
49         vfModule
50     }
51
52     public ServiceInstance convertTreeToUIModel(AAITreeNode rootNode, String globalCustomerId, String serviceType, String instantiationType) {
53         ServiceInstance serviceInstance = new ServiceInstance();
54         serviceInstance.setInstanceId(rootNode.getId());
55         serviceInstance.setInstanceName(rootNode.getName());
56         serviceInstance.setOrchStatus(rootNode.getOrchestrationStatus());
57         serviceInstance.setGlobalSubscriberId(globalCustomerId);
58         serviceInstance.setSubscriptionServiceType(serviceType);
59         serviceInstance.setIsALaCarte(StringUtils.equals(instantiationType, A_LA_CARTE));
60
61         serviceInstance.setModelInfo(createModelInfo(rootNode, ModelType.service));
62
63         //set children: vnf, network,group
64         rootNode.getChildren().forEach(child -> {
65             if (child.getType().equals(AAITreeNodeBuilder.GENERIC_VNF)) {
66                 serviceInstance.getVnfs().put(child.getUniqueNodeKey(), Vnf.from(child));
67             } else if (child.getType().equals(AAITreeNodeBuilder.NETWORK)) {
68                 serviceInstance.getNetworks().put(child.getUniqueNodeKey(), Network.from(child));
69             } else if (child.getType().equals(AAITreeNodeBuilder.INSTANCE_GROUP)) {
70                 serviceInstance.getVnfGroups().put(child.getUniqueNodeKey(), VnfGroup.from(child));
71             }
72         });
73
74         serviceInstance.setExistingVNFCounterMap(
75                 serviceInstance.getVnfs().entrySet().stream()
76                         .map(k -> k.getValue().getModelInfo().getModelVersionId())
77                         .collect(groupingBy(identity(), counting()))
78         );
79
80         serviceInstance.setExistingNetworksCounterMap(
81                 serviceInstance.getNetworks().entrySet().stream()
82                 .map(k -> k.getValue().getModelInfo().getModelVersionId())
83                 .filter(Objects::nonNull)
84                         .collect(groupingBy(identity(), counting()))
85         );
86
87
88         serviceInstance.setExistingVnfGroupCounterMap(
89                 serviceInstance.getVnfGroups().entrySet().stream()
90                 .map(k -> k.getValue().getModelInfo().getModelVersionId())
91                 .filter(Objects::nonNull)
92                         .collect(groupingBy(identity(), counting()))
93         );
94
95         return serviceInstance;
96     }
97
98     private static ModelInfo createModelInfo(AAITreeNode aaiNode, ModelType modelType) {
99         ModelInfo modelInfo = new ModelInfo();
100         modelInfo.setModelType(modelType.name());
101         modelInfo.setModelName(aaiNode.getModelName());
102         modelInfo.setModelVersion(aaiNode.getModelVersion());
103         modelInfo.setModelVersionId(aaiNode.getModelVersionId());
104         modelInfo.setModelInvariantId(aaiNode.getModelInvariantId());
105         modelInfo.setModelCustomizationId(aaiNode.getModelCustomizationId());
106
107         return modelInfo;
108     }
109 }