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;
57 import com.google.gson.Gson;
58 import com.google.gson.JsonArray;
59 import com.google.gson.JsonElement;
60 import com.google.gson.JsonParser;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
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.calculateMD5ByString(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();
116 // System.out.println(url);
117 // System.out.println(resourceImportBodyJson);
119 Map<String, String> headersMap = prepareHeadersMap(userId);
120 if (additionalHeaders != null) {
121 headersMap.putAll(additionalHeaders);
123 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(),
124 ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
127 RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
128 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
129 importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
130 importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
131 // Creator details never change after component is created - Ella,
133 importReqDetails.setCreatorUserId(userId);
134 importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
136 .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
137 importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
139 return createResourceResponse;
144 public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
145 String version) throws IOException {
147 if (resourceDetails.getUniqueId() != null) {
148 Config config = Utils.getConfig();
149 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
150 config.getCatalogBePort(), resourceDetails.getName(), version);
151 return sendDelete(url, sdncModifierDetails.getUserId());
158 public static RestResponse markResourceToDelete(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);
169 public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
171 Config config = Utils.getConfig();
172 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
174 RestResponse sendDelete = sendDelete(url, userId);
176 deleteMarkedResources(userId);
182 public static void deleteMarkedResources(String userId) throws IOException {
184 Config config = Utils.getConfig();
185 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
186 sendDelete(url, userId);
189 public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
190 String resourceVersion) throws IOException {
191 Config config = Utils.getConfig();
192 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
193 config.getCatalogBePort(), resourceName, resourceVersion);
194 RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());
196 deleteMarkedResources(sdncModifierDetails.getUserId());
201 public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
203 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
204 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
205 return checkErrorCode(deleteResponse);
208 public static Boolean removeResource(String resourceId)
209 throws FileNotFoundException, IOException, ClientProtocolException {
210 RestResponse response = deleteResource(resourceId,
211 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
212 return checkErrorCode(response);
215 // ************** GET *************
216 public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
218 Config config = Utils.getConfig();
219 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
220 return sendGet(url, sdncModifierDetails.getUserId());
223 public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
225 Config config = Utils.getConfig();
226 String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
227 componentId, moduleId);
228 return sendGet(url, sdncModifierDetails.getUserId());
231 public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
234 Config config = Utils.getConfig();
235 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
237 return sendGet(url, sdncModifierDetails.getUserId());
240 public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
243 Config config = Utils.getConfig();
244 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
245 resourceDetails.getUniqueId());
246 return sendGet(url, sdncModifierDetails.getUserId());
249 public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
252 Config config = Utils.getConfig();
253 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
254 config.getCatalogBePort(), resourceName, resourceVersion);
256 return sendGet(url, userId);
259 public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
261 Config config = Utils.getConfig();
262 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
264 return sendGet(url, sdncModifierDetails.getUserId());
268 public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
270 Config config = Utils.getConfig();
271 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
273 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
274 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
275 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
277 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
281 public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {
283 Config config = Utils.getConfig();
284 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
286 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
287 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
288 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
290 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
294 public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
296 Config config = Utils.getConfig();
297 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
299 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
300 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
301 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
303 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
307 public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
308 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
311 public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
312 throws ClientProtocolException, IOException {
313 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
316 public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
318 Config config = Utils.getConfig();
319 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
320 config.getCatalogBePort());
322 return sendGet(url, sdncModifierDetails.getUserId());
326 public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
328 Config config = Utils.getConfig();
329 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
330 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
332 Map<String, String> headersMap = new HashMap<String, String>();
333 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
334 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
335 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
336 HttpRequest http = new HttpRequest();
338 logger.debug("Send PUT request to get all categories (should be 405): {}", url);
339 return http.httpSendByMethod(url, "PUT", null, headersMap);
342 public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
344 Config config = Utils.getConfig();
345 HttpRequest http = new HttpRequest();
346 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
348 Map<String, String> headersMap = new HashMap<String, String>();
349 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
350 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
352 return http.httpSendGet(url, headersMap);
356 public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
358 Config config = Utils.getConfig();
359 HttpRequest http = new HttpRequest();
360 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
362 Map<String, String> headersMap = new HashMap<String, String>();
363 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
364 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
365 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
367 return http.httpSendGet(url, headersMap);
371 public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
373 Config config = Utils.getConfig();
374 HttpRequest http = new HttpRequest();
375 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
377 Map<String, String> headersMap = new HashMap<String, String>();
379 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
380 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
381 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
383 return http.httpSendGet(url, headersMap);
387 public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
389 Config config = Utils.getConfig();
390 HttpRequest http = new HttpRequest();
391 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
393 Map<String, String> headersMap = new HashMap<String, String>();
394 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
395 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
396 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
398 return http.httpSendGet(url, headersMap);
402 public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
404 Config config = Utils.getConfig();
405 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
406 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
408 Map<String, String> headersMap = new HashMap<String, String>();
409 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
410 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
411 HttpRequest http = new HttpRequest();
413 logger.debug("Send OPTIONS request for categories: {}", url);
414 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
417 // ********** UPDATE *************
418 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
419 User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
420 Config config = Utils.getConfig();
421 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
424 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
426 Gson gson = new Gson();
427 String userBodyJson = gson.toJson(updatedResourceDetails);
428 String userId = sdncModifierDetails.getUserId();
430 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
432 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
433 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
435 return updateResourceResponse;
438 public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
439 String encoding) throws Exception {
440 Config config = Utils.getConfig();
441 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
444 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
446 Gson gson = new Gson();
447 String userBodyJson = gson.toJson(resource);
448 String userId = sdncModifierDetails.getUserId();
450 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
452 // String resourceUniqueId =
453 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
455 // updatedResourceDetails.setUniqueId(resourceUniqueId);
456 // String resourceVersion =
457 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
459 // updatedResourceDetails.setUniqueId(resourceVersion);
461 return updateResourceResponse;
464 public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
465 User sdncModifierDetails, String uniqueId) throws Exception {
466 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
469 public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
471 Config config = Utils.getConfig();
472 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
474 String userId = sdncModifierDetails.getUserId();
476 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
478 return updateResourceResponse;
481 public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
482 String resourceId) throws IOException {
484 String userId = sdncModifierDetails.getUserId();
485 Config config = Utils.getConfig();
486 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
489 Map<String, String> headersMap = prepareHeadersMap(userId);
491 Gson gson = new Gson();
492 String userBodyJson = gson.toJson(resourceDetails);
493 String calculateMD5 = GeneralUtility.calculateMD5ByString(userBodyJson);
494 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
495 HttpRequest http = new HttpRequest();
496 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
497 if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
498 resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
499 resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
500 resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
501 String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
502 "lastUpdaterUserId");
503 resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
504 String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
505 "lastUpdaterFullName");
506 resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
507 resourceDetails.setCreatorUserId(userId);
508 resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
510 return updateResourceResponse;
513 public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
514 String vfResourceUniqueId) throws Exception {
515 ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
516 .getComponentResourceInstance(resourceDetails);
517 RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
518 resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
519 ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
520 return createResourceInstanceResponse;
523 public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
524 Component component) throws IOException {
526 Config config = Utils.getConfig();
527 Gson gson = new Gson();
528 String bodyJson = gson.toJson(body);
529 component.getComponentType();
530 String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
531 String url = String.format(Urls.ASSOCIATE__RESOURCE_INSTANCE, config.getCatalogBeHost(),
532 config.getCatalogBePort(), componentType, component.getUniqueId());
533 return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);
537 public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
538 Config config = Utils.getConfig();
539 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
540 return sendGet(url, sdncModifierDetails.getUserId());
543 public static List<Resource> restResponseToResourceObjectList(String restResponse) {
544 JsonElement jelement = new JsonParser().parse(restResponse);
545 JsonArray jsonArray = jelement.getAsJsonArray();
546 List<Resource> restResponseArray = new ArrayList<>();
547 Resource resource = null;
548 for (int i = 0; i < jsonArray.size(); i++) {
549 String resourceString = (String) jsonArray.get(i).toString();
550 resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
551 restResponseArray.add(resource);
554 return restResponseArray;
558 public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
559 if (resourceList != null && resourceList.size() > 0) {
560 for (Resource resource : resourceList) {
561 if (resource.getUniqueId().equals(uid))
569 // =======================================resource
570 // associate==================================================
571 public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
572 ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
573 return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
577 public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
578 String assocType, User sdncUserDetails) throws IOException {
580 RelationshipInstData relationshipInstData = new RelationshipInstData();
581 Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
582 Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
583 List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
584 List<RequirementDefinition> requirementList = requirementMap.get(assocType);
586 RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
587 CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
588 relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
589 relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
590 relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
591 relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());
593 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
595 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
599 private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
600 CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
603 String type = capabilityDefinitionTo.getType();
604 String requirement = requirementDefinitionFrom.getName();
605 String capability = requirementDefinitionFrom.getName();
607 JSONObject wrapper = new JSONObject();
608 JSONArray relationshipsArray = new JSONArray();
609 JSONObject relationship = new JSONObject();
610 JSONObject simpleObject = new JSONObject();
612 relationship.put("type", type);
613 simpleObject.put("relationship", relationship);
614 simpleObject.put("requirement", requirement);
615 simpleObject.put("capability", capability);
616 simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
617 simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
618 simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
619 simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
620 relationshipsArray.add(simpleObject);
622 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
623 wrapper.put("fromNode", fromNode);
624 wrapper.put("toNode", toNode);
625 wrapper.put("relationships", relationships);
630 private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
631 List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
633 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
634 if (capabilityDefinition.getOwnerId().equals(ownerId)) {
635 return capabilityDefinition;
641 private static RequirementDefinition getRequirementDefinitionByOwnerId(
642 List<RequirementDefinition> requirementDefinitionList, String ownerId) {
644 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
645 if (requirementDefinition.getOwnerId().equals(ownerId)) {
646 return requirementDefinition;
652 public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
654 List<ComponentInstance> componentInstances = component.getComponentInstances();
656 for (ComponentInstance componentInstance : componentInstances) {
657 if (componentInstance.getName().equals(resourceInstanceName)) {
658 name = componentInstance.getUniqueId();
665 public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
667 RestResponse response = ResourceRestUtils.getResource(resourceDetails,
668 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
669 assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
670 return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
673 public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
674 String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
675 return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
676 newResourceUniqueId, sdncModifierDetails, componentType);