2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.general;
23 import static org.testng.AssertJUnit.assertTrue;
25 import java.io.BufferedInputStream;
26 import java.io.BufferedOutputStream;
27 import java.io.ByteArrayInputStream;
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;
35 import com.aventstack.extentreports.Status;
36 import com.clearspring.analytics.util.Pair;
37 import com.google.gson.Gson;
38 import fj.data.Either;
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;
58 import com.clearspring.analytics.util.Pair;
59 import com.google.gson.Gson;
61 import fj.data.Either;
63 public class OnboardingUtillViaApis {
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);
73 public static Pair<String, VendorSoftwareProductObject> createVspViaApis(ResourceReqDetails resourceReqDetails, String filepath, String vnfFile, User user) throws Exception {
75 VendorSoftwareProductObject vendorSoftwareProductObject = new VendorSoftwareProductObject();
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);
91 /* public static Resource createResourceFromVSP(Pair<String, Map<String, String>> createVendorSoftwareProduct, String vspName) throws Exception {
92 List<String> tags = new ArrayList<>();
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();
113 public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails) throws Exception {
114 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
119 public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails, UserRoleEnum userRole) throws Exception {
120 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, userRole, true).left().value();
125 public static void downloadToscaCsarToDirectory(Component component, File file) {
127 Either<String, RestResponse> componentToscaArtifactPayload = AtomicOperationUtils.getComponenetArtifactPayload(component, "assettoscacsar");
128 if(componentToscaArtifactPayload.left().value() != null){
129 convertPayloadToFile(componentToscaArtifactPayload.left().value(), file);
131 } catch (Exception e) {
136 // public static void convertPayloadToFile(String payload, File file, boolean isBased64, boolean isSdcFormat) throws IOException{
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();
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));
149 // byteArray = payload.getBytes(StandardCharsets.UTF_8);
151 // File downloadedFile = new File(file.getAbsolutePath());
152 // FileOutputStream fos = new FileOutputStream(downloadedFile);
153 // fos.write(byteArray);
159 public static void convertPayloadToFile(String payload, File file) throws IOException{
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);
174 public static void convertPayloadToZipFile(String payload, File file) throws IOException{
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);
184 // ZipOutputStream fos = null;
187 // for (Charset charset : Charset.availableCharsets().values()) {
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);
197 // catch(Exception e){
201 System.out.println("");
203 // ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(byteArray));
204 // ZipEntry entry = null;
205 // while ((entry = zipStream.getNextEntry()) != null) {
207 // String entryName = entry.getName();
209 // FileOutputStream out = new FileOutputStream(file+"/"+entryName);
211 // byte[] byteBuff = new byte[4096];
212 // int bytesRead = 0;
213 // while ((bytesRead = zipStream.read(byteBuff)) != -1)
215 // out.write(byteBuff, 0, bytesRead);
219 // zipStream.closeEntry();
221 // zipStream.close();
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)));
230 while((inByte = bis.read()) != -1) bos.write(inByte);
235 public static Either<String, RestResponse> getVendorSoftwareProduct(String vspId, User user, Boolean validateState) throws Exception {
237 Config config = Utils.getConfig();
238 String url = String.format(Urls.GET_VENDOR_SOFTWARE_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort(), 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);
247 assertTrue("add property to resource failed: " + response.getResponseMessage(), response.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
249 if (response.getErrorCode() != BaseRestUtils.STATUS_CODE_SUCCESS && response.getResponse().getBytes() == null && response.getResponse().getBytes().length == 0) {
250 return Either.right(response);
252 return Either.left(response.getResponse());
256 public static ResourceReqDetails prepareOnboardedResourceDetailsBeforeCreate(ResourceReqDetails resourceDetails, VendorSoftwareProductObject vendorSoftwareProductObject) {
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");
274 return resourceDetails;
277 public static ServiceReqDetails prepareServiceDetailsBeforeCreate(User user) {
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);
285 return serviceDetails;