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 static org.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertTrue;
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.apache.commons.codec.binary.Base64;
32 import org.json.simple.JSONObject;
33 import org.json.simple.JSONValue;
34 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
35 import org.openecomp.sdc.be.model.ArtifactDefinition;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.ComponentInstance;
38 import org.openecomp.sdc.be.model.Resource;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.ci.tests.api.Urls;
41 import org.openecomp.sdc.ci.tests.config.Config;
42 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
44 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
45 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
46 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
47 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
49 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
50 import org.openecomp.sdc.ci.tests.utils.Utils;
51 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
52 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
53 import org.openecomp.sdc.common.util.ValidationUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.AssertJUnit;
58 import com.google.gson.Gson;
60 public class ArtifactRestUtils extends BaseRestUtils {
61 private static Logger logger = LoggerFactory.getLogger(ArtifactRestUtils.class.getName());
65 // Delete Artifact on rI of the asset
66 public static RestResponse externalAPIDeleteArtifactOfComponentInstanceOnAsset(Component component, User user, ComponentInstance resourceInstance, String artifactUUID) throws IOException {
67 Config config = Utils.getConfig();
69 String resourceUUID = component.getUUID();
70 String resourceInstanceName = resourceInstance.getNormalizedName();
72 System.out.println(component.getComponentType());
74 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
75 resourceType = "resources";
77 resourceType = "services";
80 String url = String.format(Urls.DELETE_EXTRNAL_API_DELETE_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName, artifactUUID);
82 return deleteInformationalArtifact(user, url);
85 // Delete Artifact of the asset
86 public static RestResponse externalAPIDeleteArtifactOfTheAsset(Component component, User user, String artifactUUID) throws IOException {
87 Config config = Utils.getConfig();
89 String resourceUUID = component.getUUID();
91 System.out.println(component.getComponentType());
93 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
94 resourceType = "resources";
96 resourceType = "services";
99 String url = String.format(Urls.DELETE_EXTRNAL_API_DELETE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, artifactUUID);
101 RestResponse restResponse = deleteInformationalArtifact(user, url);
107 // Update Artifact on rI of the asset
108 public static RestResponse externalAPIUpdateArtifactOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance, String artifactUUID) throws IOException {
109 Config config = Utils.getConfig();
111 String resourceUUID = component.getUUID();
112 String resourceInstanceName = resourceInstance.getNormalizedName();
114 System.out.println(component.getComponentType());
116 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
117 resourceType = "resources";
119 resourceType = "services";
122 String url = String.format(Urls.POST_EXTERNAL_API_UPDATE_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName, artifactUUID);
124 return updateInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
127 // Update Artifact of the asset
128 public static RestResponse externalAPIUpdateArtifactOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, String artifactUUID) throws IOException {
129 Config config = Utils.getConfig();
131 String resourceUUID = component.getUUID();
133 System.out.println(component.getComponentType());
135 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
136 resourceType = "resources";
138 resourceType = "services";
141 String url = String.format(Urls.POST_EXTERNAL_API_UPDATE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, artifactUUID);
143 return updateInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
147 // Upload Artifact on rI of the asset
148 public static RestResponse externalAPIUploadArtifactOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance) throws IOException {
149 Config config = Utils.getConfig();
151 String resourceUUID = component.getUUID();
152 String resourceInstanceName = resourceInstance.getNormalizedName();
154 System.out.println(component.getComponentType());
156 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
157 resourceType = "resources";
159 resourceType = "services";
162 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName);
164 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
167 // Upload Artifact of the asset
168 public static RestResponse externalAPIUploadArtifactOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails) throws IOException {
169 Config config = Utils.getConfig();
171 String resourceUUID = component.getUUID();
173 System.out.println(component.getComponentType());
175 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
176 resourceType = "resources";
178 resourceType = "services";
181 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID);
183 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
187 // Upload Artifact of the asset with invalid checksum
188 public static RestResponse externalAPIUploadArtifactWithInvalidCheckSumOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance) throws IOException {
189 Config config = Utils.getConfig();
191 String resourceUUID = component.getUUID();
192 String resourceInstanceName = resourceInstance.getNormalizedName();
194 System.out.println(component.getComponentType());
196 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
197 resourceType = "resources";
199 resourceType = "services";
202 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName);
204 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails) + "123", url);
207 // Upload Artifact of the asset with invalid checksum
208 public static RestResponse externalAPIUploadArtifactWithInvalidCheckSumOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails) throws IOException {
209 Config config = Utils.getConfig();
211 String resourceUUID = component.getUUID();
213 System.out.println(component.getComponentType());
215 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
216 resourceType = "resources";
218 resourceType = "services";
221 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID);
223 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails) + "123", url);
230 public static RestResponse getResourceDeploymentArtifactExternalAPI(String resourceUUID, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
231 Config config = Utils.getConfig();
234 if (resourceType.toUpperCase().equals("SERVICE")) {
235 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, artifactUUID);
238 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, artifactUUID);
241 Map<String, String> headersMap = new HashMap<>();
242 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
243 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
244 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
245 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
246 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(),acceptMultipartHeader);
248 HttpRequest http = new HttpRequest();
250 logger.debug("Send GET request to get Resource Assets: {}", url);
251 System.out.println("Send GET request to get Resource Assets: " + url);
253 logger.debug("Request headers: {}", headersMap);
254 System.out.println("Request headers: " + headersMap);
256 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
258 return sendGetResourceAssets;
264 public static RestResponse getComponentInstanceDeploymentArtifactExternalAPI(String resourceUUID, String componentNormalizedName, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
265 Config config = Utils.getConfig();
268 if (resourceType.toLowerCase().equals("service")) {
269 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
272 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
275 Map<String, String> headersMap = new HashMap<>();
276 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
277 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
278 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
279 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
280 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(),acceptMultipartHeader);
282 HttpRequest http = new HttpRequest();
284 logger.debug("Send GET request to get Resource Assets: {}", url);
285 System.out.println("Send GET request to get Resource Assets: " + url);
287 logger.debug("Request headers: {}", headersMap);
288 System.out.println("Request headers: " + headersMap);
290 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
292 return sendGetResourceAssets;
297 //*********** SERVICE ****************
298 public static RestResponse getArtifactTypesList() throws IOException {
299 Config config = Utils.getConfig();
300 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
302 return sendGet(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
305 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid) throws IOException {
306 return addInformationalArtifactToService(artifactDetails, sdncModifierDetails, serviceUid, calculateChecksum(artifactDetails));
309 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String checksum) throws IOException {
310 Config config = Utils.getConfig();
311 String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceUid);
313 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
316 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
319 return downloadServiceArtifact( service, artifact, user,addionalHeaders,true);
322 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
324 Config config = Utils.getConfig();
325 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(service.getName()), service.getVersion(), ValidationUtils.normalizeFileName(artifact.getArtifactName())));
326 String fullUrl = String.format(Urls.DOWNLOAD_SERVICE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
328 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
331 public static RestResponse downloadResourceArtifact(ServiceReqDetails service, ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
333 return downloadResourceArtifact(service, resource, artifact, user,addionalHeaders, true);
336 public static RestResponse downloadResourceArtifact(ServiceReqDetails service,ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
338 Config config = Utils.getConfig();
339 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())));
340 String fullUrl = String.format(Urls.DOWNLOAD_RESOURCE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
342 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
347 public static RestResponse downloadResourceInstanceArtifact(String serviceUniqueId,String resourceInstanceId, User user, String artifactUniqeId) throws Exception
349 Config config = Utils.getConfig();
350 String url = String.format(Urls.DOWNLOAD_COMPONENT_INSTANCE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, resourceInstanceId, artifactUniqeId);
351 RestResponse res = sendGet(url, user.getUserId(), null);
359 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod) throws IOException {
360 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactUid, sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
363 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, User sdncModifierDetails, String httpMethod) throws IOException {
364 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactReqDetails.getUniqueId(), sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
367 public static RestResponse downloadResourceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
369 return downloadComponentArtifactInternalApi(resourceId, user, artifactUniqeId, Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT);
372 public static RestResponse downloadServiceArtifactInternalApi(String componentId, User user, String artifactUniqeId) throws Exception
374 return downloadComponentArtifactInternalApi(componentId, user, artifactUniqeId, Urls.UI_DOWNLOAD_SERVICE_ARTIFACT);
376 public static RestResponse downloadComponentArtifactInternalApi(String componentId, User user, String artifactUniqeId, String urlTemplate) throws Exception
378 Config config = Utils.getConfig();
379 String url = String.format(urlTemplate, config.getCatalogBeHost(),config.getCatalogBePort(), componentId, artifactUniqeId);
380 RestResponse res = sendGet(url, user.getUserId(), null);
384 // public static RestResponse downloadServiceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
386 // Config config = Utils.getConfig();
387 // String url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), resourceId, artifactUniqeId);
388 // RestResponse res = sendGet(url, user.getUserId(), null);
393 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
395 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
398 public static RestResponse updateInformationalArtifactMetadataOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
400 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateChecksum(artifactDetails), false);
403 public static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
405 Config config = Utils.getConfig();
406 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
408 if (isPayloadUpdate){
409 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
412 Gson gson = new Gson();
413 String jsonBody = gson.toJson(artifactDetails);
415 HttpRequest http = new HttpRequest();
417 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
418 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
419 System.out.println("update artifact was finished with response: "+ res.getErrorCode());
425 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod, String checksum) throws IOException
427 Config config = Utils.getConfig();
429 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
430 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
432 Gson gson = new Gson();
433 String userBodyJson = gson.toJson(artifactReqDetails);
435 HttpRequest http = new HttpRequest();
436 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
437 RestResponse updateResourceResponse = http.httpSendByMethod(url, httpMethod, userBodyJson, headersMap);
438 // System.out.println("update artifact was finished with response: "+ updateResourceResponse.getErrorCode());
440 return updateResourceResponse;
444 public static Map<String, String> getHeadersMap(User sdncModifierDetails) {
445 Map<String, String> headersMap = new HashMap<>();
446 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
447 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
450 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
460 //*********** RESOURCE ****************
462 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
463 return addInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
466 public static RestResponse explicitAddInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
467 Config config = Utils.getConfig();
470 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
472 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
476 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException{
477 Config config = Utils.getConfig();
479 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals(ArtifactGroupTypeEnum.DEPLOYMENT.getType())){
480 //YANG_XML and OTHER deployment artifact should be added through this API, not updated
481 String artifactType = artifactDetails.getArtifactType();
482 if (!(ArtifactTypeEnum.YANG_XML.getType().equals(artifactType) ||
483 ArtifactTypeEnum.OTHER.getType().equals(artifactType) ||
484 ArtifactTypeEnum.VNF_CATALOG.getType().equals(artifactType) ||
485 ArtifactTypeEnum.VF_LICENSE.getType().equals(artifactType) ||
486 ArtifactTypeEnum.VENDOR_LICENSE.getType().equals(artifactType) ||
487 ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.getType().equals(artifactType) ||
488 ArtifactTypeEnum.MODEL_QUERY_SPEC.getType().equals(artifactType) ||
489 ArtifactTypeEnum.APPC_CONFIG.getType().equals(artifactType))){
490 //return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid);
493 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
495 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
498 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
499 return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
502 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException {
503 Config config = Utils.getConfig();
504 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")){
505 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
506 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
507 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
508 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
509 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
510 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
514 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
516 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
519 public static RestResponse uploadArtifactToPlaceholderOnResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String placeHolderLabel) throws IOException {
520 Config config = Utils.getConfig();
521 if (artifactDetails.getArtifactLabel() != null && !artifactDetails.getArtifactLabel().isEmpty()){
522 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
523 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
524 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
525 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
526 AssertJUnit.assertNotNull(artifactDefinition);
527 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
528 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
532 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
534 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
537 public static RestResponse updateArtifactToResourceInstance(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
538 Config config = Utils.getConfig();
539 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDefinition.getUniqueId());
540 return updateDeploymentArtifact(artifactDefinition, sdncModifierDetails, url);
543 public static RestResponse updateDeploymentArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException {
544 Config config = Utils.getConfig();
545 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
547 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
551 public static RestResponse updateDeploymentArtifactToRI(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
552 Config config = Utils.getConfig();
553 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_HEAT_ENV_PARAMS, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDetails.getUniqueId());
554 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
558 public static RestResponse deleteArtifactFromResourceInstance (ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceUid, String serviceId) throws IOException{
559 Config config = Utils.getConfig();
560 String url = String.format(Urls.DELETE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceUid, artifactDefinition.getUniqueId());
561 return sendDelete(url, sdncModifierDetails.getUserId());
564 public static RestResponse deleteInformationalArtifactFromResource(String resourceUid, ArtifactReqDetails artifactDetails, User sdncModifierDetails) throws IOException{
565 return deleteInformationalArtifactFromResource( resourceUid, artifactDetails.getUniqueId(), sdncModifierDetails);
568 public static RestResponse deleteInformationalArtifactFromResource( String resourceUid, String artifactId, User sdncModifierDetails) throws IOException{
569 Config config = Utils.getConfig();
570 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactId);
571 return sendDelete(url, sdncModifierDetails.getUserId());
574 public static RestResponse deleteServiceApiArtifact(ArtifactReqDetails artifactDetails, String serviceUniqueId, User user) throws Exception
576 Config config = Utils.getConfig();
577 String url = String.format(Urls.UPDATE_DELETE_SERVICE_API_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, artifactDetails.getUniqueId());
578 RestResponse res = sendDelete(url, user.getUserId());
579 logger.debug("Deleting api artifact was finished with response: {}", res.getErrorCode());
580 logger.debug("Response body: {}", res.getResponseMessage());
584 //*************** RESOURCE INSTANCE **************
586 * Add DCAE artifacts to resource instance.
587 * @param artifactDetails
588 * @param sdncModifierDetails
589 * @param resourceInstanceId
592 * @throws IOException
594 public static RestResponse addArtifactToResourceInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
595 Config config = Utils.getConfig();
596 String url = String.format(Urls.ADD_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId,resourceInstanceId, artifactDetails.getUniqueId());
597 return addArtifactToInstance(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
600 //*************** COMPONENT **************
602 public static RestResponse uploadDeploymentArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
603 Config config = Utils.getConfig();
604 Map<String, String> additionalHeaders;
605 String checksum = ResponseParser.calculateMD5Header(artifactDetails);
606 additionalHeaders = new HashMap<>();
607 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
609 ComponentTypeEnum componentType = component.getComponentType();
613 switch (componentType){
617 url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), component.getUniqueId(), artifactDetails.getUniqueId());
632 assertTrue("failed on enum selection", false);
641 Gson gson = new Gson();
642 String jsonBody = gson.toJson(artifactDetails);
643 // System.out.println("ArtifactDetails: "+ jsonBody);
645 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
646 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
647 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
649 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
653 public static RestResponse uploadArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
654 Config config = Utils.getConfig();
655 List<String> placeHolderlst = Utils.getListOfResPlaceHoldersDepArtTypes();
656 Map<String, String> additionalHeaders;
661 // if (artifactDetails.getArtifactGroupType() != null
662 // && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")
663 // && placeHolderlst.contains(artifactDetails.getArtifactType())) {
664 // Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
665 // ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
666 // artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
667 // artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
668 // checksum = ResponseParser.calculateMD5Header(artifactDetails);
669 // additionalHeaders = new HashMap<String, String>();
670 // additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
671 // url = String.format(Urls.UPDATE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
672 // config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
673 // component.getUniqueId(), artifactDetails.getUniqueId());
677 checksum = ResponseParser.calculateMD5Header(artifactDetails);
678 additionalHeaders = new HashMap<>();
679 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
680 url = String.format(Urls.UPLOAD_DELETE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
681 config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
682 component.getUniqueId(), artifactDetails.getUniqueId());
685 Gson gson = new Gson();
686 String jsonBody = gson.toJson(artifactDetails);
687 // System.out.println("ArtifactDetails: "+ jsonBody);
689 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
690 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
691 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
693 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
700 //*************** PRIVATE **************
701 private static RestResponse deleteInformationalArtifact(User sdncModifierDetails, String url) throws IOException {
702 Map<String, String> additionalHeaders;
704 additionalHeaders = new HashMap<>();
707 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
708 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
710 return sendDelete(url, sdncModifierDetails.getUserId(), additionalHeaders);
712 // Gson gson = new Gson();
713 //// System.out.println("ArtifactDetails: "+ jsonBody);
714 // String jsonBody = gson.toJson(artifactDetails);
716 // RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
717 // if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
718 // artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
720 //// System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
724 private static RestResponse updateInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
725 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
728 private static RestResponse uploadInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
729 Map<String, String> additionalHeaders = null;
730 if (checksum != null && !checksum.isEmpty()) {
731 additionalHeaders = new HashMap<>();
732 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
735 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
736 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
738 Gson gson = new Gson();
739 // System.out.println("ArtifactDetails: "+ jsonBody);
740 String jsonBody = gson.toJson(artifactDetails);
742 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
743 if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
744 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
746 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
750 private static RestResponse addArtifactToInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
751 Map<String, String> additionalHeaders;
752 additionalHeaders = new HashMap<>();
753 if (checksum != null && !checksum.isEmpty()) {
754 additionalHeaders = new HashMap<>();
755 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
757 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
758 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
760 Gson gson = new Gson();
761 String jsonBody = gson.toJson(artifactDetails);
763 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
764 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
765 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
770 private static RestResponse updateDeploymentArtifact(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String url) throws IOException {
771 Map<String, String> additionalHeaders;
772 additionalHeaders = new HashMap<>();
773 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
774 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
776 Gson gson = new Gson();
777 String jsonBody = gson.toJson(artifactDefinition);
779 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
783 private static RestResponse updateDeploymentArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String url) throws IOException {
784 Map<String, String> additionalHeaders;
786 additionalHeaders = new HashMap<>();
787 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
788 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
789 // additionalHeaders.put(HttpHeaderEnum..getValue(), "application/json;charset=UTF-8");
792 Gson gson = new Gson();
793 String jsonBody = gson.toJson(artifactDetails);
794 // System.out.println("ArtifactDetails: "+ jsonBody);
796 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
797 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
798 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
800 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
804 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws IOException
807 addionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
809 return downloadArtifact(url, user, addionalHeaders, acceptOctetStream);
812 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders, String accept) throws IOException
814 addionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
816 RestResponse res = sendGet(url, user.getUserId(), addionalHeaders);
817 // System.out.println("download artifact was finished with response: "+ res.getErrorCode());
818 // System.out.println("response is: " + res.getResponse());
822 private static Map<String,Map<String,Object>> getArtifactsListFromResponse(String jsonResponse, String fieldOfArtifactList){
823 JSONObject object = (JSONObject)JSONValue.parse(jsonResponse);
824 Map<String,Map<String,Object>> map = (Map<String,Map<String,Object>>)object.get(fieldOfArtifactList);
828 public static String calculateChecksum(ArtifactReqDetails artifactDetails) {
829 String checksum = null;
830 if (artifactDetails.getPayload() != null) {
831 checksum = ResponseParser.calculateMD5Header(artifactDetails);
836 public static String encodeUrlForDownload(String url){
838 return url.replaceAll(" ", "%20");
841 public static String getPartialUrlByArtifactName(ServiceReqDetails serviceDetails,String serviceVersion ,String artifactName){
842 return encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceVersion, artifactName));
845 public static String getUniqueIdOfArtifact(RestResponse createResponse, String artifactField, String requieredArtifactLabel) throws Exception
847 Map<String, Object> artifact = getArtifactFromRestResponse(createResponse, artifactField, requieredArtifactLabel);
848 assertNotNull(artifact);
849 return artifact.get("uniqueId").toString();
852 public static Map<String, Object> getArtifactFromRestResponse(RestResponse response, String artifactField, String requieredArtifactLabel)
854 Map<String, Map<String, Object>> map = getArtifactsListFromResponse(response.getResponse(), artifactField);
855 return map.get(requieredArtifactLabel);
860 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
862 return updateInformationalArtifactPayloadOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateMD5Header(artifactDetails));
865 private static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
867 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
870 private static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
872 Config config = Utils.getConfig();
873 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
875 if (isPayloadUpdate){
876 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
879 Gson gson = new Gson();
880 String jsonBody = gson.toJson(artifactDetails);
882 HttpRequest http = new HttpRequest();
884 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
885 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
886 // System.out.println("update artifact was finished with response: "+ res.getErrorCode());
890 public static String calculateMD5Header(ArtifactReqDetails artifactDetails)
892 Gson gson = new Gson();
893 String jsonBody = gson.toJson(artifactDetails);
894 // calculate MD5 for json body
895 return calculateMD5(jsonBody);
899 public static String calculateMD5 (String data){
900 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
901 // encode base-64 result
902 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
903 String encodeBase64Str = new String(encodeBase64);
904 return encodeBase64Str;