[SDC-29] rebase continue work to align source
[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
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;
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.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(),
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                 // System.out.println(url);
117                 // System.out.println(resourceImportBodyJson);
118
119                 Map<String, String> headersMap = prepareHeadersMap(userId);
120                 if (additionalHeaders != null) {
121                         headersMap.putAll(additionalHeaders);
122                 } else {
123                         headersMap.put(HttpHeaderEnum.Content_MD5.getValue(),
124                                         ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
125                 }
126
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,
132                         // 12/1/2016
133                         importReqDetails.setCreatorUserId(userId);
134                         importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
135                         importReqDetails
136                                         .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
137                         importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
138                 }
139                 return createResourceResponse;
140
141         }
142
143         // ***** DELETE ****
144         public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
145                         String version) throws IOException {
146
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());
152                 } else {
153                         return null;
154                 }
155
156         }
157
158         public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {
159
160                 Config config = Utils.getConfig();
161                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
162                                 resourceId);
163                 RestResponse sendDelete = sendDelete(url, userId);
164
165                 return sendDelete;
166
167         }
168
169         public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
170
171                 Config config = Utils.getConfig();
172                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
173                                 resourceId);
174                 RestResponse sendDelete = sendDelete(url, userId);
175
176                 deleteMarkedResources(userId);
177
178                 return sendDelete;
179
180         }
181
182         public static void deleteMarkedResources(String userId) throws IOException {
183                 String url;
184                 Config config = Utils.getConfig();
185                 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
186                 sendDelete(url, userId);
187         }
188
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());
195
196                 deleteMarkedResources(sdncModifierDetails.getUserId());
197
198                 return sendDelete;
199         }
200
201         public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
202                         throws IOException {
203                 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
204                                 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
205                 return checkErrorCode(deleteResponse);
206         }
207
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);
213         }
214
215         // ************** GET *************
216         public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
217
218                 Config config = Utils.getConfig();
219                 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
220                 return sendGet(url, sdncModifierDetails.getUserId());
221         }
222
223         public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
224                         throws IOException {
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());
229         }
230
231         public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
232                         throws IOException {
233
234                 Config config = Utils.getConfig();
235                 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
236                                 csarUuid);
237                 return sendGet(url, sdncModifierDetails.getUserId());
238         }
239
240         public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
241                         throws IOException {
242
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());
247         }
248
249         public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
250                         throws IOException {
251
252                 Config config = Utils.getConfig();
253                 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
254                                 config.getCatalogBePort(), resourceName, resourceVersion);
255
256                 return sendGet(url, userId);
257         }
258
259         public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
260
261                 Config config = Utils.getConfig();
262                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
263
264                 return sendGet(url, sdncModifierDetails.getUserId());
265
266         }
267         
268         public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
269
270                 Config config = Utils.getConfig();
271                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
272                 
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");
276
277                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
278
279         }
280         
281         public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {
282
283                 Config config = Utils.getConfig();
284                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
285                 
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");
289
290                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
291
292         }
293         
294         public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
295
296                 Config config = Utils.getConfig();
297                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
298                 
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");
302
303                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
304
305         }
306
307         public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
308                 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
309         }
310
311         public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
312                         throws ClientProtocolException, IOException {
313                 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
314         }
315
316         public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
317
318                 Config config = Utils.getConfig();
319                 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
320                                 config.getCatalogBePort());
321
322                 return sendGet(url, sdncModifierDetails.getUserId());
323
324         }
325
326         public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
327
328                 Config config = Utils.getConfig();
329                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
330                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
331
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();
337
338                 logger.debug("Send PUT request to get all categories (should be 405): {}", url);
339                 return http.httpSendByMethod(url, "PUT", null, headersMap);
340         }
341
342         public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
343
344                 Config config = Utils.getConfig();
345                 HttpRequest http = new HttpRequest();
346                 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
347
348                 Map<String, String> headersMap = new HashMap<String, String>();
349                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
350                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
351                 
352                 return http.httpSendGet(url, headersMap);
353
354         }
355
356         public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
357
358                 Config config = Utils.getConfig();
359                 HttpRequest http = new HttpRequest();
360                 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
361
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");
366
367                 return http.httpSendGet(url, headersMap);
368
369         }
370
371         public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
372
373                 Config config = Utils.getConfig();
374                 HttpRequest http = new HttpRequest();
375                 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
376
377                 Map<String, String> headersMap = new HashMap<String, String>();
378
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");
382
383                 return http.httpSendGet(url, headersMap);
384
385         }
386
387         public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
388
389                 Config config = Utils.getConfig();
390                 HttpRequest http = new HttpRequest();
391                 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
392
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");
397
398                 return http.httpSendGet(url, headersMap);
399
400         }
401
402         public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
403
404                 Config config = Utils.getConfig();
405                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
406                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
407
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();
412
413                 logger.debug("Send OPTIONS request for categories: {}", url);
414                 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
415         }
416
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(),
422                                 uniqueId);
423
424                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
425
426                 Gson gson = new Gson();
427                 String userBodyJson = gson.toJson(updatedResourceDetails);
428                 String userId = sdncModifierDetails.getUserId();
429
430                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
431
432                 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
433                 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
434
435                 return updateResourceResponse;
436         }
437
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(),
442                                 uniqueId);
443
444                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
445
446                 Gson gson = new Gson();
447                 String userBodyJson = gson.toJson(resource);
448                 String userId = sdncModifierDetails.getUserId();
449
450                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
451
452                 // String resourceUniqueId =
453                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
454                 // "uniqueId");
455                 // updatedResourceDetails.setUniqueId(resourceUniqueId);
456                 // String resourceVersion =
457                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
458                 // "version");
459                 // updatedResourceDetails.setUniqueId(resourceVersion);
460
461                 return updateResourceResponse;
462         }
463
464         public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
465                         User sdncModifierDetails, String uniqueId) throws Exception {
466                 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
467         }
468
469         public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
470                         throws IOException {
471                 Config config = Utils.getConfig();
472                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
473                                 resourceId);
474                 String userId = sdncModifierDetails.getUserId();
475
476                 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
477
478                 return updateResourceResponse;
479         }
480
481         public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
482                         String resourceId) throws IOException {
483
484                 String userId = sdncModifierDetails.getUserId();
485                 Config config = Utils.getConfig();
486                 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
487                                 resourceId);
488
489                 Map<String, String> headersMap = prepareHeadersMap(userId);
490
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());
509                 }
510                 return updateResourceResponse;
511         }
512
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;
521         }
522
523         public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
524                         Component component) throws IOException {
525
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);
534
535         }
536
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());
541         }
542
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);
552                 }
553
554                 return restResponseArray;
555
556         }
557
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))
562                                         return resource;
563                         }
564                 } else
565                         return null;
566                 return null;
567         }
568
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,
574                                 sdncUserDetails);
575         }
576
577         public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
578                         String assocType, User sdncUserDetails) throws IOException {
579
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);
585
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());
592
593                 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
594                                 toNode, fromNode);
595                 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
596
597         }
598
599         private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
600                         CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
601                         String fromNode) {
602
603                 String type = capabilityDefinitionTo.getType();
604                 String requirement = requirementDefinitionFrom.getName();
605                 String capability = requirementDefinitionFrom.getName();
606
607                 JSONObject wrapper = new JSONObject();
608                 JSONArray relationshipsArray = new JSONArray();
609                 JSONObject relationship = new JSONObject();
610                 JSONObject simpleObject = new JSONObject();
611
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);
621
622                 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
623                 wrapper.put("fromNode", fromNode);
624                 wrapper.put("toNode", toNode);
625                 wrapper.put("relationships", relationships);
626                 return wrapper;
627
628         }
629
630         private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
631                         List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
632
633                 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
634                         if (capabilityDefinition.getOwnerId().equals(ownerId)) {
635                                 return capabilityDefinition;
636                         }
637                 }
638                 return null;
639         }
640
641         private static RequirementDefinition getRequirementDefinitionByOwnerId(
642                         List<RequirementDefinition> requirementDefinitionList, String ownerId) {
643
644                 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
645                         if (requirementDefinition.getOwnerId().equals(ownerId)) {
646                                 return requirementDefinition;
647                         }
648                 }
649                 return null;
650         }
651
652         public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
653
654                 List<ComponentInstance> componentInstances = component.getComponentInstances();
655                 String name = null;
656                 for (ComponentInstance componentInstance : componentInstances) {
657                         if (componentInstance.getName().equals(resourceInstanceName)) {
658                                 name = componentInstance.getUniqueId();
659                                 break;
660                         }
661                 }
662                 return name;
663         }
664
665         public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
666                         throws IOException {
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());
671         }
672
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);
677         }
678
679 }