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();
68 String resourceType = null;
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();
88 String resourceType = null;
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();
110 String resourceType = null;
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();
130 String resourceType = null;
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();
150 String resourceType = null;
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();
170 String resourceType = null;
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();
190 String resourceType = null;
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();
210 String resourceType = null;
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<String,String>();
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(), "ci");
247 HttpRequest http = new HttpRequest();
249 logger.debug("Send GET request to get Resource Assets: {}",url);
250 System.out.println("Send GET request to get Resource Assets: " + url);
252 logger.debug("Request headers: {}",headersMap);
253 System.out.println("Request headers: " + headersMap);
255 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
257 return sendGetResourceAssets;
263 public static RestResponse getComponentInstanceDeploymentArtifactExternalAPI(String resourceUUID, String componentNormalizedName, String artifactUUID,User sdncModifierDetails, String resourceType) throws IOException {
264 Config config = Utils.getConfig();
267 if (resourceType.toLowerCase().equals("service")) {
268 url = String.format(Urls.GET_DOWNLOAD_SERVICE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
271 url = String.format(Urls.GET_DOWNLOAD_RESOURCE_ARTIFACT_OF_COMPONENT_INSTANCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUUID, componentNormalizedName, artifactUUID);
274 Map<String, String> headersMap = new HashMap<String,String>();
275 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
276 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
277 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
278 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
280 HttpRequest http = new HttpRequest();
282 logger.debug("Send GET request to get Resource Assets: {}",url);
283 System.out.println("Send GET request to get Resource Assets: " + url);
285 logger.debug("Request headers: {}",headersMap);
286 System.out.println("Request headers: " + headersMap);
288 RestResponse sendGetResourceAssets = http.httpSendGet(url, headersMap);
290 return sendGetResourceAssets;
295 //*********** SERVICE ****************
296 public static RestResponse getArtifactTypesList() throws IOException {
297 Config config = Utils.getConfig();
298 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
300 return sendGet(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
303 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid) throws IOException {
304 return addInformationalArtifactToService(artifactDetails, sdncModifierDetails, serviceUid, calculateChecksum(artifactDetails));
307 public static RestResponse addInformationalArtifactToService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String checksum) throws IOException {
308 Config config = Utils.getConfig();
309 String url = String.format(Urls.ADD_ARTIFACT_TO_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceUid);
311 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
314 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
317 return downloadServiceArtifact( service, artifact, user,addionalHeaders,true);
320 public static RestResponse downloadServiceArtifact(ServiceReqDetails service, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
322 Config config = Utils.getConfig();
323 String relativeUrl = encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(service.getName()), service.getVersion(), ValidationUtils.normalizeFileName(artifact.getArtifactName())));
324 String fullUrl = String.format(Urls.DOWNLOAD_SERVICE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
326 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
329 public static RestResponse downloadResourceArtifact(ServiceReqDetails service, ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders) throws Exception
331 return downloadResourceArtifact(service, resource, artifact, user,addionalHeaders, true);
334 public static RestResponse downloadResourceArtifact(ServiceReqDetails service,ResourceReqDetails resource, ArtifactReqDetails artifact, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws Exception
336 Config config = Utils.getConfig();
337 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())));
338 String fullUrl = String.format(Urls.DOWNLOAD_RESOURCE_ARTIFACT_FULL_URL, config.getCatalogBeHost(),config.getCatalogBePort(), relativeUrl);
340 return downloadArtifact(fullUrl, user, addionalHeaders,addEcompHeader);
345 public static RestResponse downloadResourceInstanceArtifact(String serviceUniqueId,String resourceInstanceId, User user, String artifactUniqeId) throws Exception
347 Config config = Utils.getConfig();
348 String url = String.format(Urls.DOWNLOAD_COMPONENT_INSTANCE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, resourceInstanceId, artifactUniqeId);
349 RestResponse res = sendGet(url, user.getUserId(), null);
357 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod) throws IOException {
358 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactUid, sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
361 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, User sdncModifierDetails, String httpMethod) throws IOException {
362 return updateInformationalArtifactOfServiceByMethod(artifactReqDetails, serviceUid, artifactReqDetails.getUniqueId(), sdncModifierDetails, httpMethod, calculateChecksum(artifactReqDetails));
365 public static RestResponse downloadResourceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
367 return downloadComponentArtifactInternalApi(resourceId, user, artifactUniqeId, Urls.UI_DOWNLOAD_RESOURCE_ARTIFACT);
370 public static RestResponse downloadServiceArtifactInternalApi(String componentId, User user, String artifactUniqeId) throws Exception
372 return downloadComponentArtifactInternalApi(componentId, user, artifactUniqeId, Urls.UI_DOWNLOAD_SERVICE_ARTIFACT);
374 public static RestResponse downloadComponentArtifactInternalApi(String componentId, User user, String artifactUniqeId, String urlTemplate) throws Exception
376 Config config = Utils.getConfig();
377 String url = String.format(urlTemplate, config.getCatalogBeHost(),config.getCatalogBePort(), componentId, artifactUniqeId);
378 RestResponse res = sendGet(url, user.getUserId(), null);
382 // public static RestResponse downloadServiceArtifactInternalApi(String resourceId, User user, String artifactUniqeId) throws Exception
384 // Config config = Utils.getConfig();
385 // String url = String.format(Urls.UI_DOWNLOAD_SERVICE_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), resourceId, artifactUniqeId);
386 // RestResponse res = sendGet(url, user.getUserId(), null);
391 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
393 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
396 public static RestResponse updateInformationalArtifactMetadataOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
398 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateChecksum(artifactDetails), false);
401 public static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
403 Config config = Utils.getConfig();
404 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
406 if (isPayloadUpdate){
407 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
410 Gson gson = new Gson();
411 String jsonBody = gson.toJson(artifactDetails);
413 HttpRequest http = new HttpRequest();
415 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
416 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
417 System.out.println("update artifact was finished with response: "+ res.getErrorCode());
423 public static RestResponse updateInformationalArtifactOfServiceByMethod(ArtifactReqDetails artifactReqDetails, String serviceUid, String artifactUid, User sdncModifierDetails, String httpMethod, String checksum) throws IOException
425 Config config = Utils.getConfig();
427 Map<String, String> headersMap = getHeadersMap(sdncModifierDetails);
428 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
430 Gson gson = new Gson();
431 String userBodyJson = gson.toJson(artifactReqDetails);
433 HttpRequest http = new HttpRequest();
434 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
435 RestResponse updateResourceResponse = http.httpSendByMethod(url, httpMethod, userBodyJson, headersMap);
436 // System.out.println("update artifact was finished with response: "+ updateResourceResponse.getErrorCode());
438 return updateResourceResponse;
442 public static Map<String, String> getHeadersMap(User sdncModifierDetails) {
443 Map<String, String> headersMap = new HashMap<String,String>();
444 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
445 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
448 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
458 //*********** RESOURCE ****************
460 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
461 return addInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
464 public static RestResponse explicitAddInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
465 Config config = Utils.getConfig();
468 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
470 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
474 public static RestResponse addInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException{
475 Config config = Utils.getConfig();
477 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals(ArtifactGroupTypeEnum.DEPLOYMENT.getType())){
478 //YANG_XML and OTHER deployment artifact should be added through this API, not updated
479 String artifactType = artifactDetails.getArtifactType();
480 if (!(ArtifactTypeEnum.YANG_XML.getType().equals(artifactType) ||
481 ArtifactTypeEnum.OTHER.getType().equals(artifactType) ||
482 ArtifactTypeEnum.VNF_CATALOG.getType().equals(artifactType) ||
483 ArtifactTypeEnum.VF_LICENSE.getType().equals(artifactType) ||
484 ArtifactTypeEnum.VENDOR_LICENSE.getType().equals(artifactType) ||
485 ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.getType().equals(artifactType) ||
486 ArtifactTypeEnum.MODEL_QUERY_SPEC.getType().equals(artifactType) ||
487 ArtifactTypeEnum.APPC_CONFIG.getType().equals(artifactType))){
488 //return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid);
491 String url = String.format(Urls.ADD_ARTIFACT_TO_RESOURCE, config.getCatalogBeHost(),config.getCatalogBePort(), resourceUid);
493 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
496 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException{
497 return updateInformationalArtifactToResource(artifactDetails, sdncModifierDetails, resourceUid, calculateChecksum(artifactDetails));
500 public static RestResponse updateInformationalArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String checksum) throws IOException {
501 Config config = Utils.getConfig();
502 if (artifactDetails.getArtifactGroupType()!=null && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")){
503 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
504 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
505 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
506 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
507 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
508 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
512 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
514 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
517 public static RestResponse uploadArtifactToPlaceholderOnResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid, String placeHolderLabel) throws IOException {
518 Config config = Utils.getConfig();
519 if (artifactDetails.getArtifactLabel() != null && !artifactDetails.getArtifactLabel().isEmpty()){
520 RestResponse resourceGetResponse = ResourceRestUtils.getResource(sdncModifierDetails, resourceUid );
521 Resource resourceRespJavaObject = ResponseParser.convertResourceResponseToJavaObject(resourceGetResponse.getResponse());
522 Map<String, ArtifactDefinition> deploymentArtifacts = resourceRespJavaObject.getDeploymentArtifacts();
523 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
524 AssertJUnit.assertNotNull(artifactDefinition);
525 artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
526 artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
530 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
532 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
535 public static RestResponse updateArtifactToResourceInstance(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
536 Config config = Utils.getConfig();
537 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDefinition.getUniqueId());
538 return updateDeploymentArtifact(artifactDefinition, sdncModifierDetails, url);
541 public static RestResponse updateDeploymentArtifactToResource(ArtifactDefinition artifact, User sdncModifierDetails, String resourceUid) throws IOException {
542 Config config = Utils.getConfig();
543 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifact.getUniqueId());
545 return updateDeploymentArtifact(artifact, sdncModifierDetails, url);
547 public static RestResponse updateDeploymentArtifactToResource(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceUid) throws IOException {
548 Config config = Utils.getConfig();
549 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactDetails.getUniqueId());
551 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
555 public static RestResponse updateDeploymentArtifactToRI(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
556 Config config = Utils.getConfig();
557 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_HEAT_ENV_PARAMS, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDetails.getUniqueId());
558 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
560 public static RestResponse updateDeploymentArtifactToRI(ArtifactDefinition artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
561 Config config = Utils.getConfig();
562 String url = String.format(Urls.UPDATE_RESOURCE_INSTANCE_HEAT_ENV_PARAMS, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceInstanceId, artifactDetails.getUniqueId());
563 return updateDeploymentArtifact(artifactDetails, sdncModifierDetails, url);
567 public static RestResponse deleteArtifactFromResourceInstance (ArtifactDefinition artifactDefinition, User sdncModifierDetails, String resourceUid, String serviceId) throws IOException{
568 Config config = Utils.getConfig();
569 String url = String.format(Urls.DELETE_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId, resourceUid, artifactDefinition.getUniqueId());
570 return sendDelete(url, sdncModifierDetails.getUserId());
573 public static RestResponse deleteInformationalArtifactFromResource(String resourceUid, ArtifactReqDetails artifactDetails, User sdncModifierDetails) throws IOException{
574 return deleteInformationalArtifactFromResource( resourceUid, artifactDetails.getUniqueId(), sdncModifierDetails);
577 public static RestResponse deleteInformationalArtifactFromResource( String resourceUid, String artifactId, User sdncModifierDetails) throws IOException{
578 Config config = Utils.getConfig();
579 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceUid, artifactId);
580 return sendDelete(url, sdncModifierDetails.getUserId());
583 public static RestResponse deleteServiceApiArtifact(ArtifactReqDetails artifactDetails, String serviceUniqueId, User user) throws Exception
585 Config config = Utils.getConfig();
586 String url = String.format(Urls.UPDATE_DELETE_SERVICE_API_ARTIFACT, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUniqueId, artifactDetails.getUniqueId());
587 RestResponse res = sendDelete(url, user.getUserId());
588 logger.debug("Deleting api artifact was finished with response: {}",res.getErrorCode());
589 logger.debug("Response body: {}",res.getResponseMessage());
593 //*************** RESOURCE INSTANCE **************
595 * Add DCAE artifacts to resource instance.
596 * @param artifactDetails
597 * @param sdncModifierDetails
598 * @param resourceInstanceId
601 * @throws IOException
603 public static RestResponse addArtifactToResourceInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String resourceInstanceId, String serviceId) throws IOException {
604 Config config = Utils.getConfig();
605 String url = String.format(Urls.ADD_RESOURCE_INSTANCE_ARTIFACT, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId,resourceInstanceId, artifactDetails.getUniqueId());
606 return addArtifactToInstance(artifactDetails, sdncModifierDetails, calculateChecksum(artifactDetails), url);
609 //*************** COMPONENT **************
611 public static RestResponse uploadDeploymentArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
612 Config config = Utils.getConfig();
613 Map<String, String> additionalHeaders = null;
614 String checksum = ResponseParser.calculateMD5Header(artifactDetails);
615 additionalHeaders = new HashMap<String, String>();
616 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
618 ComponentTypeEnum componentType = component.getComponentType();
622 switch (componentType){
626 url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), component.getUniqueId(), artifactDetails.getUniqueId());
641 assertTrue("failed on enum selection", false);
650 Gson gson = new Gson();
651 String jsonBody = gson.toJson(artifactDetails);
652 // System.out.println("ArtifactDetails: "+ jsonBody);
654 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
655 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
656 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
658 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
662 public static RestResponse uploadArtifact(ArtifactReqDetails artifactDetails, Component component, User sdncModifierDetails) throws IOException {
663 Config config = Utils.getConfig();
664 List<String> placeHolderlst = Utils.getListOfResPlaceHoldersDepArtTypes();
665 Map<String, String> additionalHeaders = null;
666 String checksum = null;
670 // if (artifactDetails.getArtifactGroupType() != null
671 // && artifactDetails.getArtifactGroupType().equals("DEPLOYMENT")
672 // && placeHolderlst.contains(artifactDetails.getArtifactType())) {
673 // Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
674 // ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactDetails.getArtifactLabel());
675 // artifactDetails.setUniqueId(artifactDefinition.getUniqueId());
676 // artifactDetails.setArtifactLabel(artifactDefinition.getArtifactLabel());
677 // checksum = ResponseParser.calculateMD5Header(artifactDetails);
678 // additionalHeaders = new HashMap<String, String>();
679 // additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
680 // url = String.format(Urls.UPDATE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
681 // config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
682 // component.getUniqueId(), artifactDetails.getUniqueId());
686 checksum = ResponseParser.calculateMD5Header(artifactDetails);
687 additionalHeaders = new HashMap<String, String>();
688 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
689 url = String.format(Urls.UPLOAD_DELETE_ARTIFACT_OF_COMPONENT, config.getCatalogBeHost(),
690 config.getCatalogBePort(), ComponentTypeEnum.findParamByType(component.getComponentType()),
691 component.getUniqueId(), artifactDetails.getUniqueId());
694 Gson gson = new Gson();
695 String jsonBody = gson.toJson(artifactDetails);
696 // System.out.println("ArtifactDetails: "+ jsonBody);
698 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
699 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
700 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
702 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
709 //*************** PRIVATE **************
710 private static RestResponse deleteInformationalArtifact(User sdncModifierDetails, String url) throws IOException {
711 Map<String, String> additionalHeaders = null;
713 additionalHeaders = new HashMap<String, String>();
716 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
717 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
719 return sendDelete(url, sdncModifierDetails.getUserId(), additionalHeaders);
721 // Gson gson = new Gson();
722 //// System.out.println("ArtifactDetails: "+ jsonBody);
723 // String jsonBody = gson.toJson(artifactDetails);
725 // RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
726 // if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
727 // artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
729 //// System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
733 private static RestResponse updateInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
734 return uploadInformationalArtifact(artifactDetails, sdncModifierDetails, checksum, url);
737 private static RestResponse uploadInformationalArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
738 Map<String, String> additionalHeaders = null;
739 if (checksum != null && !checksum.isEmpty()) {
740 additionalHeaders = new HashMap<String, String>();
741 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
744 additionalHeaders.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
745 additionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
747 Gson gson = new Gson();
748 // System.out.println("ArtifactDetails: "+ jsonBody);
749 String jsonBody = gson.toJson(artifactDetails);
751 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), acceptHeaderData, additionalHeaders);
752 if ((res.getErrorCode() == STATUS_CODE_SUCCESS) || (res.getErrorCode() == STATUS_CODE_CREATED)) {
753 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
755 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
759 private static RestResponse addArtifactToInstance(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String checksum, String url) throws IOException {
760 Map<String, String> additionalHeaders = null;
761 additionalHeaders = new HashMap<String, String>();
762 if (checksum != null && !checksum.isEmpty()) {
763 additionalHeaders = new HashMap<String, String>();
764 additionalHeaders.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
766 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
767 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
769 Gson gson = new Gson();
770 String jsonBody = gson.toJson(artifactDetails);
772 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
773 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
774 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
779 private static RestResponse updateDeploymentArtifact(ArtifactDefinition artifactDefinition, User sdncModifierDetails, String url) throws IOException {
780 Map<String, String> additionalHeaders = null;
781 additionalHeaders = new HashMap<String, String>();
782 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
783 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
785 Gson gson = new Gson();
786 String jsonBody = gson.toJson(artifactDefinition);
788 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
792 private static RestResponse updateDeploymentArtifact(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String url) throws IOException {
793 Map<String, String> additionalHeaders = null;
795 additionalHeaders = new HashMap<String, String>();
796 additionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json, text/plain, */*");
797 additionalHeaders.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
798 // additionalHeaders.put(HttpHeaderEnum..getValue(), "application/json;charset=UTF-8");
801 Gson gson = new Gson();
802 String jsonBody = gson.toJson(artifactDetails);
803 // System.out.println("ArtifactDetails: "+ jsonBody);
805 RestResponse res = sendPost(url, jsonBody, sdncModifierDetails.getUserId(), "application/json, text/plain, */*", additionalHeaders);
806 if (res.getErrorCode() == STATUS_CODE_SUCCESS) {
807 artifactDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
809 // System.out.println("Add artifact was finished with response: "+ res.getErrorCode());
813 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders,boolean addEcompHeader) throws IOException
816 addionalHeaders.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), ecomp);
818 return downloadArtifact(url, user, addionalHeaders, acceptOctetStream);
821 private static RestResponse downloadArtifact(String url, User user, Map<String, String> addionalHeaders, String accept) throws IOException
823 addionalHeaders.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
825 RestResponse res = sendGet(url, user.getUserId(), addionalHeaders);
826 // System.out.println("download artifact was finished with response: "+ res.getErrorCode());
827 // System.out.println("response is: " + res.getResponse());
831 private static Map<String,Map<String,Object>> getArtifactsListFromResponse(String jsonResponse, String fieldOfArtifactList){
832 JSONObject object = (JSONObject)JSONValue.parse(jsonResponse);
833 Map<String,Map<String,Object>> map = (Map<String,Map<String,Object>>)object.get(fieldOfArtifactList);
837 public static String calculateChecksum(ArtifactReqDetails artifactDetails) {
838 String checksum = null;
839 if (artifactDetails.getPayload() != null) {
840 checksum = ResponseParser.calculateMD5Header(artifactDetails);
845 public static String encodeUrlForDownload(String url){
847 return url.replaceAll(" ", "%20");
850 public static String getPartialUrlByArtifactName(ServiceReqDetails serviceDetails,String serviceVersion ,String artifactName){
851 return encodeUrlForDownload(String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceVersion, artifactName));
854 public static String getUniqueIdOfArtifact(RestResponse createResponse, String artifactField, String requieredArtifactLabel) throws Exception
856 Map<String, Object> artifact = getArtifactFromRestResponse(createResponse, artifactField, requieredArtifactLabel);
857 assertNotNull(artifact);
858 return artifact.get("uniqueId").toString();
861 public static Map<String, Object> getArtifactFromRestResponse(RestResponse response, String artifactField, String requieredArtifactLabel)
863 Map<String, Map<String, Object>> map = getArtifactsListFromResponse(response.getResponse(), artifactField);
864 return map.get(requieredArtifactLabel);
869 public static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid) throws IOException
871 return updateInformationalArtifactPayloadOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, calculateMD5Header(artifactDetails));
874 private static RestResponse updateInformationalArtifactPayloadOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum) throws IOException
876 return updateInformationalArtifactOfService(artifactDetails, sdncModifierDetails, serviceUid, artifactUid, checksum, true);
879 private static RestResponse updateInformationalArtifactOfService(ArtifactReqDetails artifactDetails, User sdncModifierDetails, String serviceUid, String artifactUid, String checksum, boolean isPayloadUpdate) throws IOException
881 Config config = Utils.getConfig();
882 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
884 if (isPayloadUpdate){
885 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), checksum);
888 Gson gson = new Gson();
889 String jsonBody = gson.toJson(artifactDetails);
891 HttpRequest http = new HttpRequest();
893 String url = String.format(Urls.UPDATE_OR_DELETE_ARTIFACT_OF_SERVICE, config.getCatalogBeHost(),config.getCatalogBePort(), serviceUid, artifactUid);
894 RestResponse res = http.httpSendPost(url, jsonBody, headersMap);
895 // System.out.println("update artifact was finished with response: "+ res.getErrorCode());
899 public static String calculateMD5Header(ArtifactReqDetails artifactDetails)
901 Gson gson = new Gson();
902 String jsonBody = gson.toJson(artifactDetails);
903 // calculate MD5 for json body
904 return calculateMD5(jsonBody);
908 public static String calculateMD5 (String data){
909 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
910 // encode base-64 result
911 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
912 String encodeBase64Str = new String(encodeBase64);
913 return encodeBase64Str;