Integrate VNF Repository with SDC
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / vnfMarketPlace / VNFImportView.jsx
1 /*
2  * Copyright 2017 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  
17 import React from 'react';
18 import GridSection from 'nfvo-components/grid/GridSection.jsx';
19 import GridItem from 'nfvo-components/grid/GridItem.jsx';
20 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
23 import Button from 'sdc-ui/lib/react/Button.js';
24 import VNFImportActionHelper from '../vnfMarketPlace/VNFImportActionHelper.js';
25
26
27 function VNFAction({action, isHeader, downloadCSAR, id, currSoftwareProduct}) {
28         if (isHeader) {
29                 return <span>{action}</span>;
30         }
31         return (
32                 <span>
33                         <SVGIcon name='download' color='positive' onClick={() => {downloadCSAR(id, currSoftwareProduct);}}/>
34                 </span>
35         );
36 }
37
38 function VNFSortableCellHeader({isHeader, data, isDes, onSort, activeSortColumn}) {
39         //TODO check icon sdc-ui
40         if (isHeader) {
41                 if(activeSortColumn === data) {
42                         return (
43                                 <span className='vnf-table-header' onClick={()=>{onSort(activeSortColumn);}}>
44                                         <span>{data}</span>
45                                         <span className={`header-sort-arrow ${isDes ? 'up' : 'down'}`}></span>
46                                 </span>
47                         );
48                 }
49                 else {
50                         return (
51                                 <span className='vnf-table-header' onClick={()=>{activeSortColumn = data; onSort(activeSortColumn);}}>
52                                         <span>{data}</span>
53                                 </span>
54                         );
55                 }
56         }
57         return (
58                 <span className='vnf-table-cell'>
59                         <span>{data}</span>
60                 </span>
61         );
62 }
63
64 export function VNFItemList({vnf, isHeader, isDes, onSort, activeSortColumn, downloadCSAR, selectTableRow, selectedRow, currentSoftwareProduct}) {
65         let {csarId, name, version, provider, shortDesc, action} = vnf;
66         return (
67                 <li className={`vnfBrowse-list-item ${isHeader ? 'header' : ''} ${csarId === selectedRow ? 'selectedRow' : ''}`} 
68                         data-test-id='vnfBrowse-list-item' onClick={() => {selectTableRow(csarId);}}>
69                         <div className='table-cell vnftable-name' data-test-id='vnftable-name'>
70                                 <VNFSortableCellHeader isHeader={isHeader} data={name} isDes={isDes} onSort={(activeSort)=> {onSort('name', activeSort);}} activeSortColumn={activeSortColumn}/>
71                         </div>
72                         <div className='table-cell vnftable-version' data-test-id='vnftable-version'>
73                                 <VNFSortableCellHeader isHeader={isHeader} data={version} isDes={isDes} onSort={(activeSort)=> {onSort('version', activeSort);}} activeSortColumn={activeSortColumn}/>
74                         </div>
75                         <div className='table-cell vnftable-provider' data-test-id='vnftable-provider'>
76                                 <VNFSortableCellHeader isHeader={isHeader} data={provider} isDes={isDes} onSort={(activeSort)=> {onSort('provider', activeSort);}} activeSortColumn={activeSortColumn}/>
77                         </div>
78                         <div className='table-cell vnftable-shortDesc' data-test-id='vnftable-shortDesc'>
79                                 <VNFSortableCellHeader isHeader={isHeader} data={shortDesc} isDes={isDes} onSort={(activeSort)=> {onSort('shortDesc', activeSort);}} activeSortColumn={activeSortColumn}/>
80                         </div>
81                         <div className='table-cell vnftable-action' data-test-id='vnftable-action'>
82                                 <VNFAction isHeader={isHeader} action={action} downloadCSAR= {downloadCSAR} id={csarId} currSoftwareProduct={currentSoftwareProduct}/>
83                         </div>
84                 </li>
85         );
86 }
87
88 class VNFImportView extends React.Component {
89
90         state = {
91                 localFilter: '',
92                 sortDescending: true,
93                 sortCrit: 'name',
94                 activeSortColumn : 'Name',
95                 selectedRow: ''
96         };
97
98         render() {
99                 let {onCancel, onSubmit, currentSoftwareProduct} = this.props;
100                 
101                 return (
102                         <div className='vnf-creation-page'>
103                                         <GridSection className='vnf-grid-section'>
104                                                 <GridItem colSpan='4'>
105                                                         <ListEditorView
106                                                                 title={i18n('VNF List Title')}
107                                                                 filterValue={this.state.localFilter}
108                                                                 onFilter={filter => this.setState({localFilter: filter})}>
109                                                                 <VNFItemList
110                                                                         isHeader={true}
111                                                                         vnf={{csarId: 0, name: i18n('VNF Header Name'), version: i18n('VNF Header Version'), 
112                                                                                 provider: i18n('VNF Header Vendor'), shortDesc: i18n('VNF Header Desc'), action: i18n('VNF Header Action')}}
113                                                                         isDes={this.state.sortDescending}
114                                                                         onSort={(sortCriteria, activeSortCol) => this.setState({sortDescending: !this.state.sortDescending, sortCrit: sortCriteria, activeSortColumn: activeSortCol})} 
115                                                                         activeSortColumn={this.state.activeSortColumn}/>
116                                                                 {this.sortVNFItems(this.filterVNFItems(), 
117                                                                         this.state.sortDescending, this.state.sortCrit).map(vnf => <VNFItemList key={vnf.id} vnf={vnf} 
118                                                                                 downloadCSAR={this.downloadCSAR} selectTableRow={(selID) => 
119                                                                                         {this.setState({selectedRow: selID});this.selectTableRow(selID);}} selectedRow={this.state.selectedRow} currentSoftwareProduct={currentSoftwareProduct}/>)}
120                                                         </ListEditorView>
121                                                 </GridItem>
122                                                 <GridItem colSpan='4'>
123                                                         <div className='vnf-modal'>
124                                                 <Button className='vnf-submit' type='button' btnType='default' onClick={() => 
125                                                         onSubmit(this.state.selectedRow, currentSoftwareProduct)}>{i18n('OK')}
126                                                 </Button>
127                                                 <Button className='Cancel' type='button' btnType='outline' onClick={onCancel}>{i18n('Cancel')}
128                                                 </Button>
129                                                         </div>
130                                                 </GridItem>
131                                         </GridSection>
132                         </div>
133                 );
134         }
135
136         filterVNFItems() {
137                 let {vnfItems} = this.props;
138                 let {localFilter} = this.state;
139                 if (localFilter.trim()) {
140                         const filter = new RegExp(escape(localFilter), 'i');
141                         return vnfItems.filter(
142                                 ({name = '', provider = '', version = '', shortDesc = ''}) => 
143                                         escape(name).match(filter) || escape(provider).match(filter) || escape(version).match(filter) || escape(shortDesc).match(filter));
144                 }
145                 else {
146                         return vnfItems;
147                 }
148         }
149
150         sortVNFItems(vnfItems, sortDesc, sortCrit) {
151                 if (sortDesc) {
152                         return vnfItems.sort((a, b) => 
153                         {
154                                 if(a[sortCrit] < b[sortCrit]){
155                                         return -1;
156                                 } 
157                                 else if(a[sortCrit] > b[sortCrit]){
158                                         return 1;
159                                 } 
160                                 else {
161                                         return 0;
162                                 }
163                         });
164                 }
165                 else {
166                         return vnfItems.reverse();
167                 }
168         }
169
170         downloadCSAR(id, currSoftwareProduct) {
171                 VNFImportActionHelper.download(id, currSoftwareProduct);
172         }
173         
174 }
175
176 export default VNFImportView;