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.api.Constants;
54 import org.openecomp.sdc.common.util.ValidationUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.testng.AssertJUnit;
59 import com.google.gson.Gson;
60 import com.google.gson.JsonElement;
61 import com.google.gson.JsonObject;
63 public class ArtifactRestUtils extends BaseRestUtils {
64 private static Logger logger = LoggerFactory.getLogger(ArtifactRestUtils.class.getName());
68 // Delete Artifact on rI of the asset
69 public static RestResponse externalAPIDeleteArtifactOfComponentInstanceOnAsset(Component component, User user, ComponentInstance resourceInstance, String artifactUUID) throws IOException {
70 Config config = Utils.getConfig();
71 String resourceType = null;
72 String resourceUUID = component.getUUID();
73 String resourceInstanceName = resourceInstance.getNormalizedName();
75 System.out.println(component.getComponentType());
77 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
78 resourceType = "resources";
80 resourceType = "services";
83 String url = String.format(Urls.DELETE_EXTRNAL_API_DELETE_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName, artifactUUID);
85 return deleteInformationalArtifact(user, url);
88 // Delete Artifact of the asset
89 public static RestResponse externalAPIDeleteArtifactOfTheAsset(Component component, User user, String artifactUUID) throws IOException {
90 Config config = Utils.getConfig();
91 String resourceType = null;
92 String resourceUUID = component.getUUID();
94 System.out.println(component.getComponentType());
96 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
97 resourceType = "resources";
99 resourceType = "services";
102 String url = String.format(Urls.DELETE_EXTRNAL_API_DELETE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, artifactUUID);
104 RestResponse restResponse = deleteInformationalArtifact(user, url);
110 // Update Artifact on rI of the asset
111 public static RestResponse externalAPIUpdateArtifactOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance, String artifactUUID) throws IOException {
112 Config config = Utils.getConfig();
113 String resourceType = null;
114 String resourceUUID = component.getUUID();
115 String resourceInstanceName = resourceInstance.getNormalizedName();
117 System.out.println(component.getComponentType());
119 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
120 resourceType = "resources";
122 resourceType = "services";
125 String url = String.format(Urls.POST_EXTERNAL_API_UPDATE_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName, artifactUUID);
127 return updateInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
130 // Update Artifact of the asset
131 public static RestResponse externalAPIUpdateArtifactOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, String artifactUUID) throws IOException {
132 Config config = Utils.getConfig();
133 String resourceType = null;
134 String resourceUUID = component.getUUID();
136 System.out.println(component.getComponentType());
138 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
139 resourceType = "resources";
141 resourceType = "services";
144 String url = String.format(Urls.POST_EXTERNAL_API_UPDATE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, artifactUUID);
146 return updateInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
150 // Upload Artifact on rI of the asset
151 public static RestResponse externalAPIUploadArtifactOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance) throws IOException {
152 Config config = Utils.getConfig();
153 String resourceType = null;
154 String resourceUUID = component.getUUID();
155 String resourceInstanceName = resourceInstance.getNormalizedName();
157 System.out.println(component.getComponentType());
159 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
160 resourceType = "resources";
162 resourceType = "services";
165 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName);
167 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
170 // Upload Artifact of the asset
171 public static RestResponse externalAPIUploadArtifactOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails) throws IOException {
172 Config config = Utils.getConfig();
173 String resourceType = null;
174 String resourceUUID = component.getUUID();
176 System.out.println(component.getComponentType());
178 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
179 resourceType = "resources";
181 resourceType = "services";
184 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID);
186 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails), url);
190 // Upload Artifact of the asset with invalid checksum
191 public static RestResponse externalAPIUploadArtifactWithInvalidCheckSumOfComponentInstanceOnAsset(Component component, User user, ArtifactReqDetails artifactReqDetails, ComponentInstance resourceInstance) throws IOException {
192 Config config = Utils.getConfig();
193 String resourceType = null;
194 String resourceUUID = component.getUUID();
195 String resourceInstanceName = resourceInstance.getNormalizedName();
197 System.out.println(component.getComponentType());
199 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
200 resourceType = "resources";
202 resourceType = "services";
205 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_COMPONENTINSTANCE_ON_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID, resourceInstanceName);
207 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails) + "123", url);
210 // Upload Artifact of the asset with invalid checksum
211 public static RestResponse externalAPIUploadArtifactWithInvalidCheckSumOfTheAsset(Component component, User user, ArtifactReqDetails artifactReqDetails) throws IOException {
212 Config config = Utils.getConfig();
213 String resourceType = null;
214 String resourceUUID = component.getUUID();
216 System.out.println(component.getComponentType());
218 if(component.getComponentType().toString().toLowerCase().equals("resource")) {
219 resourceType = "resources";
221 resourceType = "services";
224 String url = String.format(Urls.POST_EXTERNAL_API_UPLOAD_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceType, resourceUUID);
226 return uploadInformationalArtifact(artifactReqDetails, user, calculateChecksum(artifactReqDetails) + "123", url);
233 public static RestResponse getResourceDeploymentArtifactExternalAPI(String resourceUUID, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
234 Config config = Utils.getConfig();
237 if (resourceType.toUpperCase().equals("SERVICE")) {
238 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, artifactUUID);
241 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_ASSET, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, artifactUUID);
244 Map<String, String> headersMap = new HashMap<String,String>();
245 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
246 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
247 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
248 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
250 HttpRequest http = new HttpRequest();
252 logger.debug("Send GET request to get Resource Assets: {}", url);
253 System.out.println("Send GET request to get Resource Assets: " + url);
255 logger.debug("Request headers: {}", headersMap);
256 System.out.println("Request headers: " + headersMap);
258 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
260 return sendGetResourceAssets;
266 public static RestResponse getComponentInstanceDeploymentArtifactExternalAPI(String resourceUUID, String componentNormalizedName, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
267 Config config = Utils.getConfig();
270 if (resourceType.toLowerCase().equals("service")) {
271 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
274 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
277 Map<String, String> headersMap = new HashMap<String,String>();
278 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
279 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
280 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
281 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
283 HttpRequest http = new HttpRequest();
285 logger.debug("Send GET request to get Resource Assets: {}", url);
286 System.out.println("Send GET request to get Resource Assets: " + url);
288 logger.debug("Request headers: {}", headersMap);
289 System.out.println("Request headers: " + headersMap);
291 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
293 return sendGetResourceAssets;
298 //*********** SERVICE ****************
299 public static RestResponse getArtifactTypesList() throws IOException {
300 Config config = Utils.getConfig();
301 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
303 return sendGet(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
306 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid) throws IOException {
307 return addInformationalArtifactToService(artifactDetails, sdncModifierDetails, serviceUid, calculateChecksum(artifactDetails));
310 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String checksum) throws IOException {
311 Config config = Utils.getConfig();
312 String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceUid);
314 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
317 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
320 return downloadServiceArtifact( service, artifact, user,addionalHeaders,true);
323 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
325 Config config = Utils.getConfig();
326 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(service.getName()), service.getVersion(), ValidationUtils.normalizeFileName(artifact.getArtifactName())));
327 String fullUrl = String.format(Urls.DOWNLOAD_SERVICE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
329 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
332 public static RestResponse downloadResourceArtifact(ServiceReqDetails service, ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
334 return downloadResourceArtifact(service, resource, artifact, user,addionalHeaders, true);
337 public static RestResponse downloadResourceArtifact(ServiceReqDetails service,ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
339 Config config = Utils.getConfig();
340 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())));
341 String fullUrl = String.format(Urls.DOWNLOAD_RESOURCE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
343 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
348 public static RestResponse downloadResourceInstanceArtifact(String serviceUniqueId,String resourceInstanceId, User user, String artifactUniqeId) throws Exception
350 Config config = Utils.getConfig();
351 String url = String.format(Urls.DOWNLOAD_COMPONENT_INSTANCE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, resourceInstanceId, artifactUniqeId);
352 RestResponse res = sendGet(url, user.getUserId(), null);
360 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod) throws IOException {
361 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactUid, sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
364 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, User sdncModifierDetails, String httpMethod) throws IOException {
365 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactReqDetails.getUniqueId(), sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
368 public static RestResponse downloadResourceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
370 return downloadComponentArtifactInternalApi(resourceId, user, artifactUniqeId, Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT);
373 public static RestResponse downloadServiceArtifactInternalApi(String componentId, User user, String artifactUniqeId) throws Exception
375 return downloadComponentArtifactInternalApi(componentId, user, artifactUniqeId, Urls.UI_DOWNLOAD_SERVICE_ARTIFACT);
377 public static RestResponse downloadComponentArtifactInternalApi(String componentId, User user, String artifactUniqeId, String urlTemplate) throws Exception
379 Config config = Utils.getConfig();
380 String url = String.format(urlTemplate, config.getCatalogBeHost(),config.getCatalogBePort(), componentId, artifactUniqeId);
381 RestResponse res = sendGet(url, user.getUserId(), null);
385 // public static RestResponse downloadServiceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
387 // Config config = Utils.getConfig();
388 // String url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), resourceId, artifactUniqeId);
389 // RestResponse res = sendGet(url, user.getUserId(), null);
394 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
396 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
399 public static RestResponse updateInformationalArtifactMetadataOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
401 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateChecksum(artifactDetails), false);
404 public static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
406 Config config = Utils.getConfig();
407 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
409 if (isPayloadUpdate){
410 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
413 Gson gson = new Gson();
414 String jsonBody = gson.toJson(artifactDetails);
416 HttpRequest http = new HttpRequest();
418 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
419 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
420 System.out.println("update artifact was finished with response: "+ res.getErrorCode());
426 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod, String checksum) throws IOException
428 Config config = Utils.getConfig();
430 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
431 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
433 Gson gson = new Gson();
434 String userBodyJson = gson.toJson(artifactReqDetails);
436 HttpRequest http = new HttpRequest();
437 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
438 RestResponse updateResourceResponse = http.httpSendByMethod(url, httpMethod, userBodyJson, headersMap);
439 // System.out.println("update artifact was finished with response: "+ updateResourceResponse.getErrorCode());
441 return updateResourceResponse;
445 public static Map<String, String> getHeadersMap(User sdncModifierDetails) {
446 Map<String, String> headersMap = new HashMap<String,String>();
447 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
448 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
451 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
461 //*********** RESOURCE ****************
463 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
464 return addInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
467 public static RestResponse explicitAddInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
468 Config config = Utils.getConfig();
471 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
473 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
477 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException{
478 Config config = Utils.getConfig();
480 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals(ArtifactGroupTypeEnum.DEPLOYMENT.getType())){
481 //YANG_XML and OTHER deployment artifact should be added through this API, not updated
482 String artifactType = artifactDetails.getArtifactType();
483 if (!(ArtifactTypeEnum.YANG_XML.getType().equals(artifactType) ||
484 ArtifactTypeEnum.OTHER.getType().equals(artifactType) ||
485 ArtifactTypeEnum.VNF_CATALOG.getType().equals(artifactType) ||
486 ArtifactTypeEnum.VF_LICENSE.getType().equals(artifactType) ||
487 ArtifactTypeEnum.VENDOR_LICENSE.getType().equals(artifactType) ||
488 ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.getType().equals(artifactType) ||
489 ArtifactTypeEnum.MODEL_QUERY_SPEC.getType().equals(artifactType) ||
490 ArtifactTypeEnum.APPC_CONFIG.getType().equals(artifactType))){
491 //return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid);
494 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
496 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
499 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
500 return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
503 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException {
504 Config config = Utils.getConfig();
505 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")){
506 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
507 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
508 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
509 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
510 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
511 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
515 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
517 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
520 public static RestResponse uploadArtifactToPlaceholderOnResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String placeHolderLabel) throws IOException {
521 Config config = Utils.getConfig();
522 if (artifactDetails.getArtifactLabel() != null && !artifactDetails.getArtifactLabel().isEmpty()){
523 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
524 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
525 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
526 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
527 AssertJUnit.assertNotNull(artifactDefinition);
528 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
529 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
533 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
535 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
538 public static RestResponse updateArtifactToResourceInstance(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
539 Config config = Utils.getConfig();
540 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDefinition.getUniqueId());
541 return updateDeploymentArtifact(artifactDefinition, sdncModifierDetails, url);
544 public static RestResponse updateDeploymentArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException {
545 Config config = Utils.getConfig();
546 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
548 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
552 public static RestResponse updateDeploymentArtifactToRI(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
553 Config config = Utils.getConfig();
554 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_HEAT_ENV_PARAMS, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDetails.getUniqueId());
555 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
559 public static RestResponse deleteArtifactFromResourceInstance (ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceUid, String serviceId) throws IOException{
560 Config config = Utils.getConfig();
561 String url = String.format(Urls.DELETE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceUid, artifactDefinition.getUniqueId());
562 return sendDelete(url, sdncModifierDetails.getUserId());
565 public static RestResponse deleteInformationalArtifactFromResource(String resourceUid, ArtifactReqDetails artifactDetails, User sdncModifierDetails) throws IOException{
566 return deleteInformationalArtifactFromResource( resourceUid, artifactDetails.getUniqueId(), sdncModifierDetails);
569 public static RestResponse deleteInformationalArtifactFromResource( String resourceUid, String artifactId, User sdncModifierDetails) throws IOException{
570 Config config = Utils.getConfig();
571 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactId);
572 return sendDelete(url, sdncModifierDetails.getUserId());
575 public static RestResponse deleteServiceApiArtifact(ArtifactReqDetails artifactDetails, String serviceUniqueId, User user) throws Exception
577 Config config = Utils.getConfig();
578 String url = String.format(Urls.UPDATE_DELETE_SERVICE_API_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, artifactDetails.getUniqueId());
579 RestResponse res = sendDelete(url, user.getUserId());
580 logger.debug("Deleting api artifact was finished with response: {}", res.getErrorCode());
581 logger.debug("Response body: {}", res.getResponseMessage());
585 //*************** RESOURCE INSTANCE **************
587 * Add DCAE artifacts to resource instance.
588 * @param artifactDetails
589 * @param sdncModifierDetails
590 * @param resourceInstanceId
593 * @throws IOException
595 public static RestResponse addArtifactToResourceInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
596 Config config = Utils.getConfig();
597 String url = String.format(Urls.ADD_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId,resourceInstanceId, artifactDetails.getUniqueId());
598 return addArtifactToInstance(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
601 //*************** COMPONENT **************
603 public static RestResponse uploadDeploymentArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
604 Config config = Utils.getConfig();
605 Map<String, String> additionalHeaders = null;
606 String checksum = ResponseParser.calculateMD5Header(artifactDetails);
607 additionalHeaders = new HashMap<String, String>();
608 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
610 ComponentTypeEnum componentType = component.getComponentType();
614 switch (componentType){
618 url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), component.getUniqueId(), artifactDetails.getUniqueId());
633 assertTrue("failed on enum selection", false);
642 Gson gson = new Gson();
643 String jsonBody = gson.toJson(artifactDetails);
644 // System.out.println("ArtifactDetails: "+ jsonBody);
646 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
647 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
648 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
650 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
654 public static RestResponse uploadArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
655 Config config = Utils.getConfig();
656 List<String> placeHolderlst = Utils.getListOfResPlaceHoldersDepArtTypes();
657 Map<String, String> additionalHeaders = null;
658 String checksum = null;
662 // if (artifactDetails.getArtifactGroupType() != null
663 // && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")
664 // && placeHolderlst.contains(artifactDetails.getArtifactType())) {
665 // Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
666 // ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
667 // artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
668 // artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
669 // checksum = ResponseParser.calculateMD5Header(artifactDetails);
670 // additionalHeaders = new HashMap<String, String>();
671 // additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
672 // url = String.format(Urls.UPDATE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
673 // config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
674 // component.getUniqueId(), artifactDetails.getUniqueId());
678 checksum = ResponseParser.calculateMD5Header(artifactDetails);
679 additionalHeaders = new HashMap<String, String>();
680 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
681 url = String.format(Urls.UPLOAD_DELETE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
682 config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
683 component.getUniqueId(), artifactDetails.getUniqueId());
686 Gson gson = new Gson();
687 String jsonBody = gson.toJson(artifactDetails);
688 // System.out.println("ArtifactDetails: "+ jsonBody);
690 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
691 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
692 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
694 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
701 //*************** PRIVATE **************
702 private static RestResponse deleteInformationalArtifact(User sdncModifierDetails, String url) throws IOException {
703 Map<String, String> additionalHeaders = null;
705 additionalHeaders = new HashMap<String, String>();
708 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
709 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
711 return sendDelete(url, sdncModifierDetails.getUserId(), additionalHeaders);
713 // Gson gson = new Gson();
714 //// System.out.println("ArtifactDetails: "+ jsonBody);
715 // String jsonBody = gson.toJson(artifactDetails);
717 // RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
718 // if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
719 // artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
721 //// System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
725 private static RestResponse updateInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
726 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
729 private static RestResponse uploadInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
730 Map<String, String> additionalHeaders = null;
731 if (checksum != null && !checksum.isEmpty()) {
732 additionalHeaders = new HashMap<String, String>();
733 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
736 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
737 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
739 Gson gson = new Gson();
740 // System.out.println("ArtifactDetails: "+ jsonBody);
741 String jsonBody = gson.toJson(artifactDetails);
743 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
744 if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
745 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
747 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
751 private static RestResponse addArtifactToInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
752 Map<String, String> additionalHeaders = null;
753 additionalHeaders = new HashMap<String, String>();
754 if (checksum != null && !checksum.isEmpty()) {
755 additionalHeaders = new HashMap<String, String>();
756 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
758 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
759 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
761 Gson gson = new Gson();
762 String jsonBody = gson.toJson(artifactDetails);
764 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
765 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
766 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
771 private static RestResponse updateDeploymentArtifact(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String url) throws IOException {
772 Map<String, String> additionalHeaders = null;
773 additionalHeaders = new HashMap<String, String>();
774 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
775 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
777 Gson gson = new Gson();
778 String jsonBody = gson.toJson(artifactDefinition);
780 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
784 private static RestResponse updateDeploymentArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String url) throws IOException {
785 Map<String, String> additionalHeaders = null;
787 additionalHeaders = new HashMap<String, String>();
788 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
789 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
790 // additionalHeaders.put(HttpHeaderEnum..getValue(), "application/json;charset=UTF-8");
793 Gson gson = new Gson();
794 String jsonBody = gson.toJson(artifactDetails);
795 // System.out.println("ArtifactDetails: "+ jsonBody);
797 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
798 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
799 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
801 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
805 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws IOException
808 addionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), ecomp);
810 return downloadArtifact(url, user, addionalHeaders, acceptOctetStream);
813 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders, String accept) throws IOException
815 addionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
817 RestResponse res = sendGet(url, user.getUserId(), addionalHeaders);
818 // System.out.println("download artifact was finished with response: "+ res.getErrorCode());
819 // System.out.println("response is: " + res.getResponse());
823 private static Map<String,Map<String,Object>> getArtifactsListFromResponse(String jsonResponse, String fieldOfArtifactList){
824 JSONObject object = (JSONObject)JSONValue.parse(jsonResponse);
825 Map<String,Map<String,Object>> map = (Map<String,Map<String,Object>>)object.get(fieldOfArtifactList);
829 public static String calculateChecksum(ArtifactReqDetails artifactDetails) {
830 String checksum = null;
831 if (artifactDetails.getPayload() != null) {
832 checksum = ResponseParser.calculateMD5Header(artifactDetails);
837 public static String encodeUrlForDownload(String url){
839 return url.replaceAll(" ", "%20");
842 public static String getPartialUrlByArtifactName(ServiceReqDetails serviceDetails,String serviceVersion ,String artifactName){
843 return encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceVersion, artifactName));
846 public static String getUniqueIdOfArtifact(RestResponse createResponse, String artifactField, String requieredArtifactLabel) throws Exception
848 Map<String, Object> artifact = getArtifactFromRestResponse(createResponse, artifactField, requieredArtifactLabel);
849 assertNotNull(artifact);
850 return artifact.get("uniqueId").toString();
853 public static Map<String, Object> getArtifactFromRestResponse(RestResponse response, String artifactField, String requieredArtifactLabel)
855 Map<String, Map<String, Object>> map = getArtifactsListFromResponse(response.getResponse(), artifactField);
856 return map.get(requieredArtifactLabel);
861 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
863 return updateInformationalArtifactPayloadOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateMD5Header(artifactDetails));
866 private static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
868 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
871 private static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
873 Config config = Utils.getConfig();
874 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
876 if (isPayloadUpdate){
877 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
880 Gson gson = new Gson();
881 String jsonBody = gson.toJson(artifactDetails);
883 HttpRequest http = new HttpRequest();
885 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
886 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
887 // System.out.println("update artifact was finished with response: "+ res.getErrorCode());
891 public static String calculateMD5Header(ArtifactReqDetails artifactDetails)
893 Gson gson = new Gson();
894 String jsonBody = gson.toJson(artifactDetails);
895 // calculate MD5 for json body
896 return calculateMD5(jsonBody);
900 public static String calculateMD5 (String data){
901 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
902 // encode base-64 result
903 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
904 String encodeBase64Str = new String(encodeBase64);
905 return encodeBase64Str;
910 public static ArtifactDefinition getArtifactDataFromJson(String json) {
911 Gson gson = new Gson();
912 JsonObject jsonElement = new JsonObject();
913 jsonElement = gson.fromJson(json, jsonElement.getClass());
914 ArtifactDefinition artifact = new ArtifactDefinition();
915 String payload = null;
916 JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
917 if (artifactPayload != null && !artifactPayload.isJsonNull()) {
918 payload = artifactPayload.getAsString();
920 jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
921 artifact = gson.fromJson(jsonElement, ArtifactDefinition.class);
922 artifact.setPayloadData(payload);
924 /*atifact.setArtifactName(UPLOAD_ARTIFACT_NAME);
925 artifact.setArtifactDisplayName("configure");
926 artifact.setArtifactType("SHELL");
927 artifact.setMandatory(false);
928 artifact.setDescription("ff");
929 artifact.setPayloadData(UPLOAD_ARTIFACT_PAYLOAD);
930 artifact.setArtifactLabel("configure");*/