blob: 48d52a14c6f3ce8e4902b6750f5124c84d37c41d [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';
sebdet1e2760e2021-02-26 19:14:03 +010034import DeleteRoundedIcon from '@material-ui/icons/DeleteRounded';
sebdet37285472021-02-15 18:09:38 +010035import 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';
sebdetb8831e52021-03-12 19:30:22 +010043import DehazeIcon from '@material-ui/icons/Dehaze';
44import ArrowForwardIosIcon from '@material-ui/icons/ArrowForwardIos';
45import AddIcon from '@material-ui/icons/Add';
sebdetee233562021-03-22 18:55:46 +010046import PublishIcon from '@material-ui/icons/Publish';
sebdet37285472021-02-15 18:09:38 +010047import FormControlLabel from '@material-ui/core/FormControlLabel';
48import Switch from '@material-ui/core/Switch';
sebdeteb8e3f12021-01-24 18:12:36 +010049import MaterialTable from "material-table";
sebdeteb8e3f12021-01-24 18:12:36 +010050import PolicyService from '../../../api/PolicyService';
sebdet3718a162021-02-12 17:18:47 +010051import PolicyToscaService from '../../../api/PolicyToscaService';
sebdetee233562021-03-22 18:55:46 +010052import Select from '@material-ui/core/Select';
sebdetc427e642021-02-17 17:53:17 +010053import Alert from 'react-bootstrap/Alert';
sebdetea2969f2021-02-25 13:58:36 +010054import Tabs from 'react-bootstrap/Tabs';
55import Tab from 'react-bootstrap/Tab';
sebdet1e2760e2021-02-26 19:14:03 +010056import PolicyEditor from './PolicyEditor';
57import ToscaViewer from './ToscaViewer';
sebdetee233562021-03-22 18:55:46 +010058import PolicyDeploymentEditor from './PolicyDeploymentEditor';
sebdetc427e642021-02-17 17:53:17 +010059
60const DivWhiteSpaceStyled = styled.div`
sebdetea2969f2021-02-25 13:58:36 +010061 white-space: pre;
sebdetc427e642021-02-17 17:53:17 +010062`
sebdeteb8e3f12021-01-24 18:12:36 +010063
64const ModalStyled = styled(Modal)`
sebdetee233562021-03-22 18:55:46 +010065 @media (min-width: 800px) {
sebdet3718a162021-02-12 17:18:47 +010066 .modal-xl {
67 max-width: 96%;
68 }
69 }
sebdeteb8e3f12021-01-24 18:12:36 +010070 background-color: transparent;
71`
sebdet1e2760e2021-02-26 19:14:03 +010072const DetailedRow = styled.div`
73 margin: 0 auto;
74 background-color: ${props => props.theme.policyEditorBackgroundColor};
75 font-size: ${props => props.theme.policyEditorFontSize};
76 width: 97%;
77 margin-left: auto;
78 margin-right: 0;
sebdet37285472021-02-15 18:09:38 +010079`
80
sebdeteb8e3f12021-01-24 18:12:36 +010081
sebdet1e2760e2021-02-26 19:14:03 +010082const standardCellStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black' };
sebdetee233562021-03-22 18:55:46 +010083const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' };
84const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'};
sebdeteb8e3f12021-01-24 18:12:36 +010085
86export default class ViewAllPolicies extends React.Component {
87 state = {
88 show: true,
sebdetee233562021-03-22 18:55:46 +010089 showPolicyDeploymentDialog: false,
sebdeteb8e3f12021-01-24 18:12:36 +010090 content: 'Please select a policy to display it',
sebdetc427e642021-02-17 17:53:17 +010091 selectedRowId: -1,
sebdeteb8e3f12021-01-24 18:12:36 +010092 policiesListData: [],
sebdet1e2760e2021-02-26 19:14:03 +010093 toscaModelsListData: [],
94 jsonEditorForPolicy: new Map(),
sebdet37285472021-02-15 18:09:38 +010095 prefixGrouping: false,
sebdetc427e642021-02-17 17:53:17 +010096 showSuccessAlert: false,
97 showFailAlert: false,
sebdeteb8e3f12021-01-24 18:12:36 +010098 policyColumnsDefinition: [
99 {
sebdeteb8e3f12021-01-24 18:12:36 +0100100 title: "Policy Name", field: "name",
101 cellStyle: standardCellStyle,
102 headerStyle: headerStyle
103 },
104 {
105 title: "Policy Version", field: "version",
106 cellStyle: standardCellStyle,
sebdetee233562021-03-22 18:55:46 +0100107 headerStyle: headerStyle,
sebdeteb8e3f12021-01-24 18:12:36 +0100108 },
109 {
110 title: "Policy Type", field: "type",
111 cellStyle: standardCellStyle,
112 headerStyle: headerStyle
113 },
114 {
115 title: "Policy Type Version", field: "type_version",
116 cellStyle: standardCellStyle,
117 headerStyle: headerStyle
118 },
119 {
sebdetee233562021-03-22 18:55:46 +0100120 title: "Deployable in PDP Group", field: "supportedPdpGroupsString",
121 cellStyle: standardCellStyle,
sebdeteb8e3f12021-01-24 18:12:36 +0100122 headerStyle: headerStyle
123 },
124 {
sebdetee233562021-03-22 18:55:46 +0100125 title: "Deployed in PDP Group", field: "pdpGroupInfoString",
126 cellStyle: standardCellStyle,
sebdeteb8e3f12021-01-24 18:12:36 +0100127 headerStyle: headerStyle
128 }
129 ],
sebdet1e2760e2021-02-26 19:14:03 +0100130 toscaColumnsDefinition: [
131 {
132 title: "Policy Model Type", field: "policyModelType",
133 cellStyle: standardCellStyle,
134 headerStyle: headerStyle
135 },
136 {
137 title: "Policy Acronym", field: "policyAcronym",
138 cellStyle: standardCellStyle,
139 headerStyle: headerStyle
140 },
141 {
142 title: "Version", field: "version",
143 cellStyle: standardCellStyle,
144 headerStyle: headerStyle
145 },
146 {
147 title: "Uploaded By", field: "updatedBy",
148 cellStyle: standardCellStyle,
149 headerStyle: headerStyle
150 },
151 {
152 title: "Uploaded Date", field: "updatedDate", editable: 'never',
153 cellStyle: standardCellStyle,
154 headerStyle: headerStyle
155 }
156 ],
sebdeteb8e3f12021-01-24 18:12:36 +0100157 tableIcons: {
sebdet37285472021-02-15 18:09:38 +0100158 Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
159 Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
160 Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
sebdet1e2760e2021-02-26 19:14:03 +0100161 Delete: forwardRef((props, ref) => <DeleteRoundedIcon {...props} ref={ref} />),
sebdet37285472021-02-15 18:09:38 +0100162 DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
163 Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
164 Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
165 Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
sebdeteb8e3f12021-01-24 18:12:36 +0100166 FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
167 LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
168 NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
169 PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
170 ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
171 Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
sebdet37285472021-02-15 18:09:38 +0100172 SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />),
173 ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
174 ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />)
sebdeteb8e3f12021-01-24 18:12:36 +0100175 }
176 };
177
178 constructor(props, context) {
179 super(props, context);
180 this.handleClose = this.handleClose.bind(this);
sebdet37285472021-02-15 18:09:38 +0100181 this.handlePrefixGrouping = this.handlePrefixGrouping.bind(this);
sebdeta0a3a032021-02-16 14:53:43 +0100182 this.handleDeletePolicy = this.handleDeletePolicy.bind(this);
sebdetc427e642021-02-17 17:53:17 +0100183 this.disableAlert = this.disableAlert.bind(this);
sebdetb8831e52021-03-12 19:30:22 +0100184 this.getAllPolicies = this.getAllPolicies.bind(this);
185 this.getAllToscaModels = this.getAllToscaModels.bind(this);
sebdetee233562021-03-22 18:55:46 +0100186 this.generateAdditionalPolicyColumns = this.generateAdditionalPolicyColumns.bind(this);
sebdeteb8e3f12021-01-24 18:12:36 +0100187 this.getAllPolicies();
sebdet1e2760e2021-02-26 19:14:03 +0100188 this.getAllToscaModels();
189 }
sebdeteb8e3f12021-01-24 18:12:36 +0100190
sebdetee233562021-03-22 18:55:46 +0100191 generateAdditionalPolicyColumns(policiesData) {
192 policiesData.forEach(policy => {
193 let supportedPdpGroupsString = "";
194 if (typeof policy.supportedPdpGroups !== "undefined") {
195 for (const pdpGroup of policy["supportedPdpGroups"]) {
196 for (const pdpSubGroup of Object.values(pdpGroup)[0]) {
197 supportedPdpGroupsString += (Object.keys(pdpGroup)[0] + "/" + pdpSubGroup + "\r\n");
198 }
199 }
200 policy["supportedPdpGroupsString"] = supportedPdpGroupsString;
201 }
202
203 let infoPdpGroup = "";
204 if (typeof policy.pdpGroupInfo !== "undefined") {
205 policy["pdpGroupInfo"].forEach(pdpGroupElem => {
206 let groupName = Object.keys(pdpGroupElem)[0];
207 pdpGroupElem[groupName]["pdpSubgroups"].forEach(pdpSubGroupElem => {
208 infoPdpGroup += (groupName + "/" + pdpSubGroupElem["pdpType"] + " ("
209 + pdpGroupElem[groupName]["pdpGroupState"] + ")" + "\r\n");
210 });
211 policy["pdpGroupInfoString"] = infoPdpGroup;
212 });
213 }
214 });
215 }
216
sebdet1e2760e2021-02-26 19:14:03 +0100217 getAllToscaModels() {
218 PolicyToscaService.getToscaPolicyModels().then(toscaModelsList => {
219 this.setState({ toscaModelsListData: toscaModelsList });
220 });
sebdeteb8e3f12021-01-24 18:12:36 +0100221 }
222
sebdeteb8e3f12021-01-24 18:12:36 +0100223 getAllPolicies() {
224 PolicyService.getPoliciesList().then(allPolicies => {
sebdetee233562021-03-22 18:55:46 +0100225 this.generateAdditionalPolicyColumns(allPolicies["policies"])
sebdeteb8e3f12021-01-24 18:12:36 +0100226 this.setState({ policiesListData: allPolicies["policies"] })
227 });
sebdetee233562021-03-22 18:55:46 +0100228
sebdeteb8e3f12021-01-24 18:12:36 +0100229 }
230
231 handleClose() {
232 this.setState({ show: false });
233 this.props.history.push('/')
234 }
235
sebdet37285472021-02-15 18:09:38 +0100236 handlePrefixGrouping(event) {
237 this.setState({prefixGrouping: event.target.checked});
sebdeta0a3a032021-02-16 14:53:43 +0100238 }
sebdet37285472021-02-15 18:09:38 +0100239
sebdeta0a3a032021-02-16 14:53:43 +0100240 handleDeletePolicy(event, rowData) {
sebdetea2969f2021-02-25 13:58:36 +0100241 PolicyService.deletePolicy(rowData["type"], rowData["type_version"], rowData["name"],rowData["version"]).then(
242 respPolicyDeletion => {
sebdetb8831e52021-03-12 19:30:22 +0100243 if (typeof(respPolicyDeletion) === "undefined") {
sebdetea2969f2021-02-25 13:58:36 +0100244 //it indicates a failure
245 this.setState({
246 showFailAlert: true,
247 showMessage: 'Policy Deletion Failure'
248 });
249 } else {
250 this.setState({
251 showSuccessAlert: true,
252 showMessage: 'Policy successfully Deleted'
253 });
254 }
sebdetb8831e52021-03-12 19:30:22 +0100255 this.getAllPolicies();
sebdetea2969f2021-02-25 13:58:36 +0100256 }
257 )
sebdet37285472021-02-15 18:09:38 +0100258 }
259
sebdetc427e642021-02-17 17:53:17 +0100260 disableAlert() {
261 this.setState ({ showSuccessAlert: false, showFailAlert: false });
262 }
263
sebdet1e2760e2021-02-26 19:14:03 +0100264 renderPoliciesTab() {
265 return (
266 <Tab eventKey="policies" title="Policies in Policy Framework">
267 <Modal.Body>
268 <FormControlLabel
269 control={<Switch checked={this.state.prefixGrouping} onChange={this.handlePrefixGrouping} />}
270 label="Group by prefix"
271 />
272 <MaterialTable
273 title={"Policies"}
274 data={this.state.policiesListData}
275 columns={this.state.policyColumnsDefinition}
276 icons={this.state.tableIcons}
277 onRowClick={(event, rowData, togglePanel) => togglePanel()}
278 options={{
279 grouping: true,
280 exportButton: true,
281 headerStyle:rowHeaderStyle,
282 rowStyle: rowData => ({
283 backgroundColor: (this.state.selectedRowId !== -1 && this.state.selectedRowId === rowData.tableData.id) ? '#EEE' : '#FFF'
284 }),
285 actionsColumnIndex: -1
286 }}
287 detailPanel={[
288 {
sebdetb8831e52021-03-12 19:30:22 +0100289 icon: ArrowForwardIosIcon,
sebdet1e2760e2021-02-26 19:14:03 +0100290 tooltip: 'Show Configuration',
291 render: rowData => {
292 return (
293 <DetailedRow>
sebdetb8831e52021-03-12 19:30:22 +0100294 <PolicyEditor policyModelType={rowData["type"]} policyModelTypeVersion={rowData["type_version"]} policyName={rowData["name"]} policyVersion={rowData["version"]} policyProperties={rowData["properties"]} policyUpdateFunction={this.getAllPolicies} />
sebdet1e2760e2021-02-26 19:14:03 +0100295 </DetailedRow>
296 )
297 },
298 },
299 {
sebdetb8831e52021-03-12 19:30:22 +0100300 icon: DehazeIcon,
sebdet1e2760e2021-02-26 19:14:03 +0100301 tooltip: 'Show Raw Data',
302 render: rowData => {
303 return (
304 <DetailedRow>
305 <pre>{JSON.stringify(rowData, null, 2)}</pre>
306 </DetailedRow>
307 )
308 },
309 },
sebdetee233562021-03-22 18:55:46 +0100310 {
311 icon: PublishIcon,
312 tooltip: 'PDP Group Deployment',
313 render: rowData => {
314 return (
315 <PolicyDeploymentEditor policyData={rowData}/>
316 )
317 },
318 }
sebdet1e2760e2021-02-26 19:14:03 +0100319 ]}
320 actions={[
321 {
322 icon: forwardRef((props, ref) => <DeleteRoundedIcon {...props} ref={ref} />),
323 tooltip: 'Delete Policy',
324 onClick: (event, rowData) => this.handleDeletePolicy(event, rowData)
325 }
326 ]}
327 />
328 </Modal.Body>
329 </Tab>
330 );
331 }
332
333 renderToscaTab() {
334 return (
335 <Tab eventKey="tosca models" title="Tosca Models in Policy Framework">
336 <Modal.Body>
337 <FormControlLabel
338 control={<Switch checked={this.state.prefixGrouping} onChange={this.handlePrefixGrouping} />}
339 label="Group by prefix"
340 />
341 <MaterialTable
342 title={"Tosca Models"}
343 data={this.state.toscaModelsListData}
344 columns={this.state.toscaColumnsDefinition}
345 icons={this.state.tableIcons}
346 onRowClick={(event, rowData, togglePanel) => togglePanel()}
347 options={{
348 grouping: true,
349 exportButton: true,
350 headerStyle:rowHeaderStyle,
351 rowStyle: rowData => ({
352 backgroundColor: (this.state.selectedRowId !== -1 && this.state.selectedRowId === rowData.tableData.id) ? '#EEE' : '#FFF'
353 }),
354 actionsColumnIndex: -1
355 }}
356 detailPanel={[
357 {
sebdetb8831e52021-03-12 19:30:22 +0100358 icon: ArrowForwardIosIcon,
sebdet1e2760e2021-02-26 19:14:03 +0100359 tooltip: 'Show Tosca',
360 render: rowData => {
361 return (
362 <DetailedRow>
363 <ToscaViewer toscaData={rowData} />
364 </DetailedRow>
365 )
366 },
367 },
368 {
sebdetb8831e52021-03-12 19:30:22 +0100369 icon: DehazeIcon,
sebdet1e2760e2021-02-26 19:14:03 +0100370 tooltip: 'Show Raw Data',
371 render: rowData => {
372 return (
373 <DetailedRow>
374 <pre>{JSON.stringify(rowData, null, 2)}</pre>
375 </DetailedRow>
376 )
377 },
378 },
379 {
sebdetb8831e52021-03-12 19:30:22 +0100380 icon: AddIcon,
sebdet1e2760e2021-02-26 19:14:03 +0100381 tooltip: 'Create a policy from this model',
382 render: rowData => {
383 return (
384 <DetailedRow>
sebdetb8831e52021-03-12 19:30:22 +0100385 <PolicyEditor policyModelType={rowData["policyModelType"]} policyModelTypeVersion={rowData["version"]} policyProperties={{}} policyUpdateFunction={this.getAllPolicies} />
sebdet1e2760e2021-02-26 19:14:03 +0100386 </DetailedRow>
387 )
388 },
389 },
390 ]}
sebdet1e2760e2021-02-26 19:14:03 +0100391 />
392 </Modal.Body>
393 </Tab>
394 );
395 }
396
sebdeteb8e3f12021-01-24 18:12:36 +0100397 render() {
398 return (
sebdet37285472021-02-15 18:09:38 +0100399 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}>
sebdeteb8e3f12021-01-24 18:12:36 +0100400 <Modal.Header closeButton>
401 </Modal.Header>
sebdetea2969f2021-02-25 13:58:36 +0100402 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}>
sebdet1e2760e2021-02-26 19:14:03 +0100403 {this.renderPoliciesTab()}
404 {this.renderToscaTab()}
sebdetea2969f2021-02-25 13:58:36 +0100405 </Tabs>
sebdetc427e642021-02-17 17:53:17 +0100406 <Alert variant="success" show={this.state.showSuccessAlert} onClose={this.disableAlert} dismissible>
sebdetea2969f2021-02-25 13:58:36 +0100407 <DivWhiteSpaceStyled>
408 {this.state.showMessage}
409 </DivWhiteSpaceStyled>
410 </Alert>
411 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
412 <DivWhiteSpaceStyled>
413 {this.state.showMessage}
414 </DivWhiteSpaceStyled>
415 </Alert>
sebdeteb8e3f12021-01-24 18:12:36 +0100416 <Modal.Footer>
417 <Button variant="secondary" onClick={this.handleClose}>Close</Button>
418 </Modal.Footer>
sebdetee233562021-03-22 18:55:46 +0100419 </ModalStyled>
sebdeteb8e3f12021-01-24 18:12:36 +0100420 );
421 }
422 }