Create on boarding docker
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / general / OnboardingUtillViaApis.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.general;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import java.io.BufferedInputStream;
26 import java.io.BufferedOutputStream;
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35 import com.aventstack.extentreports.Status;
36 import com.clearspring.analytics.util.Pair;
37 import com.google.gson.Gson;
38 import fj.data.Either;
39
40 import org.apache.commons.codec.binary.Base64;
41 import org.openecomp.sdc.be.model.Component;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.ci.tests.api.Urls;
45 import org.openecomp.sdc.ci.tests.config.Config;
46 import org.openecomp.sdc.ci.tests.datatypes.AmdocsLicenseMembers;
47 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
48 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
49 import org.openecomp.sdc.ci.tests.datatypes.VendorSoftwareProductObject;
50 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
52 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
53 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
54 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
55 import org.openecomp.sdc.ci.tests.utils.Utils;
56 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
57
58 import com.clearspring.analytics.util.Pair;
59 import com.google.gson.Gson;
60
61 import fj.data.Either;
62
63 public class OnboardingUtillViaApis {
64
65 //      protected static Map<String, String> prepareHeadersMap(String userId) {
66 //              Map<String, String> headersMap = new HashMap<String, String>();
67 //              headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
68 //              headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
69 //              headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
70 //              return headersMap;
71 //      }
72
73         public static Pair<String, VendorSoftwareProductObject> createVspViaApis(ResourceReqDetails resourceReqDetails, String filepath, String vnfFile, User user) throws Exception {
74
75                 VendorSoftwareProductObject vendorSoftwareProductObject = new VendorSoftwareProductObject();
76
77                 AmdocsLicenseMembers amdocsLicenseMembers = VendorLicenseModelRestUtils.createVendorLicense(user);
78                 Pair<String, VendorSoftwareProductObject> createVendorSoftwareProduct = VendorSoftwareProductRestUtils.createVendorSoftwareProduct(resourceReqDetails, vnfFile, filepath, user, amdocsLicenseMembers);
79                 VendorSoftwareProductObject map = createVendorSoftwareProduct.right;
80                 vendorSoftwareProductObject.setAttContact(map.getAttContact());
81                 vendorSoftwareProductObject.setCategory(map.getCategory());
82                 vendorSoftwareProductObject.setComponentId(map.getComponentId());
83                 vendorSoftwareProductObject.setDescription(map.getDescription());
84                 vendorSoftwareProductObject.setSubCategory(map.getSubCategory());
85                 vendorSoftwareProductObject.setVendorName(map.getVendorName());
86                 vendorSoftwareProductObject.setVspId(map.getVspId());
87                 Pair<String, VendorSoftwareProductObject> pair = new Pair<String, VendorSoftwareProductObject>(createVendorSoftwareProduct.left, vendorSoftwareProductObject);
88                 return pair;
89         }
90         
91 /*      public static Resource createResourceFromVSP(Pair<String, Map<String, String>> createVendorSoftwareProduct, String vspName) throws Exception {
92                 List<String> tags = new ArrayList<>();
93                 tags.add(vspName);
94                 Map<String, String> map = createVendorSoftwareProduct.right;
95                 ResourceReqDetails resourceDetails = new ResourceReqDetails();
96                 resourceDetails.setCsarUUID(map.get("vspId"));
97                 resourceDetails.setCsarVersion("1.0");
98                 resourceDetails.setName(vspName);
99                 resourceDetails.setTags(tags);
100                 resourceDetails.setDescription(map.get("description"));
101                 resourceDetails.setResourceType(map.get("componentType"));
102                 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(), ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
103                 resourceDetails.setVendorName(map.get("vendorName"));
104                 resourceDetails.setVendorRelease("1.0");
105                 resourceDetails.setResourceType("VF");
106                 resourceDetails.setResourceVendorModelNumber("666");
107                 resourceDetails.setContactId(map.get("attContact"));
108                 resourceDetails.setIcon("defaulticon");
109                 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
110                 
111                 return resource; 
112         }*/
113         public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails) throws Exception {
114                 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
115                 return resource;
116
117         }
118         
119         public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails, UserRoleEnum userRole) throws Exception {
120                 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, userRole, true).left().value();
121                 return resource;
122
123         }
124         
125         public static void downloadToscaCsarToDirectory(Component component, File file) {
126                 try {
127                         Either<String, RestResponse> componentToscaArtifactPayload = AtomicOperationUtils.getComponenetArtifactPayload(component, "assettoscacsar");
128                         if(componentToscaArtifactPayload.left().value() != null){
129                                 convertPayloadToFile(componentToscaArtifactPayload.left().value(), file);
130                         }
131                 } catch (Exception e) {
132                         e.printStackTrace();
133                 }
134         }
135         
136 //      public static void convertPayloadToFile(String payload, File file, boolean isBased64, boolean isSdcFormat) throws IOException{
137 //              
138 //              Gson gson = new Gson();
139 //              byte[] byteArray = null;
140 //              Map<String, String> fromJson;
141 //              @SuppressWarnings("unchecked")
142 //              String string = null;// = fromJson.get("base64Contents").toString();
143 //              if(isSdcFormat){
144 //                      fromJson = gson.fromJson(payload, Map.class);
145 //                      string = fromJson.get("base64Contents").toString();
146 //              }else if (isBased64) {
147 //                      byteArray = Base64.decode(string.getBytes(StandardCharsets.UTF_8));
148 //              }else{
149 //                      byteArray = payload.getBytes(StandardCharsets.UTF_8);
150 //              }
151 //              File downloadedFile = new File(file.getAbsolutePath());
152 //              FileOutputStream fos = new FileOutputStream(downloadedFile);
153 //              fos.write(byteArray);
154 //              fos.flush();
155 //              fos.close();
156 //              
157 //      }
158
159         public static void convertPayloadToFile(String payload, File file) throws IOException{
160                 
161                 Gson gson = new Gson();
162                 @SuppressWarnings("unchecked")
163                 Map<String, String> fromJson = gson.fromJson(payload, Map.class);
164                 String string = fromJson.get("base64Contents").toString();
165                 byte[] byteArray = Base64.decodeBase64(string.getBytes(StandardCharsets.UTF_8));
166                 File downloadedFile = new File(file.getAbsolutePath());
167                 FileOutputStream fos = new FileOutputStream(downloadedFile);
168                 fos.write(byteArray);
169                 fos.flush();
170                 fos.close();
171         }
172         
173         
174         public static void convertPayloadToZipFile(String payload, File file) throws IOException{
175                 
176                 byte[] byteArray = payload.getBytes(StandardCharsets.ISO_8859_1);
177                 File downloadedFile = new File(file.getAbsolutePath());
178                 FileOutputStream fos = new FileOutputStream(downloadedFile);
179                 fos.write(byteArray);
180                 fos.flush();
181                 fos.close();
182                 
183                 
184 //              ZipOutputStream fos = null;
185 //              
186 //              
187 //              for (Charset charset : Charset.availableCharsets().values()) {
188 //                      try{
189 //              //              System.out.println("How to do it???");
190 //                              File downloadedFile = new File(file.getAbsolutePath() + "_" + charset +".csar");
191 //                              fos = new ZipOutputStream(new FileOutputStream(downloadedFile));
192 //                              byte[] byteArray = payload.getBytes(charset);
193 //                              fos.write(byteArray);
194 //                              fos.flush();
195 //                              
196 //                      }
197 //                      catch(Exception e){
198 //                              fos.close();
199 //                      }
200 //              }
201                 System.out.println("");
202                 
203 //              ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(byteArray));
204 //              ZipEntry entry = null;
205 //              while ((entry = zipStream.getNextEntry()) != null) {
206 //
207 //                  String entryName = entry.getName();
208 //
209 //                  FileOutputStream out = new FileOutputStream(file+"/"+entryName);
210 //
211 //                  byte[] byteBuff = new byte[4096];
212 //                  int bytesRead = 0;
213 //                  while ((bytesRead = zipStream.read(byteBuff)) != -1)
214 //                  {
215 //                      out.write(byteBuff, 0, bytesRead);
216 //                  }
217 //
218 //                  out.close();
219 //                  zipStream.closeEntry();
220 //              }
221 //              zipStream.close();
222 //              
223                 
224                 
225                 
226                 BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(payload.getBytes(StandardCharsets.ISO_8859_1)));
227                 String filePath = file.toString();
228                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
229                 int inByte;
230                 while((inByte = bis.read()) != -1) bos.write(inByte);
231                 bis.close();
232                 bos.close();
233         }
234         
235         public static Either<String, RestResponse> getVendorSoftwareProduct(String vspId, User user, Boolean validateState) throws Exception {
236
237                 Config config = Utils.getConfig();
238                 String url = String.format(Urls.GET_VENDOR_SOFTWARE_PRODUCT, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspId);
239                 String userId = user.getUserId();
240                 Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
241                 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), "123456");
242                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "*/*");
243                 headersMap.put("Accept-Encoding", "gzip, deflate, br");
244                 HttpRequest http = new HttpRequest();
245                 RestResponse response = http.httpSendGet(url, headersMap);
246                 if (validateState) {
247                         assertTrue("add property to resource failed: " + response.getResponseMessage(), response.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
248                 }
249                 if (response.getErrorCode() != BaseRestUtils.STATUS_CODE_SUCCESS && response.getResponse().getBytes() == null && response.getResponse().getBytes().length == 0) {
250                         return Either.right(response);
251                 }
252                 return Either.left(response.getResponse());
253                 
254         }
255
256         public static ResourceReqDetails prepareOnboardedResourceDetailsBeforeCreate(ResourceReqDetails resourceDetails, VendorSoftwareProductObject vendorSoftwareProductObject) {
257
258                 List<String> tags = new ArrayList<>();
259                 tags.add(vendorSoftwareProductObject.getName());
260 //              ResourceReqDetails resourceDetails = new ResourceReqDetails();
261                 resourceDetails.setCsarUUID(vendorSoftwareProductObject.getVspId());
262                 resourceDetails.setCsarVersion(vendorSoftwareProductObject.getVersion());
263                 resourceDetails.setName(vendorSoftwareProductObject.getName());
264                 resourceDetails.setTags(tags);
265                 resourceDetails.setDescription(vendorSoftwareProductObject.getDescription());
266 //              resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(), ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
267                 resourceDetails.setVendorName(vendorSoftwareProductObject.getVendorName());
268 //              resourceDetails.setVendorRelease("1.0");
269                 resourceDetails.setResourceType("VF");
270                 resourceDetails.setResourceVendorModelNumber("666");
271                 resourceDetails.setContactId(vendorSoftwareProductObject.getAttContact());
272 //              resourceDetails.setIcon("defaulticon");
273
274                 return resourceDetails;
275         }
276         
277         public static ServiceReqDetails prepareServiceDetailsBeforeCreate(User user) {
278
279                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(ServiceCategoriesEnum.NETWORK_L4, user);
280                 serviceDetails.setServiceType("MyServiceType");
281                 serviceDetails.setServiceRole("MyServiceRole");
282                 serviceDetails.setNamingPolicy("MyServiceNamingPolicy");
283                 serviceDetails.setEcompGeneratedNaming(false);
284                 
285                 return serviceDetails;
286         }
287 }