blob: 625aeb341e84649b2da993fe23e8e7db7d7cf2cc [file] [log] [blame]
sebdeteb8e3f12021-01-24 18:12:36 +01001/*-
2 * ============LICENSE_START=======================================================
3 * ONAP POLICY-CLAMP
4 * ================================================================================
5 * Copyright (C) 2021 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 Button from 'react-bootstrap/Button';
26import Modal from 'react-bootstrap/Modal';
27import styled from 'styled-components';
sebdet37285472021-02-15 18:09:38 +010028import AddBox from '@material-ui/icons/AddBox';
29import ArrowDownward from '@material-ui/icons/ArrowDownward';
30import Check from '@material-ui/icons/Check';
sebdeteb8e3f12021-01-24 18:12:36 +010031import ChevronLeft from '@material-ui/icons/ChevronLeft';
32import ChevronRight from '@material-ui/icons/ChevronRight';
33import Clear from '@material-ui/icons/Clear';
sebdet37285472021-02-15 18:09:38 +010034import DeleteOutline from '@material-ui/icons/DeleteOutline';
35import Edit from '@material-ui/icons/Edit';
36import FilterList from '@material-ui/icons/FilterList';
sebdeteb8e3f12021-01-24 18:12:36 +010037import FirstPage from '@material-ui/icons/FirstPage';
38import LastPage from '@material-ui/icons/LastPage';
sebdet37285472021-02-15 18:09:38 +010039import Remove from '@material-ui/icons/Remove';
40import SaveAlt from '@material-ui/icons/SaveAlt';
sebdeteb8e3f12021-01-24 18:12:36 +010041import Search from '@material-ui/icons/Search';
sebdet37285472021-02-15 18:09:38 +010042import ViewColumn from '@material-ui/icons/ViewColumn';
43import FormControlLabel from '@material-ui/core/FormControlLabel';
44import Switch from '@material-ui/core/Switch';
sebdeteb8e3f12021-01-24 18:12:36 +010045import MaterialTable from "material-table";
sebdeteb8e3f12021-01-24 18:12:36 +010046import PolicyService from '../../../api/PolicyService';
sebdet3718a162021-02-12 17:18:47 +010047import PolicyToscaService from '../../../api/PolicyToscaService';
sebdeteb8e3f12021-01-24 18:12:36 +010048import Select from 'react-select';
sebdet3718a162021-02-12 17:18:47 +010049import JSONEditor from '@json-editor/json-editor';
sebdeteb8e3f12021-01-24 18:12:36 +010050
51const ModalStyled = styled(Modal)`
sebdet3718a162021-02-12 17:18:47 +010052 @media (min-width: 1200px) {
53 .modal-xl {
54 max-width: 96%;
55 }
56 }
sebdeteb8e3f12021-01-24 18:12:36 +010057 background-color: transparent;
58`
sebdet37285472021-02-15 18:09:38 +010059const JsonEditorDiv = styled.div`
60 margin-top: 20px;
61 background-color: ${props => props.theme.toscaTextareaBackgroundColor};
62 text-align: justify;
63 font-size: ${props => props.theme.toscaTextareaFontSize};
64 width: 100%;
65 height: 30%;
66 border: 1px solid black;
67`
68
sebdeteb8e3f12021-01-24 18:12:36 +010069
70const standardCellStyle = { border: '1px solid black' };
71const cellPdpGroupStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black'};
72const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' };
73const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'};
sebdeteb8e3f12021-01-24 18:12:36 +010074
75export default class ViewAllPolicies extends React.Component {
76 state = {
77 show: true,
78 content: 'Please select a policy to display it',
79 selectedRow: -1,
80 policiesListData: [],
sebdet37285472021-02-15 18:09:38 +010081 prefixGrouping: false,
sebdeteb8e3f12021-01-24 18:12:36 +010082 policyColumnsDefinition: [
83 {
sebdeteb8e3f12021-01-24 18:12:36 +010084 title: "Policy Name", field: "name",
85 cellStyle: standardCellStyle,
86 headerStyle: headerStyle
87 },
88 {
89 title: "Policy Version", field: "version",
90 cellStyle: standardCellStyle,
91 headerStyle: headerStyle
92 },
93 {
94 title: "Policy Type", field: "type",
95 cellStyle: standardCellStyle,
96 headerStyle: headerStyle
97 },
98 {
99 title: "Policy Type Version", field: "type_version",
100 cellStyle: standardCellStyle,
101 headerStyle: headerStyle
102 },
103 {
104 title: "Deployed in PDP", field: "pdpGroupInfo.pdpGroup",
105 cellStyle: cellPdpGroupStyle,
106 headerStyle: headerStyle,
sebdet37285472021-02-15 18:09:38 +0100107 render: rowData => this.renderPdpGroupDropBox(rowData),
108 grouping: false
sebdeteb8e3f12021-01-24 18:12:36 +0100109 },
110 {
111 title: "PDP Group", field: "pdpGroupInfo.pdpGroup",
112 cellStyle: cellPdpGroupStyle,
113 headerStyle: headerStyle
114 },
115 {
116 title: "PDP SubGroup", field: "pdpGroupInfo.pdpSubGroup",
117 cellStyle: cellPdpGroupStyle,
118 headerStyle: headerStyle
119 }
120 ],
121 tableIcons: {
sebdet37285472021-02-15 18:09:38 +0100122 Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
123 Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
124 Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
125 Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
126 DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
127 Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
128 Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
129 Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
sebdeteb8e3f12021-01-24 18:12:36 +0100130 FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
131 LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
132 NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
133 PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
134 ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
135 Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
sebdet37285472021-02-15 18:09:38 +0100136 SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />),
137 ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
138 ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />)
sebdeteb8e3f12021-01-24 18:12:36 +0100139 }
140 };
141
142 constructor(props, context) {
143 super(props, context);
144 this.handleClose = this.handleClose.bind(this);
145 this.renderPdpGroupDropBox = this.renderPdpGroupDropBox.bind(this);
146 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
sebdet3718a162021-02-12 17:18:47 +0100147 this.createJsonEditor = this.createJsonEditor.bind(this);
sebdet37285472021-02-15 18:09:38 +0100148 this.handlePrefixGrouping = this.handlePrefixGrouping.bind(this);
149 this.deletePolicy = this.deletePolicy.bind(this);
150 this.handleUpdatePolicy = this.handleUpdatePolicy.bind(this);
sebdeteb8e3f12021-01-24 18:12:36 +0100151 this.getAllPolicies();
152
153 }
154
155 handlePdpGroupChange(e) {
156 let pdpSplit = e.value.split("/");
157 let selectedPdpGroup = pdpSplit[0];
158 let selectedSubPdpGroup = pdpSplit[1];
159 if (typeof selectedSubPdpGroup !== "undefined") {
sebdet3718a162021-02-12 17:18:47 +0100160 let temp = this.state.policiesListData;
161 temp[this.state.selectedRow]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup};
162 this.setState({policiesListData: temp});
sebdeteb8e3f12021-01-24 18:12:36 +0100163 } else {
164 delete this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"];
165 }
166 }
167
sebdet3718a162021-02-12 17:18:47 +0100168 createJsonEditor(toscaModel, editorData) {
sebdet37285472021-02-15 18:09:38 +0100169 document.getElementById("policy-editor").innerHTML = "";
sebdet3718a162021-02-12 17:18:47 +0100170 JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({
171 getTab: function(text,tabId) {
172 var liel = document.createElement('li');
173 liel.classList.add('nav-item');
174 var ael = document.createElement("a");
175 ael.classList.add("nav-link");
176 ael.setAttribute("style",'padding:10px;max-width:160px;');
177 ael.setAttribute("href", "#" + tabId);
178 ael.setAttribute('data-toggle', 'tab');
179 text.setAttribute("style",'word-wrap:break-word;');
180 ael.appendChild(text);
181 liel.appendChild(ael);
182 return liel;
183 }
184 });
185 return new JSONEditor(document.getElementById("policy-editor"),
sebdet37285472021-02-15 18:09:38 +0100186 {
187 schema: toscaModel,
sebdet3718a162021-02-12 17:18:47 +0100188 startval: editorData,
189 theme: 'myBootstrap4',
190 object_layout: 'grid',
191 disable_properties: false,
192 disable_edit_json: false,
193 disable_array_reorder: true,
194 disable_array_delete_last_row: true,
195 disable_array_delete_all_rows: false,
196 array_controls_top: true,
197 keep_oneof_values: false,
198 collapsed:true,
199 show_errors: 'always',
200 display_required_only: false,
201 show_opt_in: false,
202 prompt_before_delete: true,
203 required_by_default: false
204 })
205 }
206
sebdeteb8e3f12021-01-24 18:12:36 +0100207 renderPdpGroupDropBox(dataRow) {
208 let optionItems = [{label: "NOT DEPLOYED", value: "NOT DEPLOYED"}];
209 let selectedItem = {label: "NOT DEPLOYED", value: "NOT DEPLOYED"};
210 if (typeof dataRow.supportedPdpGroups !== "undefined") {
211 for (const pdpGroup of dataRow["supportedPdpGroups"]) {
212 for (const pdpSubGroup of Object.values(pdpGroup)[0]) {
213 optionItems.push({ label: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup,
214 value: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup });
215 }
216 }
217 }
218 if (typeof dataRow.pdpGroupInfo !== "undefined") {
219 selectedItem = {label: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"],
220 value: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"]};
221 }
222 return (<div style={{width: '250px'}}><Select value={selectedItem} options={optionItems} onChange={this.handlePdpGroupChange}/></div>);
223 }
224
225 getAllPolicies() {
226 PolicyService.getPoliciesList().then(allPolicies => {
227 this.setState({ policiesListData: allPolicies["policies"] })
228 });
229 }
230
231 handleClose() {
232 this.setState({ show: false });
233 this.props.history.push('/')
234 }
235
sebdet37285472021-02-15 18:09:38 +0100236 handleUpdatePolicy() {
237 this.setState({ show: false });
238 this.props.history.push('/')
239 }
240
sebdet3718a162021-02-12 17:18:47 +0100241 handleOnRowClick(rowData) {
242 PolicyToscaService.getToscaPolicyModel(rowData["type"], rowData["type_version"]).then(respJsonPolicyTosca => {
243 this.setState({
244 selectedRow: rowData.tableData.id,
245 selectedRowJsonSchema: respJsonPolicyTosca,
246 selectedRowPolicyProperties: rowData["properties"],
247 jsonEditorForPolicy: this.createJsonEditor(respJsonPolicyTosca, rowData["properties"])
248 });
249 });
250 }
251
sebdet37285472021-02-15 18:09:38 +0100252 handlePrefixGrouping(event) {
253 this.setState({prefixGrouping: event.target.checked});
254 };
255
256 deletePolicy(event, rowData) {
257 return null;
258 }
259
sebdeteb8e3f12021-01-24 18:12:36 +0100260 render() {
261 return (
sebdet37285472021-02-15 18:09:38 +0100262 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}>
sebdeteb8e3f12021-01-24 18:12:36 +0100263 <Modal.Header closeButton>
264 </Modal.Header>
265 <Modal.Body>
sebdet37285472021-02-15 18:09:38 +0100266 <FormControlLabel
267 control={<Switch checked={this.state.prefixGrouping} onChange={this.handlePrefixGrouping} />}
268 label="Group by prefix"
269 />
sebdeteb8e3f12021-01-24 18:12:36 +0100270 <MaterialTable
271 title={"View All Policies in Policy Engine"}
272 data={this.state.policiesListData}
273 columns={this.state.policyColumnsDefinition}
274 icons={this.state.tableIcons}
sebdet3718a162021-02-12 17:18:47 +0100275 onRowClick={(event, rowData) => {this.handleOnRowClick(rowData)}}
sebdeteb8e3f12021-01-24 18:12:36 +0100276 options={{
sebdet37285472021-02-15 18:09:38 +0100277 grouping: true,
278 exportButton: true,
sebdeteb8e3f12021-01-24 18:12:36 +0100279 headerStyle:rowHeaderStyle,
280 rowStyle: rowData => ({
sebdet37285472021-02-15 18:09:38 +0100281 backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF'
sebdeteb8e3f12021-01-24 18:12:36 +0100282 })
283 }}
sebdet37285472021-02-15 18:09:38 +0100284 actions={[
285 {
286 icon: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
287 tooltip: 'Delete Policy',
288 onClick: (event, rowData) => this.deletePolicy(event, rowData)
289 }
290 ]}
291
sebdeteb8e3f12021-01-24 18:12:36 +0100292 />
sebdet37285472021-02-15 18:09:38 +0100293 <JsonEditorDiv>
294 <h5>Policy Properties Editor</h5>
295 <div id="policy-editor" title="Policy Properties"/>
296 <Button variant="secondary" onClick={this.handleUpdatePolicy}>Update</Button>
297 </JsonEditorDiv>
sebdeteb8e3f12021-01-24 18:12:36 +0100298 </Modal.Body>
299 <Modal.Footer>
300 <Button variant="secondary" onClick={this.handleClose}>Close</Button>
301 </Modal.Footer>
302 </ModalStyled>
303 );
304 }
305 }