2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2022 Nordix Foundation.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 import React, { forwardRef } from 'react'
25 import Button from 'react-bootstrap/Button';
26 import Modal from 'react-bootstrap/Modal';
27 import styled from 'styled-components';
28 import AddBox from '@material-ui/icons/AddBox';
29 import ArrowDownward from '@material-ui/icons/ArrowDownward';
30 import Check from '@material-ui/icons/Check';
31 import ChevronLeft from '@material-ui/icons/ChevronLeft';
32 import ChevronRight from '@material-ui/icons/ChevronRight';
33 import Clear from '@material-ui/icons/Clear';
34 import DeleteRoundedIcon from '@material-ui/icons/DeleteRounded';
35 import Edit from '@material-ui/icons/Edit';
36 import FilterList from '@material-ui/icons/FilterList';
37 import FirstPage from '@material-ui/icons/FirstPage';
38 import LastPage from '@material-ui/icons/LastPage';
39 import Remove from '@material-ui/icons/Remove';
40 import SaveAlt from '@material-ui/icons/SaveAlt';
41 import Search from '@material-ui/icons/Search';
42 import ViewColumn from '@material-ui/icons/ViewColumn';
43 import DehazeIcon from '@material-ui/icons/Dehaze';
44 import ArrowForwardIosIcon from '@material-ui/icons/ArrowForwardIos';
45 import AddIcon from '@material-ui/icons/Add';
46 import PublishIcon from '@material-ui/icons/Publish';
47 import MaterialTable from "material-table";
48 import PolicyService from '../../../api/PolicyService';
49 import PolicyToscaService from '../../../api/PolicyToscaService';
50 import Alert from 'react-bootstrap/Alert';
51 import Tabs from 'react-bootstrap/Tabs';
52 import Tab from 'react-bootstrap/Tab';
53 import PolicyEditor from './PolicyEditor';
54 import ToscaViewer from './ToscaViewer';
55 import PolicyDeploymentEditor from './PolicyDeploymentEditor';
56 import PoliciesTreeViewer from './PoliciesTreeViewer';
57 import PolicyToscaFileSelector from './PolicyToscaFileSelector';
59 const DivWhiteSpaceStyled = styled.div`
63 const ModalStyled = styled(Modal)`
64 @media (min-width: 800px) {
69 background-color: transparent;
71 const DetailedRow = styled.div`
73 background-color: ${ props => props.theme.policyEditorBackgroundColor };
74 font-size: ${ props => props.theme.policyEditorFontSize };
81 const PoliciesTreeViewerDiv = styled.div`
88 const MaterialTableDiv = styled.div`
94 const standardCellStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black' };
95 const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' };
96 const rowHeaderStyle = { backgroundColor: '#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black' };
98 export default class ViewAllPolicies extends React.Component {
101 policiesListData: [],
102 policiesListDataFiltered: [],
103 toscaModelsListData: [],
104 toscaModelsListDataFiltered: [],
105 jsonEditorForPolicy: new Map(),
106 showSuccessAlert: false,
107 showFailAlert: false,
108 showFileSelector: false,
109 policyColumnsDefinition: [
111 title: "Policy Name", field: "name",
112 cellStyle: standardCellStyle,
113 headerStyle: headerStyle
116 title: "Policy Version", field: "version",
117 cellStyle: standardCellStyle,
118 headerStyle: headerStyle,
121 title: "Policy Type", field: "type",
122 cellStyle: standardCellStyle,
123 headerStyle: headerStyle
126 title: "Policy Type Version", field: "type_version",
127 cellStyle: standardCellStyle,
128 headerStyle: headerStyle
131 title: "Deployable in PDP Group", field: "supportedPdpGroupsString",
132 cellStyle: standardCellStyle,
133 headerStyle: headerStyle
136 title: "Deployed in PDP Group", field: "pdpGroupInfoString",
137 cellStyle: standardCellStyle,
138 headerStyle: headerStyle
141 toscaColumnsDefinition: [
143 title: "Policy Model Type", field: "policyModelType",
144 cellStyle: standardCellStyle,
145 headerStyle: headerStyle
148 title: "Policy Acronym", field: "policyAcronym",
149 cellStyle: standardCellStyle,
150 headerStyle: headerStyle
153 title: "Version", field: "version",
154 cellStyle: standardCellStyle,
155 headerStyle: headerStyle
158 title: "Uploaded By", field: "updatedBy",
159 cellStyle: standardCellStyle,
160 headerStyle: headerStyle
163 title: "Uploaded Date", field: "updatedDate", editable: 'never',
164 cellStyle: standardCellStyle,
165 headerStyle: headerStyle
169 Add: forwardRef((props, ref) => <AddBox { ...props } ref={ ref }/>),
170 Check: forwardRef((props, ref) => <Check { ...props } ref={ ref }/>),
171 Clear: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>),
172 Delete: forwardRef((props, ref) => <DeleteRoundedIcon { ...props } ref={ ref }/>),
173 DetailPanel: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>),
174 Edit: forwardRef((props, ref) => <Edit { ...props } ref={ ref }/>),
175 Export: forwardRef((props, ref) => <SaveAlt { ...props } ref={ ref }/>),
176 Filter: forwardRef((props, ref) => <FilterList { ...props } ref={ ref }/>),
177 FirstPage: forwardRef((props, ref) => <FirstPage { ...props } ref={ ref }/>),
178 LastPage: forwardRef((props, ref) => <LastPage { ...props } ref={ ref }/>),
179 NextPage: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>),
180 PreviousPage: forwardRef((props, ref) => <ChevronLeft { ...props } ref={ ref }/>),
181 ResetSearch: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>),
182 Search: forwardRef((props, ref) => <Search { ...props } ref={ ref }/>),
183 SortArrow: forwardRef((props, ref) => <ArrowDownward { ...props } ref={ ref }/>),
184 ThirdStateCheck: forwardRef((props, ref) => <Remove { ...props } ref={ ref }/>),
185 ViewColumn: forwardRef((props, ref) => <ViewColumn { ...props } ref={ ref }/>)
189 constructor(props, context) {
190 super(props, context);
191 this.handleClose = this.handleClose.bind(this);
192 this.handleDeletePolicy = this.handleDeletePolicy.bind(this);
193 this.disableAlert = this.disableAlert.bind(this);
194 this.getAllPolicies = this.getAllPolicies.bind(this);
195 this.getAllToscaModels = this.getAllToscaModels.bind(this);
196 this.generateAdditionalPolicyColumns = this.generateAdditionalPolicyColumns.bind(this);
197 this.filterPolicies = this.filterPolicies.bind(this);
198 this.filterTosca = this.filterTosca.bind(this);
199 this.showFileSelector = this.showFileSelector.bind(this);
200 this.disableFileSelector = this.disableFileSelector.bind(this);
201 this.getAllPolicies();
202 this.getAllToscaModels();
205 generateAdditionalPolicyColumns(policiesData) {
206 policiesData.forEach(policy => {
207 let supportedPdpGroupsString = "";
208 if (typeof policy.supportedPdpGroups !== "undefined") {
209 for (const pdpGroup of policy["supportedPdpGroups"]) {
210 for (const pdpSubGroup of Object.values(pdpGroup)[0]) {
211 supportedPdpGroupsString += (Object.keys(pdpGroup)[0] + "/" + pdpSubGroup + "\r\n");
214 policy["supportedPdpGroupsString"] = supportedPdpGroupsString;
217 let infoPdpGroup = "";
218 if (typeof policy.pdpGroupInfo !== "undefined") {
219 policy["pdpGroupInfo"].forEach(pdpGroupElem => {
220 let groupName = Object.keys(pdpGroupElem)[0];
221 pdpGroupElem[groupName]["pdpSubgroups"].forEach(pdpSubGroupElem => {
222 infoPdpGroup += (groupName + "/" + pdpSubGroupElem["pdpType"] + " ("
223 + pdpGroupElem[groupName]["pdpGroupState"] + ")" + "\r\n");
225 policy["pdpGroupInfoString"] = infoPdpGroup;
231 getAllToscaModels() {
232 PolicyToscaService.getToscaPolicyModels().then(toscaModelsList => {
234 toscaModelsListData: toscaModelsList,
235 toscaModelsListDataFiltered: toscaModelsList
241 PolicyService.getPoliciesList().then(allPolicies => {
242 this.generateAdditionalPolicyColumns(allPolicies["policies"])
244 policiesListData: allPolicies["policies"],
245 policiesListDataFiltered: allPolicies["policies"],
252 console.log("handleClose called");
253 this.setState({ show: false });
254 this.props.history.push('/')
257 handleDeletePolicy(event, rowData) {
258 PolicyService.deletePolicy(rowData["type"], rowData["type_version"], rowData["name"], rowData["version"]).then(
259 respPolicyDeletion => {
260 if (typeof (respPolicyDeletion) === "undefined") {
261 //it indicates a failure
264 showMessage: 'Policy Deletion Failure'
268 showSuccessAlert: true,
269 showMessage: 'Policy successfully Deleted'
271 this.getAllPolicies();
278 this.setState({ showSuccessAlert: false, showFailAlert: false });
281 filterPolicies(prefixForFiltering) {
282 this.setState({ policiesListDataFiltered: this.state.policiesListData.filter(element => element.name.startsWith(prefixForFiltering)) });
285 filterTosca(prefixForFiltering) {
286 this.setState({ toscaModelsListDataFiltered: this.state.toscaModelsListData.filter(element => element.policyModelType.startsWith(prefixForFiltering)) });
290 this.setState({ showFileSelector: true });
293 disableFileSelector() {
294 this.setState({ showFileSelector: false });
297 renderPoliciesTab() {
299 <Tab eventKey="policies" title="Policies in Policy Framework">
302 <PoliciesTreeViewerDiv>
303 <PoliciesTreeViewer policiesData={ this.state.policiesListData } valueForTreeCreation="name" policiesFilterFunction={ this.filterPolicies }/>
304 </PoliciesTreeViewerDiv>
308 data={ this.state.policiesListDataFiltered }
309 columns={ this.state.policyColumnsDefinition }
310 icons={ this.state.tableIcons }
311 onRowClick={ (event, rowData, togglePanel) => togglePanel() }
315 headerStyle: rowHeaderStyle,
316 actionsColumnIndex: -1
320 icon: ArrowForwardIosIcon,
321 tooltip: 'Show Configuration',
325 <PolicyEditor policyModelType={ rowData["type"] } policyModelTypeVersion={ rowData["type_version"] }
326 policyName={ rowData["name"] } policyVersion={ rowData["version"] } policyProperties={ rowData["properties"] }
327 policiesTableUpdateFunction={ this.getAllPolicies }/>
334 openIcon: DehazeIcon,
335 tooltip: 'Show Raw Data',
339 <pre>{ JSON.stringify(rowData, null, 2) }</pre>
346 openIcon: PublishIcon,
347 tooltip: 'PDP Group Deployment',
351 <PolicyDeploymentEditor policyData={ rowData } policiesTableUpdateFunction={ this.getAllPolicies }/>
359 icon: DeleteRoundedIcon,
360 tooltip: 'Delete Policy',
361 onClick: (event, rowData) => this.handleDeletePolicy(event, rowData)
374 <Tab eventKey="tosca models" title="Tosca Models in Policy Framework">
377 <PoliciesTreeViewerDiv>
378 <PoliciesTreeViewer policiesData={ this.state.toscaModelsListData } valueForTreeCreation="policyModelType" policiesFilterFunction={ this.filterTosca }/>
379 </PoliciesTreeViewerDiv>
382 title={ "Tosca Models" }
383 data={ this.state.toscaModelsListDataFiltered }
384 columns={ this.state.toscaColumnsDefinition }
385 icons={ this.state.tableIcons }
386 onRowClick={ (event, rowData, togglePanel) => togglePanel() }
390 headerStyle: rowHeaderStyle,
391 actionsColumnIndex: -1
396 tooltip: 'Add New Tosca Model',
398 onClick: () => this.showFileSelector()
403 icon: ArrowForwardIosIcon,
404 tooltip: 'Show Tosca',
408 <ToscaViewer toscaData={ rowData }/>
415 openIcon: DehazeIcon,
416 tooltip: 'Show Raw Data',
420 <pre>{ JSON.stringify(rowData, null, 2) }</pre>
428 tooltip: 'Create a policy from this model',
432 <PolicyEditor policyModelType={ rowData["policyModelType"] } policyModelTypeVersion={ rowData["version"] } policyProperties={ {} } policiesTableUpdateFunction={ this.getAllPolicies }/>
449 <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }>
450 <Modal.Header closeButton>
452 <Tabs id="controlled-tab-example" activeKey={ this.state.key } onSelect={ key => this.setState({ key, selectedRowData: {} }) }>
453 { this.renderPoliciesTab() }
454 { this.renderToscaTab() }
456 <Alert variant="success" show={ this.state.showSuccessAlert } onClose={ this.disableAlert } dismissible>
457 <DivWhiteSpaceStyled>
458 { this.state.showMessage }
459 </DivWhiteSpaceStyled>
461 <Alert variant="danger" show={ this.state.showFailAlert } onClose={ this.disableAlert } dismissible>
462 <DivWhiteSpaceStyled>
463 { this.state.showMessage }
464 </DivWhiteSpaceStyled>
467 <Button variant="secondary" onClick={ this.handleClose }>Close</Button>
470 <PolicyToscaFileSelector show={ this.state.showFileSelector } disableFunction={ this.disableFileSelector } toscaTableUpdateFunction={ this.getAllToscaModels }/>