blob: 216a8abd2238a9c7f655c88fd357e8733dc62d8b [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';
28import ArrowUpward from '@material-ui/icons/ArrowUpward';
29import ChevronLeft from '@material-ui/icons/ChevronLeft';
30import ChevronRight from '@material-ui/icons/ChevronRight';
31import Clear from '@material-ui/icons/Clear';
32import FirstPage from '@material-ui/icons/FirstPage';
33import LastPage from '@material-ui/icons/LastPage';
34import Search from '@material-ui/icons/Search';
35import MaterialTable from "material-table";
36import LoopCache from '../../../api/LoopCache';
37import PolicyService from '../../../api/PolicyService';
38import Select from 'react-select';
39
40const ModalStyled = styled(Modal)`
41 background-color: transparent;
42`
43
44const standardCellStyle = { border: '1px solid black' };
45const cellPdpGroupStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black'};
46const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' };
47const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'};
48const selectPdpGroupStyle = {
49 width: 2000
50}
51
52export default class ViewAllPolicies extends React.Component {
53 state = {
54 show: true,
55 content: 'Please select a policy to display it',
56 selectedRow: -1,
57 policiesListData: [],
58 policyColumnsDefinition: [
59 {
60 title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
61 cellStyle: standardCellStyle,
62 headerStyle: headerStyle
63 },
64 {
65 title: "Policy Name", field: "name",
66 cellStyle: standardCellStyle,
67 headerStyle: headerStyle
68 },
69 {
70 title: "Policy Version", field: "version",
71 cellStyle: standardCellStyle,
72 headerStyle: headerStyle
73 },
74 {
75 title: "Policy Type", field: "type",
76 cellStyle: standardCellStyle,
77 headerStyle: headerStyle
78 },
79 {
80 title: "Policy Type Version", field: "type_version",
81 cellStyle: standardCellStyle,
82 headerStyle: headerStyle
83 },
84 {
85 title: "Deployed in PDP", field: "pdpGroupInfo.pdpGroup",
86 cellStyle: cellPdpGroupStyle,
87 headerStyle: headerStyle,
88 render: rowData => this.renderPdpGroupDropBox(rowData)
89 },
90 {
91 title: "PDP Group", field: "pdpGroupInfo.pdpGroup",
92 cellStyle: cellPdpGroupStyle,
93 headerStyle: headerStyle
94 },
95 {
96 title: "PDP SubGroup", field: "pdpGroupInfo.pdpSubGroup",
97 cellStyle: cellPdpGroupStyle,
98 headerStyle: headerStyle
99 }
100 ],
101 tableIcons: {
102 FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
103 LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
104 NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
105 PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
106 ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
107 Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
108 SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
109 }
110 };
111
112 constructor(props, context) {
113 super(props, context);
114 this.handleClose = this.handleClose.bind(this);
115 this.renderPdpGroupDropBox = this.renderPdpGroupDropBox.bind(this);
116 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
117
118 this.getAllPolicies();
119
120 }
121
122 handlePdpGroupChange(e) {
123 let pdpSplit = e.value.split("/");
124 let selectedPdpGroup = pdpSplit[0];
125 let selectedSubPdpGroup = pdpSplit[1];
126 if (typeof selectedSubPdpGroup !== "undefined") {
127 this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup};
128 } else {
129 delete this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"];
130 }
131 }
132
133 renderPdpGroupDropBox(dataRow) {
134 let optionItems = [{label: "NOT DEPLOYED", value: "NOT DEPLOYED"}];
135 let selectedItem = {label: "NOT DEPLOYED", value: "NOT DEPLOYED"};
136 if (typeof dataRow.supportedPdpGroups !== "undefined") {
137 for (const pdpGroup of dataRow["supportedPdpGroups"]) {
138 for (const pdpSubGroup of Object.values(pdpGroup)[0]) {
139 optionItems.push({ label: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup,
140 value: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup });
141 }
142 }
143 }
144 if (typeof dataRow.pdpGroupInfo !== "undefined") {
145 selectedItem = {label: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"],
146 value: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"]};
147 }
148 return (<div style={{width: '250px'}}><Select value={selectedItem} options={optionItems} onChange={this.handlePdpGroupChange}/></div>);
149 }
150
151 getAllPolicies() {
152 PolicyService.getPoliciesList().then(allPolicies => {
153 this.setState({ policiesListData: allPolicies["policies"] })
154 });
155 }
156
157 handleClose() {
158 this.setState({ show: false });
159 this.props.history.push('/')
160 }
161
162 render() {
163 return (
164 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}>
165 <Modal.Header closeButton>
166 </Modal.Header>
167 <Modal.Body>
168 <MaterialTable
169 title={"View All Policies in Policy Engine"}
170 data={this.state.policiesListData}
171 columns={this.state.policyColumnsDefinition}
172 icons={this.state.tableIcons}
173 onRowClick={(event, rowData) => {this.setState({selectedRow: rowData.tableData.id})}}
174 options={{
175 headerStyle:rowHeaderStyle,
176 rowStyle: rowData => ({
177 backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF'
178 })
179 }}
180 />
181 </Modal.Body>
182 <Modal.Footer>
183 <Button variant="secondary" onClick={this.handleClose}>Close</Button>
184 </Modal.Footer>
185 </ModalStyled>
186 );
187 }
188 }