Sync Integ to Master
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / ResourceRestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils.rest;
22
23 import static org.testng.AssertJUnit.assertEquals;
24
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;
30 import java.util.Map;
31
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;
58
59 import com.google.gson.Gson;
60 import com.google.gson.JsonArray;
61 import com.google.gson.JsonElement;
62 import com.google.gson.JsonParser;
63
64 public class ResourceRestUtils extends BaseRestUtils {
65         private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());
66
67         // ****** CREATE *******
68
69         public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
70                         throws Exception {
71
72                 Config config = Utils.getConfig();
73                 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
74
75                 String userId = sdncModifierDetails.getUserId();
76
77                 Map<String, String> headersMap = prepareHeadersMap(userId);
78
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(),
92                                         "lastUpdaterUserId");
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,
98                         // 12/1/2016
99                         resourceDetails.setCreatorUserId(userId);
100                         resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
101                 }
102                 return createResourceResponse;
103
104         }
105
106         public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
107                         Map<String, String> additionalHeaders) throws JSONException, IOException {
108
109                 Config config = Utils.getConfig();
110                 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
111                 String userId = sdncModifierDetails.getUserId();
112
113                 Gson gson = new Gson();
114                 String resourceImportBodyJson = gson.toJson(importReqDetails);
115                 HttpRequest http = new HttpRequest();
116
117                 Map<String, String> headersMap = prepareHeadersMap(userId);
118                 if (additionalHeaders != null) {
119                         headersMap.putAll(additionalHeaders);
120                 } else {
121                         headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
122                 }
123
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,
129                         // 12/1/2016
130                         importReqDetails.setCreatorUserId(userId);
131                         importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
132                         importReqDetails
133                                         .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
134                         importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
135                 }
136                 return createResourceResponse;
137
138         }
139
140         // ***** DELETE ****
141         public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
142                         String version) throws IOException {
143
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());
149                 } else {
150                         return null;
151                 }
152
153         }
154
155         public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {
156
157                 Config config = Utils.getConfig();
158                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
159                                 resourceId);
160                 RestResponse sendDelete = sendDelete(url, userId);
161
162                 return sendDelete;
163
164         }
165
166         public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
167
168                 Config config = Utils.getConfig();
169                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
170                                 resourceId);
171                 RestResponse sendDelete = sendDelete(url, userId);
172
173                 deleteMarkedResources(userId);
174
175                 return sendDelete;
176
177         }
178
179         public static void deleteMarkedResources(String userId) throws IOException {
180                 String url;
181                 Config config = Utils.getConfig();
182                 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
183                 sendDelete(url, userId);
184         }
185
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());
192
193                 deleteMarkedResources(sdncModifierDetails.getUserId());
194
195                 return sendDelete;
196         }
197
198         public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
199                         throws IOException {
200                 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
201                                 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
202                 return checkErrorCode(deleteResponse);
203         }
204
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);
210         }
211
212         // ************** GET *************
213         public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
214
215                 Config config = Utils.getConfig();
216                 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
217                 return sendGet(url, sdncModifierDetails.getUserId());
218         }
219
220         public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
221                         throws IOException {
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());
226         }
227
228         public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
229                         throws IOException {
230
231                 Config config = Utils.getConfig();
232                 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
233                                 csarUuid);
234                 return sendGet(url, sdncModifierDetails.getUserId());
235         }
236
237         public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
238                         throws IOException {
239
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());
244         }
245
246         public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
247                         throws IOException {
248
249                 Config config = Utils.getConfig();
250                 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
251                                 config.getCatalogBePort(), resourceName, resourceVersion);
252
253                 return sendGet(url, userId);
254         }
255
256         public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
257
258                 Config config = Utils.getConfig();
259                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
260
261                 return sendGet(url, sdncModifierDetails.getUserId());
262
263         }
264         
265         public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
266
267                 Config config = Utils.getConfig();
268                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
269                 
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);
273
274                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
275
276         }
277         
278         public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {
279
280                 Config config = Utils.getConfig();
281                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
282                 
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);
286
287                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
288
289         }
290         
291         public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
292
293                 Config config = Utils.getConfig();
294                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
295                 
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);
299
300                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
301
302         }
303
304         public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
305                 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
306         }
307
308         public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
309                         throws ClientProtocolException, IOException {
310                 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
311         }
312
313         public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
314
315                 Config config = Utils.getConfig();
316                 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
317                                 config.getCatalogBePort());
318
319                 return sendGet(url, sdncModifierDetails.getUserId());
320
321         }
322
323         public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
324
325                 Config config = Utils.getConfig();
326                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
327                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
328
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();
334
335                 logger.debug("Send PUT request to get all categories (should be 405): {}", url);
336                 return http.httpSendByMethod(url, "PUT", null, headersMap);
337         }
338
339         public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
340
341                 Config config = Utils.getConfig();
342                 HttpRequest http = new HttpRequest();
343                 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
344
345                 Map<String, String> headersMap = new HashMap<String, String>();
346                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
347                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
348                 
349                 return http.httpSendGet(url, headersMap);
350
351         }
352
353         public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
354
355                 Config config = Utils.getConfig();
356                 HttpRequest http = new HttpRequest();
357                 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
358
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");
363
364                 return http.httpSendGet(url, headersMap);
365
366         }
367
368         public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
369
370                 Config config = Utils.getConfig();
371                 HttpRequest http = new HttpRequest();
372                 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
373
374                 Map<String, String> headersMap = new HashMap<>();
375
376                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
377                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
378                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
379
380                 return http.httpSendGet(url, headersMap);
381
382         }
383
384         public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
385
386                 Config config = Utils.getConfig();
387                 HttpRequest http = new HttpRequest();
388                 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
389
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");
394
395                 return http.httpSendGet(url, headersMap);
396
397         }
398
399         public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
400
401                 Config config = Utils.getConfig();
402                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
403                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
404
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();
409
410                 logger.debug("Send OPTIONS request for categories: {}", url);
411                 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
412         }
413
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(),
419                                 uniqueId);
420
421                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
422
423                 Gson gson = new Gson();
424                 String userBodyJson = gson.toJson(updatedResourceDetails);
425                 String userId = sdncModifierDetails.getUserId();
426
427                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
428
429                 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
430                 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
431
432                 return updateResourceResponse;
433         }
434
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(),
439                                 uniqueId);
440
441                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
442
443                 Gson gson = new Gson();
444                 String userBodyJson = gson.toJson(resource);
445                 String userId = sdncModifierDetails.getUserId();
446
447                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
448
449                 // String resourceUniqueId =
450                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
451                 // "uniqueId");
452                 // updatedResourceDetails.setUniqueId(resourceUniqueId);
453                 // String resourceVersion =
454                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
455                 // "version");
456                 // updatedResourceDetails.setUniqueId(resourceVersion);
457
458                 return updateResourceResponse;
459         }
460
461         public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
462                         User sdncModifierDetails, String uniqueId) throws Exception {
463                 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
464         }
465
466         public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
467                         throws IOException {
468                 Config config = Utils.getConfig();
469                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
470                                 resourceId);
471                 String userId = sdncModifierDetails.getUserId();
472
473                 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
474
475                 return updateResourceResponse;
476         }
477
478         public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
479                         String resourceId) throws IOException {
480
481                 String userId = sdncModifierDetails.getUserId();
482                 Config config = Utils.getConfig();
483                 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
484                                 resourceId);
485
486                 Map<String, String> headersMap = prepareHeadersMap(userId);
487
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());
506                 }
507                 return updateResourceResponse;
508         }
509
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;
518         }
519
520         public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
521                         Component component) throws IOException {
522
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);
531
532         }
533
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());
538         }
539
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);
549                 }
550
551                 return restResponseArray;
552
553         }
554
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))
559                                         return resource;
560                         }
561                 } else
562                         return null;
563                 return null;
564         }
565
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,
571                                 sdncUserDetails);
572         }
573
574         public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
575                         String assocType, User sdncUserDetails) throws IOException {
576
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);
582
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());
589
590                 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
591                                 toNode, fromNode);
592                 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
593
594         }
595
596         private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
597                         CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
598                         String fromNode) {
599
600                 String type = capabilityDefinitionTo.getType();
601                 String requirement = requirementDefinitionFrom.getName();
602                 String capability = requirementDefinitionFrom.getName();
603
604                 JSONObject wrapper = new JSONObject();
605                 JSONArray relationshipsArray = new JSONArray();
606                 JSONObject relationship = new JSONObject();
607                 JSONObject simpleObject = new JSONObject();
608
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);
618
619                 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
620                 wrapper.put("fromNode", fromNode);
621                 wrapper.put("toNode", toNode);
622                 wrapper.put("relationships", relationships);
623                 return wrapper;
624
625         }
626
627         private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
628                         List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
629
630                 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
631                         if (capabilityDefinition.getOwnerId().equals(ownerId)) {
632                                 return capabilityDefinition;
633                         }
634                 }
635                 return null;
636         }
637
638         private static RequirementDefinition getRequirementDefinitionByOwnerId(
639                         List<RequirementDefinition> requirementDefinitionList, String ownerId) {
640
641                 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
642                         if (requirementDefinition.getOwnerId().equals(ownerId)) {
643                                 return requirementDefinition;
644                         }
645                 }
646                 return null;
647         }
648
649         public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
650
651                 List<ComponentInstance> componentInstances = component.getComponentInstances();
652                 String name = null;
653                 for (ComponentInstance componentInstance : componentInstances) {
654                         if (componentInstance.getName().equals(resourceInstanceName)) {
655                                 name = componentInstance.getUniqueId();
656                                 break;
657                         }
658                 }
659                 return name;
660         }
661
662         public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
663                         throws IOException {
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());
668         }
669
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);
674         }
675
676 }