[SDC-29] rebase continue work to align source
[sdc.git] / asdc-tests / 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.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.http.client.ClientProtocolException;
38 import org.json.JSONException;
39 import org.json.simple.JSONArray;
40 import org.json.simple.JSONObject;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
43 import org.openecomp.sdc.be.model.CapabilityDefinition;
44 import org.openecomp.sdc.be.model.Component;
45 import org.openecomp.sdc.be.model.ComponentInstance;
46 import org.openecomp.sdc.be.model.RequirementDefinition;
47 import org.openecomp.sdc.be.model.Resource;
48 import org.openecomp.sdc.be.model.User;
49 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
50 import org.openecomp.sdc.ci.tests.api.Urls;
51 import org.openecomp.sdc.ci.tests.config.Config;
52 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
53 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
54 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
55 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
56 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
57 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
58 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
59 import org.openecomp.sdc.ci.tests.utils.Utils;
60 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
61 import org.openecomp.sdc.common.util.GeneralUtility;
62
63 import com.google.gson.Gson;
64 import com.google.gson.JsonArray;
65 import com.google.gson.JsonElement;
66 import com.google.gson.JsonParser;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69
70 public class ResourceRestUtils extends BaseRestUtils {
71         
72         private static final String CSARS_PATH = "/src/test/resources/CI/csars/";
73         private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());
74
75         // ****** CREATE *******
76
77         public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
78                         throws Exception {
79
80                 Config config = Utils.getConfig();
81                 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
82
83                 String userId = sdncModifierDetails.getUserId();
84
85                 Map<String, String> headersMap = prepareHeadersMap(userId);
86
87                 Gson gson = new Gson();
88                 String userBodyJson = gson.toJson(resourceDetails);
89                 String calculateMD5 = GeneralUtility.calculateMD5ByString(userBodyJson);
90                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
91                 HttpRequest http = new HttpRequest();
92                 // System.out.println(url);
93                 // System.out.println(userBodyJson);
94                 RestResponse createResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
95                 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
96                         resourceDetails.setUUID(ResponseParser.getUuidFromResponse(createResourceResponse));
97                         resourceDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
98                         resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
99                         String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
100                                         "lastUpdaterUserId");
101                         resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
102                         String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
103                                         "lastUpdaterFullName");
104                         resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
105                         // Creator details never change after component is created - Ella,
106                         // 12/1/2016
107                         resourceDetails.setCreatorUserId(userId);
108                         resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
109                 }
110                 return createResourceResponse;
111
112         }
113
114         public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
115                         Map<String, String> additionalHeaders) throws JSONException, IOException {
116
117                 Config config = Utils.getConfig();
118                 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
119                 String userId = sdncModifierDetails.getUserId();
120
121                 Gson gson = new Gson();
122                 String resourceImportBodyJson = gson.toJson(importReqDetails);
123                 HttpRequest http = new HttpRequest();
124                 // System.out.println(url);
125                 // System.out.println(resourceImportBodyJson);
126
127                 Map<String, String> headersMap = prepareHeadersMap(userId);
128                 if (additionalHeaders != null) {
129                         headersMap.putAll(additionalHeaders);
130                 } else {
131                         headersMap.put(HttpHeaderEnum.Content_MD5.getValue(),
132                                         ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
133                 }
134
135                 RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
136                 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
137                         importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
138                         importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
139                         // Creator details never change after component is created - Ella,
140                         // 12/1/2016
141                         importReqDetails.setCreatorUserId(userId);
142                         importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
143                         importReqDetails
144                                         .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
145                         importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
146                 }
147                 return createResourceResponse;
148
149         }
150
151         // ***** DELETE ****
152         public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
153                         String version) throws IOException {
154
155                 if (resourceDetails.getUniqueId() != null) {
156                         Config config = Utils.getConfig();
157                         String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
158                                         config.getCatalogBePort(), resourceDetails.getName(), version);
159                         return sendDelete(url, sdncModifierDetails.getUserId());
160                 } else {
161                         return null;
162                 }
163
164         }
165
166         public static RestResponse markResourceToDelete(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                 return sendDelete;
174
175         }
176
177         public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
178
179                 Config config = Utils.getConfig();
180                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
181                                 resourceId);
182                 RestResponse sendDelete = sendDelete(url, userId);
183
184                 deleteMarkedResources(userId);
185
186                 return sendDelete;
187
188         }
189
190         public static void deleteMarkedResources(String userId) throws IOException {
191                 String url;
192                 Config config = Utils.getConfig();
193                 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
194                 sendDelete(url, userId);
195         }
196
197         public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
198                         String resourceVersion) throws IOException {
199                 Config config = Utils.getConfig();
200                 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
201                                 config.getCatalogBePort(), resourceName, resourceVersion);
202                 RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());
203
204                 deleteMarkedResources(sdncModifierDetails.getUserId());
205
206                 return sendDelete;
207         }
208
209         public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
210                         throws IOException {
211                 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
212                                 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
213                 return checkErrorCode(deleteResponse);
214         }
215
216         public static Boolean removeResource(String resourceId)
217                         throws FileNotFoundException, IOException, ClientProtocolException {
218                 RestResponse response = deleteResource(resourceId,
219                                 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
220                 return checkErrorCode(response);
221         }
222
223         // ************** GET *************
224         public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
225
226                 Config config = Utils.getConfig();
227                 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
228                 return sendGet(url, sdncModifierDetails.getUserId());
229         }
230
231         public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
232                         throws IOException {
233                 Config config = Utils.getConfig();
234                 String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
235                                 componentId, moduleId);
236                 return sendGet(url, sdncModifierDetails.getUserId());
237         }
238
239         public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
240                         throws IOException {
241
242                 Config config = Utils.getConfig();
243                 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
244                                 csarUuid);
245                 return sendGet(url, sdncModifierDetails.getUserId());
246         }
247
248         public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
249                         throws IOException {
250
251                 Config config = Utils.getConfig();
252                 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
253                                 resourceDetails.getUniqueId());
254                 return sendGet(url, sdncModifierDetails.getUserId());
255         }
256
257         public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
258                         throws IOException {
259
260                 Config config = Utils.getConfig();
261                 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
262                                 config.getCatalogBePort(), resourceName, resourceVersion);
263
264                 return sendGet(url, userId);
265         }
266
267         public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
268
269                 Config config = Utils.getConfig();
270                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
271
272                 return sendGet(url, sdncModifierDetails.getUserId());
273
274         }
275         
276         public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
277
278                 Config config = Utils.getConfig();
279                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
280                 
281                 Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
282                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
283                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
284
285                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
286
287         }
288         
289         public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
290
291                 Config config = Utils.getConfig();
292                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
293                 
294                 Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
295                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
296                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
297
298                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
299
300         }
301
302         public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
303                 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
304         }
305
306         public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
307                         throws ClientProtocolException, IOException {
308                 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
309         }
310
311         public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
312
313                 Config config = Utils.getConfig();
314                 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
315                                 config.getCatalogBePort());
316
317                 return sendGet(url, sdncModifierDetails.getUserId());
318
319         }
320
321         public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
322
323                 Config config = Utils.getConfig();
324                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
325                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
326
327                 Map<String, String> headersMap = new HashMap<String, String>();
328                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
329                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
330                 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
331                 HttpRequest http = new HttpRequest();
332
333                 logger.debug("Send PUT request to get all categories (should be 405): {}",url);
334                 return http.httpSendByMethod(url, "PUT", null, headersMap);
335         }
336
337         public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
338
339                 Config config = Utils.getConfig();
340                 HttpRequest http = new HttpRequest();
341                 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
342
343                 Map<String, String> headersMap = new HashMap<String, String>();
344                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
345                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
346
347                 return http.httpSendGet(url, headersMap);
348
349         }
350
351         public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
352
353                 Config config = Utils.getConfig();
354                 HttpRequest http = new HttpRequest();
355                 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
356
357                 Map<String, String> headersMap = new HashMap<String, String>();
358                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
359                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
360                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
361
362                 return http.httpSendGet(url, headersMap);
363         }
364
365         public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
366
367                 Config config = Utils.getConfig();
368                 HttpRequest http = new HttpRequest();
369                 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
370
371                 Map<String, String> headersMap = new HashMap<String, String>();
372
373                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
374                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
375                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
376
377                 return http.httpSendGet(url, headersMap);
378
379         }
380
381         public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
382
383                 Config config = Utils.getConfig();
384                 HttpRequest http = new HttpRequest();
385                 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
386
387                 Map<String, String> headersMap = new HashMap<String, String>();
388                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
389                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
390                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
391
392                 return http.httpSendGet(url, headersMap);
393
394         }
395         
396         public static RestResponse getResourceFilteredDataByParams(User sdncModifierDetails, String uniqueId , List<String> parameters) throws IOException {
397                 Config config = Utils.getConfig();
398                 String urlGetResourceDataByParams = Urls.GET_RESOURCE_DATA_BY_PARAMS;
399                 String joinedParameters = StringUtils.join(parameters , "&");
400                 String url = String.format(urlGetResourceDataByParams + joinedParameters , config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
401                 return sendGet(url, sdncModifierDetails.getUserId());
402                 
403         }
404
405
406         public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
407
408                 Config config = Utils.getConfig();
409                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
410                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
411
412                 Map<String, String> headersMap = new HashMap<String, String>();
413                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
414                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
415                 HttpRequest http = new HttpRequest();
416
417                 logger.debug("Send OPTIONS request for categories: {}",url);
418                 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
419         }
420
421         // ********** UPDATE *************
422         public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
423                         User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
424                 Config config = Utils.getConfig();
425                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
426                                 uniqueId);
427
428                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
429
430                 Gson gson = new Gson();
431                 String userBodyJson = gson.toJson(updatedResourceDetails);
432                 String userId = sdncModifierDetails.getUserId();
433
434                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
435
436                 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
437                 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
438
439                 return updateResourceResponse;
440         }
441
442         public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
443                         String encoding) throws Exception {
444                 Config config = Utils.getConfig();
445                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
446                                 uniqueId);
447
448                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
449
450                 Gson gson = new Gson();
451                 String userBodyJson = gson.toJson(resource);
452                 String userId = sdncModifierDetails.getUserId();
453
454                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
455
456                 // String resourceUniqueId =
457                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
458                 // "uniqueId");
459                 // updatedResourceDetails.setUniqueId(resourceUniqueId);
460                 // String resourceVersion =
461                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
462                 // "version");
463                 // updatedResourceDetails.setUniqueId(resourceVersion);
464
465                 return updateResourceResponse;
466         }
467
468         public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
469                         User sdncModifierDetails, String uniqueId) throws Exception {
470                 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
471         }
472
473         public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
474                         throws IOException {
475                 Config config = Utils.getConfig();
476                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
477                                 resourceId);
478                 String userId = sdncModifierDetails.getUserId();
479
480                 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
481
482                 return updateResourceResponse;
483         }
484
485         public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
486                         String resourceId) throws IOException {
487
488                 String userId = sdncModifierDetails.getUserId();
489                 Config config = Utils.getConfig();
490                 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
491                                 resourceId);
492
493                 Map<String, String> headersMap = prepareHeadersMap(userId);
494
495                 Gson gson = new Gson();
496                 String userBodyJson = gson.toJson(resourceDetails);
497                 String calculateMD5 = GeneralUtility.calculateMD5ByString(userBodyJson);
498                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
499                 HttpRequest http = new HttpRequest();
500                 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
501                 if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
502                         resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
503                         resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
504                         resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
505                         String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
506                                         "lastUpdaterUserId");
507                         resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
508                         String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
509                                         "lastUpdaterFullName");
510                         resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
511                         resourceDetails.setCreatorUserId(userId);
512                         resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
513                 }
514                 return updateResourceResponse;
515         }
516
517         public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
518                         String vfResourceUniqueId) throws Exception {
519                 ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
520                                 .getComponentResourceInstance(resourceDetails);
521                 RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
522                                 resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
523                 ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
524                 return createResourceInstanceResponse;
525         }
526
527         public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
528                         Component component) throws IOException {
529
530                 Config config = Utils.getConfig();
531                 Gson gson = new Gson();
532                 String bodyJson = gson.toJson(body);
533                 component.getComponentType();
534                 String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
535                 String url = String.format(Urls.ASSOCIATE_RESOURCE_INSTANCE, config.getCatalogBeHost(),
536                                 config.getCatalogBePort(), componentType, component.getUniqueId());
537                 return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);
538
539         }
540
541         public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
542                 Config config = Utils.getConfig();
543                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
544                 return sendGet(url, sdncModifierDetails.getUserId());
545         }
546
547         public static List<Resource> restResponseToResourceObjectList(String restResponse) {
548                 JsonElement jelement = new JsonParser().parse(restResponse);
549                 JsonArray jsonArray = jelement.getAsJsonArray();
550                 List<Resource> restResponseArray = new ArrayList<>();
551                 Resource resource = null;
552                 for (int i = 0; i < jsonArray.size(); i++) {
553                         String resourceString = (String) jsonArray.get(i).toString();
554                         resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
555                         restResponseArray.add(resource);
556                 }
557
558                 return restResponseArray;
559
560         }
561
562         public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
563                 if (resourceList != null && resourceList.size() > 0) {
564                         for (Resource resource : resourceList) {
565                                 if (resource.getUniqueId().equals(uid))
566                                         return resource;
567                         }
568                 } else
569                         return null;
570                 return null;
571         }
572
573         // =======================================resource
574         // associate==================================================
575         public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
576                         ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
577                 return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
578                                 sdncUserDetails);
579         }
580
581         public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
582                         String assocType, User sdncUserDetails) throws IOException {
583
584                 RelationshipInstData relationshipInstData = new RelationshipInstData();
585                 Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
586                 Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
587                 List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
588                 List<RequirementDefinition> requirementList = requirementMap.get(assocType);
589
590                 RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
591                 CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
592                 relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
593                 relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
594                 relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
595                 relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());
596
597                 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
598                                 toNode, fromNode);
599                 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
600
601         }
602
603         private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
604                         CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
605                         String fromNode) {
606
607                 String type = capabilityDefinitionTo.getType();
608                 String requirement = requirementDefinitionFrom.getName();
609                 String capability = requirementDefinitionFrom.getName();
610
611                 JSONObject wrapper = new JSONObject();
612                 JSONArray relationshipsArray = new JSONArray();
613                 JSONObject relationship = new JSONObject();
614                 JSONObject simpleObject = new JSONObject();
615
616                 relationship.put("type", type);
617                 simpleObject.put("relationship", relationship);
618                 simpleObject.put("requirement", requirement);
619                 simpleObject.put("capability", capability);
620                 simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
621                 simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
622                 simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
623                 simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
624                 relationshipsArray.add(simpleObject);
625
626                 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
627                 wrapper.put("fromNode", fromNode);
628                 wrapper.put("toNode", toNode);
629                 wrapper.put("relationships", relationships);
630                 return wrapper;
631
632         }
633
634         private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
635                         List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
636
637                 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
638                         if (capabilityDefinition.getOwnerId().equals(ownerId)) {
639                                 return capabilityDefinition;
640                         }
641                 }
642                 return null;
643         }
644
645         private static RequirementDefinition getRequirementDefinitionByOwnerId(
646                         List<RequirementDefinition> requirementDefinitionList, String ownerId) {
647
648                 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
649                         if (requirementDefinition.getOwnerId().equals(ownerId)) {
650                                 return requirementDefinition;
651                         }
652                 }
653                 return null;
654         }
655
656         public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
657
658                 List<ComponentInstance> componentInstances = component.getComponentInstances();
659                 String name = null;
660                 for (ComponentInstance componentInstance : componentInstances) {
661                         if (componentInstance.getName().equals(resourceInstanceName)) {
662                                 name = componentInstance.getUniqueId();
663                                 break;
664                         }
665                 }
666                 return name;
667         }
668
669         public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
670                         throws IOException {
671                 RestResponse response = ResourceRestUtils.getResource(resourceDetails,
672                                 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
673                 assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
674                 return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
675         }
676
677         public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
678                         String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
679                 return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
680                                 newResourceUniqueId, sdncModifierDetails, componentType);
681         }
682         
683         public static Resource importResourceFromCsar(String csarName) throws Exception{
684                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
685                 String payloadName = csarName;
686                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
687                 String rootPath = System.getProperty("user.dir");
688                 Path path = null;
689                 byte[] data = null;
690                 
691                 String payloadData = null;
692
693                 path = Paths.get(rootPath + CSARS_PATH + csarName);
694                 data = Files.readAllBytes(path);
695                 payloadData = Base64.encodeBase64String(data);
696                 resourceDetails.setPayloadData(payloadData);
697
698                 // create new resource from Csar
699                 resourceDetails.setCsarUUID(payloadName);
700                 resourceDetails.setPayloadName(payloadName);
701                 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
702                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
703                 BaseRestUtils.checkCreateResponse(createResource);
704                 Resource resource = ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
705                 return resource;
706                 
707                 // add to restResourceUtil
708         }
709
710
711 }