blob: a1cf9d5c1e58765967b40f5ba2292b48230f8979 [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);
sebdeta0a3a032021-02-16 14:53:43 +0100149 this.handleDeletePolicy = this.handleDeletePolicy.bind(this);
sebdet37285472021-02-15 18:09:38 +0100150 this.handleUpdatePolicy = this.handleUpdatePolicy.bind(this);
sebdeta0a3a032021-02-16 14:53:43 +0100151 this.handleCreateNewVersion = this.handleCreateNewVersion(this);
sebdeteb8e3f12021-01-24 18:12:36 +0100152 this.getAllPolicies();
153
154 }
155
156 handlePdpGroupChange(e) {
157 let pdpSplit = e.value.split("/");
158 let selectedPdpGroup = pdpSplit[0];
159 let selectedSubPdpGroup = pdpSplit[1];
160 if (typeof selectedSubPdpGroup !== "undefined") {
sebdet3718a162021-02-12 17:18:47 +0100161 let temp = this.state.policiesListData;
162 temp[this.state.selectedRow]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup};
163 this.setState({policiesListData: temp});
sebdeteb8e3f12021-01-24 18:12:36 +0100164 } else {
165 delete this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"];
166 }
167 }
168
sebdet3718a162021-02-12 17:18:47 +0100169 createJsonEditor(toscaModel, editorData) {
sebdet37285472021-02-15 18:09:38 +0100170 document.getElementById("policy-editor").innerHTML = "";
sebdet3718a162021-02-12 17:18:47 +0100171 JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({
172 getTab: function(text,tabId) {
173 var liel = document.createElement('li');
174 liel.classList.add('nav-item');
175 var ael = document.createElement("a");
176 ael.classList.add("nav-link");
177 ael.setAttribute("style",'padding:10px;max-width:160px;');
178 ael.setAttribute("href", "#" + tabId);
179 ael.setAttribute('data-toggle', 'tab');
180 text.setAttribute("style",'word-wrap:break-word;');
181 ael.appendChild(text);
182 liel.appendChild(ael);
183 return liel;
184 }
185 });
186 return new JSONEditor(document.getElementById("policy-editor"),
sebdet37285472021-02-15 18:09:38 +0100187 {
188 schema: toscaModel,
sebdet3718a162021-02-12 17:18:47 +0100189 startval: editorData,
190 theme: 'myBootstrap4',
191 object_layout: 'grid',
192 disable_properties: false,
193 disable_edit_json: false,
194 disable_array_reorder: true,
195 disable_array_delete_last_row: true,
196 disable_array_delete_all_rows: false,
197 array_controls_top: true,
198 keep_oneof_values: false,
199 collapsed:true,
200 show_errors: 'always',
201 display_required_only: false,
202 show_opt_in: false,
203 prompt_before_delete: true,
204 required_by_default: false
205 })
206 }
207
sebdeteb8e3f12021-01-24 18:12:36 +0100208 renderPdpGroupDropBox(dataRow) {
209 let optionItems = [{label: "NOT DEPLOYED", value: "NOT DEPLOYED"}];
210 let selectedItem = {label: "NOT DEPLOYED", value: "NOT DEPLOYED"};
211 if (typeof dataRow.supportedPdpGroups !== "undefined") {
212 for (const pdpGroup of dataRow["supportedPdpGroups"]) {
213 for (const pdpSubGroup of Object.values(pdpGroup)[0]) {
214 optionItems.push({ label: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup,
215 value: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup });
216 }
217 }
218 }
219 if (typeof dataRow.pdpGroupInfo !== "undefined") {
220 selectedItem = {label: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"],
221 value: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"]};
222 }
223 return (<div style={{width: '250px'}}><Select value={selectedItem} options={optionItems} onChange={this.handlePdpGroupChange}/></div>);
224 }
225
226 getAllPolicies() {
227 PolicyService.getPoliciesList().then(allPolicies => {
228 this.setState({ policiesListData: allPolicies["policies"] })
229 });
230 }
231
232 handleClose() {
233 this.setState({ show: false });
234 this.props.history.push('/')
235 }
236
sebdet3718a162021-02-12 17:18:47 +0100237 handleOnRowClick(rowData) {
238 PolicyToscaService.getToscaPolicyModel(rowData["type"], rowData["type_version"]).then(respJsonPolicyTosca => {
239 this.setState({
240 selectedRow: rowData.tableData.id,
241 selectedRowJsonSchema: respJsonPolicyTosca,
242 selectedRowPolicyProperties: rowData["properties"],
243 jsonEditorForPolicy: this.createJsonEditor(respJsonPolicyTosca, rowData["properties"])
244 });
245 });
246 }
247
sebdet37285472021-02-15 18:09:38 +0100248 handlePrefixGrouping(event) {
249 this.setState({prefixGrouping: event.target.checked});
sebdeta0a3a032021-02-16 14:53:43 +0100250 }
sebdet37285472021-02-15 18:09:38 +0100251
sebdeta0a3a032021-02-16 14:53:43 +0100252 handleDeletePolicy(event, rowData) {
sebdet37285472021-02-15 18:09:38 +0100253 return null;
254 }
255
sebdeta0a3a032021-02-16 14:53:43 +0100256 handleCreateNewVersion(event,rowData) {
257 return null;
258 }
259
260 handleUpdatePolicy() {
261 this.setState({ show: false });
262 this.props.history.push('/')
263 }
264
sebdeteb8e3f12021-01-24 18:12:36 +0100265 render() {
266 return (
sebdet37285472021-02-15 18:09:38 +0100267 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}>
sebdeteb8e3f12021-01-24 18:12:36 +0100268 <Modal.Header closeButton>
269 </Modal.Header>
270 <Modal.Body>
sebdet37285472021-02-15 18:09:38 +0100271 <FormControlLabel
272 control={<Switch checked={this.state.prefixGrouping} onChange={this.handlePrefixGrouping} />}
273 label="Group by prefix"
274 />
sebdeteb8e3f12021-01-24 18:12:36 +0100275 <MaterialTable
276 title={"View All Policies in Policy Engine"}
277 data={this.state.policiesListData}
278 columns={this.state.policyColumnsDefinition}
279 icons={this.state.tableIcons}
sebdet3718a162021-02-12 17:18:47 +0100280 onRowClick={(event, rowData) => {this.handleOnRowClick(rowData)}}
sebdeteb8e3f12021-01-24 18:12:36 +0100281 options={{
sebdet37285472021-02-15 18:09:38 +0100282 grouping: true,
283 exportButton: true,
sebdeteb8e3f12021-01-24 18:12:36 +0100284 headerStyle:rowHeaderStyle,
285 rowStyle: rowData => ({
sebdet37285472021-02-15 18:09:38 +0100286 backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF'
sebdeteb8e3f12021-01-24 18:12:36 +0100287 })
288 }}
sebdet37285472021-02-15 18:09:38 +0100289 actions={[
290 {
291 icon: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
292 tooltip: 'Delete Policy',
sebdeta0a3a032021-02-16 14:53:43 +0100293 onClick: (event, rowData) => this.handleDeletePolicy(event, rowData)
sebdet37285472021-02-15 18:09:38 +0100294 }
295 ]}
296
sebdeteb8e3f12021-01-24 18:12:36 +0100297 />
sebdet37285472021-02-15 18:09:38 +0100298 <JsonEditorDiv>
299 <h5>Policy Properties Editor</h5>
300 <div id="policy-editor" title="Policy Properties"/>
sebdeta0a3a032021-02-16 14:53:43 +0100301 <Button variant="secondary" title="Create a new policy version from the defined parameters" onClick={this.handleCreateNewVersion}>Create New Version</Button>
302 <Button variant="secondary" title="Update the current policy version, BE CAREFUL this will undeploy the policy from PDP, delete it and then recreate the policy" onClick={this.handleUpdatePolicy}>Update Current Version</Button>
sebdet37285472021-02-15 18:09:38 +0100303 </JsonEditorDiv>
sebdeteb8e3f12021-01-24 18:12:36 +0100304 </Modal.Body>
305 <Modal.Footer>
306 <Button variant="secondary" onClick={this.handleClose}>Close</Button>
307 </Modal.Footer>
308 </ModalStyled>
309 );
310 }
311 }