Implant vid-app-common org.onap.vid.job (main and test)
[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 static java.util.function.Function.identity;
24 import static java.util.stream.Collectors.counting;
25 import static java.util.stream.Collectors.groupingBy;
26 import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
27
28 import java.util.Map;
29 import java.util.Objects;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.vid.model.aaiTree.AAITreeNode;
32 import org.onap.vid.model.aaiTree.CollectionResource;
33 import org.onap.vid.model.aaiTree.Network;
34 import org.onap.vid.model.aaiTree.Node;
35 import org.onap.vid.model.aaiTree.NodeType;
36 import org.onap.vid.model.aaiTree.ServiceInstance;
37 import org.onap.vid.model.aaiTree.Vnf;
38 import org.onap.vid.model.aaiTree.VnfGroup;
39 import org.onap.vid.model.aaiTree.Vrf;
40 import org.onap.vid.mso.model.ModelInfo;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class AAITreeConverter {
45
46     public static final String VNF_TYPE = "vnf-type";
47     public static final String NETWORK_TYPE = "network-type";
48     public static final String NETWORK_ROLE = "network-role";
49     public static final String PHYSICAL_NETWORK_NAME = "physical-network-name";
50     public static final String SERVICE_INSTANCE = "service-instance";
51     public static final String TENANT = "tenant";
52     public static final String VPN_BINDING = "vpn-binding";
53
54     public static final String IS_BASE_VF_MODULE = "is-base-vf-module";
55     public static final String SERVICE_INSTANCE_SERVICE_INSTANCE_NAME = "service-instance.service-instance-name";
56     public static final String SERVICE_INSTANCE_SERVICE_INSTANCE_ID = "service-instance.service-instance-id";
57     public static final String TENANT_TENANT_NAME = "tenant.tenant-name";
58
59     public ServiceInstance convertTreeToUIModel(AAITreeNode rootNode, String globalCustomerId, String serviceType, String instantiationType, String instanceRole, String instanceType) {
60         ServiceInstance serviceInstance = new ServiceInstance();
61         serviceInstance.setInstanceId(rootNode.getId());
62         serviceInstance.setInstanceName(rootNode.getName());
63         serviceInstance.setOrchStatus(rootNode.getOrchestrationStatus());
64         serviceInstance.setGlobalSubscriberId(globalCustomerId);
65         serviceInstance.setSubscriptionServiceType(serviceType);
66         serviceInstance.setIsALaCarte(StringUtils.equals(instantiationType, A_LA_CARTE));
67
68         serviceInstance.setModelInfo(createModelInfo(rootNode));
69
70         //set children: vnf, network,group
71         rootNode.getChildren().forEach(child -> {
72             if (child.getType() == NodeType.GENERIC_VNF) {
73                 serviceInstance.getVnfs().put(child.getUniqueNodeKey(), Vnf.from(child));
74             } else if (child.getType() == NodeType.NETWORK) {
75                 serviceInstance.getNetworks().put(child.getUniqueNodeKey(), Network.from(child));
76             } else if (child.getType() == NodeType.INSTANCE_GROUP) {
77                 serviceInstance.getVnfGroups().put(child.getUniqueNodeKey(), new VnfGroup(child));
78             } else if (child.getType() == NodeType.COLLECTION_RESOURCE) {
79                 serviceInstance.getCollectionResources().put(child.getUniqueNodeKey(), new CollectionResource(child));
80             } else if (isChildVrf(instanceType, instanceRole, child)){
81                 serviceInstance.getVrfs().put(child.getUniqueNodeKey(), Vrf.from(child));
82             }
83         });
84
85         serviceInstance.setExistingVNFCounterMap(
86                 getExistingCounterMap(serviceInstance.getVnfs())
87         );
88
89         serviceInstance.setExistingNetworksCounterMap(
90                 getExistingCounterMap(serviceInstance.getNetworks())
91         );
92
93
94         serviceInstance.setExistingVnfGroupCounterMap(
95                 getExistingCounterMap(serviceInstance.getVnfGroups())
96         );
97
98         serviceInstance.setExistingVRFCounterMap(
99                 getExistingCounterMap(serviceInstance.getVrfs())
100         );
101
102         return serviceInstance;
103     }
104
105     protected boolean isChildVrf(String instanceType, String serviceRole, AAITreeNode child) {
106         return child.getType() == NodeType.CONFIGURATION && StringUtils.equalsIgnoreCase(instanceType, "BONDING") && StringUtils.equalsIgnoreCase(serviceRole, "INFRASTRUCTURE-VPN");
107     }
108
109     private <T extends Node> Map<String, Long> getExistingCounterMap(Map<String, T> nodeList) {
110         return nodeList.entrySet().stream()
111                 .map(k -> {
112                     ModelInfo modelInfo = k.getValue().getModelInfo();
113                     return StringUtils.defaultIfEmpty(modelInfo.getModelCustomizationId(), modelInfo.getModelVersionId());
114                 })
115                 .filter(Objects::nonNull)
116                 .collect(groupingBy(identity(), counting()));
117     }
118
119     private static ModelInfo createModelInfo(AAITreeNode aaiNode) {
120         ModelInfo modelInfo = new ModelInfo();
121         modelInfo.setModelType(aaiNode.getType().getModelType());
122         modelInfo.setModelName(aaiNode.getModelName());
123         modelInfo.setModelVersion(aaiNode.getModelVersion());
124         modelInfo.setModelVersionId(aaiNode.getModelVersionId());
125         modelInfo.setModelInvariantId(aaiNode.getModelInvariantId());
126         modelInfo.setModelCustomizationId(aaiNode.getModelCustomizationId());
127
128         return modelInfo;
129     }
130 }