7c16b78634dcc92bb88601b8adfc1b93ac072b7e
[clamp.git] / ui-react / src / components / dialogs / Loop / ModifyLoopModal.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 import React, { forwardRef } from 'react'
25 import MaterialTable from "material-table";
26 import Button from 'react-bootstrap/Button';
27 import Modal from 'react-bootstrap/Modal';
28 import styled from 'styled-components';
29 import PolicyToscaService from '../../../api/PolicyToscaService';
30 import ArrowUpward from '@material-ui/icons/ArrowUpward';
31 import ChevronLeft from '@material-ui/icons/ChevronLeft';
32 import ChevronRight from '@material-ui/icons/ChevronRight';
33 import Clear from '@material-ui/icons/Clear';
34 import FirstPage from '@material-ui/icons/FirstPage';
35 import LastPage from '@material-ui/icons/LastPage';
36 import Search from '@material-ui/icons/Search';
37 import LoopService from '../../../api/LoopService';
38 import Tabs from 'react-bootstrap/Tabs';
39 import Tab from 'react-bootstrap/Tab';
40
41
42 const ModalStyled = styled(Modal)`
43         background-color: transparent;
44 `
45 const TextModal = styled.textarea`
46         margin-top: 20px;
47         white-space:pre;
48         background-color: ${props => props.theme.toscaTextareaBackgroundColor};
49         text-align: justify;
50         font-size: ${props => props.theme.toscaTextareaFontSize};
51         width: 100%;
52         height: 300px;
53 `
54 const cellStyle = { border: '1px solid black' };
55 const headerStyle = { backgroundColor: '#ddd',  border: '2px solid black'       };
56 const rowHeaderStyle = {backgroundColor:'#ddd',  fontSize: '15pt', text: 'bold', border: '1px solid black'};
57
58 export default class ModifyLoopModal extends React.Component {
59
60         state = {
61                 show: true,
62                 loopCache: this.props.loopCache,
63                 content: 'Please select Tosca model to view the details',
64                 selectedRowData: {},
65                 toscaPolicyModelsData: [],
66                 selectedPolicyModelsData: [],
67                 key: 'add',
68                 toscaColumns: [
69                         { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
70                                 cellStyle: cellStyle,
71                                 headerStyle: headerStyle
72                         },
73                         { title: "Policy Model Type", field: "policyModelType",
74                                 cellStyle: cellStyle,
75                                 headerStyle: headerStyle
76                         },
77                         { title: "Policy Acronym", field: "policyAcronym",
78                                 cellStyle: cellStyle,
79                                 headerStyle: headerStyle
80                         },
81                         { title: "Version", field: "version",
82                                 cellStyle: cellStyle,
83                                 headerStyle: headerStyle
84                         },
85                         { title: "Uploaded By", field: "updatedBy",
86                                 cellStyle: cellStyle,
87                                 headerStyle: headerStyle
88                         },
89                         { title: "Uploaded Date", field: "updatedDate", editable: 'never',
90                                 cellStyle: cellStyle,
91                                 headerStyle: headerStyle
92                         },
93              { title: "Add", field: "updatedDate", editable: 'never',
94                 cellStyle: cellStyle,
95                 headerStyle: headerStyle
96              }
97                 ],
98                 tableIcons: {
99                     FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
100                     LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
101                     NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
102                     PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
103                     ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
104                     Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
105                     SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
106                 }
107         };
108
109         constructor(props, context) {
110                 super(props, context);
111                 this.handleClose = this.handleClose.bind(this);
112                 this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this);
113                 this.handleYamlContent = this.handleYamlContent.bind(this);
114                 this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this);
115                 this.handleAdd = this.handleAdd.bind(this);
116                 this.handleRemove = this.handleRemove.bind(this);
117                 this.initializeToscaPolicyModelsInfo();
118         }
119
120         componentWillReceiveProps(newProps) {
121                 this.setState({
122                         loopCache: newProps.loopCache,
123                         temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties()))
124                 });
125         }
126
127         initializeToscaPolicyModelsInfo() {
128                 var operationalPolicies = this.state.loopCache.getOperationalPolicies();
129                 var selectedPolicyModels = [];
130                 for (var policy in operationalPolicies) {
131                         selectedPolicyModels.push(operationalPolicies[policy]["policyModel"]);
132                 }
133
134                 PolicyToscaService.getToscaPolicyModels().then(allToscaModels => {
135                         this.setState({ toscaPolicyModelsData: allToscaModels,
136                                                         selectedPolicyModelsData: selectedPolicyModels});
137                 });
138         }
139
140         getToscaPolicyModelYaml(policyModelType, policyModelVersion) {
141                 if (typeof policyModelType !== "undefined") {
142                         PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => {
143                                 if (toscaYaml.length !== 0) {
144                                         this.setState({content: toscaYaml})
145                                 } else {
146                                         this.setState({ content: 'No Tosca model Yaml available' })
147                                 }
148                         });
149                 } else {
150                         this.setState({ content: 'Please select Tosca model to view the details' })
151                 }
152         }
153
154         handleYamlContent(event) {
155                 this.setState({ content: event.target.value });
156         }
157
158         handleClose() {
159                 this.setState({ show: false });
160                 this.props.history.push('/');
161         }
162
163         handleAdd() {
164                 LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version);
165                 this.handleClose();
166                 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
167         }
168
169         handleRemove() {
170                 LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version);
171                 this.handleClose();
172                 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
173         }
174
175         render() {
176                 return (
177                         <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
178                                 <Modal.Header closeButton>
179                                         <Modal.Title>Modify Loop Operational Policies</Modal.Title>
180                                 </Modal.Header>
181                                 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}>
182                                         <Tab eventKey="add" title="Add Operational Policies">
183                                                 <Modal.Body>
184                                                         <MaterialTable
185                                                         title={"View Tosca Policy Models"}
186                                                         data={this.state.toscaPolicyModelsData}
187                                                         columns={this.state.toscaColumns}
188                                                         icons={this.state.tableIcons}
189                                                         onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version);this.setState({selectedRowData: rowData})}}
190                                                         options={{
191                                                                 headerStyle: rowHeaderStyle,
192                                                                 rowStyle: rowData => ({
193                                                                         backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
194                                                                         && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
195                                                                 })
196                                                         }}
197                                                         />
198                                                         <div>
199                                                                 <TextModal value={this.state.content} onChange={this.handleYamlContent}/>
200                                                         </div>
201                                                 </Modal.Body>
202                                         </Tab>
203                                         <Tab eventKey="remove" title="Remove Operational Policies">
204                                                 <Modal.Body>
205                                                         <MaterialTable
206                                                         title={"Already added Tosca Policy Models"}
207                                                         data={this.state.selectedPolicyModelsData}
208                                                         columns={this.state.toscaColumns}
209                                                         icons={this.state.tableIcons}
210                                                         onRowClick={(event, rowData) => {this.setState({selectedRowData: rowData})}}
211                                                         options={{
212                                                                 headerStyle: rowHeaderStyle,
213                                                                 rowStyle: rowData => ({
214                                                                         backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
215                                                                         && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
216                                                                 })
217                                                         }}
218                                                         />
219                                                 </Modal.Body>
220                                         </Tab>
221                                 </Tabs>
222                                 <Modal.Footer>
223                                         <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
224                                         <Button variant="primary" disabled={(this.state.key === "remove")} type="submit" onClick={this.handleAdd}>Add</Button>
225                                         <Button variant="primary" disabled={(this.state.key === "add")} type="submit" onClick={this.handleRemove}>Remove</Button>
226                                 </Modal.Footer>
227
228                         </ModalStyled>
229                 );
230         }
231 }