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.assertEquals;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.http.client.ClientProtocolException;
38 import org.json.JSONException;
39 import org.json.simple.JSONArray;
40 import org.json.simple.JSONObject;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
43 import org.openecomp.sdc.be.model.CapabilityDefinition;
44 import org.openecomp.sdc.be.model.Component;
45 import org.openecomp.sdc.be.model.ComponentInstance;
46 import org.openecomp.sdc.be.model.RequirementDefinition;
47 import org.openecomp.sdc.be.model.Resource;
48 import org.openecomp.sdc.be.model.User;
49 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
50 import org.openecomp.sdc.ci.tests.api.Urls;
51 import org.openecomp.sdc.ci.tests.config.Config;
52 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
53 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
54 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
55 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
56 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
57 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
58 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
59 import org.openecomp.sdc.ci.tests.utils.Utils;
60 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
61 import org.openecomp.sdc.common.util.GeneralUtility;
63 import com.google.gson.Gson;
64 import com.google.gson.JsonArray;
65 import com.google.gson.JsonElement;
66 import com.google.gson.JsonParser;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
70 public class ResourceRestUtils extends BaseRestUtils {
72 private static final String CSARS_PATH = "/src/test/resources/CI/csars/";
73 private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());
75 // ****** CREATE *******
77 public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
80 Config config = Utils.getConfig();
81 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
83 String userId = sdncModifierDetails.getUserId();
85 Map<String, String> headersMap = prepareHeadersMap(userId);
87 Gson gson = new Gson();
88 String userBodyJson = gson.toJson(resourceDetails);
89 String calculateMD5 = GeneralUtility.calculateMD5ByString(userBodyJson);
90 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
91 HttpRequest http = new HttpRequest();
92 // System.out.println(url);
93 // System.out.println(userBodyJson);
94 RestResponse createResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
95 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
96 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(createResourceResponse));
97 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
98 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
99 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
100 "lastUpdaterUserId");
101 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
102 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
103 "lastUpdaterFullName");
104 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
105 // Creator details never change after component is created - Ella,
107 resourceDetails.setCreatorUserId(userId);
108 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
110 return createResourceResponse;
114 public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
115 Map<String, String> additionalHeaders) throws JSONException, IOException {
117 Config config = Utils.getConfig();
118 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
119 String userId = sdncModifierDetails.getUserId();
121 Gson gson = new Gson();
122 String resourceImportBodyJson = gson.toJson(importReqDetails);
123 HttpRequest http = new HttpRequest();
124 // System.out.println(url);
125 // System.out.println(resourceImportBodyJson);
127 Map<String, String> headersMap = prepareHeadersMap(userId);
128 if (additionalHeaders != null) {
129 headersMap.putAll(additionalHeaders);
131 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(),
132 ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
135 RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
136 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
137 importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
138 importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
139 // Creator details never change after component is created - Ella,
141 importReqDetails.setCreatorUserId(userId);
142 importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
144 .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
145 importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
147 return createResourceResponse;
152 public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
153 String version) throws IOException {
155 if (resourceDetails.getUniqueId() != null) {
156 Config config = Utils.getConfig();
157 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
158 config.getCatalogBePort(), resourceDetails.getName(), version);
159 return sendDelete(url, sdncModifierDetails.getUserId());
166 public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {
168 Config config = Utils.getConfig();
169 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
171 RestResponse sendDelete = sendDelete(url, userId);
177 public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
179 Config config = Utils.getConfig();
180 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
182 RestResponse sendDelete = sendDelete(url, userId);
184 deleteMarkedResources(userId);
190 public static void deleteMarkedResources(String userId) throws IOException {
192 Config config = Utils.getConfig();
193 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
194 sendDelete(url, userId);
197 public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
198 String resourceVersion) throws IOException {
199 Config config = Utils.getConfig();
200 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
201 config.getCatalogBePort(), resourceName, resourceVersion);
202 RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());
204 deleteMarkedResources(sdncModifierDetails.getUserId());
209 public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
211 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
212 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
213 return checkErrorCode(deleteResponse);
216 public static Boolean removeResource(String resourceId)
217 throws FileNotFoundException, IOException, ClientProtocolException {
218 RestResponse response = deleteResource(resourceId,
219 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
220 return checkErrorCode(response);
223 // ************** GET *************
224 public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
226 Config config = Utils.getConfig();
227 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
228 return sendGet(url, sdncModifierDetails.getUserId());
231 public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
233 Config config = Utils.getConfig();
234 String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
235 componentId, moduleId);
236 return sendGet(url, sdncModifierDetails.getUserId());
239 public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
242 Config config = Utils.getConfig();
243 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
245 return sendGet(url, sdncModifierDetails.getUserId());
248 public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
251 Config config = Utils.getConfig();
252 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
253 resourceDetails.getUniqueId());
254 return sendGet(url, sdncModifierDetails.getUserId());
257 public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
260 Config config = Utils.getConfig();
261 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
262 config.getCatalogBePort(), resourceName, resourceVersion);
264 return sendGet(url, userId);
267 public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
269 Config config = Utils.getConfig();
270 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
272 return sendGet(url, sdncModifierDetails.getUserId());
276 public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
278 Config config = Utils.getConfig();
279 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
281 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
282 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
283 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
285 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
289 public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
291 Config config = Utils.getConfig();
292 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
294 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
295 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
296 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
298 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
302 public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
303 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
306 public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
307 throws ClientProtocolException, IOException {
308 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
311 public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
313 Config config = Utils.getConfig();
314 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
315 config.getCatalogBePort());
317 return sendGet(url, sdncModifierDetails.getUserId());
321 public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
323 Config config = Utils.getConfig();
324 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
325 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
327 Map<String, String> headersMap = new HashMap<String, String>();
328 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
329 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
330 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
331 HttpRequest http = new HttpRequest();
333 logger.debug("Send PUT request to get all categories (should be 405): {}",url);
334 return http.httpSendByMethod(url, "PUT", null, headersMap);
337 public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
339 Config config = Utils.getConfig();
340 HttpRequest http = new HttpRequest();
341 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
343 Map<String, String> headersMap = new HashMap<String, String>();
344 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
345 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
347 return http.httpSendGet(url, headersMap);
351 public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
353 Config config = Utils.getConfig();
354 HttpRequest http = new HttpRequest();
355 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
357 Map<String, String> headersMap = new HashMap<String, String>();
358 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
359 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
360 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
362 return http.httpSendGet(url, headersMap);
365 public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
367 Config config = Utils.getConfig();
368 HttpRequest http = new HttpRequest();
369 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
371 Map<String, String> headersMap = new HashMap<String, String>();
373 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
374 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
375 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
377 return http.httpSendGet(url, headersMap);
381 public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
383 Config config = Utils.getConfig();
384 HttpRequest http = new HttpRequest();
385 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
387 Map<String, String> headersMap = new HashMap<String, String>();
388 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
389 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
390 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
392 return http.httpSendGet(url, headersMap);
396 public static RestResponse getResourceFilteredDataByParams(User sdncModifierDetails, String uniqueId , List<String> parameters) throws IOException {
397 Config config = Utils.getConfig();
398 String urlGetResourceDataByParams = Urls.GET_RESOURCE_DATA_BY_PARAMS;
399 String joinedParameters = StringUtils.join(parameters , "&");
400 String url = String.format(urlGetResourceDataByParams + joinedParameters , config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
401 return sendGet(url, sdncModifierDetails.getUserId());
406 public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
408 Config config = Utils.getConfig();
409 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
410 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
412 Map<String, String> headersMap = new HashMap<String, String>();
413 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
414 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
415 HttpRequest http = new HttpRequest();
417 logger.debug("Send OPTIONS request for categories: {}",url);
418 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
421 // ********** UPDATE *************
422 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
423 User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
424 Config config = Utils.getConfig();
425 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
428 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
430 Gson gson = new Gson();
431 String userBodyJson = gson.toJson(updatedResourceDetails);
432 String userId = sdncModifierDetails.getUserId();
434 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
436 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
437 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
439 return updateResourceResponse;
442 public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
443 String encoding) throws Exception {
444 Config config = Utils.getConfig();
445 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
448 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
450 Gson gson = new Gson();
451 String userBodyJson = gson.toJson(resource);
452 String userId = sdncModifierDetails.getUserId();
454 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
456 // String resourceUniqueId =
457 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
459 // updatedResourceDetails.setUniqueId(resourceUniqueId);
460 // String resourceVersion =
461 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
463 // updatedResourceDetails.setUniqueId(resourceVersion);
465 return updateResourceResponse;
468 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
469 User sdncModifierDetails, String uniqueId) throws Exception {
470 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
473 public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
475 Config config = Utils.getConfig();
476 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
478 String userId = sdncModifierDetails.getUserId();
480 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
482 return updateResourceResponse;
485 public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
486 String resourceId) throws IOException {
488 String userId = sdncModifierDetails.getUserId();
489 Config config = Utils.getConfig();
490 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
493 Map<String, String> headersMap = prepareHeadersMap(userId);
495 Gson gson = new Gson();
496 String userBodyJson = gson.toJson(resourceDetails);
497 String calculateMD5 = GeneralUtility.calculateMD5ByString(userBodyJson);
498 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
499 HttpRequest http = new HttpRequest();
500 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
501 if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
502 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
503 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
504 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
505 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
506 "lastUpdaterUserId");
507 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
508 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
509 "lastUpdaterFullName");
510 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
511 resourceDetails.setCreatorUserId(userId);
512 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
514 return updateResourceResponse;
517 public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
518 String vfResourceUniqueId) throws Exception {
519 ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
520 .getComponentResourceInstance(resourceDetails);
521 RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
522 resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
523 ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
524 return createResourceInstanceResponse;
527 public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
528 Component component) throws IOException {
530 Config config = Utils.getConfig();
531 Gson gson = new Gson();
532 String bodyJson = gson.toJson(body);
533 component.getComponentType();
534 String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
535 String url = String.format(Urls.ASSOCIATE_RESOURCE_INSTANCE, config.getCatalogBeHost(),
536 config.getCatalogBePort(), componentType, component.getUniqueId());
537 return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);
541 public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
542 Config config = Utils.getConfig();
543 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
544 return sendGet(url, sdncModifierDetails.getUserId());
547 public static List<Resource> restResponseToResourceObjectList(String restResponse) {
548 JsonElement jelement = new JsonParser().parse(restResponse);
549 JsonArray jsonArray = jelement.getAsJsonArray();
550 List<Resource> restResponseArray = new ArrayList<>();
551 Resource resource = null;
552 for (int i = 0; i < jsonArray.size(); i++) {
553 String resourceString = (String) jsonArray.get(i).toString();
554 resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
555 restResponseArray.add(resource);
558 return restResponseArray;
562 public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
563 if (resourceList != null && resourceList.size() > 0) {
564 for (Resource resource : resourceList) {
565 if (resource.getUniqueId().equals(uid))
573 // =======================================resource
574 // associate==================================================
575 public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
576 ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
577 return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
581 public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
582 String assocType, User sdncUserDetails) throws IOException {
584 RelationshipInstData relationshipInstData = new RelationshipInstData();
585 Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
586 Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
587 List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
588 List<RequirementDefinition> requirementList = requirementMap.get(assocType);
590 RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
591 CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
592 relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
593 relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
594 relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
595 relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());
597 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
599 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
603 private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
604 CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
607 String type = capabilityDefinitionTo.getType();
608 String requirement = requirementDefinitionFrom.getName();
609 String capability = requirementDefinitionFrom.getName();
611 JSONObject wrapper = new JSONObject();
612 JSONArray relationshipsArray = new JSONArray();
613 JSONObject relationship = new JSONObject();
614 JSONObject simpleObject = new JSONObject();
616 relationship.put("type", type);
617 simpleObject.put("relationship", relationship);
618 simpleObject.put("requirement", requirement);
619 simpleObject.put("capability", capability);
620 simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
621 simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
622 simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
623 simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
624 relationshipsArray.add(simpleObject);
626 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
627 wrapper.put("fromNode", fromNode);
628 wrapper.put("toNode", toNode);
629 wrapper.put("relationships", relationships);
634 private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
635 List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
637 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
638 if (capabilityDefinition.getOwnerId().equals(ownerId)) {
639 return capabilityDefinition;
645 private static RequirementDefinition getRequirementDefinitionByOwnerId(
646 List<RequirementDefinition> requirementDefinitionList, String ownerId) {
648 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
649 if (requirementDefinition.getOwnerId().equals(ownerId)) {
650 return requirementDefinition;
656 public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
658 List<ComponentInstance> componentInstances = component.getComponentInstances();
660 for (ComponentInstance componentInstance : componentInstances) {
661 if (componentInstance.getName().equals(resourceInstanceName)) {
662 name = componentInstance.getUniqueId();
669 public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
671 RestResponse response = ResourceRestUtils.getResource(resourceDetails,
672 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
673 assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
674 return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
677 public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
678 String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
679 return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
680 newResourceUniqueId, sdncModifierDetails, componentType);
683 public static Resource importResourceFromCsar(String csarName) throws Exception{
684 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
685 String payloadName = csarName;
686 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
687 String rootPath = System.getProperty("user.dir");
691 String payloadData = null;
693 path = Paths.get(rootPath + CSARS_PATH + csarName);
694 data = Files.readAllBytes(path);
695 payloadData = Base64.encodeBase64String(data);
696 resourceDetails.setPayloadData(payloadData);
698 // create new resource from Csar
699 resourceDetails.setCsarUUID(payloadName);
700 resourceDetails.setPayloadName(payloadName);
701 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
702 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
703 BaseRestUtils.checkCreateResponse(createResource);
704 Resource resource = ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
707 // add to restResourceUtil