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.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
32 import org.apache.http.client.ClientProtocolException;
33 import org.json.JSONException;
34 import org.json.simple.JSONArray;
35 import org.json.simple.JSONObject;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.model.CapabilityDefinition;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.ComponentInstance;
40 import org.openecomp.sdc.be.model.RequirementDefinition;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
44 import org.openecomp.sdc.ci.tests.api.Urls;
45 import org.openecomp.sdc.ci.tests.config.Config;
46 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
47 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
48 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
52 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
53 import org.openecomp.sdc.ci.tests.utils.Utils;
54 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
55 import org.openecomp.sdc.common.util.GeneralUtility;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 import com.google.gson.Gson;
60 import com.google.gson.JsonArray;
61 import com.google.gson.JsonElement;
62 import com.google.gson.JsonParser;
64 public class ResourceRestUtils extends BaseRestUtils {
65 private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());
67 // ****** CREATE *******
69 public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
72 Config config = Utils.getConfig();
73 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
75 String userId = sdncModifierDetails.getUserId();
77 Map<String, String> headersMap = prepareHeadersMap(userId);
79 Gson gson = new Gson();
80 String userBodyJson = gson.toJson(resourceDetails);
81 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
82 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
83 HttpRequest http = new HttpRequest();
84 // System.out.println(url);
85 // System.out.println(userBodyJson);
86 RestResponse createResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
87 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
88 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(createResourceResponse));
89 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
90 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
91 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
93 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
94 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
95 "lastUpdaterFullName");
96 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
97 // Creator details never change after component is created - Ella,
99 resourceDetails.setCreatorUserId(userId);
100 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
102 return createResourceResponse;
106 public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
107 Map<String, String> additionalHeaders) throws JSONException, IOException {
109 Config config = Utils.getConfig();
110 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
111 String userId = sdncModifierDetails.getUserId();
113 Gson gson = new Gson();
114 String resourceImportBodyJson = gson.toJson(importReqDetails);
115 HttpRequest http = new HttpRequest();
117 Map<String, String> headersMap = prepareHeadersMap(userId);
118 if (additionalHeaders != null) {
119 headersMap.putAll(additionalHeaders);
121 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
124 RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
125 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
126 importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
127 importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
128 // Creator details never change after component is created - Ella,
130 importReqDetails.setCreatorUserId(userId);
131 importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
133 .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
134 importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
136 return createResourceResponse;
141 public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
142 String version) throws IOException {
144 if (resourceDetails.getUniqueId() != null) {
145 Config config = Utils.getConfig();
146 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
147 config.getCatalogBePort(), resourceDetails.getName(), version);
148 return sendDelete(url, sdncModifierDetails.getUserId());
155 public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {
157 Config config = Utils.getConfig();
158 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
160 RestResponse sendDelete = sendDelete(url, userId);
166 public static RestResponse deleteResource(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);
173 deleteMarkedResources(userId);
179 public static void deleteMarkedResources(String userId) throws IOException {
181 Config config = Utils.getConfig();
182 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
183 sendDelete(url, userId);
186 public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
187 String resourceVersion) throws IOException {
188 Config config = Utils.getConfig();
189 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
190 config.getCatalogBePort(), resourceName, resourceVersion);
191 RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());
193 deleteMarkedResources(sdncModifierDetails.getUserId());
198 public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
200 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
201 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
202 return checkErrorCode(deleteResponse);
205 public static Boolean removeResource(String resourceId)
206 throws FileNotFoundException, IOException, ClientProtocolException {
207 RestResponse response = deleteResource(resourceId,
208 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
209 return checkErrorCode(response);
212 // ************** GET *************
213 public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
215 Config config = Utils.getConfig();
216 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
217 return sendGet(url, sdncModifierDetails.getUserId());
220 public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
222 Config config = Utils.getConfig();
223 String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
224 componentId, moduleId);
225 return sendGet(url, sdncModifierDetails.getUserId());
228 public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
231 Config config = Utils.getConfig();
232 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
234 return sendGet(url, sdncModifierDetails.getUserId());
237 public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
240 Config config = Utils.getConfig();
241 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
242 resourceDetails.getUniqueId());
243 return sendGet(url, sdncModifierDetails.getUserId());
246 public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
249 Config config = Utils.getConfig();
250 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
251 config.getCatalogBePort(), resourceName, resourceVersion);
253 return sendGet(url, userId);
256 public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
258 Config config = Utils.getConfig();
259 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
261 return sendGet(url, sdncModifierDetails.getUserId());
265 public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
267 Config config = Utils.getConfig();
268 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
270 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
271 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
272 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
274 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
278 public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {
280 Config config = Utils.getConfig();
281 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
283 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
284 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
285 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
287 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
291 public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
293 Config config = Utils.getConfig();
294 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
296 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
297 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
298 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
300 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
304 public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
305 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
308 public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
309 throws ClientProtocolException, IOException {
310 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
313 public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
315 Config config = Utils.getConfig();
316 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
317 config.getCatalogBePort());
319 return sendGet(url, sdncModifierDetails.getUserId());
323 public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
325 Config config = Utils.getConfig();
326 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
327 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
329 Map<String, String> headersMap = new HashMap<String, String>();
330 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
331 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
332 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
333 HttpRequest http = new HttpRequest();
335 logger.debug("Send PUT request to get all categories (should be 405): {}", url);
336 return http.httpSendByMethod(url, "PUT", null, headersMap);
339 public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
341 Config config = Utils.getConfig();
342 HttpRequest http = new HttpRequest();
343 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
345 Map<String, String> headersMap = new HashMap<String, String>();
346 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
347 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
349 return http.httpSendGet(url, headersMap);
353 public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
355 Config config = Utils.getConfig();
356 HttpRequest http = new HttpRequest();
357 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
359 Map<String, String> headersMap = new HashMap<>();
360 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
361 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
362 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
364 return http.httpSendGet(url, headersMap);
368 public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
370 Config config = Utils.getConfig();
371 HttpRequest http = new HttpRequest();
372 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
374 Map<String, String> headersMap = new HashMap<>();
376 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
377 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
378 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
380 return http.httpSendGet(url, headersMap);
384 public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
386 Config config = Utils.getConfig();
387 HttpRequest http = new HttpRequest();
388 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
390 Map<String, String> headersMap = new HashMap<>();
391 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
392 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
393 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
395 return http.httpSendGet(url, headersMap);
399 public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
401 Config config = Utils.getConfig();
402 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
403 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
405 Map<String, String> headersMap = new HashMap<String, String>();
406 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
407 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
408 HttpRequest http = new HttpRequest();
410 logger.debug("Send OPTIONS request for categories: {}", url);
411 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
414 // ********** UPDATE *************
415 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
416 User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
417 Config config = Utils.getConfig();
418 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
421 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
423 Gson gson = new Gson();
424 String userBodyJson = gson.toJson(updatedResourceDetails);
425 String userId = sdncModifierDetails.getUserId();
427 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
429 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
430 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
432 return updateResourceResponse;
435 public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
436 String encoding) throws Exception {
437 Config config = Utils.getConfig();
438 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
441 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
443 Gson gson = new Gson();
444 String userBodyJson = gson.toJson(resource);
445 String userId = sdncModifierDetails.getUserId();
447 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
449 // String resourceUniqueId =
450 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
452 // updatedResourceDetails.setUniqueId(resourceUniqueId);
453 // String resourceVersion =
454 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
456 // updatedResourceDetails.setUniqueId(resourceVersion);
458 return updateResourceResponse;
461 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
462 User sdncModifierDetails, String uniqueId) throws Exception {
463 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
466 public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
468 Config config = Utils.getConfig();
469 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
471 String userId = sdncModifierDetails.getUserId();
473 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
475 return updateResourceResponse;
478 public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
479 String resourceId) throws IOException {
481 String userId = sdncModifierDetails.getUserId();
482 Config config = Utils.getConfig();
483 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
486 Map<String, String> headersMap = prepareHeadersMap(userId);
488 Gson gson = new Gson();
489 String userBodyJson = gson.toJson(resourceDetails);
490 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
491 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
492 HttpRequest http = new HttpRequest();
493 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
494 if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
495 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
496 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
497 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
498 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
499 "lastUpdaterUserId");
500 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
501 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
502 "lastUpdaterFullName");
503 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
504 resourceDetails.setCreatorUserId(userId);
505 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
507 return updateResourceResponse;
510 public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
511 String vfResourceUniqueId) throws Exception {
512 ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
513 .getComponentResourceInstance(resourceDetails);
514 RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
515 resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
516 ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
517 return createResourceInstanceResponse;
520 public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
521 Component component) throws IOException {
523 Config config = Utils.getConfig();
524 Gson gson = new Gson();
525 String bodyJson = gson.toJson(body);
526 component.getComponentType();
527 String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
528 String url = String.format(Urls.ASSOCIATE__RESOURCE_INSTANCE, config.getCatalogBeHost(),
529 config.getCatalogBePort(), componentType, component.getUniqueId());
530 return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);
534 public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
535 Config config = Utils.getConfig();
536 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
537 return sendGet(url, sdncModifierDetails.getUserId());
540 public static List<Resource> restResponseToResourceObjectList(String restResponse) {
541 JsonElement jelement = new JsonParser().parse(restResponse);
542 JsonArray jsonArray = jelement.getAsJsonArray();
543 List<Resource> restResponseArray = new ArrayList<>();
544 Resource resource = null;
545 for (int i = 0; i < jsonArray.size(); i++) {
546 String resourceString = (String) jsonArray.get(i).toString();
547 resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
548 restResponseArray.add(resource);
551 return restResponseArray;
555 public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
556 if (resourceList != null && resourceList.size() > 0) {
557 for (Resource resource : resourceList) {
558 if (resource.getUniqueId().equals(uid))
566 // =======================================resource
567 // associate==================================================
568 public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
569 ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
570 return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
574 public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
575 String assocType, User sdncUserDetails) throws IOException {
577 RelationshipInstData relationshipInstData = new RelationshipInstData();
578 Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
579 Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
580 List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
581 List<RequirementDefinition> requirementList = requirementMap.get(assocType);
583 RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
584 CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
585 relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
586 relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
587 relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
588 relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());
590 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
592 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
596 private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
597 CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
600 String type = capabilityDefinitionTo.getType();
601 String requirement = requirementDefinitionFrom.getName();
602 String capability = requirementDefinitionFrom.getName();
604 JSONObject wrapper = new JSONObject();
605 JSONArray relationshipsArray = new JSONArray();
606 JSONObject relationship = new JSONObject();
607 JSONObject simpleObject = new JSONObject();
609 relationship.put("type", type);
610 simpleObject.put("relationship", relationship);
611 simpleObject.put("requirement", requirement);
612 simpleObject.put("capability", capability);
613 simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
614 simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
615 simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
616 simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
617 relationshipsArray.add(simpleObject);
619 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
620 wrapper.put("fromNode", fromNode);
621 wrapper.put("toNode", toNode);
622 wrapper.put("relationships", relationships);
627 private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
628 List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
630 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
631 if (capabilityDefinition.getOwnerId().equals(ownerId)) {
632 return capabilityDefinition;
638 private static RequirementDefinition getRequirementDefinitionByOwnerId(
639 List<RequirementDefinition> requirementDefinitionList, String ownerId) {
641 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
642 if (requirementDefinition.getOwnerId().equals(ownerId)) {
643 return requirementDefinition;
649 public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
651 List<ComponentInstance> componentInstances = component.getComponentInstances();
653 for (ComponentInstance componentInstance : componentInstances) {
654 if (componentInstance.getName().equals(resourceInstanceName)) {
655 name = componentInstance.getUniqueId();
662 public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
664 RestResponse response = ResourceRestUtils.getResource(resourceDetails,
665 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
666 assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
667 return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
670 public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
671 String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
672 return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
673 newResourceUniqueId, sdncModifierDetails, componentType);