2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils;
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;
30 import java.util.Map.Entry;
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;
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;
52 // import org.apache.log4j.Logger;
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;
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;
64 public class DistributionUtils {
66 final static String serviceDistributionSuffix = "/sdc/v1/catalog/services/";
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;
80 public static ServiceDistributionStatus getLatestServiceDistributionObjectFromMap(Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap) {
82 ServiceDistributionStatus serviceDistributionStatus = null;
83 if (serviceDistributionStatusMap.size() == 1 ){
84 for (Entry<Long, ServiceDistributionStatus> entry : serviceDistributionStatusMap.entrySet()) {
85 return entry.getValue();
89 serviceDistributionStatus = getFilteredServiceDistributionObject(serviceDistributionStatusMap);
92 return serviceDistributionStatus;
95 private static ServiceDistributionStatus getFilteredServiceDistributionObject(Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap) {
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));
103 public static Map<String, String> getArtifactsMapOfDistributedService(Service service) throws Exception{
105 Map<String, String> expectedDistributionArtifactMap = new HashMap<String, String>();
106 expectedDistributionArtifactMap = addServiceDeploymentArtifactToMap(service, expectedDistributionArtifactMap);
107 expectedDistributionArtifactMap = addComponentInstancesDeploymentArtifactToMap(service, expectedDistributionArtifactMap);
109 return expectedDistributionArtifactMap;
113 public static Map<String, String> addServiceDeploymentArtifactToMap(Service service, Map<String, String> distributionArtifactMap){
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);
123 return distributionArtifactMap;
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;
131 public static String buildResourceInstanceDeploymentUrl(Service service, String artifactName, String artifactUUID){
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;
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();
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);
162 return distributionArtifactMap;