0311c6b042ff0b8e823d9c7b35169370b473e7d2
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2017-2018 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vnfsdk.marketplace.onboarding.hooks.validatelifecycle;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Map;
21
22 import org.apache.http.entity.ContentType;
23 import org.apache.http.entity.mime.MultipartEntityBuilder;
24 import org.onap.vnfsdk.marketplace.common.CommonConstant;
25 import org.onap.vnfsdk.marketplace.common.FileUtil;
26 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
27 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
28 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
29 import org.onap.vnfsdk.marketplace.rest.RestConstant;
30 import org.onap.vnfsdk.marketplace.rest.RestResponse;
31 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.fasterxml.jackson.core.JsonParseException;
36 /** note jackson has security vulnerabilities. use with care */
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41
42 /** CALL Flow: onBoardingHandler --> LifecycleTestHook--> LifecycleTestExecutor */
43 public class LifecycleTestExceutor {
44         private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);
45         public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/onapapi/catalog/v1/csars";
46
47         private LifecycleTestExceutor() {
48                 // Empty constructor
49         }
50
51         /**
52          * Interface to upload package to catalogue
53          * 
54          * @param onBoradFuncTestReq
55          * @return- csarId or null (in case of failure)
56          */
57         @SuppressWarnings("unchecked")
58         public static String uploadPackageToCatalouge(OnBoradingRequest onBoradFuncTestReq) {
59                 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
60                 logger.info("Package file path uploadPackageToCatalouge:" + packagePath);
61
62                 String catalougeCsarId = null;
63
64                 // Validate package path
65                 if (false == FileUtil.validatePath(packagePath)) {
66                         logger.error("Failed to validate  package path");
67                         return catalougeCsarId;
68                 }
69
70                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
71                 if (null == oMsbDetails) {
72                         logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
73                         return catalougeCsarId;
74                 }
75
76                 File fileData = new File(packagePath);
77
78                 // Validate file
79                 if (false == FileUtil.validateFile(fileData)) {
80                         logger.error("Failed to validate file information");
81                         return catalougeCsarId;
82                 }
83
84                 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
85                 builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
86
87                 // IP and Port needs to be configured !!!
88                 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
89                                 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()), CommonConstant.CATALOUGE_UPLOAD_URL,
90                                 builder.build());
91                 if (false == checkValidResponse(rsp)) {
92                         logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
93                         return catalougeCsarId;
94                 }
95
96                 logger.info("Response for uploadPackageToCatalouge :" + rsp.getResult());
97                 catalougeCsarId = getCsarIdValue(rsp.getResult());
98
99                 logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
100                 return catalougeCsarId;
101         }
102
103         /**
104          * Interface to execute lifecycle test
105          * 
106          * @param onBoradFuncTestReq,
107          *            oLifeCycleTestReq
108          * @return result of the test or null (in case of failure)
109          */
110         public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq) {
111
112                 String result = null;
113                 if ((null == onBoradFuncTestReq.getPackagePath()) || (null == onBoradFuncTestReq.getPackageName())) {
114                         logger.error("Package path or name is invalid");
115                         return result;
116                 }
117
118                 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
119                 logger.info("Package file path Function test:" + packagePath);
120
121                 // Validate package path
122                 if (false == FileUtil.validatePath(packagePath)) {
123                         logger.error("Failed to validate  path");
124                         return result;
125                 }
126
127                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
128                 if (null == oMsbDetails) {
129                         logger.error("Failed to get MSB details during execlifecycleTest !!!");
130                         return result;
131                 }
132
133                 String rawDataJson = ""; //TBD - Use Gson - jackson has security issue//JsonUtil.toJson(oLifeCycleTestReq);
134 //              if (null == rawDataJson) {
135 //                      logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
136 //                      return result;
137 //              }
138
139                 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
140                                 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
141
142                 if (false == checkValidResponse(oResponse)) {
143                         logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
144                         return result;
145                 }
146
147                 result = oResponse.getResult();
148                 logger.info("Response execlifecycleTest :" + oResponse.getResult());
149                 return result;
150         }
151
152         /**
153          * Check Response is Valid
154          * 
155          * @param rsp
156          * @return valid(true) or invalid(false)
157          */
158         private static boolean checkValidResponse(RestResponse rsp) {
159                 if ((null == rsp.getStatusCode()) || (null == rsp.getResult())
160                                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode()
161                                                 && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode())) {
162                         return false;
163                 }
164                 return true;
165         }
166
167         /**
168          * Get csar Id value
169          *
170          * @param strJsonData
171          * @return empty(failure), or csarId(success)
172          */
173         private static String getCsarIdValue(String strJsonData) {
174                 ObjectMapper mapper = new ObjectMapper();
175                 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
176                 Map<String, String> dataMap = null;
177
178                 try {
179                         dataMap = mapper.readValue(strJsonData, Map.class);
180                 } catch (JsonParseException e) {
181                         logger.error("JsonParseException:Failed to upload package to catalouge:", e);
182                 } catch (JsonMappingException e) {
183                         logger.error("JsonMappingException:Failed to upload package to catalouge:", e);
184                 } catch (IOException e) {
185                         logger.error("IOException:Failed to upload package to catalouge:", e);
186                 }
187                 try {
188                         if (null != dataMap) {
189                                 return dataMap.get("csarId");
190                         }
191                 } catch (NullPointerException e) {
192                         logger.error("NullPointerException:Failed to get csarId", e);
193                 }
194                 return "";
195         }
196 }