sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP CLAMP |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2020 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 from 'react' |
| 25 | import Button from 'react-bootstrap/Button'; |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 26 | import Form from 'react-bootstrap/Form'; |
| 27 | import Col from 'react-bootstrap/Col'; |
| 28 | import Row from 'react-bootstrap/Row'; |
| 29 | import Select from 'react-select'; |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 30 | import Modal from 'react-bootstrap/Modal'; |
| 31 | import styled from 'styled-components'; |
| 32 | import LoopService from '../../../api/LoopService'; |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 33 | import LoopCache from '../../../api/LoopCache'; |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 34 | import JSONEditor from '@json-editor/json-editor'; |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 35 | import Alert from 'react-bootstrap/Alert'; |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 36 | |
| 37 | const ModalStyled = styled(Modal)` |
| 38 | background-color: transparent; |
| 39 | ` |
| 40 | |
| 41 | export default class PolicyModal extends React.Component { |
| 42 | |
| 43 | state = { |
| 44 | show: true, |
| 45 | loopCache: this.props.loopCache, |
| 46 | jsonEditor: null, |
| 47 | policyName: this.props.match.params.policyName, |
| 48 | // This is to indicate whether it's an operational or config policy (in terms of loop instance) |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 49 | policyInstanceType: this.props.match.params.policyInstanceType, |
| 50 | pdpGroup: null, |
| 51 | pdpGroupList: [], |
| 52 | pdpSubgroupList: [], |
| 53 | chosenPdpGroup: '', |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 54 | chosenPdpSubgroup: '', |
| 55 | showSucAlert: false, |
| 56 | showFailAlert: false |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | constructor(props, context) { |
| 60 | super(props, context); |
| 61 | this.handleClose = this.handleClose.bind(this); |
| 62 | this.handleSave = this.handleSave.bind(this); |
| 63 | this.renderJsonEditor = this.renderJsonEditor.bind(this); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 64 | this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this); |
| 65 | this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 66 | this.createJsonEditor = this.createJsonEditor.bind(this); |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 67 | this.handleRefresh = this.handleRefresh.bind(this); |
| 68 | this.disableAlert = this.disableAlert.bind(this); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | handleSave() { |
| 72 | var errors = this.state.jsonEditor.validate(); |
| 73 | var editorData = this.state.jsonEditor.getValue(); |
| 74 | |
| 75 | if (errors.length !== 0) { |
| 76 | console.error("Errors detected during policy data validation ", errors); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 77 | return; |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 78 | } |
| 79 | else { |
| 80 | console.info("NO validation errors found in policy data"); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 81 | if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') { |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 82 | this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 83 | this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 84 | LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => { |
| 85 | this.setState({ show: false }); |
| 86 | this.props.history.push('/'); |
| 87 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
| 88 | }); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 89 | } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') { |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 90 | this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 91 | this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup); |
| 92 | LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => { |
| 93 | this.setState({ show: false }); |
| 94 | this.props.history.push('/'); |
| 95 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
| 96 | }); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | handleClose() { |
| 102 | this.setState({ show: false }); |
| 103 | this.props.history.push('/'); |
| 104 | } |
| 105 | |
| 106 | componentDidMount() { |
| 107 | this.renderJsonEditor(); |
| 108 | } |
| 109 | |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 110 | createJsonEditor(toscaModel, editorData) { |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 111 | JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({ |
| 112 | getTab: function(text,tabId) { |
| 113 | var liel = document.createElement('li'); |
| 114 | liel.classList.add('nav-item'); |
| 115 | var ael = document.createElement("a"); |
| 116 | ael.classList.add("nav-link"); |
| 117 | ael.setAttribute("style",'padding:10px;max-width:160px;'); |
| 118 | ael.setAttribute("href", "#" + tabId); |
| 119 | ael.setAttribute('data-toggle', 'tab'); |
| 120 | text.setAttribute("style",'word-wrap:break-word;'); |
| 121 | ael.appendChild(text); |
| 122 | liel.appendChild(ael); |
| 123 | return liel; |
| 124 | } |
| 125 | }); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 126 | return new JSONEditor(document.getElementById("editor"), |
| 127 | { schema: toscaModel, |
| 128 | startval: editorData, |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 129 | theme: 'myBootstrap4', |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 130 | object_layout: 'grid', |
sebdet | 1b4f20d | 2020-03-17 07:47:03 -0700 | [diff] [blame] | 131 | disable_properties: false, |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 132 | disable_edit_json: false, |
| 133 | disable_array_reorder: true, |
| 134 | disable_array_delete_last_row: true, |
| 135 | disable_array_delete_all_rows: false, |
sebdet | 49ab84a | 2020-03-13 15:27:41 -0700 | [diff] [blame] | 136 | array_controls_top: true, |
| 137 | keep_oneof_values: false, |
| 138 | collapsed:true, |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 139 | show_errors: 'always', |
| 140 | display_required_only: false, |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 141 | show_opt_in: false, |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 142 | prompt_before_delete: true, |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 143 | required_by_default: false |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 144 | }) |
| 145 | } |
| 146 | |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 147 | renderJsonEditor() { |
| 148 | console.debug("Rendering PolicyModal ", this.state.policyName); |
| 149 | var toscaModel = {}; |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 150 | var editorData = {}; |
| 151 | var pdpGroupValues = {}; |
| 152 | var chosenPdpGroupValue, chosenPdpSubgroupValue; |
| 153 | if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') { |
| 154 | toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName); |
| 155 | editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 156 | pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 157 | chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName); |
| 158 | chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName); |
| 159 | } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') { |
| 160 | toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName); |
| 161 | editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 162 | pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 163 | chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName); |
| 164 | chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName); |
| 165 | } |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 166 | |
| 167 | if (toscaModel == null) { |
| 168 | return; |
| 169 | } |
| 170 | |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 171 | var pdpSubgroupValues = []; |
| 172 | if (typeof(chosenPdpGroupValue) !== "undefined") { |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 173 | var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue)); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 174 | pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } }); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 175 | } |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 176 | this.setState({ |
| 177 | jsonEditor: this.createJsonEditor(toscaModel,editorData), |
| 178 | pdpGroup: pdpGroupValues, |
| 179 | pdpGroupList: pdpGroupValues.map(entry => { |
| 180 | return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] }; |
| 181 | }), |
| 182 | pdpSubgroupList: pdpSubgroupValues, |
| 183 | chosenPdpGroup: chosenPdpGroupValue, |
| 184 | chosenPdpSubgroup: chosenPdpSubgroupValue |
| 185 | }) |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | handlePdpGroupChange(e) { |
| 189 | var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value)); |
| 190 | const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } }); |
| 191 | if (this.state.chosenPdpGroup !== e.value) { |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 192 | this.setState({ |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 193 | chosenPdpGroup: e.value, |
| 194 | chosenPdpSubgroup: '', |
| 195 | pdpSubgroupList: pdpSubgroupValues |
| 196 | }); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | handlePdpSubgroupChange(e) { |
| 201 | this.setState({ chosenPdpSubgroup: e.value }); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 202 | } |
| 203 | |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 204 | handleRefresh() { |
| 205 | var newLoopCache, toscaModel, editorData; |
| 206 | if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') { |
| 207 | LoopService.refreshMicroServicePolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => { |
| 208 | newLoopCache = new LoopCache(data); |
| 209 | toscaModel = newLoopCache.getMicroServiceJsonRepresentationForName(this.state.policyName); |
| 210 | editorData = newLoopCache.getMicroServicePropertiesForName(this.state.policyName); |
| 211 | document.getElementById("editor").innerHTML = ""; |
| 212 | this.setState({ |
| 213 | loopCache: newLoopCache, |
| 214 | jsonEditor: this.createJsonEditor(toscaModel,editorData), |
| 215 | showSucAlert: true, |
| 216 | showMessage: "Successfully refreshed" |
| 217 | }); |
| 218 | }) |
| 219 | .catch(error => { |
| 220 | console.error("Error while refreshing the Operational Policy Json Representation"); |
| 221 | this.setState({ |
| 222 | showFailAlert: true, |
| 223 | showMessage: "Refreshing of UI failed" |
| 224 | }); |
| 225 | }); |
| 226 | } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') { |
| 227 | LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => { |
| 228 | var newLoopCache = new LoopCache(data); |
| 229 | toscaModel = newLoopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName); |
| 230 | editorData = newLoopCache.getOperationalPolicyPropertiesForName(this.state.policyName); |
| 231 | document.getElementById("editor").innerHTML = ""; |
| 232 | this.setState({ |
| 233 | loopCache: newLoopCache, |
| 234 | jsonEditor: this.createJsonEditor(toscaModel,editorData), |
| 235 | showSucAlert: true, |
| 236 | showMessage: "Successfully refreshed" |
| 237 | }); |
| 238 | }) |
| 239 | .catch(error => { |
| 240 | console.error("Error while refreshing the Operational Policy Json Representation"); |
| 241 | this.setState({ |
| 242 | showFailAlert: true, |
| 243 | showMessage: "Refreshing of UI failed" |
| 244 | }); |
| 245 | }); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | disableAlert() { |
| 250 | this.setState ({ showSucAlert: false, showFailAlert: false }); |
| 251 | } |
| 252 | |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 253 | render() { |
| 254 | return ( |
| 255 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}> |
| 256 | <Modal.Header closeButton> |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 257 | <Modal.Title>Edit the policy</Modal.Title> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 258 | </Modal.Header> |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 259 | <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible> |
| 260 | {this.state.showMessage} |
| 261 | </Alert> |
| 262 | <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible> |
| 263 | {this.state.showMessage} |
| 264 | </Alert> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 265 | <Modal.Body> |
| 266 | <div id="editor" /> |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 267 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
| 268 | <Form.Label column sm="2">Pdp Group Info</Form.Label> |
| 269 | <Col sm="3"> |
| 270 | <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} /> |
| 271 | </Col> |
| 272 | <Col sm="3"> |
| 273 | <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} /> |
| 274 | </Col> |
| 275 | </Form.Group> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 276 | </Modal.Body> |
| 277 | <Modal.Footer> |
| 278 | <Button variant="secondary" onClick={this.handleClose}> |
| 279 | Close |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 280 | </Button> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 281 | <Button variant="primary" onClick={this.handleSave}> |
| 282 | Save Changes |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 283 | </Button> |
xuegao | e18ed70 | 2020-03-17 10:46:39 +0100 | [diff] [blame] | 284 | <Button variant="primary" onClick={this.handleRefresh}> |
| 285 | Refresh |
| 286 | </Button> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 287 | </Modal.Footer> |
| 288 | </ModalStyled> |
| 289 | |
| 290 | ); |
| 291 | } |
| 292 | } |