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 com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import org.apache.http.client.ClientProtocolException;
28 import org.json.JSONException;
29 import org.json.simple.JSONArray;
30 import org.json.simple.JSONObject;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.model.*;
33 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
34 import org.openecomp.sdc.ci.tests.api.Urls;
35 import org.openecomp.sdc.ci.tests.config.Config;
36 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
37 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
38 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
39 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
40 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
41 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
42 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
43 import org.openecomp.sdc.ci.tests.utils.Utils;
44 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
45 import org.openecomp.sdc.common.util.GeneralUtility;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import java.io.FileNotFoundException;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
56 import static org.testng.AssertJUnit.assertEquals;
58 public class ResourceRestUtils extends BaseRestUtils {
59 private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());
61 // ****** CREATE *******
63 public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
66 Config config = Utils.getConfig();
67 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
69 String userId = sdncModifierDetails.getUserId();
71 Map<String, String> headersMap = prepareHeadersMap(userId);
73 Gson gson = new Gson();
74 String userBodyJson = gson.toJson(resourceDetails);
75 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
76 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
77 HttpRequest http = new HttpRequest();
78 RestResponse createResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
79 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
80 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(createResourceResponse));
81 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
82 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
83 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
85 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
86 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
87 "lastUpdaterFullName");
88 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
89 // Creator details never change after component is created - Ella,
91 resourceDetails.setCreatorUserId(userId);
92 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
94 return createResourceResponse;
98 public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
99 Map<String, String> additionalHeaders) throws JSONException, IOException {
101 Config config = Utils.getConfig();
102 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
103 String userId = sdncModifierDetails.getUserId();
105 Gson gson = new Gson();
106 String resourceImportBodyJson = gson.toJson(importReqDetails);
107 HttpRequest http = new HttpRequest();
109 Map<String, String> headersMap = prepareHeadersMap(userId);
110 if (additionalHeaders != null) {
111 headersMap.putAll(additionalHeaders);
113 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
116 RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
117 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
118 importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
119 importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
120 // Creator details never change after component is created - Ella,
122 importReqDetails.setCreatorUserId(userId);
123 importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
125 .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
126 importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
128 return createResourceResponse;
133 public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
134 String version) throws IOException {
136 if (resourceDetails.getUniqueId() != null) {
137 Config config = Utils.getConfig();
138 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
139 config.getCatalogBePort(), resourceDetails.getName(), version);
140 return sendDelete(url, sdncModifierDetails.getUserId());
147 public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {
149 Config config = Utils.getConfig();
150 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
152 RestResponse sendDelete = sendDelete(url, userId);
158 public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
160 Config config = Utils.getConfig();
161 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
163 RestResponse sendDelete = sendDelete(url, userId);
165 deleteMarkedResources(userId);
171 public static void deleteMarkedResources(String userId) throws IOException {
173 Config config = Utils.getConfig();
174 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
175 sendDelete(url, userId);
178 public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
179 String resourceVersion) throws IOException {
180 Config config = Utils.getConfig();
181 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
182 config.getCatalogBePort(), resourceName, resourceVersion);
183 RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());
185 deleteMarkedResources(sdncModifierDetails.getUserId());
190 public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
192 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
193 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
194 return checkErrorCode(deleteResponse);
197 public static Boolean removeResource(String resourceId)
198 throws FileNotFoundException, IOException, ClientProtocolException {
199 RestResponse response = deleteResource(resourceId,
200 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
201 return checkErrorCode(response);
204 // ************** GET *************
205 public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
207 Config config = Utils.getConfig();
208 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
209 return sendGet(url, sdncModifierDetails.getUserId());
212 public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
214 Config config = Utils.getConfig();
215 String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
216 componentId, moduleId);
217 return sendGet(url, sdncModifierDetails.getUserId());
220 public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
223 Config config = Utils.getConfig();
224 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
226 return sendGet(url, sdncModifierDetails.getUserId());
229 public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
232 Config config = Utils.getConfig();
233 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
234 resourceDetails.getUniqueId());
235 return sendGet(url, sdncModifierDetails.getUserId());
238 public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
241 Config config = Utils.getConfig();
242 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
243 config.getCatalogBePort(), resourceName, resourceVersion);
245 return sendGet(url, userId);
248 public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
250 Config config = Utils.getConfig();
251 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
253 return sendGet(url, sdncModifierDetails.getUserId());
257 public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
259 Config config = Utils.getConfig();
260 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
262 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
263 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
264 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
266 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
270 public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {
272 Config config = Utils.getConfig();
273 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
275 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
276 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
277 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
279 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
283 public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
285 Config config = Utils.getConfig();
286 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
288 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
289 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
290 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
292 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
296 public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
297 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
300 public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
301 throws ClientProtocolException, IOException {
302 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
305 public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
307 Config config = Utils.getConfig();
308 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
309 config.getCatalogBePort());
311 return sendGet(url, sdncModifierDetails.getUserId());
315 public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
317 Config config = Utils.getConfig();
318 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
319 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
321 Map<String, String> headersMap = new HashMap<String, String>();
322 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
323 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
324 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
325 HttpRequest http = new HttpRequest();
327 logger.debug("Send PUT request to get all categories (should be 405): {}", url);
328 return http.httpSendByMethod(url, "PUT", null, headersMap);
331 public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
333 Config config = Utils.getConfig();
334 HttpRequest http = new HttpRequest();
335 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
337 Map<String, String> headersMap = new HashMap<String, String>();
338 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
339 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
341 return http.httpSendGet(url, headersMap);
345 public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
347 Config config = Utils.getConfig();
348 HttpRequest http = new HttpRequest();
349 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
351 Map<String, String> headersMap = new HashMap<>();
352 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
353 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
354 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
356 return http.httpSendGet(url, headersMap);
360 public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
362 Config config = Utils.getConfig();
363 HttpRequest http = new HttpRequest();
364 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
366 Map<String, String> headersMap = new HashMap<>();
368 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
369 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
370 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
372 return http.httpSendGet(url, headersMap);
376 public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
378 Config config = Utils.getConfig();
379 HttpRequest http = new HttpRequest();
380 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
382 Map<String, String> headersMap = new HashMap<>();
383 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
384 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
385 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
387 return http.httpSendGet(url, headersMap);
391 public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
393 Config config = Utils.getConfig();
394 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
395 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
397 Map<String, String> headersMap = new HashMap<String, String>();
398 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
399 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
400 HttpRequest http = new HttpRequest();
402 logger.debug("Send OPTIONS request for categories: {}", url);
403 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
406 // ********** UPDATE *************
407 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
408 User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
409 Config config = Utils.getConfig();
410 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
413 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
415 Gson gson = new Gson();
416 String userBodyJson = gson.toJson(updatedResourceDetails);
417 String userId = sdncModifierDetails.getUserId();
419 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
421 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
422 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
424 return updateResourceResponse;
427 public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
428 String encoding) throws Exception {
429 Config config = Utils.getConfig();
430 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
433 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
435 Gson gson = new Gson();
436 String userBodyJson = gson.toJson(resource);
437 String userId = sdncModifierDetails.getUserId();
439 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
441 // String resourceUniqueId =
442 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
444 // updatedResourceDetails.setUniqueId(resourceUniqueId);
445 // String resourceVersion =
446 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
448 // updatedResourceDetails.setUniqueId(resourceVersion);
450 return updateResourceResponse;
453 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
454 User sdncModifierDetails, String uniqueId) throws Exception {
455 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
458 public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
460 Config config = Utils.getConfig();
461 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
463 String userId = sdncModifierDetails.getUserId();
465 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
467 return updateResourceResponse;
470 public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
471 String resourceId) throws IOException {
473 String userId = sdncModifierDetails.getUserId();
474 Config config = Utils.getConfig();
475 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
478 Map<String, String> headersMap = prepareHeadersMap(userId);
480 Gson gson = new Gson();
481 String userBodyJson = gson.toJson(resourceDetails);
482 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
483 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
484 HttpRequest http = new HttpRequest();
485 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
486 if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
487 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
488 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
489 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
490 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
491 "lastUpdaterUserId");
492 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
493 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
494 "lastUpdaterFullName");
495 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
496 resourceDetails.setCreatorUserId(userId);
497 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
499 return updateResourceResponse;
502 public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
503 String vfResourceUniqueId) throws Exception {
504 ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
505 .getComponentResourceInstance(resourceDetails);
506 RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
507 resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
508 ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
509 return createResourceInstanceResponse;
512 public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
513 Component component) throws IOException {
515 Config config = Utils.getConfig();
516 Gson gson = new Gson();
517 String bodyJson = gson.toJson(body);
518 component.getComponentType();
519 String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
520 String url = String.format(Urls.ASSOCIATE__RESOURCE_INSTANCE, config.getCatalogBeHost(),
521 config.getCatalogBePort(), componentType, component.getUniqueId());
522 return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);
526 public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
527 Config config = Utils.getConfig();
528 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
529 return sendGet(url, sdncModifierDetails.getUserId());
532 public static List<Resource> restResponseToResourceObjectList(String restResponse) {
533 JsonElement jelement = new JsonParser().parse(restResponse);
534 JsonArray jsonArray = jelement.getAsJsonArray();
535 List<Resource> restResponseArray = new ArrayList<>();
536 Resource resource = null;
537 for (int i = 0; i < jsonArray.size(); i++) {
538 String resourceString = (String) jsonArray.get(i).toString();
539 resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
540 restResponseArray.add(resource);
543 return restResponseArray;
547 public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
548 if (resourceList != null && resourceList.size() > 0) {
549 for (Resource resource : resourceList) {
550 if (resource.getUniqueId().equals(uid))
558 // =======================================resource
559 // associate==================================================
560 public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
561 ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
562 return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
566 public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
567 String assocType, User sdncUserDetails) throws IOException {
569 RelationshipInstData relationshipInstData = new RelationshipInstData();
570 Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
571 Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
572 List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
573 List<RequirementDefinition> requirementList = requirementMap.get(assocType);
575 RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
576 CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
577 relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
578 relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
579 relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
580 relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());
582 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
584 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
588 private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
589 CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
592 String type = capabilityDefinitionTo.getType();
593 String requirement = requirementDefinitionFrom.getName();
594 String capability = requirementDefinitionFrom.getName();
596 JSONObject wrapper = new JSONObject();
597 JSONArray relationshipsArray = new JSONArray();
598 JSONObject relationship = new JSONObject();
599 JSONObject simpleObject = new JSONObject();
601 relationship.put("type", type);
602 simpleObject.put("relationship", relationship);
603 simpleObject.put("requirement", requirement);
604 simpleObject.put("capability", capability);
605 simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
606 simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
607 simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
608 simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
609 relationshipsArray.add(simpleObject);
611 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
612 wrapper.put("fromNode", fromNode);
613 wrapper.put("toNode", toNode);
614 wrapper.put("relationships", relationships);
619 private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
620 List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
622 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
623 if (capabilityDefinition.getOwnerId().equals(ownerId)) {
624 return capabilityDefinition;
630 private static RequirementDefinition getRequirementDefinitionByOwnerId(
631 List<RequirementDefinition> requirementDefinitionList, String ownerId) {
633 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
634 if (requirementDefinition.getOwnerId().equals(ownerId)) {
635 return requirementDefinition;
641 public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
643 List<ComponentInstance> componentInstances = component.getComponentInstances();
645 for (ComponentInstance componentInstance : componentInstances) {
646 if (componentInstance.getName().equals(resourceInstanceName)) {
647 name = componentInstance.getUniqueId();
654 public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
656 RestResponse response = ResourceRestUtils.getResource(resourceDetails,
657 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
658 assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
659 return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
662 public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
663 String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
664 return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
665 newResourceUniqueId, sdncModifierDetails, componentType);