blob: 8886dbfdfb24ba3ca8c83bebac5fb13789505a74 [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';
sebdetaa486be2020-02-18 02:00:11 -080040
41
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',
sebdetaa486be2020-02-18 02:00:11 -080068 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 },
sebdetab3eab62020-04-16 12:09:24 +020081 { title: "Policy Name", field: "policyName",
82 cellStyle: cellStyle,
83 headerStyle: headerStyle
84 },
sebdetaa486be2020-02-18 02:00:11 -080085 { title: "Version", field: "version",
86 cellStyle: cellStyle,
87 headerStyle: headerStyle
88 },
89 { title: "Uploaded By", field: "updatedBy",
90 cellStyle: cellStyle,
91 headerStyle: headerStyle
92 },
93 { title: "Uploaded Date", field: "updatedDate", editable: 'never',
94 cellStyle: cellStyle,
95 headerStyle: headerStyle
96 },
sebdetab3eab62020-04-16 12:09:24 +020097 { title: "Created Date", field: "createdDate", editable: 'never',
sebdetaa486be2020-02-18 02:00:11 -080098 cellStyle: cellStyle,
99 headerStyle: headerStyle
100 }
101 ],
102 tableIcons: {
103 FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
104 LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
105 NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
106 PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
107 ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
108 Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
109 SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
110 }
111 };
112
113 constructor(props, context) {
114 super(props, context);
115 this.handleClose = this.handleClose.bind(this);
xuegaofb4b25f2020-03-11 11:22:15 +0100116 this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this);
sebdetaa486be2020-02-18 02:00:11 -0800117 this.handleYamlContent = this.handleYamlContent.bind(this);
118 this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this);
119 this.handleAdd = this.handleAdd.bind(this);
xuegaofb4b25f2020-03-11 11:22:15 +0100120 this.handleRemove = this.handleRemove.bind(this);
121 this.initializeToscaPolicyModelsInfo();
sebdetaa486be2020-02-18 02:00:11 -0800122 }
123
124 componentWillReceiveProps(newProps) {
125 this.setState({
126 loopCache: newProps.loopCache,
127 temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties()))
128 });
129 }
130
xuegaofb4b25f2020-03-11 11:22:15 +0100131 initializeToscaPolicyModelsInfo() {
132 var operationalPolicies = this.state.loopCache.getOperationalPolicies();
133 var selectedPolicyModels = [];
134 for (var policy in operationalPolicies) {
sebdetab3eab62020-04-16 12:09:24 +0200135 var newRow = operationalPolicies[policy]["policyModel"];
sebdet6e7d0482020-04-16 16:14:45 +0200136 newRow["policyName"] = operationalPolicies[policy].name;
sebdetab3eab62020-04-16 12:09:24 +0200137 selectedPolicyModels.push(newRow);
xuegaofb4b25f2020-03-11 11:22:15 +0100138 }
139
140 PolicyToscaService.getToscaPolicyModels().then(allToscaModels => {
141 this.setState({ toscaPolicyModelsData: allToscaModels,
142 selectedPolicyModelsData: selectedPolicyModels});
sebdetaa486be2020-02-18 02:00:11 -0800143 });
144 }
145
146 getToscaPolicyModelYaml(policyModelType, policyModelVersion) {
147 if (typeof policyModelType !== "undefined") {
148 PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => {
149 if (toscaYaml.length !== 0) {
150 this.setState({content: toscaYaml})
151 } else {
152 this.setState({ content: 'No Tosca model Yaml available' })
153 }
154 });
155 } else {
156 this.setState({ content: 'Please select Tosca model to view the details' })
157 }
158 }
159
160 handleYamlContent(event) {
161 this.setState({ content: event.target.value });
162 }
163
164 handleClose() {
165 this.setState({ show: false });
166 this.props.history.push('/');
167 }
168
169 handleAdd() {
xuegaofb4b25f2020-03-11 11:22:15 +0100170 LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version);
xuegaofb4b25f2020-03-11 11:22:15 +0100171 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
xuegaoa2625fe2020-03-18 12:55:22 +0100172 this.handleClose();
xuegaofb4b25f2020-03-11 11:22:15 +0100173 }
174
175 handleRemove() {
sebdetab3eab62020-04-16 12:09:24 +0200176 LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version,this.state.selectedRowData.policyName);
xuegaofb4b25f2020-03-11 11:22:15 +0100177 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
xuegaoa2625fe2020-03-18 12:55:22 +0100178 this.handleClose();
sebdetaa486be2020-02-18 02:00:11 -0800179 }
180
181 render() {
182 return (
sebdetedaf4f92020-04-16 00:43:48 +0200183 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static">
sebdetaa486be2020-02-18 02:00:11 -0800184 <Modal.Header closeButton>
xuegaofb4b25f2020-03-11 11:22:15 +0100185 <Modal.Title>Modify Loop Operational Policies</Modal.Title>
sebdetaa486be2020-02-18 02:00:11 -0800186 </Modal.Header>
xuegaofb4b25f2020-03-11 11:22:15 +0100187 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}>
188 <Tab eventKey="add" title="Add Operational Policies">
189 <Modal.Body>
190 <MaterialTable
191 title={"View Tosca Policy Models"}
192 data={this.state.toscaPolicyModelsData}
193 columns={this.state.toscaColumns}
194 icons={this.state.tableIcons}
195 onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version);this.setState({selectedRowData: rowData})}}
196 options={{
197 headerStyle: rowHeaderStyle,
198 rowStyle: rowData => ({
199 backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
200 && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
201 })
202 }}
203 />
204 <div>
205 <TextModal value={this.state.content} onChange={this.handleYamlContent}/>
206 </div>
207 </Modal.Body>
208 </Tab>
209 <Tab eventKey="remove" title="Remove Operational Policies">
210 <Modal.Body>
211 <MaterialTable
sebdetab3eab62020-04-16 12:09:24 +0200212 title={"Tosca Policy Models already added"}
xuegaofb4b25f2020-03-11 11:22:15 +0100213 data={this.state.selectedPolicyModelsData}
214 columns={this.state.toscaColumns}
215 icons={this.state.tableIcons}
216 onRowClick={(event, rowData) => {this.setState({selectedRowData: rowData})}}
217 options={{
218 headerStyle: rowHeaderStyle,
219 rowStyle: rowData => ({
220 backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
221 && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
222 })
223 }}
224 />
225 </Modal.Body>
226 </Tab>
227 </Tabs>
sebdetaa486be2020-02-18 02:00:11 -0800228 <Modal.Footer>
229 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
xuegaofb4b25f2020-03-11 11:22:15 +0100230 <Button variant="primary" disabled={(this.state.key === "remove")} type="submit" onClick={this.handleAdd}>Add</Button>
231 <Button variant="primary" disabled={(this.state.key === "add")} type="submit" onClick={this.handleRemove}>Remove</Button>
sebdetaa486be2020-02-18 02:00:11 -0800232 </Modal.Footer>
xuegaofb4b25f2020-03-11 11:22:15 +0100233
sebdetaa486be2020-02-18 02:00:11 -0800234 </ModalStyled>
235 );
236 }
237}