[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / sanity / OnboardViaApis.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.execute.sanity;
22
23 import static org.testng.AssertJUnit.assertEquals;
24
25 import java.awt.AWTException;
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.nio.charset.StandardCharsets;
29 import java.sql.Timestamp;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.UUID;
36
37 import org.bouncycastle.util.encoders.Base64;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.Service;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
45 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
46 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
47 import org.openecomp.sdc.ci.tests.utilities.OnboardingUtils;
48 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
49 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
50 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
52 import org.slf4j.LoggerFactory;
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.DataProvider;
55 import org.testng.annotations.Test;
56
57 import com.clearspring.analytics.util.Pair;
58 import com.google.gson.Gson;
59
60 import ch.qos.logback.classic.Level;
61 import ch.qos.logback.classic.LoggerContext;
62 import fj.data.Either;
63
64 public class OnboardViaApis{
65         
66
67         private static final String FULL_PATH = "C://tmp//CSARs//";
68         
69         public static Object[][] provideData(Object[] fileNamesFromFolder, String filepath) {
70                 Object[][] arObject = new Object[fileNamesFromFolder.length][];
71
72                 int index = 0;
73                 for (Object obj : fileNamesFromFolder) {
74                         arObject[index++] = new Object[] { filepath, obj };
75                 }
76                 return arObject;
77         }
78
79         @DataProvider(name = "VNF_List" , parallel = false)
80         private static final Object[][] VnfList() throws Exception {
81                 String filepath = FileHandling.getVnfRepositoryPath();
82                 
83                 Object[] fileNamesFromFolder = FileHandling.getZipFileNamesFromFolder(filepath);
84                 System.out.println(String.format("There are %s zip file(s) to test", fileNamesFromFolder.length));
85                 return provideData(fileNamesFromFolder, filepath);
86         }
87
88         
89 //-------------------------------------------------------------------------------------------------------
90         User sdncDesignerDetails1 = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
91                 private static String vendorId;
92                 private static String vendorLicenseName;
93                 private static String vendorLicenseAgreementId;
94                 private static String featureGroupId;
95                 ResourceReqDetails resourceDetails;
96                 Timestamp timestamp = new Timestamp(System.currentTimeMillis());
97         
98         
99                 @BeforeMethod
100                 public void before(){
101                         LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
102                         lc.getLogger("org.apache").setLevel(Level.OFF);
103                         lc.getLogger("org.*").setLevel(Level.OFF);
104                         lc.getLogger("org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest").setLevel(Level.OFF);
105                         resourceDetails = ElementFactory.getDefaultResource();
106                         
107                 }
108                 
109         @Test(dataProvider = "VNF_List")
110         public void onboardVNFTestViaApis(String filepath, String vnfFile) throws Exception, Throwable {
111                 Service service = null;
112                 String fullFileName = FULL_PATH + vnfFile + ".csar";
113                 Timestamp timestamp = new Timestamp(System.currentTimeMillis());
114                 System.err.println(timestamp + " Starting test with VNF: " + vnfFile);
115                 service = runOnboardViaApisOnly(filepath, vnfFile);
116                 timestamp = new Timestamp(System.currentTimeMillis());
117                 System.err.println(timestamp + " Finished test with VNF: " + vnfFile);
118                 timestamp = new Timestamp(System.currentTimeMillis());
119                 System.err.println(timestamp + " Starting download service csar file: " + vnfFile);
120                 File file = new File(fullFileName);
121                 downloadToscaCsarToDirectory(service, file);
122                 timestamp = new Timestamp(System.currentTimeMillis());
123                 System.err.println(timestamp + " Finished download service csar file: " + vnfFile);
124                 System.out.println("end");
125                 
126         }
127         
128         public static void downloadToscaCsarToDirectory(Service service, File file) {
129                 try {
130                         Either<String,RestResponse> serviceToscaArtifactPayload = AtomicOperationUtils.getServiceToscaArtifactPayload(service, "assettoscacsar");
131                         if(serviceToscaArtifactPayload.left().value() != null){
132                                 Gson gson = new Gson();
133                                 @SuppressWarnings("unchecked")
134                                 Map<String, String> fromJson = gson.fromJson(serviceToscaArtifactPayload.left().value(), Map.class);
135                                 String string = fromJson.get("base64Contents").toString();
136                                 byte[] byteArray = Base64.decode(string.getBytes(StandardCharsets.UTF_8));
137                                 File downloadedFile = new File(file.getAbsolutePath());
138                                 FileOutputStream fos = new FileOutputStream(downloadedFile);
139                                 fos.write(byteArray);
140                                 fos.flush();
141                                 fos.close();
142                         }
143                         
144                 } catch (Exception e) {
145                         // TODO Auto-generated catch block
146                         e.printStackTrace();
147                 }
148                 
149         }
150         
151         public Service runOnboardViaApisOnly(String filepath, String vnfFile) throws Exception, AWTException {
152                 Timestamp timestamp = new Timestamp(System.currentTimeMillis());
153                 System.err.println(timestamp + " Starting onboard VNF: " + vnfFile);
154                 Pair<String,Map<String,String>> onboardAndValidate = onboardAndValidateViaApi(filepath, vnfFile, sdncDesignerDetails1);
155                 String vspName = onboardAndValidate.left;
156                 timestamp = new Timestamp(System.currentTimeMillis());
157                 System.err.println(timestamp + " Finished onboard VNF: " + vnfFile);
158                 Resource resource = AtomicOperationUtils.getResourceObject(resourceDetails.getUniqueId());
159                 
160                 AtomicOperationUtils.changeComponentState(resource, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true);
161                 resource = AtomicOperationUtils.getResourceObject(resource.getUniqueId());
162                 // create service
163                 
164                 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
165                 Either<ComponentInstance,RestResponse> addComponentInstanceToComponentContainer = AtomicOperationUtils.addComponentInstanceToComponentContainer(resource, service, UserRoleEnum.DESIGNER, true);
166                 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
167                 return service;
168         }       
169         
170         
171         
172         public Pair<String, Map<String, String>> onboardAndValidateViaApi(String filepath, String vnfFile, User user) throws Exception {
173         
174                 createVendorLicense(user);
175                 Pair<String, Map<String, String>> createVendorSoftwareProduct = createVendorSoftwareProduct(vnfFile, filepath, user);
176                 String vspName = createVendorSoftwareProduct.left;
177                 List<String> tags = new ArrayList<>();
178                 tags.add(vspName);
179                 Map<String, String> map = createVendorSoftwareProduct.right;
180                 
181                 resourceDetails.setCsarUUID(map.get("vspId"));
182                 resourceDetails.setCsarVersion("1.0");
183                 resourceDetails.setName(vspName);
184                 resourceDetails.setTags(tags);
185                 resourceDetails.setResourceType(map.get("componentType"));
186                 resourceDetails.setVendorName(map.get("vendorName"));
187                 resourceDetails.setVendorRelease("1.0");
188                 resourceDetails.setResourceType("VF");
189                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncDesignerDetails1);
190                 
191                 return createVendorSoftwareProduct;
192         }
193         
194         public static Pair<String, Map<String, String>> createVendorSoftwareProduct(String HeatFileName, String filepath, User user)
195                         throws Exception {
196                 Pair<String, Map<String, String>> pair = createVSP(HeatFileName, filepath, user);
197                 
198                 String vspid = pair.right.get("vspId");
199                                 
200                 prepareVspForUse(user, vspid);
201                 
202                 return pair;
203         }
204         
205         public static void prepareVspForUse(User user, String vspid) throws Exception {
206                 RestResponse checkin = OnboardingUtils.checkinVendorSoftwareProduct(vspid, user);
207                 assertEquals("did not succeed to checking new VSP", 200, checkin.getErrorCode().intValue());
208
209                 RestResponse submit = OnboardingUtils.submitVendorSoftwareProduct(vspid, user);
210                 assertEquals("did not succeed to submit new VSP", 200, submit.getErrorCode().intValue());
211
212                 RestResponse createPackage = OnboardingUtils.createPackageOfVendorSoftwareProduct(vspid, user);
213                 assertEquals("did not succeed to create package of new VSP ", 200, createPackage.getErrorCode().intValue());
214
215         }
216         public static void createVendorLicense(User user) throws Exception {
217                 vendorLicenseName = "ciLicense" + UUID.randomUUID().toString().split("-")[0];
218                 RestResponse vendorLicenseResponse = OnboardingUtils.createVendorLicenseModels_1(vendorLicenseName, user);
219                 assertEquals("did not succeed to create vendor license model", 200,
220                                 vendorLicenseResponse.getErrorCode().intValue());
221                 vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "value");
222
223                 RestResponse vendorKeyGroupsResponse = OnboardingUtils.createVendorKeyGroups_2(vendorId, user);
224                 assertEquals("did not succeed to create vendor key groups", 200,
225                                 vendorKeyGroupsResponse.getErrorCode().intValue());
226                 String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(), "value");
227
228                 RestResponse vendorEntitlementPool = OnboardingUtils.createVendorEntitlementPool_3(vendorId, user);
229                 assertEquals("did not succeed to create vendor entitlement pool", 200,
230                                 vendorEntitlementPool.getErrorCode().intValue());
231                 String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(),
232                                 "value");
233
234                 RestResponse vendorLicenseFeatureGroups = OnboardingUtils.createVendorLicenseFeatureGroups_4(vendorId, keyGroupId,
235                                 entitlementPoolId, user);
236                 assertEquals("did not succeed to create vendor license feature groups", 200,
237                                 vendorLicenseFeatureGroups.getErrorCode().intValue());
238                 featureGroupId = ResponseParser.getValueFromJsonResponse(vendorLicenseFeatureGroups.getResponse(), "value");
239
240                 RestResponse vendorLicenseAgreement = OnboardingUtils.createVendorLicenseAgreement_5(vendorId, featureGroupId, user);
241                 assertEquals("did not succeed to create vendor license agreement", 200,
242                                 vendorLicenseAgreement.getErrorCode().intValue());
243                 vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(),
244                                 "value");
245
246                 RestResponse checkinVendorLicense = OnboardingUtils.checkinVendorLicense(vendorId, user);
247                 assertEquals("did not succeed to checkin vendor license", 200, checkinVendorLicense.getErrorCode().intValue());
248
249                 RestResponse submitVendorLicense = OnboardingUtils.submitVendorLicense(vendorId, user);
250                 assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());
251
252         }
253         
254         
255         public static Pair<String, Map<String, String>> createVSP(String HeatFileName, String filepath, User user) throws Exception {
256                 String vspName = OnboardingUtils.handleFilename(HeatFileName);
257                 
258                 Pair<RestResponse, Map<String, String>> createNewVspPair = OnboardingUtils.createNewVendorSoftwareProduct(vspName, vendorLicenseName, vendorId, vendorLicenseAgreementId, featureGroupId, user);
259                 RestResponse createNewVendorSoftwareProduct = createNewVspPair.left;
260                 assertEquals("did not succeed to create new VSP", 200,createNewVendorSoftwareProduct.getErrorCode().intValue());
261                 String vspid = ResponseParser.getValueFromJsonResponse(createNewVendorSoftwareProduct.getResponse(), "vspId");
262                 String componentId = ResponseParser.getValueFromJsonResponse(createNewVendorSoftwareProduct.getResponse(), "componentId");
263                 
264                 Map<String, String> vspMeta = createNewVspPair.right;
265                 Map<String, String> vspObject = new HashMap<String, String>();
266                 Iterator<String> iterator = vspMeta.keySet().iterator();
267                 while(iterator.hasNext()){
268                         Object key = iterator.next();
269                         Object value = vspMeta.get(key);
270                         vspObject.put(key.toString(), value.toString());
271                 }
272                 vspObject.put("vspId", vspid);
273                 vspObject.put("componentId", componentId);
274                 vspObject.put("vendorName", vendorLicenseName);
275                 vspObject.put("attContact", user.getUserId());
276                 
277                 RestResponse uploadHeatPackage = OnboardingUtils.uploadHeatPackage(filepath, HeatFileName, vspid, user);
278                 assertEquals("did not succeed to upload HEAT package", 200, uploadHeatPackage.getErrorCode().intValue());
279                 
280                 RestResponse validateUpload = OnboardingUtils.validateUpload(vspid, user);
281                 assertEquals("did not succeed to validate upload process", 200, validateUpload.getErrorCode().intValue());
282                 
283                 Pair<String, Map<String, String>> pair = new Pair<String, Map<String, String>>(vspName, vspObject);
284                 
285                 return pair;
286         }
287
288         
289         
290         
291         
292         
293         
294         
295         
296 //      ----------------------------------------------------------------------------------------------------------------------------------------
297         
298         
299         
300         
301
302
303 }