[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / OnboardingUtils.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.utilities;
22
23 import static org.testng.AssertJUnit.assertEquals;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.StringWriter;
30 import java.nio.file.FileSystems;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.UUID;
38
39 import org.apache.commons.io.IOUtils;
40 import org.apache.http.HttpEntity;
41 import org.apache.http.client.ClientProtocolException;
42 import org.apache.http.client.methods.CloseableHttpResponse;
43 import org.apache.http.client.methods.HttpPost;
44 import org.apache.http.entity.mime.MultipartEntityBuilder;
45 import org.apache.http.entity.mime.content.FileBody;
46 import org.apache.http.impl.client.CloseableHttpClient;
47 import org.apache.http.impl.client.HttpClients;
48 import org.json.JSONException;
49 import org.json.JSONObject;
50 import org.json.simple.JSONArray;
51 import org.json.simple.JSONValue;
52 import org.openecomp.sdc.be.model.User;
53 import org.openecomp.sdc.ci.tests.config.Config;
54 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
55 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
56 import org.openecomp.sdc.ci.tests.datatypes.LifeCycleStateEnum;
57 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
58 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
59 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
60 import org.openecomp.sdc.ci.tests.execute.devCI.ArtifactFromCsar;
61 import org.openecomp.sdc.ci.tests.execute.setup.ArtifactsCorrelationManager;
62 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
63 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
64 import org.openecomp.sdc.ci.tests.pages.DeploymentArtifactPage;
65 import org.openecomp.sdc.ci.tests.pages.HomePage;
66 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
67 import org.openecomp.sdc.ci.tests.utils.Utils;
68 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
69 import org.openecomp.sdc.ci.tests.verificator.VfVerificator;
70 import org.openqa.selenium.WebElement;
71 import org.openqa.selenium.support.ui.ExpectedConditions;
72 import org.openqa.selenium.support.ui.WebDriverWait;
73 import org.testng.Assert;
74
75 import com.aventstack.extentreports.Status;
76 import com.clearspring.analytics.util.Pair;
77
78 public class OnboardingUtils {
79
80         public OnboardingUtils() {
81         }
82
83         private static String vendorId;
84         private static String vendorLicenseName;
85         private static String vendorLicenseAgreementId;
86         private static String featureGroupId;
87
88         public static Pair<String, Map<String, String>> createVendorSoftwareProduct(String HeatFileName, String filepath, User user)
89                         throws Exception {
90                 Pair<String, Map<String, String>> pair = createVSP(HeatFileName, filepath, user);
91                 
92                 String vspid = pair.right.get("vspId");
93                                 
94                 prepareVspForUse(user, vspid);
95                 
96                 return pair;
97         }
98
99         public static void prepareVspForUse(User user, String vspid) throws Exception {
100                 RestResponse checkin = checkinVendorSoftwareProduct(vspid, user);
101                 assertEquals("did not succeed to checking new VSP", 200, checkin.getErrorCode().intValue());
102
103                 RestResponse submit = submitVendorSoftwareProduct(vspid, user);
104                 assertEquals("did not succeed to submit new VSP", 200, submit.getErrorCode().intValue());
105
106                 RestResponse createPackage = createPackageOfVendorSoftwareProduct(vspid, user);
107                 assertEquals("did not succeed to create package of new VSP ", 200, createPackage.getErrorCode().intValue());
108
109                 SetupCDTest.getExtendTest().log(Status.INFO, "Succeeded in creating the vendor software product");
110         }
111
112         public static Pair<String, Map<String, String>> createVSP(String HeatFileName, String filepath, User user) throws Exception {
113                 String vspName = handleFilename(HeatFileName);
114                 
115                 SetupCDTest.getExtendTest().log(Status.INFO, "Starting to create the vendor software product");
116                 
117                 Pair<RestResponse, Map<String, String>> createNewVspPair = createNewVendorSoftwareProduct(vspName, vendorLicenseName, vendorId, vendorLicenseAgreementId, featureGroupId, user);
118                 RestResponse createNewVendorSoftwareProduct = createNewVspPair.left;
119                 assertEquals("did not succeed to create new VSP", 200,createNewVendorSoftwareProduct.getErrorCode().intValue());
120                 String vspid = ResponseParser.getValueFromJsonResponse(createNewVendorSoftwareProduct.getResponse(), "vspId");
121                 String componentId = ResponseParser.getValueFromJsonResponse(createNewVendorSoftwareProduct.getResponse(), "componentId");
122                 
123                 Map<String, String> vspMeta = createNewVspPair.right;
124                 Map<String, String> vspObject = new HashMap<String, String>();
125                 Iterator<String> iterator = vspMeta.keySet().iterator();
126                 while(iterator.hasNext()){
127                         Object key = iterator.next();
128                         Object value = vspMeta.get(key);
129                         vspObject.put(key.toString(), value.toString());
130                 }
131                 vspObject.put("vspId", vspid);
132                 vspObject.put("componentId", componentId);
133                 vspObject.put("vendorName", vendorLicenseName);
134                 vspObject.put("attContact", user.getUserId());
135                 
136                 RestResponse uploadHeatPackage = uploadHeatPackage(filepath, HeatFileName, vspid, user);
137                 assertEquals("did not succeed to upload HEAT package", 200, uploadHeatPackage.getErrorCode().intValue());
138                 
139                 RestResponse validateUpload = validateUpload(vspid, user);
140                 assertEquals("did not succeed to validate upload process", 200, validateUpload.getErrorCode().intValue());
141                 
142                 Pair<String, Map<String, String>> pair = new Pair<String, Map<String, String>>(vspName, vspObject);
143                 
144                 return pair;
145         }
146         
147         public static void updateVspWithVfcArtifacts(String filepath, String vspId, String updatedSnmpPoll, String updatedSnmpTrap, String componentId, User user) throws Exception{
148                 RestResponse checkout = checkoutVendorSoftwareProduct(vspId, user);
149                 assertEquals("did not succeed to checkout new VSP", 200, checkout.getErrorCode().intValue());
150                 ExtentTestActions.log(Status.INFO, "Deleting SNMP POLL");
151                 deleteSnmpArtifact(componentId, vspId, user, SnmpTypeEnum.SNMP_POLL);
152                 ExtentTestActions.log(Status.INFO, "Deleting SNMP TRAP");
153                 deleteSnmpArtifact(componentId, vspId, user, SnmpTypeEnum.SNMP_TRAP);
154                 addVFCArtifacts(filepath, updatedSnmpPoll, updatedSnmpTrap, vspId, user, componentId);
155                 prepareVspForUse(user, vspId);
156         }
157         
158         public static String updateVendorSoftwareProduct(String vspId, String HeatFileName, String filepath, User user)
159                         throws Exception, Throwable {
160                 String vspName = handleFilename(HeatFileName);
161                 SetupCDTest.getExtendTest().log(Status.INFO, "Starting to update the vendor software product");
162
163                 RestResponse checkout = checkoutVendorSoftwareProduct(vspId, user);
164                 assertEquals("did not succeed to checkout new VSP", 200, checkout.getErrorCode().intValue());
165
166                 RestResponse uploadHeatPackage = uploadHeatPackage(filepath, HeatFileName, vspId, user);
167                 assertEquals("did not succeed to upload HEAT package", 200, uploadHeatPackage.getErrorCode().intValue());
168                 
169                 RestResponse validateUpload = validateUpload(vspId, user);
170                 assertEquals("did not succeed to validate upload process", 200, validateUpload.getErrorCode().intValue());
171                 
172                 RestResponse checkin = checkinVendorSoftwareProduct(vspId, user);
173                 assertEquals("did not succeed to checking VSP", 200, checkin.getErrorCode().intValue());
174
175
176                 RestResponse submit = submitVendorSoftwareProduct(vspId, user);
177                 assertEquals("did not succeed to submit VSP", 200, submit.getErrorCode().intValue());
178
179                 RestResponse createPackage = createPackageOfVendorSoftwareProduct(vspId, user);
180                 assertEquals("did not succeed to update package of VSP ", 200, createPackage.getErrorCode().intValue());
181
182                 SetupCDTest.getExtendTest().log(Status.INFO, "Succeeded in updating the vendor software product");
183
184                 return vspName;
185         }
186
187         public static String handleFilename(String heatFileName) {
188                 final String namePrefix = "ciVFOnboarded-";
189                 final String nameSuffix = "-" + getShortUUID();
190
191                 String subHeatFileName = heatFileName.substring(0, heatFileName.lastIndexOf("."));
192
193                 if ((namePrefix + subHeatFileName + nameSuffix).length() >= 50) {
194                         subHeatFileName = subHeatFileName.substring(0, 50 - namePrefix.length() - nameSuffix.length());
195                 }
196
197                 if (subHeatFileName.contains("(") || subHeatFileName.contains(")")) {
198                         subHeatFileName = subHeatFileName.replace("(", "-");
199                         subHeatFileName = subHeatFileName.replace(")", "-");
200                 }
201
202                 String vnfName = namePrefix + subHeatFileName + nameSuffix;
203                 return vnfName;
204         }
205         
206         public static String addVFCArtifacts(String filepath, String snmpPoll, String snmpTrap, String vspid, User user, String vspComponentId) throws Exception{
207                 vspComponentId = (vspComponentId == null) ? getVSPComponentId(vspid, user) : vspComponentId;
208                 if (vspComponentId != null){
209                         if (snmpPoll != null){
210                                 ExtentTestActions.log(Status.INFO, "Adding VFC artifact of type SNMP POLL with the file " + snmpPoll);
211                                 RestResponse uploadSnmpPollArtifact = uploadSnmpPollArtifact(filepath, snmpPoll, vspid, user, vspComponentId);
212                                 assertEquals("Did not succeed to add SNMP POLL", 200, uploadSnmpPollArtifact.getErrorCode().intValue());
213                         }
214                         if (snmpTrap != null){
215                                 ExtentTestActions.log(Status.INFO, "Adding VFC artifact of type SNMP TRAP with the file " + snmpTrap);
216                                 RestResponse uploadSnmpTrapArtifact = uploadSnmpTrapArtifact(filepath, snmpTrap, vspid, user, vspComponentId);
217                                 assertEquals("Did not succeed to add SNMP TRAP", 200, uploadSnmpTrapArtifact.getErrorCode().intValue());
218                         }
219                 }
220                 
221                 return vspComponentId;
222         }
223         
224         public static String addVFCArtifacts(String filepath, String snmpPoll, String snmpTrap, String vspid, User user) throws Exception{
225                 return addVFCArtifacts(filepath, snmpPoll, snmpTrap, vspid, user, null);
226         }
227
228         private static RestResponse uploadSnmpPollArtifact(String filepath, String zipArtifact, String vspid, User user,
229                         String vspComponentId) throws FileNotFoundException, IOException, ClientProtocolException {
230                 Config config = Utils.getConfig();
231                 String snmpPollUrl = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/versions/0.1/components/%s/monitors/snmp/upload", 
232                                 config.getCatalogBeHost(),config.getCatalogBePort(), vspid, vspComponentId);
233                 return uploadFile(filepath, zipArtifact, snmpPollUrl, user);
234         }
235         
236         private static RestResponse uploadSnmpTrapArtifact(String filepath, String zipArtifact, String vspid, User user,
237                         String vspComponentId) throws FileNotFoundException, IOException, ClientProtocolException {
238                 Config config = Utils.getConfig();
239                 String snmpTrapUrl = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/versions/0.1/components/%s/monitors/snmp-trap/upload", 
240                                 config.getCatalogBeHost(),config.getCatalogBePort(), vspid, vspComponentId);
241                 return uploadFile(filepath, zipArtifact, snmpTrapUrl, user);
242         }
243         
244         private static RestResponse deleteSnmpArtifact(String componentId, String vspId, User user, SnmpTypeEnum snmpType) throws Exception
245         {
246                 Config config = Utils.getConfig();
247                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/versions/0.1/components/%s/monitors/%s", 
248                                 config.getCatalogBeHost(),config.getCatalogBePort(), vspId, componentId, snmpType.getValue());
249                 String userId = user.getUserId();
250
251                 Map<String, String> headersMap = prepareHeadersMap(userId);
252
253                 HttpRequest http = new HttpRequest();
254                 RestResponse response = http.httpSendDelete(url, headersMap);
255                 return response;
256         }
257         
258         
259
260         private static String getVSPComponentId(String vspid, User user) throws Exception, JSONException {
261                 RestResponse components = getVSPComponents(vspid, user);
262                 String response = components.getResponse();
263                 Map<String, Object> responseMap = (Map<String, Object>) JSONValue.parse(response);
264                 JSONArray results = (JSONArray)responseMap.get("results");
265                 for (Object res : results){
266                         Map<String, Object> compMap= (Map<String, Object>) JSONValue.parse(res.toString());
267                         String componentId = compMap.get("id").toString();
268                         return componentId;
269                 }
270                 return null;
271         }
272         
273         private static RestResponse getVSPComponents(String vspid, User user) throws Exception{
274                 Config config = Utils.getConfig();
275                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/versions/0.1/components", config.getCatalogBeHost(),config.getCatalogBePort(), vspid);
276                 String userId = user.getUserId();
277
278                 Map<String, String> headersMap = prepareHeadersMap(userId);
279
280                 HttpRequest http = new HttpRequest();
281                 RestResponse response = http.httpSendGet(url, headersMap);
282                 return response;
283         }
284
285         public static void createVendorLicense(User user) throws Exception {
286                 SetupCDTest.getExtendTest().log(Status.INFO, "Starting to create the vendor license");
287                 vendorLicenseName = "ciLicense" + getShortUUID();
288                 RestResponse vendorLicenseResponse = createVendorLicenseModels_1(vendorLicenseName, user);
289                 assertEquals("did not succeed to create vendor license model", 200,
290                                 vendorLicenseResponse.getErrorCode().intValue());
291                 vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "value");
292
293                 RestResponse vendorKeyGroupsResponse = createVendorKeyGroups_2(vendorId, user);
294                 assertEquals("did not succeed to create vendor key groups", 200,
295                                 vendorKeyGroupsResponse.getErrorCode().intValue());
296                 String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(), "value");
297
298                 RestResponse vendorEntitlementPool = createVendorEntitlementPool_3(vendorId, user);
299                 assertEquals("did not succeed to create vendor entitlement pool", 200,
300                                 vendorEntitlementPool.getErrorCode().intValue());
301                 String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(),
302                                 "value");
303
304                 RestResponse vendorLicenseFeatureGroups = createVendorLicenseFeatureGroups_4(vendorId, keyGroupId,
305                                 entitlementPoolId, user);
306                 assertEquals("did not succeed to create vendor license feature groups", 200,
307                                 vendorLicenseFeatureGroups.getErrorCode().intValue());
308                 featureGroupId = ResponseParser.getValueFromJsonResponse(vendorLicenseFeatureGroups.getResponse(), "value");
309
310                 RestResponse vendorLicenseAgreement = createVendorLicenseAgreement_5(vendorId, featureGroupId, user);
311                 assertEquals("did not succeed to create vendor license agreement", 200,
312                                 vendorLicenseAgreement.getErrorCode().intValue());
313                 vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(),
314                                 "value");
315
316                 RestResponse checkinVendorLicense = checkinVendorLicense(vendorId, user);
317                 assertEquals("did not succeed to checkin vendor license", 200, checkinVendorLicense.getErrorCode().intValue());
318
319                 RestResponse submitVendorLicense = submitVendorLicense(vendorId, user);
320                 assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());
321
322                 SetupCDTest.getExtendTest().log(Status.INFO, "Succeeded in creating the vendor license");
323         }
324
325         private static String getShortUUID() {
326                 return UUID.randomUUID().toString().split("-")[0];
327         }
328
329         private static RestResponse actionOnComponent(String vspid, String action, String onboardComponent, User user)
330                         throws Exception {
331                 Config config = Utils.getConfig();
332                 String url = String.format("http://%s:%s/onboarding-api/v1.0/" + onboardComponent + "/%s/versions/0.1/actions",
333                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
334                 String userId = user.getUserId();
335
336                 JSONObject jObject = new JSONObject();
337                 jObject.put("action", action);
338
339                 Map<String, String> headersMap = prepareHeadersMap(userId);
340
341                 HttpRequest http = new HttpRequest();
342                 RestResponse response = http.httpSendPut(url, jObject.toString(), headersMap);
343                 return response;
344         }
345
346         public static RestResponse checkinVendorLicense(String vspid, User user) throws Exception {
347                 return actionOnComponent(vspid, "Checkin", "vendor-license-models", user);
348         }
349
350         public static RestResponse submitVendorLicense(String vspid, User user) throws Exception {
351                 return actionOnComponent(vspid, "Submit", "vendor-license-models", user);
352         }
353
354         public static RestResponse createVendorLicenseModels_1(String name, User user) throws Exception {
355                 Config config = Utils.getConfig();
356                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models", config.getCatalogBeHost(),
357                                 config.getCatalogBePort());
358                 String userId = user.getUserId();
359
360                 JSONObject jObject = new JSONObject();
361                 jObject.put("vendorName", name);
362                 jObject.put("description", "new vendor license model");
363                 jObject.put("iconRef", "icon");
364
365                 Map<String, String> headersMap = prepareHeadersMap(userId);
366
367                 HttpRequest http = new HttpRequest();
368                 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
369                 return response;
370
371         }
372
373         public static RestResponse createVendorLicenseAgreement_5(String vspid, String featureGroupId, User user)
374                         throws Exception {
375                 Config config = Utils.getConfig();
376                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/versions/0.1/license-agreements",
377                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
378                 String userId = user.getUserId();
379
380                 JSONObject licenseTermpObject = new JSONObject();
381                 licenseTermpObject.put("choice", "Fixed_Term");
382                 licenseTermpObject.put("other", "");
383
384                 JSONObject jObjectBody = new JSONObject();
385                 jObjectBody.put("name", "abc");
386                 jObjectBody.put("description", "new vendor license agreement");
387                 jObjectBody.put("requirementsAndConstrains", "abc");
388                 jObjectBody.put("licenseTerm", licenseTermpObject);
389                 jObjectBody.put("addedFeatureGroupsIds", Arrays.asList(featureGroupId).toArray());
390
391                 Map<String, String> headersMap = prepareHeadersMap(userId);
392
393                 HttpRequest http = new HttpRequest();
394                 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
395                 return response;
396         }
397
398         public static RestResponse createVendorLicenseFeatureGroups_4(String vspid, String licenseKeyGroupId,
399                         String entitlementPoolId, User user) throws Exception {
400                 Config config = Utils.getConfig();
401                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/versions/0.1/feature-groups",
402                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
403                 String userId = user.getUserId();
404
405                 JSONObject jObject = new JSONObject();
406                 jObject.put("name", "xyz");
407                 jObject.put("description", "new vendor license feature groups");
408                 jObject.put("partNumber", "123abc456");
409                 jObject.put("addedLicenseKeyGroupsIds", Arrays.asList(licenseKeyGroupId).toArray());
410                 jObject.put("addedEntitlementPoolsIds", Arrays.asList(entitlementPoolId).toArray());
411
412                 Map<String, String> headersMap = prepareHeadersMap(userId);
413
414                 HttpRequest http = new HttpRequest();
415                 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
416                 return response;
417
418         }
419
420         public static RestResponse createVendorEntitlementPool_3(String vspid, User user) throws Exception {
421                 Config config = Utils.getConfig();
422                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/versions/0.1/entitlement-pools",
423                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
424                 String userId = user.getUserId();
425
426                 JSONObject jEntitlementMetricObject = new JSONObject();
427                 jEntitlementMetricObject.put("choice", "CPU");
428                 jEntitlementMetricObject.put("other", "");
429
430                 JSONObject jAggregationFunctionObject = new JSONObject();
431                 jAggregationFunctionObject.put("choice", "Peak");
432                 jAggregationFunctionObject.put("other", "");
433
434                 JSONObject jOperationalScope = new JSONObject();
435                 jOperationalScope.put("choices", Arrays.asList("Availability_Zone").toArray());
436                 jOperationalScope.put("other", "");
437
438                 JSONObject jTimeObject = new JSONObject();
439                 jTimeObject.put("choice", "Hour");
440                 jTimeObject.put("other", "");
441
442                 JSONObject jObjectBody = new JSONObject();
443                 jObjectBody.put("name", "def");
444                 jObjectBody.put("description", "new vendor license entitlement pool");
445                 jObjectBody.put("thresholdValue", "23");
446                 jObjectBody.put("thresholdUnits", "Absolute");
447                 jObjectBody.put("entitlementMetric", jEntitlementMetricObject);
448                 jObjectBody.put("increments", "abcd");
449                 jObjectBody.put("aggregationFunction", jAggregationFunctionObject);
450                 jObjectBody.put("operationalScope", jOperationalScope);
451                 jObjectBody.put("time", jTimeObject);
452                 jObjectBody.put("manufacturerReferenceNumber", "123aaa");
453
454                 Map<String, String> headersMap = prepareHeadersMap(userId);
455
456                 HttpRequest http = new HttpRequest();
457                 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
458                 return response;
459         }
460
461         public static RestResponse createVendorKeyGroups_2(String vspid, User user) throws Exception {
462                 Config config = Utils.getConfig();
463                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/versions/0.1/license-key-groups",
464                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
465                 String userId = user.getUserId();
466
467                 JSONObject jOperationalScope = new JSONObject();
468                 jOperationalScope.put("choices", Arrays.asList("Tenant").toArray());
469                 jOperationalScope.put("other", "");
470
471                 JSONObject jObjectBody = new JSONObject();
472                 jObjectBody.put("name", "keyGroup");
473                 jObjectBody.put("description", "new vendor license key group");
474                 jObjectBody.put("operationalScope", jOperationalScope);
475                 jObjectBody.put("type", "Universal");
476
477                 Map<String, String> headersMap = prepareHeadersMap(userId);
478
479                 HttpRequest http = new HttpRequest();
480                 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
481                 return response;
482         }
483
484         public static Pair<RestResponse, Map<String, String>> createNewVendorSoftwareProduct(String name, String vendorName, String vendorId,
485                         String licenseAgreementId, String featureGroupsId, User user) throws Exception {
486                 
487                 Map<String, String> vspMetadta = new HashMap<String, String>();
488                 
489                 Config config = Utils.getConfig();
490                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products",
491                                 config.getCatalogBeHost(), config.getCatalogBePort());
492
493                 String userId = user.getUserId();
494
495                 JSONObject jlicensingDataObj = new JSONObject();
496                 jlicensingDataObj.put("licenseAgreement", licenseAgreementId);
497                 jlicensingDataObj.put("featureGroups", Arrays.asList(featureGroupsId).toArray());
498                 
499                 JSONObject jlicensingVersionObj = new JSONObject();
500                 jlicensingVersionObj.put("id", "1.0");
501                 jlicensingVersionObj.put("label", "1.0");
502
503                 JSONObject jObject = new JSONObject();
504                 jObject.put("name", name);
505                 jObject.put("description", "new VSP description");
506                 jObject.put("category", "resourceNewCategory.generic");
507                 jObject.put("subCategory", "resourceNewCategory.generic.database");
508                 jObject.put("licensingVersion", jlicensingVersionObj);
509                 jObject.put("vendorName", vendorName);
510                 jObject.put("vendorId", vendorId);
511                 jObject.put("icon", "icon");
512                 jObject.put("licensingData", jlicensingDataObj);
513                 
514                 vspMetadta.put("description", jObject.getString("description"));
515                 vspMetadta.put("category", jObject.getString("category"));
516                 vspMetadta.put("subCategory", jObject.getString("subCategory").split("\\.")[2]);
517                 
518                 Map<String, String> headersMap = prepareHeadersMap(userId);
519                 HttpRequest http = new HttpRequest();
520
521                 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
522                 
523                 return new Pair<RestResponse, Map<String, String>>(response, vspMetadta);
524         }
525         
526         public static RestResponse getVendorSoftwareProduct(Map vspObject, User user) throws Exception {
527                 Config config = Utils.getConfig();
528                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/" + vspObject.get("vspId"),
529                                 config.getCatalogBeHost(), config.getCatalogBePort());
530
531                 String userId = user.getUserId();
532
533                 Map<String, String> headersMap = prepareHeadersMap(userId);
534                 HttpRequest http = new HttpRequest();
535
536                 RestResponse response = http.httpsSendGet(url, headersMap);
537
538                 return response;
539         }
540         
541         public static RestResponse validateUpload(String vspid, User user) throws Exception {
542                 Config config = Utils.getConfig();
543                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/versions/0.1/orchestration-template-candidate/process",
544                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
545
546                 String userId = user.getUserId();
547
548                 Map<String, String> headersMap = prepareHeadersMap(userId);
549                 HttpRequest http = new HttpRequest();
550                 
551                 String body =null;
552
553                 RestResponse response = http.httpSendPut(url, body, headersMap);
554
555                 return response;
556         }
557
558         public static RestResponse uploadHeatPackage(String filepath, String filename, String vspid, User user) throws Exception {
559                 Config config = Utils.getConfig();
560                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/versions/0.1/orchestration-template-candidate", config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
561                 return uploadFile(filepath, filename, url, user);
562         }
563
564         private static RestResponse uploadFile(String filepath, String filename, String url, User user)
565                         throws FileNotFoundException, IOException, ClientProtocolException {
566                 CloseableHttpResponse response = null;
567
568                 MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
569                 mpBuilder.addPart("upload", new FileBody(getTestZipFile(filepath, filename)));
570
571                 Map<String, String> headersMap = prepareHeadersMap(user.getUserId());
572                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "multipart/form-data");
573
574                 CloseableHttpClient client = HttpClients.createDefault();
575                 try {
576                         HttpPost httpPost = new HttpPost(url);
577                         RestResponse restResponse = new RestResponse();
578
579                         Iterator<String> iterator = headersMap.keySet().iterator();
580                         while (iterator.hasNext()) {
581                                 String key = iterator.next();
582                                 String value = headersMap.get(key);
583                                 httpPost.addHeader(key, value);
584                         }
585                         httpPost.setEntity(mpBuilder.build());
586                         response = client.execute(httpPost);
587                         HttpEntity entity = response.getEntity();
588                         String responseBody = null;
589                         if (entity != null) {
590                                 InputStream instream = entity.getContent();
591                                 StringWriter writer = new StringWriter();
592                                 IOUtils.copy(instream, writer);
593                                 responseBody = writer.toString();
594                                 try {
595
596                                 } finally {
597                                         instream.close();
598                                 }
599                         }
600
601                         restResponse.setErrorCode(response.getStatusLine().getStatusCode());
602                         restResponse.setResponse(responseBody);
603
604                         return restResponse;
605
606                 } finally {
607                         closeResponse(response);
608                         closeHttpClient(client);
609
610                 }
611         }
612
613         private static void closeResponse(CloseableHttpResponse response) {
614                 try {
615                         if (response != null) {
616                                 response.close();
617                         }
618                 } catch (IOException e) {
619                         System.out.println(String.format("failed to close client or response: %s", e.getMessage()));
620                 }
621         }
622
623         private static void closeHttpClient(CloseableHttpClient client) {
624                 try {
625                         if (client != null) {
626                                 client.close();
627                         }
628                 } catch (IOException e) {
629                         System.out.println(String.format("failed to close client or response: %s", e.getMessage()));
630                 }
631         }
632
633         private static File getTestZipFile(String filepath, String filename) throws IOException {
634                 Config config = Utils.getConfig();
635                 String sourceDir = config.getImportResourceTestsConfigDir();
636                 java.nio.file.Path filePath = FileSystems.getDefault().getPath(filepath + File.separator + filename);
637                 return filePath.toFile();
638         }
639
640         public static RestResponse checkinVendorSoftwareProduct(String vspid, User user) throws Exception {
641                 return actionOnComponent(vspid, "Checkin", "vendor-software-products", user);
642         }
643         
644         private static RestResponse checkoutVendorSoftwareProduct(String vspid, User user) throws Exception {
645                 return actionOnComponent(vspid, "Checkout", "vendor-software-products", user);
646         }
647
648         public static RestResponse submitVendorSoftwareProduct(String vspid, User user) throws Exception {
649                 return actionOnComponent(vspid, "Submit", "vendor-software-products", user);
650         }
651
652         public static RestResponse createPackageOfVendorSoftwareProduct(String vspid, User user) throws Exception {
653                 return actionOnComponent(vspid, "Create_Package", "vendor-software-products", user);
654         }
655
656         protected static Map<String, String> prepareHeadersMap(String userId) {
657                 Map<String, String> headersMap = new HashMap<String, String>();
658                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
659                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
660                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
661                 return headersMap;
662         }
663
664         
665         private static void importUpdateVSP(Pair<String, Map<String, String>> vsp, boolean isUpdate) throws Exception{
666                 String vspName = vsp.left;
667                 Map<String, String> vspMetadata = vsp.right;
668                 boolean vspFound = HomePage.searchForVSP(vspName);
669
670                 if (vspFound){
671                         
672                         List<WebElement> elemenetsFromTable = HomePage.getElemenetsFromTable();
673 //                      WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), 30);
674 //                      WebElement findElement = wait.until(ExpectedConditions.visibilityOf(elemenetsFromTable.get(1)));
675 //                      findElement.click();
676                         elemenetsFromTable.get(1).click();
677                         GeneralUIUtils.waitForLoader();
678                         
679                         if (isUpdate){
680                                 GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ImportVfRepository.UPDATE_VSP.getValue());
681
682                         }
683                         else{
684                                 GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ImportVfRepository.IMPORT_VSP.getValue());
685                         }
686                         
687                         String lifeCycleState = ResourceGeneralPage.getLifeCycleState();
688                         boolean needCheckout = lifeCycleState.equals(LifeCycleStateEnum.CHECKIN.getValue()) || lifeCycleState.equals(LifeCycleStateEnum.CERTIFIED.getValue());
689                         if (needCheckout)
690                         {
691                                 try {
692                                         ResourceGeneralPage.clickCheckoutButton();
693                                         Assert.assertTrue(ResourceGeneralPage.getLifeCycleState().equals(LifeCycleStateEnum.CHECKOUT.getValue()), "Did not succeed to checkout");
694                                         
695                                 } catch (Exception e) {
696                                         ExtentTestActions.log(Status.ERROR, "Did not succeed to checkout");
697                                         e.printStackTrace();
698                                 }
699                                 GeneralUIUtils.waitForLoader();
700                         }
701                         
702                         //Metadata verification 
703                         VfVerificator.verifyOnboardedVnfMetadata(vspName, vspMetadata);
704                         
705                         ExtentTestActions.log(Status.INFO, "Clicking create/update VNF");
706                         String duration = GeneralUIUtils.getActionDuration(() -> waitUntilVnfCreated());
707                     ExtentTestActions.log(Status.INFO, "Succeeded in importing/updating " + vspName, duration);
708                 }
709                 else{
710                         Assert.fail("Did not find VSP named " + vspName);
711                 }
712         }
713
714         private static void waitUntilVnfCreated() {
715                 GeneralUIUtils.clickOnElementByTestIdWithoutWait(DataTestIdEnum.GeneralElementsEnum.CREATE_BUTTON.getValue());
716                 GeneralUIUtils.waitForLoader(60*10);
717                 GeneralUIUtils.waitForAngular();
718                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.CHECKIN_BUTTON.getValue());
719         }
720         
721         public static void updateVSP(Pair<String, Map<String, String>> vsp) throws Exception{
722                 ExtentTestActions.log(Status.INFO, "Updating VSP " + vsp.left);
723                 importUpdateVSP(vsp, true);
724         }
725         
726         public static void importVSP(Pair<String, Map<String, String>> vsp) throws Exception{
727                 ExtentTestActions.log(Status.INFO, "Importing VSP " + vsp.left);
728                 importUpdateVSP(vsp, false);
729         }
730
731         public static void updateVnfAndValidate(String filepath, Pair<String, Map<String, String>> vsp, String updatedVnfFile, User user) throws Exception, Throwable {
732                 ExtentTestActions.log(Status.INFO, String.format("Going to update the VNF with %s......", updatedVnfFile));
733                 System.out.println(String.format("Going to update the VNF with %s......", updatedVnfFile));
734                 
735                 Map<String, String> vspMap = vsp.right;
736                 String vspId = vspMap.get("vspId");
737                 
738                 updateVendorSoftwareProduct(vspId, updatedVnfFile, filepath, user);
739                 HomePage.showVspRepository();
740                 updateVSP(vsp);
741                 ResourceGeneralPage.getLeftMenu().moveToDeploymentArtifactScreen();
742                 DeploymentArtifactPage.verifyArtifactsExistInTable(filepath, updatedVnfFile);
743         }
744
745         public static Pair<String, Map<String, String>> onboardAndValidate(String filepath, String vnfFile, User user) throws Exception {
746                 ExtentTestActions.log(Status.INFO, String.format("Going to onboard the VNF %s", vnfFile));
747                 System.out.println(String.format("Going to onboard the VNF %s", vnfFile));
748         
749                 createVendorLicense(user);
750                 Pair<String, Map<String, String>> createVendorSoftwareProduct = createVendorSoftwareProduct(vnfFile, filepath, user);
751                 String vspName = createVendorSoftwareProduct.left;
752                 
753                 DownloadManager.downloadCsarByNameFromVSPRepository(vspName, createVendorSoftwareProduct.right.get("vspId"));
754                 File latestFilefromDir = FileHandling.getLastModifiedFileFromDir();
755                 
756                 ExtentTestActions.log(Status.INFO, String.format("Searching for onboarded %s", vnfFile));
757                 HomePage.showVspRepository();
758                 ExtentTestActions.log(Status.INFO,String.format("Going to import %s", vnfFile.substring(0, vnfFile.indexOf("."))));
759                 importVSP(createVendorSoftwareProduct);
760                 
761                 ResourceGeneralPage.getLeftMenu().moveToDeploymentArtifactScreen();
762                 
763                 // Verify deployment artifacts
764                 Map<String, Object> combinedMap = ArtifactFromCsar.combineHeatArtifacstWithFolderArtifacsToMap(latestFilefromDir.getAbsolutePath());
765                 
766                 LinkedList<HeatMetaFirstLevelDefinition> deploymentArtifacts = ((LinkedList<HeatMetaFirstLevelDefinition>) combinedMap.get("Deployment"));
767                 ArtifactsCorrelationManager.addVNFartifactDetails(vspName, deploymentArtifacts);
768                 
769                 for(HeatMetaFirstLevelDefinition deploymentArtifact: deploymentArtifacts) {
770                         if(deploymentArtifact.getType().equals("HEAT_ENV")) {
771                                 continue;
772                         } else if(deploymentArtifact.getFileName().contains(".")) {
773                                 ArtifactUIUtils.validateArtifactNameVersionType(deploymentArtifact.getFileName().trim().substring(0, deploymentArtifact.getFileName().lastIndexOf(".")), "1", deploymentArtifact.getType());
774                         } else {
775                                 ArtifactUIUtils.validateArtifactNameVersionType(deploymentArtifact.getFileName().trim(), "1", deploymentArtifact.getType());
776                         }
777                         
778                 }
779                 
780                 DeploymentArtifactPage.verifyArtifactsExistInTable(filepath, vnfFile);
781                 return createVendorSoftwareProduct;
782         }
783
784 }
785
786 enum SnmpTypeEnum{
787         SNMP_POLL ("snmp"),
788         SNMP_TRAP ("snmp-trap");
789         
790         private String value;
791
792         public String getValue() {
793                 return value;
794         }
795
796         private SnmpTypeEnum(String value) {
797                 this.value = value;
798         }
799         
800         
801 }