sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 1 | /*- |
| 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 | |
| 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'; |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 28 | import AddBox from '@material-ui/icons/AddBox'; |
| 29 | import ArrowDownward from '@material-ui/icons/ArrowDownward'; |
| 30 | import Check from '@material-ui/icons/Check'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 31 | import ChevronLeft from '@material-ui/icons/ChevronLeft'; |
| 32 | import ChevronRight from '@material-ui/icons/ChevronRight'; |
| 33 | import Clear from '@material-ui/icons/Clear'; |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 34 | import DeleteOutline from '@material-ui/icons/DeleteOutline'; |
| 35 | import Edit from '@material-ui/icons/Edit'; |
| 36 | import FilterList from '@material-ui/icons/FilterList'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 37 | import FirstPage from '@material-ui/icons/FirstPage'; |
| 38 | import LastPage from '@material-ui/icons/LastPage'; |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 39 | import Remove from '@material-ui/icons/Remove'; |
| 40 | import SaveAlt from '@material-ui/icons/SaveAlt'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 41 | import Search from '@material-ui/icons/Search'; |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 42 | import ViewColumn from '@material-ui/icons/ViewColumn'; |
| 43 | import FormControlLabel from '@material-ui/core/FormControlLabel'; |
| 44 | import Switch from '@material-ui/core/Switch'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 45 | import MaterialTable from "material-table"; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 46 | import PolicyService from '../../../api/PolicyService'; |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 47 | import PolicyToscaService from '../../../api/PolicyToscaService'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 48 | import Select from 'react-select'; |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 49 | import JSONEditor from '@json-editor/json-editor'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 50 | |
| 51 | const ModalStyled = styled(Modal)` |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 52 | @media (min-width: 1200px) { |
| 53 | .modal-xl { |
| 54 | max-width: 96%; |
| 55 | } |
| 56 | } |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 57 | background-color: transparent; |
| 58 | ` |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 59 | const 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 | |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 69 | |
| 70 | const standardCellStyle = { border: '1px solid black' }; |
| 71 | const cellPdpGroupStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black'}; |
| 72 | const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; |
| 73 | const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 74 | |
| 75 | export 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: [], |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 81 | prefixGrouping: false, |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 82 | policyColumnsDefinition: [ |
| 83 | { |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 84 | 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, |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 107 | render: rowData => this.renderPdpGroupDropBox(rowData), |
| 108 | grouping: false |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 109 | }, |
| 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: { |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 122 | 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} />), |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 130 | 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} />), |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 136 | 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} />) |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 139 | } |
| 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); |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 147 | this.createJsonEditor = this.createJsonEditor.bind(this); |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 148 | this.handlePrefixGrouping = this.handlePrefixGrouping.bind(this); |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 149 | this.handleDeletePolicy = this.handleDeletePolicy.bind(this); |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 150 | this.handleUpdatePolicy = this.handleUpdatePolicy.bind(this); |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 151 | this.handleCreateNewVersion = this.handleCreateNewVersion(this); |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 152 | 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") { |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 161 | let temp = this.state.policiesListData; |
| 162 | temp[this.state.selectedRow]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup}; |
| 163 | this.setState({policiesListData: temp}); |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 164 | } else { |
| 165 | delete this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"]; |
| 166 | } |
| 167 | } |
| 168 | |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 169 | createJsonEditor(toscaModel, editorData) { |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 170 | document.getElementById("policy-editor").innerHTML = ""; |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 171 | 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"), |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 187 | { |
| 188 | schema: toscaModel, |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 189 | 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 | |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 208 | 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 | |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 237 | 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 | |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 248 | handlePrefixGrouping(event) { |
| 249 | this.setState({prefixGrouping: event.target.checked}); |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 250 | } |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 251 | |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 252 | handleDeletePolicy(event, rowData) { |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 253 | return null; |
| 254 | } |
| 255 | |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 256 | handleCreateNewVersion(event,rowData) { |
| 257 | return null; |
| 258 | } |
| 259 | |
| 260 | handleUpdatePolicy() { |
| 261 | this.setState({ show: false }); |
| 262 | this.props.history.push('/') |
| 263 | } |
| 264 | |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 265 | render() { |
| 266 | return ( |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 267 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}> |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 268 | <Modal.Header closeButton> |
| 269 | </Modal.Header> |
| 270 | <Modal.Body> |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 271 | <FormControlLabel |
| 272 | control={<Switch checked={this.state.prefixGrouping} onChange={this.handlePrefixGrouping} />} |
| 273 | label="Group by prefix" |
| 274 | /> |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 275 | <MaterialTable |
| 276 | title={"View All Policies in Policy Engine"} |
| 277 | data={this.state.policiesListData} |
| 278 | columns={this.state.policyColumnsDefinition} |
| 279 | icons={this.state.tableIcons} |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame] | 280 | onRowClick={(event, rowData) => {this.handleOnRowClick(rowData)}} |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 281 | options={{ |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 282 | grouping: true, |
| 283 | exportButton: true, |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 284 | headerStyle:rowHeaderStyle, |
| 285 | rowStyle: rowData => ({ |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 286 | backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 287 | }) |
| 288 | }} |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 289 | actions={[ |
| 290 | { |
| 291 | icon: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />), |
| 292 | tooltip: 'Delete Policy', |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 293 | onClick: (event, rowData) => this.handleDeletePolicy(event, rowData) |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 294 | } |
| 295 | ]} |
| 296 | |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 297 | /> |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 298 | <JsonEditorDiv> |
| 299 | <h5>Policy Properties Editor</h5> |
| 300 | <div id="policy-editor" title="Policy Properties"/> |
sebdet | a0a3a03 | 2021-02-16 14:53:43 +0100 | [diff] [blame^] | 301 | <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> |
sebdet | 3728547 | 2021-02-15 18:09:38 +0100 | [diff] [blame] | 303 | </JsonEditorDiv> |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 304 | </Modal.Body> |
| 305 | <Modal.Footer> |
| 306 | <Button variant="secondary" onClick={this.handleClose}>Close</Button> |
| 307 | </Modal.Footer> |
| 308 | </ModalStyled> |
| 309 | ); |
| 310 | } |
| 311 | } |