8d467de2ae9c499adf683a8986f3abeefce31bb5
[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 (!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 (!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 (!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 (!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 = "";
134                 //TBD - Use Gson - jackson has security issue//JsonUtil.toJson(oLifeCycleTestReq);
135
136                 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
137                                 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
138
139                 if (!checkValidResponse(oResponse)) {
140                         logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
141                         return result;
142                 }
143
144                 result = oResponse.getResult();
145                 logger.info("Response execlifecycleTest :" + oResponse.getResult());
146                 return result;
147         }
148
149         /**
150          * Check Response is Valid
151          * 
152          * @param rsp
153          * @return valid(true) or invalid(false)
154          */
155         private static boolean checkValidResponse(RestResponse rsp) {
156                 return ((null != rsp.getStatusCode()) && (null != rsp.getResult())
157                                 && (RestConstant.RESPONSE_CODE_200 == rsp.getStatusCode()
158                                 || RestConstant.RESPONSE_CODE_201 == rsp.getStatusCode()));
159         }
160
161         /**
162          * Get csar Id value
163          *
164          * @param strJsonData
165          * @return empty(failure), or csarId(success)
166          */
167         private static String getCsarIdValue(String strJsonData) {
168                 ObjectMapper mapper = new ObjectMapper();
169                 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
170                 Map<String, String> dataMap = null;
171
172                 try {
173                         dataMap = mapper.readValue(strJsonData, Map.class);
174                 } catch (JsonParseException e) {
175                         logger.error("JsonParseException:Failed to upload package to catalouge:", e);
176                 } catch (JsonMappingException e) {
177                         logger.error("JsonMappingException:Failed to upload package to catalouge:", e);
178                 } catch (IOException e) {
179                         logger.error("IOException:Failed to upload package to catalouge:", e);
180                 }
181                 try {
182                         if (null != dataMap) {
183                                 return dataMap.get("csarId");
184                         }
185                 } catch (NullPointerException e) {
186                         logger.error("NullPointerException:Failed to get csarId", e);
187                 }
188                 return "";
189         }
190 }