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'; |
| 28 | import ArrowUpward from '@material-ui/icons/ArrowUpward'; |
| 29 | import ChevronLeft from '@material-ui/icons/ChevronLeft'; |
| 30 | import ChevronRight from '@material-ui/icons/ChevronRight'; |
| 31 | import Clear from '@material-ui/icons/Clear'; |
| 32 | import FirstPage from '@material-ui/icons/FirstPage'; |
| 33 | import LastPage from '@material-ui/icons/LastPage'; |
| 34 | import Search from '@material-ui/icons/Search'; |
| 35 | import MaterialTable from "material-table"; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 36 | import PolicyService from '../../../api/PolicyService'; |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 37 | import PolicyToscaService from '../../../api/PolicyToscaService'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 38 | import Select from 'react-select'; |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 39 | import JSONEditor from '@json-editor/json-editor'; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 40 | |
| 41 | const ModalStyled = styled(Modal)` |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 42 | @media (min-width: 1200px) { |
| 43 | .modal-xl { |
| 44 | max-width: 96%; |
| 45 | } |
| 46 | } |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 47 | background-color: transparent; |
| 48 | ` |
| 49 | |
| 50 | const standardCellStyle = { border: '1px solid black' }; |
| 51 | const cellPdpGroupStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black'}; |
| 52 | const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; |
| 53 | const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 54 | |
| 55 | export default class ViewAllPolicies extends React.Component { |
| 56 | state = { |
| 57 | show: true, |
| 58 | content: 'Please select a policy to display it', |
| 59 | selectedRow: -1, |
| 60 | policiesListData: [], |
| 61 | policyColumnsDefinition: [ |
| 62 | { |
| 63 | title: "#", field: "index", render: rowData => rowData.tableData.id + 1, |
| 64 | cellStyle: standardCellStyle, |
| 65 | headerStyle: headerStyle |
| 66 | }, |
| 67 | { |
| 68 | title: "Policy Name", field: "name", |
| 69 | cellStyle: standardCellStyle, |
| 70 | headerStyle: headerStyle |
| 71 | }, |
| 72 | { |
| 73 | title: "Policy Version", field: "version", |
| 74 | cellStyle: standardCellStyle, |
| 75 | headerStyle: headerStyle |
| 76 | }, |
| 77 | { |
| 78 | title: "Policy Type", field: "type", |
| 79 | cellStyle: standardCellStyle, |
| 80 | headerStyle: headerStyle |
| 81 | }, |
| 82 | { |
| 83 | title: "Policy Type Version", field: "type_version", |
| 84 | cellStyle: standardCellStyle, |
| 85 | headerStyle: headerStyle |
| 86 | }, |
| 87 | { |
| 88 | title: "Deployed in PDP", field: "pdpGroupInfo.pdpGroup", |
| 89 | cellStyle: cellPdpGroupStyle, |
| 90 | headerStyle: headerStyle, |
| 91 | render: rowData => this.renderPdpGroupDropBox(rowData) |
| 92 | }, |
| 93 | { |
| 94 | title: "PDP Group", field: "pdpGroupInfo.pdpGroup", |
| 95 | cellStyle: cellPdpGroupStyle, |
| 96 | headerStyle: headerStyle |
| 97 | }, |
| 98 | { |
| 99 | title: "PDP SubGroup", field: "pdpGroupInfo.pdpSubGroup", |
| 100 | cellStyle: cellPdpGroupStyle, |
| 101 | headerStyle: headerStyle |
| 102 | } |
| 103 | ], |
| 104 | tableIcons: { |
| 105 | FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />), |
| 106 | LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />), |
| 107 | NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), |
| 108 | PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />), |
| 109 | ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />), |
| 110 | Search: forwardRef((props, ref) => <Search {...props} ref={ref} />), |
| 111 | SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />) |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | constructor(props, context) { |
| 116 | super(props, context); |
| 117 | this.handleClose = this.handleClose.bind(this); |
| 118 | this.renderPdpGroupDropBox = this.renderPdpGroupDropBox.bind(this); |
| 119 | this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this); |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 120 | this.createJsonEditor = this.createJsonEditor.bind(this); |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 121 | this.getAllPolicies(); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | handlePdpGroupChange(e) { |
| 126 | let pdpSplit = e.value.split("/"); |
| 127 | let selectedPdpGroup = pdpSplit[0]; |
| 128 | let selectedSubPdpGroup = pdpSplit[1]; |
| 129 | if (typeof selectedSubPdpGroup !== "undefined") { |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 130 | let temp = this.state.policiesListData; |
| 131 | temp[this.state.selectedRow]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup}; |
| 132 | this.setState({policiesListData: temp}); |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 133 | } else { |
| 134 | delete this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"]; |
| 135 | } |
| 136 | } |
| 137 | |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 138 | createJsonEditor(toscaModel, editorData) { |
| 139 | JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({ |
| 140 | getTab: function(text,tabId) { |
| 141 | var liel = document.createElement('li'); |
| 142 | liel.classList.add('nav-item'); |
| 143 | var ael = document.createElement("a"); |
| 144 | ael.classList.add("nav-link"); |
| 145 | ael.setAttribute("style",'padding:10px;max-width:160px;'); |
| 146 | ael.setAttribute("href", "#" + tabId); |
| 147 | ael.setAttribute('data-toggle', 'tab'); |
| 148 | text.setAttribute("style",'word-wrap:break-word;'); |
| 149 | ael.appendChild(text); |
| 150 | liel.appendChild(ael); |
| 151 | return liel; |
| 152 | } |
| 153 | }); |
| 154 | return new JSONEditor(document.getElementById("policy-editor"), |
| 155 | { schema: toscaModel, |
| 156 | startval: editorData, |
| 157 | theme: 'myBootstrap4', |
| 158 | object_layout: 'grid', |
| 159 | disable_properties: false, |
| 160 | disable_edit_json: false, |
| 161 | disable_array_reorder: true, |
| 162 | disable_array_delete_last_row: true, |
| 163 | disable_array_delete_all_rows: false, |
| 164 | array_controls_top: true, |
| 165 | keep_oneof_values: false, |
| 166 | collapsed:true, |
| 167 | show_errors: 'always', |
| 168 | display_required_only: false, |
| 169 | show_opt_in: false, |
| 170 | prompt_before_delete: true, |
| 171 | required_by_default: false |
| 172 | }) |
| 173 | } |
| 174 | |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 175 | renderPdpGroupDropBox(dataRow) { |
| 176 | let optionItems = [{label: "NOT DEPLOYED", value: "NOT DEPLOYED"}]; |
| 177 | let selectedItem = {label: "NOT DEPLOYED", value: "NOT DEPLOYED"}; |
| 178 | if (typeof dataRow.supportedPdpGroups !== "undefined") { |
| 179 | for (const pdpGroup of dataRow["supportedPdpGroups"]) { |
| 180 | for (const pdpSubGroup of Object.values(pdpGroup)[0]) { |
| 181 | optionItems.push({ label: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup, |
| 182 | value: Object.keys(pdpGroup)[0]+"/"+pdpSubGroup }); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | if (typeof dataRow.pdpGroupInfo !== "undefined") { |
| 187 | selectedItem = {label: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"], |
| 188 | value: dataRow["pdpGroupInfo"]["pdpGroup"]+"/"+dataRow["pdpGroupInfo"]["pdpSubGroup"]}; |
| 189 | } |
| 190 | return (<div style={{width: '250px'}}><Select value={selectedItem} options={optionItems} onChange={this.handlePdpGroupChange}/></div>); |
| 191 | } |
| 192 | |
| 193 | getAllPolicies() { |
| 194 | PolicyService.getPoliciesList().then(allPolicies => { |
| 195 | this.setState({ policiesListData: allPolicies["policies"] }) |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | handleClose() { |
| 200 | this.setState({ show: false }); |
| 201 | this.props.history.push('/') |
| 202 | } |
| 203 | |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 204 | handleOnRowClick(rowData) { |
| 205 | PolicyToscaService.getToscaPolicyModel(rowData["type"], rowData["type_version"]).then(respJsonPolicyTosca => { |
| 206 | this.setState({ |
| 207 | selectedRow: rowData.tableData.id, |
| 208 | selectedRowJsonSchema: respJsonPolicyTosca, |
| 209 | selectedRowPolicyProperties: rowData["properties"], |
| 210 | jsonEditorForPolicy: this.createJsonEditor(respJsonPolicyTosca, rowData["properties"]) |
| 211 | }); |
| 212 | }); |
| 213 | } |
| 214 | |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 215 | render() { |
| 216 | return ( |
| 217 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}> |
| 218 | <Modal.Header closeButton> |
| 219 | </Modal.Header> |
| 220 | <Modal.Body> |
| 221 | <MaterialTable |
| 222 | title={"View All Policies in Policy Engine"} |
| 223 | data={this.state.policiesListData} |
| 224 | columns={this.state.policyColumnsDefinition} |
| 225 | icons={this.state.tableIcons} |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 226 | onRowClick={(event, rowData) => {this.handleOnRowClick(rowData)}} |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 227 | options={{ |
| 228 | headerStyle:rowHeaderStyle, |
| 229 | rowStyle: rowData => ({ |
| 230 | backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' |
| 231 | }) |
| 232 | }} |
| 233 | /> |
sebdet | 3718a16 | 2021-02-12 17:18:47 +0100 | [diff] [blame^] | 234 | <div id="policy-editor" /> |
sebdet | eb8e3f1 | 2021-01-24 18:12:36 +0100 | [diff] [blame] | 235 | </Modal.Body> |
| 236 | <Modal.Footer> |
| 237 | <Button variant="secondary" onClick={this.handleClose}>Close</Button> |
| 238 | </Modal.Footer> |
| 239 | </ModalStyled> |
| 240 | ); |
| 241 | } |
| 242 | } |