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.rest;
23 import com.google.gson.Gson;
24 import org.apache.commons.codec.binary.Base64;
25 import org.json.simple.JSONObject;
26 import org.json.simple.JSONValue;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.model.*;
29 import org.openecomp.sdc.ci.tests.api.Urls;
30 import org.openecomp.sdc.ci.tests.config.Config;
31 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
32 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
33 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
34 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
35 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
36 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
38 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
39 import org.openecomp.sdc.ci.tests.utils.Utils;
40 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
41 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
42 import org.openecomp.sdc.common.util.ValidationUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.testng.AssertJUnit;
47 import java.io.IOException;
48 import java.util.HashMap;
49 import java.util.List;
52 import static org.testng.AssertJUnit.assertNotNull;
53 import static org.testng.AssertJUnit.assertTrue;
55 public class ArtifactRestUtils extends BaseRestUtils {
56 private static Logger logger = LoggerFactory.getLogger(ArtifactRestUtils.class.getName());
60 // Delete Artifact on rI of the asset
61 public static RestResponse externalAPIDeleteArtifactOfComponentInstanceOnAsset(Component component, User user, ComponentInstance resourceInstance, String artifactUUID) throws IOException {
62 Config config = Utils.getConfig();
64 String resourceUUID = component.getUUID();
65 String resourceInstanceName = resourceInstance.getNormalizedName();
67 System.out.println(component.getComponentType());
69 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
70 resourceType = "resources";
72 resourceType = "services";
75 String url = String.format(Urls.DELETE_EXTRNAL_API_DELETE_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName, artifactUUID);
77 return deleteInformationalArtifact(user, url);
80 // Delete Artifact of the asset
81 public static RestResponse externalAPIDeleteArtifactOfTheAsset(Component component, User user, String artifactUUID) throws IOException {
82 Config config = Utils.getConfig();
84 String resourceUUID = component.getUUID();
86 System.out.println(component.getComponentType());
88 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
89 resourceType = "resources";
91 resourceType = "services";
94 String url = String.format(Urls.DELETE_EXTRNAL_API_DELETE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, artifactUUID);
96 RestResponse restResponse = deleteInformationalArtifact(user, url);
102 // Update Artifact on rI of the asset
103 public static RestResponse externalAPIUpdateArtifactOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance, String artifactUUID) throws IOException {
104 Config config = Utils.getConfig();
106 String resourceUUID = component.getUUID();
107 String resourceInstanceName = resourceInstance.getNormalizedName();
109 System.out.println(component.getComponentType());
111 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
112 resourceType = "resources";
114 resourceType = "services";
117 String url = String.format(Urls.POST_EXTERNAL_API_UPDATE_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName, artifactUUID);
119 return updateInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
122 // Update Artifact of the asset
123 public static RestResponse externalAPIUpdateArtifactOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, String artifactUUID) throws IOException {
124 Config config = Utils.getConfig();
126 String resourceUUID = component.getUUID();
128 System.out.println(component.getComponentType());
130 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
131 resourceType = "resources";
133 resourceType = "services";
136 String url = String.format(Urls.POST_EXTERNAL_API_UPDATE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, artifactUUID);
138 return updateInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
142 // Upload Artifact on rI of the asset
143 public static RestResponse externalAPIUploadArtifactOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance) throws IOException {
144 Config config = Utils.getConfig();
146 String resourceUUID = component.getUUID();
147 String resourceInstanceName = resourceInstance.getNormalizedName();
149 System.out.println(component.getComponentType());
151 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
152 resourceType = "resources";
154 resourceType = "services";
157 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName);
159 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
162 // Upload Artifact of the asset
163 public static RestResponse externalAPIUploadArtifactOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails) throws IOException {
164 Config config = Utils.getConfig();
166 String resourceUUID = component.getUUID();
168 System.out.println(component.getComponentType());
170 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
171 resourceType = "resources";
173 resourceType = "services";
176 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID);
178 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
182 // Upload Artifact of the asset with invalid checksum
183 public static RestResponse externalAPIUploadArtifactWithInvalidCheckSumOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance) throws IOException {
184 Config config = Utils.getConfig();
186 String resourceUUID = component.getUUID();
187 String resourceInstanceName = resourceInstance.getNormalizedName();
189 System.out.println(component.getComponentType());
191 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
192 resourceType = "resources";
194 resourceType = "services";
197 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName);
199 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails) + "123", url);
202 // Upload Artifact of the asset with invalid checksum
203 public static RestResponse externalAPIUploadArtifactWithInvalidCheckSumOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails) throws IOException {
204 Config config = Utils.getConfig();
206 String resourceUUID = component.getUUID();
208 System.out.println(component.getComponentType());
210 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
211 resourceType = "resources";
213 resourceType = "services";
216 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID);
218 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails) + "123", url);
225 public static RestResponse getResourceDeploymentArtifactExternalAPI(String resourceUUID, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
226 Config config = Utils.getConfig();
229 if (resourceType.toUpperCase().equals("SERVICE")) {
230 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, artifactUUID);
233 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, artifactUUID);
236 Map<String, String> headersMap = new HashMap<>();
237 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
238 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
239 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
240 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
241 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(),acceptMultipartHeader);
243 HttpRequest http = new HttpRequest();
245 logger.debug("Send GET request to get Resource Assets: {}", url);
246 System.out.println("Send GET request to get Resource Assets: " + url);
248 logger.debug("Request headers: {}", headersMap);
249 System.out.println("Request headers: " + headersMap);
251 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
253 return sendGetResourceAssets;
259 public static RestResponse getComponentInstanceDeploymentArtifactExternalAPI(String resourceUUID, String componentNormalizedName, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
260 Config config = Utils.getConfig();
263 if (resourceType.toLowerCase().equals("service")) {
264 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
267 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
270 Map<String, String> headersMap = new HashMap<>();
271 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
272 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
273 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
274 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
275 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(),acceptMultipartHeader);
277 HttpRequest http = new HttpRequest();
279 logger.debug("Send GET request to get Resource Assets: {}", url);
280 System.out.println("Send GET request to get Resource Assets: " + url);
282 logger.debug("Request headers: {}", headersMap);
283 System.out.println("Request headers: " + headersMap);
285 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
287 return sendGetResourceAssets;
292 //*********** SERVICE ****************
293 public static RestResponse getArtifactTypesList() throws IOException {
294 Config config = Utils.getConfig();
295 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
297 return sendGet(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
300 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid) throws IOException {
301 return addInformationalArtifactToService(artifactDetails, sdncModifierDetails, serviceUid, calculateChecksum(artifactDetails));
304 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String checksum) throws IOException {
305 Config config = Utils.getConfig();
306 String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceUid);
308 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
311 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
314 return downloadServiceArtifact( service, artifact, user,addionalHeaders,true);
317 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
319 Config config = Utils.getConfig();
320 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(service.getName()), service.getVersion(), ValidationUtils.normalizeFileName(artifact.getArtifactName())));
321 String fullUrl = String.format(Urls.DOWNLOAD_SERVICE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
323 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
326 public static RestResponse downloadResourceArtifact(ServiceReqDetails service, ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
328 return downloadResourceArtifact(service, resource, artifact, user,addionalHeaders, true);
331 public static RestResponse downloadResourceArtifact(ServiceReqDetails service,ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
333 Config config = Utils.getConfig();
334 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(service.getName()),service.getVersion(),ValidationUtils.convertToSystemName(resource.getName()), resource.getVersion(), ValidationUtils.normalizeFileName(artifact.getArtifactName())));
335 String fullUrl = String.format(Urls.DOWNLOAD_RESOURCE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
337 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
342 public static RestResponse downloadResourceInstanceArtifact(String serviceUniqueId,String resourceInstanceId, User user, String artifactUniqueId) throws Exception
344 Config config = Utils.getConfig();
345 String url = String.format(Urls.DOWNLOAD_COMPONENT_INSTANCE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, resourceInstanceId, artifactUniqueId);
346 RestResponse res = sendGet(url, user.getUserId(), null);
354 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod) throws IOException {
355 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactUid, sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
358 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, User sdncModifierDetails, String httpMethod) throws IOException {
359 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactReqDetails.getUniqueId(), sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
362 public static RestResponse downloadResourceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
364 return downloadComponentArtifactInternalApi(resourceId, user, artifactUniqeId, Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT);
367 public static RestResponse downloadServiceArtifactInternalApi(String componentId, User user, String artifactUniqeId) throws Exception
369 return downloadComponentArtifactInternalApi(componentId, user, artifactUniqeId, Urls.UI_DOWNLOAD_SERVICE_ARTIFACT);
371 public static RestResponse downloadComponentArtifactInternalApi(String componentId, User user, String artifactUniqeId, String urlTemplate) throws Exception
373 Config config = Utils.getConfig();
374 String url = String.format(urlTemplate, config.getCatalogBeHost(),config.getCatalogBePort(), componentId, artifactUniqeId);
375 RestResponse res = sendGet(url, user.getUserId(), null);
379 // public static RestResponse downloadServiceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
381 // Config config = Utils.getConfig();
382 // String url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), resourceId, artifactUniqeId);
383 // RestResponse res = sendGet(url, user.getUserId(), null);
388 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
390 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
393 public static RestResponse updateInformationalArtifactMetadataOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
395 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateChecksum(artifactDetails), false);
398 public static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
400 Config config = Utils.getConfig();
401 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
403 if (isPayloadUpdate){
404 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
407 Gson gson = new Gson();
408 String jsonBody = gson.toJson(artifactDetails);
410 HttpRequest http = new HttpRequest();
412 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
413 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
414 System.out.println("update artifact was finished with response: "+ res.getErrorCode());
420 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod, String checksum) throws IOException
422 Config config = Utils.getConfig();
424 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
425 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
427 Gson gson = new Gson();
428 String userBodyJson = gson.toJson(artifactReqDetails);
430 HttpRequest http = new HttpRequest();
431 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
432 RestResponse updateResourceResponse = http.httpSendByMethod(url, httpMethod, userBodyJson, headersMap);
433 // System.out.println("update artifact was finished with response: "+ updateResourceResponse.getErrorCode());
435 return updateResourceResponse;
439 public static Map<String, String> getHeadersMap(User sdncModifierDetails) {
440 Map<String, String> headersMap = new HashMap<>();
441 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
442 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
445 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
455 //*********** RESOURCE ****************
457 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
458 return addInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
461 public static RestResponse explicitAddInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
462 Config config = Utils.getConfig();
465 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
467 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
471 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException{
472 Config config = Utils.getConfig();
474 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals(ArtifactGroupTypeEnum.DEPLOYMENT.getType())){
475 //YANG_XML and OTHER deployment artifact should be added through this API, not updated
476 String artifactType = artifactDetails.getArtifactType();
477 if (!(ArtifactTypeEnum.YANG_XML.getType().equals(artifactType) ||
478 ArtifactTypeEnum.OTHER.getType().equals(artifactType) ||
479 ArtifactTypeEnum.VNF_CATALOG.getType().equals(artifactType) ||
480 ArtifactTypeEnum.VF_LICENSE.getType().equals(artifactType) ||
481 ArtifactTypeEnum.VENDOR_LICENSE.getType().equals(artifactType) ||
482 ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.getType().equals(artifactType) ||
483 ArtifactTypeEnum.MODEL_QUERY_SPEC.getType().equals(artifactType) ||
484 ArtifactTypeEnum.APPC_CONFIG.getType().equals(artifactType))){
485 //return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid);
488 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
490 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
493 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
494 return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
497 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException {
498 Config config = Utils.getConfig();
499 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")){
500 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
501 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
502 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
503 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
504 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
505 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
509 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
511 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
514 public static RestResponse uploadArtifactToPlaceholderOnResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String placeHolderLabel) throws IOException {
515 Config config = Utils.getConfig();
516 if (artifactDetails.getArtifactLabel() != null && !artifactDetails.getArtifactLabel().isEmpty()){
517 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
518 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
519 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
520 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
521 AssertJUnit.assertNotNull(artifactDefinition);
522 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
523 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
527 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
529 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
532 public static RestResponse updateArtifactToResourceInstance(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
533 Config config = Utils.getConfig();
534 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDefinition.getUniqueId());
535 return updateDeploymentArtifact(artifactDefinition, sdncModifierDetails, url);
538 public static RestResponse updateDeploymentArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException {
539 Config config = Utils.getConfig();
540 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
542 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
546 public static RestResponse updateDeploymentArtifactToRI(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
547 Config config = Utils.getConfig();
548 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_HEAT_ENV_PARAMS, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDetails.getUniqueId());
549 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
553 public static RestResponse deleteArtifactFromResourceInstance (ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceUid, String serviceId) throws IOException{
554 Config config = Utils.getConfig();
555 String url = String.format(Urls.DELETE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceUid, artifactDefinition.getUniqueId());
556 return sendDelete(url, sdncModifierDetails.getUserId());
559 public static RestResponse deleteInformationalArtifactFromResource(String resourceUid, ArtifactReqDetails artifactDetails, User sdncModifierDetails) throws IOException{
560 return deleteInformationalArtifactFromResource( resourceUid, artifactDetails.getUniqueId(), sdncModifierDetails);
563 public static RestResponse deleteInformationalArtifactFromResource( String resourceUid, String artifactId, User sdncModifierDetails) throws IOException{
564 Config config = Utils.getConfig();
565 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactId);
566 return sendDelete(url, sdncModifierDetails.getUserId());
569 public static RestResponse deleteServiceApiArtifact(ArtifactReqDetails artifactDetails, String serviceUniqueId, User user) throws Exception
571 Config config = Utils.getConfig();
572 String url = String.format(Urls.UPDATE_DELETE_SERVICE_API_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, artifactDetails.getUniqueId());
573 RestResponse res = sendDelete(url, user.getUserId());
574 logger.debug("Deleting api artifact was finished with response: {}", res.getErrorCode());
575 logger.debug("Response body: {}", res.getResponseMessage());
579 //*************** RESOURCE INSTANCE **************
581 * Add DCAE artifacts to resource instance.
582 * @param artifactDetails
583 * @param sdncModifierDetails
584 * @param resourceInstanceId
587 * @throws IOException
589 public static RestResponse addArtifactToResourceInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
590 Config config = Utils.getConfig();
591 String url = String.format(Urls.ADD_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId,resourceInstanceId, artifactDetails.getUniqueId());
592 return addArtifactToInstance(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
595 //*************** COMPONENT **************
597 public static RestResponse uploadDeploymentArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
598 Config config = Utils.getConfig();
599 Map<String, String> additionalHeaders;
600 String checksum = ResponseParser.calculateMD5Header(artifactDetails);
601 additionalHeaders = new HashMap<>();
602 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
604 ComponentTypeEnum componentType = component.getComponentType();
608 switch (componentType){
612 url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), component.getUniqueId(), artifactDetails.getUniqueId());
627 assertTrue("failed on enum selection", false);
636 Gson gson = new Gson();
637 String jsonBody = gson.toJson(artifactDetails);
638 // System.out.println("ArtifactDetails: "+ jsonBody);
640 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
641 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
642 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
644 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
648 public static RestResponse uploadArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
649 Config config = Utils.getConfig();
650 List<String> placeHolderlst = Utils.getListOfResPlaceHoldersDepArtTypes();
651 Map<String, String> additionalHeaders;
656 // if (artifactDetails.getArtifactGroupType() != null
657 // && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")
658 // && placeHolderlst.contains(artifactDetails.getArtifactType())) {
659 // Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
660 // ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
661 // artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
662 // artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
663 // checksum = ResponseParser.calculateMD5Header(artifactDetails);
664 // additionalHeaders = new HashMap<String, String>();
665 // additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
666 // url = String.format(Urls.UPDATE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
667 // config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
668 // component.getUniqueId(), artifactDetails.getUniqueId());
672 checksum = ResponseParser.calculateMD5Header(artifactDetails);
673 additionalHeaders = new HashMap<>();
674 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
675 url = String.format(Urls.UPLOAD_DELETE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
676 config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
677 component.getUniqueId(), artifactDetails.getUniqueId());
680 Gson gson = new Gson();
681 String jsonBody = gson.toJson(artifactDetails);
682 // System.out.println("ArtifactDetails: "+ jsonBody);
684 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
685 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
686 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
688 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
695 //*************** PRIVATE **************
696 private static RestResponse deleteInformationalArtifact(User sdncModifierDetails, String url) throws IOException {
697 Map<String, String> additionalHeaders;
699 additionalHeaders = new HashMap<>();
702 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
703 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
705 return sendDelete(url, sdncModifierDetails.getUserId(), additionalHeaders);
707 // Gson gson = new Gson();
708 //// System.out.println("ArtifactDetails: "+ jsonBody);
709 // String jsonBody = gson.toJson(artifactDetails);
711 // RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
712 // if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
713 // artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
715 //// System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
719 private static RestResponse updateInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
720 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
723 private static RestResponse uploadInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
724 Map<String, String> additionalHeaders = null;
725 if (checksum != null && !checksum.isEmpty()) {
726 additionalHeaders = new HashMap<>();
727 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
730 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
731 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
733 Gson gson = new Gson();
734 // System.out.println("ArtifactDetails: "+ jsonBody);
735 String jsonBody = gson.toJson(artifactDetails);
737 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
738 if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
739 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
741 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
745 private static RestResponse addArtifactToInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
746 Map<String, String> additionalHeaders;
747 additionalHeaders = new HashMap<>();
748 if (checksum != null && !checksum.isEmpty()) {
749 additionalHeaders = new HashMap<>();
750 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
752 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
753 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
755 Gson gson = new Gson();
756 String jsonBody = gson.toJson(artifactDetails);
758 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
759 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
760 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
765 private static RestResponse updateDeploymentArtifact(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String url) throws IOException {
766 Map<String, String> additionalHeaders;
767 additionalHeaders = new HashMap<>();
768 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
769 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
771 Gson gson = new Gson();
772 String jsonBody = gson.toJson(artifactDefinition);
774 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
778 private static RestResponse updateDeploymentArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String url) throws IOException {
779 Map<String, String> additionalHeaders;
781 additionalHeaders = new HashMap<>();
782 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
783 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
784 // additionalHeaders.put(HttpHeaderEnum..getValue(), "application/json;charset=UTF-8");
787 Gson gson = new Gson();
788 String jsonBody = gson.toJson(artifactDetails);
789 // System.out.println("ArtifactDetails: "+ jsonBody);
791 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
792 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
793 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
795 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
799 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws IOException
802 addionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
804 return downloadArtifact(url, user, addionalHeaders, acceptOctetStream);
807 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders, String accept) throws IOException
809 addionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
811 RestResponse res = sendGet(url, user.getUserId(), addionalHeaders);
812 // System.out.println("download artifact was finished with response: "+ res.getErrorCode());
813 // System.out.println("response is: " + res.getResponse());
817 private static Map<String,Map<String,Object>> getArtifactsListFromResponse(String jsonResponse, String fieldOfArtifactList){
818 JSONObject object = (JSONObject)JSONValue.parse(jsonResponse);
819 Map<String,Map<String,Object>> map = (Map<String,Map<String,Object>>)object.get(fieldOfArtifactList);
823 public static String calculateChecksum(ArtifactReqDetails artifactDetails) {
824 String checksum = null;
825 if (artifactDetails.getPayload() != null) {
826 checksum = ResponseParser.calculateMD5Header(artifactDetails);
831 public static String encodeUrlForDownload(String url){
833 return url.replaceAll(" ", "%20");
836 public static String getPartialUrlByArtifactName(ServiceReqDetails serviceDetails,String serviceVersion ,String artifactName){
837 return encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceVersion, artifactName));
840 public static String getUniqueIdOfArtifact(RestResponse createResponse, String artifactField, String requieredArtifactLabel) throws Exception
842 Map<String, Object> artifact = getArtifactFromRestResponse(createResponse, artifactField, requieredArtifactLabel);
843 assertNotNull(artifact);
844 return artifact.get("uniqueId").toString();
847 public static Map<String, Object> getArtifactFromRestResponse(RestResponse response, String artifactField, String requieredArtifactLabel)
849 Map<String, Map<String, Object>> map = getArtifactsListFromResponse(response.getResponse(), artifactField);
850 return map.get(requieredArtifactLabel);
855 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
857 return updateInformationalArtifactPayloadOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateMD5Header(artifactDetails));
860 private static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
862 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
865 private static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
867 Config config = Utils.getConfig();
868 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
870 if (isPayloadUpdate){
871 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
874 Gson gson = new Gson();
875 String jsonBody = gson.toJson(artifactDetails);
877 HttpRequest http = new HttpRequest();
879 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
880 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
881 // System.out.println("update artifact was finished with response: "+ res.getErrorCode());
885 public static String calculateMD5Header(ArtifactReqDetails artifactDetails)
887 Gson gson = new Gson();
888 String jsonBody = gson.toJson(artifactDetails);
889 // calculate MD5 for json body
890 return calculateMD5(jsonBody);
894 public static String calculateMD5 (String data){
895 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
896 // encode base-64 result
897 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
898 String encodeBase64Str = new String(encodeBase64);
899 return encodeBase64Str;
905 * @param restResponse restResponse object
906 * @return readable content(converted from base64)
907 * @throws IOException ioexception
909 public static String getDecodedArtifactPayloadFromResponse(RestResponse restResponse) throws IOException {
910 Gson gson = new Gson();
911 Map<String, String> fromJson = gson.fromJson(restResponse.getResponse(), Map.class);
912 String payloadData = fromJson.get("base64Contents").toString();
913 String decodedPaypload = org.openecomp.sdc.ci.tests.utils.Decoder.decode(payloadData);
914 return decodedPaypload;