blob: 4c6b6a7294efaf535dc37c8a2c4fea6df911d6ef [file] [log] [blame]
sebdetaa486be2020-02-18 02:00:11 -08001/*-
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
24import React, { forwardRef } from 'react'
25import MaterialTable from "material-table";
26import Button from 'react-bootstrap/Button';
27import Modal from 'react-bootstrap/Modal';
28import styled from 'styled-components';
29import PolicyToscaService from '../../../api/PolicyToscaService';
30import ArrowUpward from '@material-ui/icons/ArrowUpward';
31import ChevronLeft from '@material-ui/icons/ChevronLeft';
32import ChevronRight from '@material-ui/icons/ChevronRight';
33import Clear from '@material-ui/icons/Clear';
34import FirstPage from '@material-ui/icons/FirstPage';
35import LastPage from '@material-ui/icons/LastPage';
36import Search from '@material-ui/icons/Search';
37import LoopService from '../../../api/LoopService';
xuegaofb4b25f2020-03-11 11:22:15 +010038import Tabs from 'react-bootstrap/Tabs';
39import Tab from 'react-bootstrap/Tab';
xuegaoc6561392020-04-21 13:39:50 +020040import Alert from 'react-bootstrap/Alert';
sebdetaa486be2020-02-18 02:00:11 -080041
42const ModalStyled = styled(Modal)`
43 background-color: transparent;
44`
45const 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`
54const cellStyle = { border: '1px solid black' };
55const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' };
56const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'};
57
58export 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: [],
xuegaofb4b25f2020-03-11 11:22:15 +010066 selectedPolicyModelsData: [],
67 key: 'add',
xuegaoc6561392020-04-21 13:39:50 +020068 showFailAlert: false,
sebdetaa486be2020-02-18 02:00:11 -080069 toscaColumns: [
70 { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
71 cellStyle: cellStyle,
72 headerStyle: headerStyle
73 },
74 { title: "Policy Model Type", field: "policyModelType",
75 cellStyle: cellStyle,
76 headerStyle: headerStyle
77 },
78 { title: "Policy Acronym", field: "policyAcronym",
79 cellStyle: cellStyle,
80 headerStyle: headerStyle
81 },
82 { title: "Version", field: "version",
83 cellStyle: cellStyle,
84 headerStyle: headerStyle
85 },
86 { title: "Uploaded By", field: "updatedBy",
87 cellStyle: cellStyle,
88 headerStyle: headerStyle
89 },
90 { title: "Uploaded Date", field: "updatedDate", editable: 'never',
91 cellStyle: cellStyle,
92 headerStyle: headerStyle
93 },
94 { title: "Add", field: "updatedDate", editable: 'never',
95 cellStyle: cellStyle,
96 headerStyle: headerStyle
97 }
98 ],
99 tableIcons: {
100 FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
101 LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
102 NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
103 PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
104 ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
105 Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
106 SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
107 }
108 };
109
110 constructor(props, context) {
111 super(props, context);
112 this.handleClose = this.handleClose.bind(this);
xuegaofb4b25f2020-03-11 11:22:15 +0100113 this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this);
sebdetaa486be2020-02-18 02:00:11 -0800114 this.handleYamlContent = this.handleYamlContent.bind(this);
115 this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this);
116 this.handleAdd = this.handleAdd.bind(this);
xuegaofb4b25f2020-03-11 11:22:15 +0100117 this.handleRemove = this.handleRemove.bind(this);
118 this.initializeToscaPolicyModelsInfo();
sebdetaa486be2020-02-18 02:00:11 -0800119 }
120
121 componentWillReceiveProps(newProps) {
122 this.setState({
123 loopCache: newProps.loopCache,
124 temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties()))
125 });
126 }
127
xuegaofb4b25f2020-03-11 11:22:15 +0100128 initializeToscaPolicyModelsInfo() {
129 var operationalPolicies = this.state.loopCache.getOperationalPolicies();
130 var selectedPolicyModels = [];
131 for (var policy in operationalPolicies) {
132 selectedPolicyModels.push(operationalPolicies[policy]["policyModel"]);
133 }
134
135 PolicyToscaService.getToscaPolicyModels().then(allToscaModels => {
136 this.setState({ toscaPolicyModelsData: allToscaModels,
137 selectedPolicyModelsData: selectedPolicyModels});
sebdetaa486be2020-02-18 02:00:11 -0800138 });
139 }
140
141 getToscaPolicyModelYaml(policyModelType, policyModelVersion) {
142 if (typeof policyModelType !== "undefined") {
143 PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => {
144 if (toscaYaml.length !== 0) {
145 this.setState({content: toscaYaml})
146 } else {
147 this.setState({ content: 'No Tosca model Yaml available' })
148 }
149 });
150 } else {
151 this.setState({ content: 'Please select Tosca model to view the details' })
152 }
153 }
154
155 handleYamlContent(event) {
156 this.setState({ content: event.target.value });
157 }
158
159 handleClose() {
160 this.setState({ show: false });
161 this.props.history.push('/');
162 }
163
xuegaoc6561392020-04-21 13:39:50 +0200164 renderAlert() {
165 return (
166 <div>
167 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
168 {this.state.showMessage}
169 </Alert>
170 </div>
171 );
172 }
173
sebdetaa486be2020-02-18 02:00:11 -0800174 handleAdd() {
xuegaoc6561392020-04-21 13:39:50 +0200175 LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version)
176 .then(pars => {
177 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
178 this.handleClose();
179 })
180 .catch(error => {
181 this.setState({ showFailAlert: true, showMessage: "Adding failed with error: " + error.message});
182 });
xuegaofb4b25f2020-03-11 11:22:15 +0100183 }
184
185 handleRemove() {
186 LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version);
xuegaofb4b25f2020-03-11 11:22:15 +0100187 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
xuegaoa2625fe2020-03-18 12:55:22 +0100188 this.handleClose();
sebdetaa486be2020-02-18 02:00:11 -0800189 }
190
191 render() {
192 return (
193 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
194 <Modal.Header closeButton>
xuegaofb4b25f2020-03-11 11:22:15 +0100195 <Modal.Title>Modify Loop Operational Policies</Modal.Title>
sebdetaa486be2020-02-18 02:00:11 -0800196 </Modal.Header>
xuegaofb4b25f2020-03-11 11:22:15 +0100197 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}>
198 <Tab eventKey="add" title="Add Operational Policies">
199 <Modal.Body>
200 <MaterialTable
201 title={"View Tosca Policy Models"}
202 data={this.state.toscaPolicyModelsData}
203 columns={this.state.toscaColumns}
204 icons={this.state.tableIcons}
205 onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version);this.setState({selectedRowData: rowData})}}
206 options={{
207 headerStyle: rowHeaderStyle,
208 rowStyle: rowData => ({
209 backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
210 && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
xuegaoc6561392020-04-21 13:39:50 +0200211 })
xuegaofb4b25f2020-03-11 11:22:15 +0100212 }}
213 />
214 <div>
215 <TextModal value={this.state.content} onChange={this.handleYamlContent}/>
216 </div>
217 </Modal.Body>
xuegaoc6561392020-04-21 13:39:50 +0200218 {this.renderAlert()}
xuegaofb4b25f2020-03-11 11:22:15 +0100219 </Tab>
220 <Tab eventKey="remove" title="Remove Operational Policies">
221 <Modal.Body>
222 <MaterialTable
223 title={"Already added Tosca Policy Models"}
224 data={this.state.selectedPolicyModelsData}
225 columns={this.state.toscaColumns}
226 icons={this.state.tableIcons}
227 onRowClick={(event, rowData) => {this.setState({selectedRowData: rowData})}}
228 options={{
229 headerStyle: rowHeaderStyle,
230 rowStyle: rowData => ({
231 backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
232 && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
233 })
234 }}
235 />
236 </Modal.Body>
237 </Tab>
238 </Tabs>
sebdetaa486be2020-02-18 02:00:11 -0800239 <Modal.Footer>
240 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
xuegaofb4b25f2020-03-11 11:22:15 +0100241 <Button variant="primary" disabled={(this.state.key === "remove")} type="submit" onClick={this.handleAdd}>Add</Button>
242 <Button variant="primary" disabled={(this.state.key === "add")} type="submit" onClick={this.handleRemove}>Remove</Button>
sebdetaa486be2020-02-18 02:00:11 -0800243 </Modal.Footer>
xuegaofb4b25f2020-03-11 11:22:15 +0100244
sebdetaa486be2020-02-18 02:00:11 -0800245 </ModalStyled>
246 );
247 }
248}