VSP Compliance Check for Compute Flavor
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / validation / SoftwareProductValidationView.jsx
1 /**
2  * Copyright (c) 2019 Vodafone Group
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 import React, { Component } from 'react';
17 import PropTypes from 'prop-types';
18
19 import i18n from 'nfvo-utils/i18n/i18n.js';
20 import Button from 'sdc-ui/lib/react/Button.js';
21 import { Tab, Tabs } from 'sdc-ui/lib/react';
22 import { tabsMapping } from './SoftwareProductValidationConstants.js';
23 import VspValidationInputs from './inputs/VspValidationInputs.js';
24 import VspValidationSetup from './setup/VspValidationSetup.js';
25
26 class SoftwareProductValidation extends Component {
27     static propTypes = {
28         onErrorThrown: PropTypes.func,
29         softwareProductValidation: PropTypes.object,
30         onTestSubmit: PropTypes.func,
31         setVspTestsMap: PropTypes.func,
32         setActiveTab: PropTypes.func,
33         setComplianceChecked: PropTypes.func,
34         setCertificationChecked: PropTypes.func
35     };
36
37     constructor(props) {
38         super(props);
39         this.state = {
40             complianceCheckList: null,
41             certificationCheckList: null,
42             flatTestsMap: {},
43             generalInfo: {},
44             activeTab: tabsMapping.SETUP,
45             goToValidationInput: false
46         };
47     }
48
49     setMapAndGeneralData(element, testScenario) {
50         let flatTestMap = this.state.flatTestsMap;
51         let generalInputData = this.state.generalInfo;
52         flatTestMap[element.testCaseName] = {
53             title: element.description,
54             parameters: element.inputs,
55             endpoint: element.endpoint,
56             testCaseName: element.testCaseName,
57             testSuiteName: element.testSuiteName,
58             scenario: testScenario
59         };
60         generalInputData[element.testCaseName] = {};
61         element.inputs.forEach(key => {
62             generalInputData[element.testCaseName][key.name] = {
63                 isValid: true,
64                 errorText: ''
65             };
66         });
67
68         this.setState({
69             flatTestsMap: flatTestMap,
70             generalInfo: generalInputData
71         });
72     }
73
74     buildChildElements(setItem, testScenario) {
75         let parentElement = {};
76         parentElement.value = setItem.name;
77         parentElement.label = setItem.description;
78         parentElement.children = [];
79         if (setItem.children !== undefined) {
80             setItem.children.forEach(element => {
81                 let childElement = this.buildChildElements(
82                     element,
83                     testScenario
84                 );
85                 if (childElement.children.length !== 0) {
86                     parentElement.children.push(childElement);
87                 }
88             });
89         }
90         if (setItem.tests !== undefined) {
91             setItem.tests.forEach(element => {
92                 parentElement.children.push({
93                     value: element.testCaseName,
94                     label: element.description
95                 });
96                 this.setMapAndGeneralData(element, testScenario);
97             });
98         }
99         return parentElement;
100     }
101
102     buildCheckboxParentNode(parentNode, data) {
103         parentNode.value = data.description;
104         parentNode.label = 'All';
105         parentNode.children = [];
106         let scenario = data.name;
107         data.children.forEach(element => {
108             let childElement = this.buildChildElements(element, scenario);
109             if (childElement.children.length !== 0) {
110                 parentNode.children.push(childElement);
111             }
112         });
113         if (data.tests !== undefined) {
114             data.tests.forEach(element => {
115                 parentNode.children.push({
116                     value: element.testCaseName,
117                     label: element.description
118                 });
119                 this.setMapAndGeneralData(element, scenario);
120             });
121         }
122         return parentNode;
123     }
124
125     prepareDataForCheckboxes(res) {
126         let complianceData = {};
127         let certificationData = {};
128         let complianceList = [];
129         let certificationList = [];
130         let { setVspTestsMap } = this.props;
131         if (Object.keys(res).length !== 0 && res.children) {
132             res.children.forEach(element => {
133                 if (element.name === 'certification') {
134                     certificationData = element;
135                 } else if (element.name === 'compliance') {
136                     complianceData = element;
137                 }
138             });
139
140             let complianceParentNode = {};
141             if (
142                 Object.keys(complianceData).length !== 0 &&
143                 complianceData.children !== undefined
144             ) {
145                 complianceParentNode = this.buildCheckboxParentNode(
146                     complianceParentNode,
147                     complianceData
148                 );
149                 if (complianceParentNode.children.length !== 0) {
150                     complianceList.push(complianceParentNode);
151                 }
152             }
153
154             let certificationParentNode = {};
155             if (
156                 Object.keys(certificationData).length !== 0 &&
157                 certificationData.children !== undefined
158             ) {
159                 certificationParentNode = this.buildCheckboxParentNode(
160                     certificationParentNode,
161                     certificationData
162                 );
163                 if (certificationParentNode.children.length !== 0) {
164                     certificationList.push(certificationParentNode);
165                 }
166             }
167         }
168         this.setState({
169             certificationCheckList: certificationList,
170             complianceCheckList: complianceList
171         });
172         setVspTestsMap(this.state.flatTestsMap);
173     }
174
175     resetState() {
176         this.setState({
177             complianceCheckList: [],
178             certificationCheckList: [],
179             flatTestsMap: {},
180             activeTab: tabsMapping.SETUP,
181             goToValidationInput: false
182         });
183     }
184
185     componentWillMount() {}
186
187     shouldComponentUpdate() {
188         return true;
189     }
190
191     componentDidMount() {
192         let { softwareProductValidation } = this.props;
193         if (softwareProductValidation.vspChecks !== undefined) {
194             this.prepareDataForCheckboxes(softwareProductValidation.vspChecks);
195         }
196     }
197
198     componentWillUnmount() {
199         this.resetState();
200     }
201
202     componentWillReceiveProps(nextProps) {
203         if (
204             nextProps.softwareProductValidation.vspChecks !==
205             this.props.softwareProductValidation.vspChecks
206         ) {
207             let { softwareProductValidation, setActiveTab } = nextProps;
208             if (softwareProductValidation.vspChecks !== undefined) {
209                 this.prepareDataForCheckboxes(
210                     softwareProductValidation.vspChecks
211                 );
212             }
213             this.setState({
214                 activeTab: tabsMapping.SETUP,
215                 goToValidationInput: false
216             });
217             setActiveTab({ activeTab: tabsMapping.SETUP });
218         }
219     }
220
221     prepareDataForValidationInputsSection() {
222         let {
223             softwareProductId,
224             version,
225             onTestSubmit,
226             onErrorThrown,
227             setTestsRequest,
228             setGeneralInfo
229         } = this.props;
230         return {
231             softwareProductId,
232             version,
233             onTestSubmit,
234             onErrorThrown,
235             setTestsRequest,
236             setGeneralInfo
237         };
238     }
239
240     prepareDataForCheckboxTreeSection() {
241         let {
242             softwareProductValidation,
243             setComplianceChecked,
244             setCertificationChecked
245         } = this.props;
246         let complianceCheckList = this.state.complianceCheckList;
247         let certificationCheckList = this.state.certificationCheckList;
248         return {
249             softwareProductValidation,
250             setComplianceChecked,
251             setCertificationChecked,
252             complianceCheckList,
253             certificationCheckList
254         };
255     }
256
257     handleTabPress(key) {
258         let { setActiveTab } = this.props;
259         switch (key) {
260             case tabsMapping.SETUP:
261                 this.setState({ activeTab: tabsMapping.SETUP });
262                 setActiveTab({ activeTab: tabsMapping.SETUP });
263                 return;
264             case tabsMapping.INPUTS:
265             default:
266                 setActiveTab({ activeTab: tabsMapping.INPUTS });
267                 this.setState({
268                     goToValidationInput: true,
269                     activeTab: tabsMapping.INPUTS
270                 });
271                 return;
272         }
273     }
274
275     fetchDefaultValue(value) {
276         let { softwareProductId, version } = this.props;
277         let versionName = parseFloat(version.name).toFixed(1),
278             versionNumber =
279                 versionName > 1 ? (versionName - 1).toFixed(1) : versionName,
280             versionUUID = version.id;
281         value =
282             value === '$vspid'
283                 ? softwareProductId
284                 : value === '$vspPreviousVersion' ? versionNumber : value;
285         value = value === '$vspVersionUUID' ? versionUUID : value || '';
286         return value;
287     }
288
289     formTestsRequest(item, testsRequest) {
290         let { vspTestsMap } = this.props.softwareProductValidation;
291         testsRequest[item] = {
292             parameters: {},
293             scenario: vspTestsMap[item]['scenario'],
294             testCaseName: vspTestsMap[item]['testCaseName'],
295             testSuiteName: vspTestsMap[item]['testSuiteName'],
296             endpoint: vspTestsMap[item]['endpoint']
297         };
298         vspTestsMap[item].parameters.forEach(parameter => {
299             testsRequest[item].parameters[
300                 parameter.name
301             ] = this.fetchDefaultValue(parameter.defaultValue);
302         });
303         return testsRequest;
304     }
305
306     onGoToInputs() {
307         let {
308             setActiveTab,
309             softwareProductValidation,
310             setTestsRequest
311         } = this.props;
312         setActiveTab({ activeTab: tabsMapping.INPUTS });
313         let testsRequest = {};
314         if (softwareProductValidation.complianceChecked) {
315             softwareProductValidation.complianceChecked.forEach(item => {
316                 testsRequest = this.formTestsRequest(item, testsRequest);
317             });
318         }
319         if (softwareProductValidation.certificationChecked) {
320             softwareProductValidation.certificationChecked.forEach(item => {
321                 testsRequest = this.formTestsRequest(item, testsRequest);
322             });
323         }
324         setTestsRequest(testsRequest, this.state.generalInfo);
325         this.setState({
326             goToValidationInput: true,
327             activeTab: tabsMapping.INPUTS
328         });
329     }
330
331     onGoToSetup() {
332         let { setActiveTab } = this.props;
333         setActiveTab({ activeTab: tabsMapping.SETUP });
334         this.setState({
335             goToValidationInput: false,
336             activeTab: tabsMapping.SETUP
337         });
338     }
339
340     render() {
341         let { softwareProductValidation } = this.props;
342         let isNextDisabled =
343             (softwareProductValidation.certificationChecked === undefined ||
344                 softwareProductValidation.certificationChecked.length === 0) &&
345             (softwareProductValidation.complianceChecked === undefined ||
346                 softwareProductValidation.complianceChecked.length === 0);
347
348         return (
349             <div className="vsp-validation-view">
350                 <div className="validation-view-controllers">
351                     {this.state.activeTab === tabsMapping.SETUP && (
352                         <Button
353                             btnType="secondary"
354                             data-test-id="go-to-vsp-validation-inputs"
355                             disabled={isNextDisabled}
356                             className="change-tabs-btn"
357                             onClick={() => this.onGoToInputs()}>
358                             {i18n('NEXT')}
359                         </Button>
360                     )}
361                     {this.state.activeTab === tabsMapping.INPUTS && (
362                         <Button
363                             btnType="secondary"
364                             data-test-id="go-to-vsp-validation-setup"
365                             className="change-tabs-btn"
366                             onClick={() => this.onGoToSetup()}>
367                             {i18n('BACK')}
368                         </Button>
369                     )}
370                 </div>
371                 <Tabs
372                     className="validation-tabs"
373                     type="header"
374                     activeTab={this.state.activeTab}
375                     onTabClick={key => this.handleTabPress(key)}>
376                     <Tab
377                         tabId={tabsMapping.SETUP}
378                         title={i18n('Setup')}
379                         disabled={this.state.goToValidationInput}>
380                         <div className="validation-view-tab">
381                             {this.state.complianceCheckList &&
382                                 this.state.certificationCheckList && (
383                                     <VspValidationSetup
384                                         {...this.prepareDataForCheckboxTreeSection()}
385                                     />
386                                 )}
387                         </div>
388                     </Tab>
389                     <Tab
390                         tabId={tabsMapping.INPUTS}
391                         title={i18n('Inputs')}
392                         disabled={!this.state.goToValidationInput}>
393                         <div className="validation-view-tab">
394                             <VspValidationInputs
395                                 {...this.prepareDataForValidationInputsSection()}
396                             />
397                         </div>
398                     </Tab>
399                 </Tabs>
400             </div>
401         );
402     }
403 }
404
405 export default SoftwareProductValidation;