[SDC] rebase 1710 code
[sdc.git] / asdc-tests / 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 //
43 //
44 //
45 // import static org.testng.AssertJUnit.assertTrue;
46 // import static org.testng.AssertJUnit.assertEquals;
47 // import static org.testng.AssertJUnit.assertNotNull;
48 // import java.io.IOException;
49 // import java.util.HashMap;
50 // import java.util.Map;
51 //
52 // import org.apache.log4j.Logger;
53 //
54 // import org.openecomp.sdc.be.model.ArtifactDefinition;
55 // import org.openecomp.sdc.ci.tests.api.Urls;
56 // import org.openecomp.sdc.ci.tests.config.Config;
57 // import
58 // org.openecomp.sdc.ci.tests.executeOnUGN.distributionClient.ClientConfiguration;
59 // import org.openecomp.sdc.ci.tests.http.HttpHeaderEnum;
60 // import org.openecomp.sdc.ci.tests.http.RestResponse;
61 // import org.openecomp.sdc.ci.tests.run.StartTest;
62 // import com.google.gson.Gson;
63 //
64  public class DistributionUtils {
65          
66          final static String serviceDistributionSuffix = "/sdc/v1/catalog/services/";
67
68          public static ServiceDistributionStatus getLatestServiceDistributionObject(Service service) throws IOException, ParseException {
69                         ServiceDistributionStatus serviceDistributionStatus = null;
70                         RestResponse distributionServiceList = ServiceRestUtils.getDistributionServiceList(service, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
71                         Map<Long, ServiceDistributionStatus> serviveDistributionStatusMap = ResponseParser.convertServiceDistributionStatusToObject(distributionServiceList.getResponse());
72                         if(serviveDistributionStatusMap.size() != 0){
73                                 serviceDistributionStatus = getLatestServiceDistributionObjectFromMap(serviveDistributionStatusMap);
74                                 return serviceDistributionStatus;
75                         }
76                         
77                         return null;
78         }
79
80         public static ServiceDistributionStatus getLatestServiceDistributionObjectFromMap(Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap) {
81                 
82                 ServiceDistributionStatus serviceDistributionStatus = null;
83                 if (serviceDistributionStatusMap.size() == 1 ){
84                         for (Entry<Long, ServiceDistributionStatus> entry : serviceDistributionStatusMap.entrySet()) {
85                                 return entry.getValue();
86                         }
87                 }
88                 else{
89                         serviceDistributionStatus = getFilteredServiceDistributionObject(serviceDistributionStatusMap);
90                 }
91                 
92                 return serviceDistributionStatus;
93         }
94
95         private static ServiceDistributionStatus getFilteredServiceDistributionObject(Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap) {
96                 
97                 List<Long> list = new ArrayList<Long>();
98                 list.addAll(serviceDistributionStatusMap.keySet());
99                 Collections.sort(list);
100                 return serviceDistributionStatusMap.get(list.get(list.size() - 1));
101         }
102         
103         public static Map<String, String> getArtifactsMapOfDistributedService(Service service) throws Exception{
104                 
105                 Map<String, String> expectedDistributionArtifactMap = new HashMap<String, String>();
106                 expectedDistributionArtifactMap = addServiceDeploymentArtifactToMap(service, expectedDistributionArtifactMap);
107                 expectedDistributionArtifactMap = addComponentInstancesDeploymentArtifactToMap(service, expectedDistributionArtifactMap);
108                 
109                 return expectedDistributionArtifactMap;
110         }
111         
112         
113         public static Map<String, String> addServiceDeploymentArtifactToMap(Service service, Map<String, String> distributionArtifactMap){
114                 
115                 Map<String, ArtifactDefinition> deploymentArtifacts = service.getDeploymentArtifacts();
116                 if (deploymentArtifacts != null && deploymentArtifacts.size() > 0){
117                         for(Entry<String, ArtifactDefinition> artifact : deploymentArtifacts.entrySet()){
118                                 String url = buildServiceDeploymentUrl(service, artifact.getValue().getArtifactName(), artifact.getValue().getArtifactUUID());
119                                 distributionArtifactMap.put(artifact.getKey(), url);
120                         }
121                 }
122                 
123                 return distributionArtifactMap;
124         }
125         
126         private static String buildServiceDeploymentUrl(Service service, String artifactName, String artifactUUID) {
127 //              format  "/sdc/v1/catalog/services/" + service.getSystemName() + "/" + service.getVersion() + "/artifacts/AAI-" + service.getName() + "-service-1.xml"
128                 return serviceDistributionSuffix + service.getSystemName() + "/" + service.getVersion() + "/artifacts/" + artifactName;
129         }
130
131         public static String buildResourceInstanceDeploymentUrl(Service service, String artifactName, String artifactUUID){
132                 
133 //                      /sdc/v1/catalog/services/Servicefordistribution/1.0  /resourceInstances/nestedfrommarina2   /artifacts/FEAdd_On_Module_vProbeLauncher.yaml
134                 String resourceInstanceNormalizedName = getResourceInstanceNormalizeName(service, artifactName, artifactUUID );
135                 return serviceDistributionSuffix + service.getSystemName() + "/" + service.getVersion() + "/resourceInstances/" + resourceInstanceNormalizedName  +"/artifacts/" + artifactName;
136         }
137         
138         public static String getResourceInstanceNormalizeName(Service service, String artifactName, String artifactUUID) {
139                 for (ComponentInstance componentInstance : service.getComponentInstances()){
140                         for(String key : componentInstance.getDeploymentArtifacts().keySet()){
141                                 if(componentInstance.getDeploymentArtifacts().get(key).getArtifactUUID().equals(artifactUUID)) {
142                                         return componentInstance.getNormalizedName();
143                                 }
144                         }
145                 }
146                 return null;
147         }
148
149         public static Map<String, String> addComponentInstancesDeploymentArtifactToMap(Service service, Map<String, String> distributionArtifactMap){
150 //                      TODO Andrey create correct method to build RI url
151                 if(service.getComponentInstances() != null && service.getComponentInstances().size() != 0){
152                 for(ComponentInstance componentInstance : service.getComponentInstances()){
153                         if (componentInstance.getDeploymentArtifacts() != null && componentInstance.getDeploymentArtifacts().size() != 0){
154                                 for(Entry<String, ArtifactDefinition> artifact : componentInstance.getDeploymentArtifacts().entrySet()){
155                                         String url = buildResourceInstanceDeploymentUrl(service, artifact.getValue().getArtifactName(), artifact.getValue().getArtifactUUID());;
156                                         distributionArtifactMap.put(artifact.getKey(), url);
157                                 }
158                         }
159                 }
160         }
161                 
162                 return distributionArtifactMap;
163         }
164          
165          
166  }