Make Service base type optional
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / backend / 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.onap.sdc.backend.ci.tests.utils.general;
22
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;
42
43 import java.io.*;
44 import java.nio.charset.StandardCharsets;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Map;
48
49 import static org.testng.AssertJUnit.assertTrue;
50
51 public class OnboardingUtillViaApis {
52
53         public static VendorSoftwareProductObject createVspViaApis(ResourceReqDetails resourceReqDetails, String filepath, String vnfFile, User user) throws Exception {
54
55                 VendorLicenseModel vendorLicenseModel = VendorLicenseModelRestUtils.createVendorLicense(user);
56                 return VendorSoftwareProductRestUtils.createVendorSoftwareProduct(resourceReqDetails, vnfFile, filepath, user,
57             vendorLicenseModel);
58         }
59         
60         public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails) throws Exception {
61                 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
62                 return resource;
63
64         }
65         
66         public static Resource createResourceFromVSP(ResourceReqDetails resourceDetails, UserRoleEnum userRole) throws Exception {
67                 Resource resource = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, userRole, true).left().value();
68                 return resource;
69
70         }
71         
72         public static void downloadToscaCsarToDirectory(Component component, File file) {
73                 try {
74                         Either<String, RestResponse> componentToscaArtifactPayload = AtomicOperationUtils.getComponenetArtifactPayload(component, "assettoscacsar");
75                         if(componentToscaArtifactPayload.left().value() != null){
76                                 convertPayloadToFile(componentToscaArtifactPayload.left().value(), file);
77                         }
78                 } catch (Exception e) {
79                         e.printStackTrace();
80                 }
81         }
82         
83
84         public static void convertPayloadToFile(String payload, File file) throws IOException{
85                 
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);
93                 fos.write(byteArray);
94                 fos.flush();
95                 fos.close();
96         }
97         
98         
99         public static void convertPayloadToZipFile(String payload, File file) throws IOException{
100                 
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);
105                 fos.flush();
106                 fos.close();
107                 
108                 System.out.println("");
109                 
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)));
113                 int inByte;
114                 while((inByte = bis.read()) != -1) bos.write(inByte);
115                 bis.close();
116                 bos.close();
117         }
118         
119         public static Either<String, RestResponse> getVendorSoftwareProduct(String vspId, User user, Boolean validateState) throws IOException {
120
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);
130                 if (validateState) {
131                         assertTrue("add property to resource failed: " + response.getResponseMessage(), response.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
132                 }
133                 if (response.getErrorCode() != BaseRestUtils.STATUS_CODE_SUCCESS && response.getResponse().getBytes() == null && response.getResponse().getBytes().length == 0) {
134                         return Either.right(response);
135                 }
136                 return Either.left(response.getResponse());
137                 
138         }
139
140         public static ResourceReqDetails prepareOnboardedResourceDetailsBeforeCreate(ResourceReqDetails resourceDetails, VendorSoftwareProductObject vendorSoftwareProductObject) {
141
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());
154
155                 return resourceDetails;
156         }
157         
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");
166
167                 return serviceDetails;
168         }
169 }