[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / DistributionUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.ci.tests.utils;
22
23 import java.io.IOException;
24 import java.text.ParseException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.openecomp.sdc.be.model.ArtifactDefinition;
33 import org.openecomp.sdc.be.model.ComponentInstance;
34 import org.openecomp.sdc.be.model.Service;
35 import org.openecomp.sdc.ci.tests.datatypes.ServiceDistributionStatus;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
38 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
39 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
40 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
41
42  public class DistributionUtils {
43          
44          final static String serviceDistributionSuffix = "/sdc/v1/catalog/services/";
45
46          public static ServiceDistributionStatus getLatestServiceDistributionObject(Service service) throws IOException, ParseException {
47                         ServiceDistributionStatus serviceDistributionStatus = null;
48                         RestResponse distributionServiceList = ServiceRestUtils.getDistributionServiceList(service, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
49                         Map<Long, ServiceDistributionStatus> serviveDistributionStatusMap = ResponseParser.convertServiceDistributionStatusToObject(distributionServiceList.getResponse());
50                         if(serviveDistributionStatusMap.size() != 0){
51                                 serviceDistributionStatus = getLatestServiceDistributionObjectFromMap(serviveDistributionStatusMap);
52                                 return serviceDistributionStatus;
53                         }
54                         
55                         return null;
56         }
57
58         public static ServiceDistributionStatus getLatestServiceDistributionObjectFromMap(Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap) {
59                 
60                 ServiceDistributionStatus serviceDistributionStatus = null;
61                 if (serviceDistributionStatusMap.size() == 1 ){
62                         for (Entry<Long, ServiceDistributionStatus> entry : serviceDistributionStatusMap.entrySet()) {
63                                 return entry.getValue();
64                         }
65                 }
66                 else{
67                         serviceDistributionStatus = getFilteredServiceDistributionObject(serviceDistributionStatusMap);
68                 }
69                 
70                 return serviceDistributionStatus;
71         }
72
73         private static ServiceDistributionStatus getFilteredServiceDistributionObject(Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap) {
74                 
75                 List<Long> list = new ArrayList<Long>();
76                 list.addAll(serviceDistributionStatusMap.keySet());
77                 Collections.sort(list);
78                 return serviceDistributionStatusMap.get(list.get(list.size() - 1));
79         }
80         
81         public static Map<String, String> getArtifactsMapOfDistributedService(Service service) throws Exception{
82                 
83                 Map<String, String> expectedDistributionArtifactMap = new HashMap<String, String>();
84                 expectedDistributionArtifactMap = addServiceDeploymentArtifactToMap(service, expectedDistributionArtifactMap);
85                 expectedDistributionArtifactMap = addComponentInstancesDeploymentArtifactToMap(service, expectedDistributionArtifactMap);
86                 
87                 return expectedDistributionArtifactMap;
88         }
89         
90         
91         public static Map<String, String> addServiceDeploymentArtifactToMap(Service service, Map<String, String> distributionArtifactMap){
92                 
93                 Map<String, ArtifactDefinition> deploymentArtifacts = service.getDeploymentArtifacts();
94                 if (deploymentArtifacts != null && deploymentArtifacts.size() > 0){
95                         for(Entry<String, ArtifactDefinition> artifact : deploymentArtifacts.entrySet()){
96                                 String url = buildServiceDeploymentUrl(service, artifact.getValue().getArtifactName(), artifact.getValue().getArtifactUUID());
97                                 distributionArtifactMap.put(artifact.getKey(), url);
98                         }
99                 }
100                 
101                 return distributionArtifactMap;
102         }
103         
104         private static String buildServiceDeploymentUrl(Service service, String artifactName, String artifactUUID) {
105 //              format  "/sdc/v1/catalog/services/" + service.getSystemName() + "/" + service.getVersion() + "/artifacts/AAI-" + service.getName() + "-service-1.xml"
106                 return serviceDistributionSuffix + service.getSystemName() + "/" + service.getVersion() + "/artifacts/" + artifactName;
107         }
108
109         public static String buildResourceInstanceDeploymentUrl(Service service, String artifactName, String artifactUUID){
110                 
111 //                      /sdc/v1/catalog/services/Servicefordistribution/1.0  /resourceInstances/nestedfrommarina2   /artifacts/FEAdd_On_Module_vProbeLauncher.yaml
112                 String resourceInstanceNormalizedName = getResourceInstanceNormalizeName(service, artifactName, artifactUUID );
113                 return serviceDistributionSuffix + service.getSystemName() + "/" + service.getVersion() + "/resourceInstances/" + resourceInstanceNormalizedName  +"/artifacts/" + artifactName;
114         }
115         
116         public static String getResourceInstanceNormalizeName(Service service, String artifactName, String artifactUUID) {
117                 for (ComponentInstance componentInstance : service.getComponentInstances()){
118                         for(String key : componentInstance.getDeploymentArtifacts().keySet()){
119                                 if(componentInstance.getDeploymentArtifacts().get(key).getArtifactUUID().equals(artifactUUID)) {
120                                         return componentInstance.getNormalizedName();
121                                 }
122                         }
123                 }
124                 return null;
125         }
126
127         public static Map<String, String> addComponentInstancesDeploymentArtifactToMap(Service service, Map<String, String> distributionArtifactMap){
128 //                      TODO Andrey create correct method to build RI url
129                 if(service.getComponentInstances() != null && service.getComponentInstances().size() != 0){
130                 for(ComponentInstance componentInstance : service.getComponentInstances()){
131                         if (componentInstance.getDeploymentArtifacts() != null && componentInstance.getDeploymentArtifacts().size() != 0){
132                                 for(Entry<String, ArtifactDefinition> artifact : componentInstance.getDeploymentArtifacts().entrySet()){
133                                         String url = buildResourceInstanceDeploymentUrl(service, artifact.getValue().getArtifactName(), artifact.getValue().getArtifactUUID());;
134                                         distributionArtifactMap.put(artifact.getKey(), url);
135                                 }
136                         }
137                 }
138         }
139                 
140                 return distributionArtifactMap;
141         }
142          
143          
144  }