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.onap.sdc.backend.ci.tests.utils.general;
23 import com.google.gson.Gson;
24 import fj.data.Either;
25 import org.apache.commons.codec.binary.Base64;
26 import org.onap.sdc.backend.ci.tests.datatypes.enums.ServiceCategoriesEnum;
27 import org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum;
28 import org.onap.sdc.backend.ci.tests.datatypes.http.HttpHeaderEnum;
29 import org.onap.sdc.backend.ci.tests.datatypes.http.HttpRequest;
30 import org.onap.sdc.backend.ci.tests.datatypes.http.RestResponse;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.model.User;
34 import org.onap.sdc.backend.ci.tests.api.Urls;
35 import org.onap.sdc.backend.ci.tests.config.Config;
36 import org.onap.sdc.backend.ci.tests.datatypes.VendorLicenseModel;
37 import org.onap.sdc.backend.ci.tests.datatypes.ResourceReqDetails;
38 import org.onap.sdc.backend.ci.tests.datatypes.ServiceReqDetails;
39 import org.onap.sdc.backend.ci.tests.datatypes.VendorSoftwareProductObject;
40 import org.onap.sdc.backend.ci.tests.utils.Utils;
41 import org.onap.sdc.backend.ci.tests.utils.rest.BaseRestUtils;
44 import java.nio.charset.StandardCharsets;
45 import java.util.ArrayList;
46 import java.util.List;
49 import static org.testng.AssertJUnit.assertTrue;
51 public class OnboardingUtillViaApis {
53 public static VendorSoftwareProductObject createVspViaApis(ResourceReqDetails resourceReqDetails, String filepath, String vnfFile, User user) throws Exception {
55 VendorLicenseModel vendorLicenseModel = VendorLicenseModelRestUtils.createVendorLicense(user);
56 return VendorSoftwareProductRestUtils.createVendorSoftwareProduct(resourceReqDetails, vnfFile, filepath, user,
60 public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails) throws Exception {
61 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
66 public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails, UserRoleEnum userRole) throws Exception {
67 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, userRole, true).left().value();
72 public static void downloadToscaCsarToDirectory(Component component, File file) {
74 Either<String, RestResponse> componentToscaArtifactPayload = AtomicOperationUtils.getComponenetArtifactPayload(component, "assettoscacsar");
75 if(componentToscaArtifactPayload.left().value() != null){
76 convertPayloadToFile(componentToscaArtifactPayload.left().value(), file);
78 } catch (Exception e) {
84 public static void convertPayloadToFile(String payload, File file) throws IOException{
86 Gson gson = new Gson();
87 @SuppressWarnings("unchecked")
88 Map<String, String> fromJson = gson.fromJson(payload, Map.class);
89 String string = fromJson.get("base64Contents").toString();
90 byte[] byteArray = Base64.decodeBase64(string.getBytes(StandardCharsets.UTF_8));
91 File downloadedFile = new File(file.getAbsolutePath());
92 FileOutputStream fos = new FileOutputStream(downloadedFile);
99 public static void convertPayloadToZipFile(String payload, File file) throws IOException{
101 byte[] byteArray = payload.getBytes(StandardCharsets.ISO_8859_1);
102 File downloadedFile = new File(file.getAbsolutePath());
103 FileOutputStream fos = new FileOutputStream(downloadedFile);
104 fos.write(byteArray);
108 System.out.println("");
110 BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(payload.getBytes(StandardCharsets.ISO_8859_1)));
111 String filePath = file.toString();
112 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
114 while((inByte = bis.read()) != -1) bos.write(inByte);
119 public static Either<String, RestResponse> getVendorSoftwareProduct(String vspId, User user, Boolean validateState) throws IOException {
121 Config config = Utils.getConfig();
122 String url = String.format(Urls.GET_VENDOR_SOFTWARE_PRODUCT, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspId);
123 String userId = user.getUserId();
124 Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
125 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), "123456");
126 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "*/*");
127 headersMap.put("Accept-Encoding", "gzip, deflate, br");
128 HttpRequest http = new HttpRequest();
129 RestResponse response = http.httpSendGet(url, headersMap);
131 assertTrue("add property to resource failed: " + response.getResponseMessage(), response.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
133 if (response.getErrorCode() != BaseRestUtils.STATUS_CODE_SUCCESS && response.getResponse().getBytes() == null && response.getResponse().getBytes().length == 0) {
134 return Either.right(response);
136 return Either.left(response.getResponse());
140 public static ResourceReqDetails prepareOnboardedResourceDetailsBeforeCreate(ResourceReqDetails resourceDetails, VendorSoftwareProductObject vendorSoftwareProductObject) {
142 List<String> tags = new ArrayList<>();
143 tags.add(vendorSoftwareProductObject.getName());
144 resourceDetails.setCsarUUID(vendorSoftwareProductObject.getVspId());
145 resourceDetails.setCsarVersion(vendorSoftwareProductObject.getVersion());
146 resourceDetails.setCsarVersionId(vendorSoftwareProductObject.getVersionId());
147 resourceDetails.setName(vendorSoftwareProductObject.getName());
148 resourceDetails.setTags(tags);
149 resourceDetails.setDescription(vendorSoftwareProductObject.getDescription());
150 resourceDetails.setVendorName(vendorSoftwareProductObject.getVendorName());
151 resourceDetails.setResourceType("VF");
152 resourceDetails.setResourceVendorModelNumber("666");
153 resourceDetails.setContactId(vendorSoftwareProductObject.getAttContact());
155 return resourceDetails;
158 public static ServiceReqDetails prepareServiceDetailsBeforeCreate(User user) {
159 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(ServiceCategoriesEnum.NETWORK_L4, user);
160 serviceDetails.setServiceType("MyServiceType");
161 serviceDetails.setServiceRole("MyServiceRole");
162 serviceDetails.setNamingPolicy("MyServiceNamingPolicy");
163 serviceDetails.setEcompGeneratedNaming(false);
164 serviceDetails.setDerivedFromGenericType("org.openecomp.resource.abstract.nodes.service");
165 serviceDetails.setDerivedFromGenericVersion("1.0");
167 return serviceDetails;