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'; |
| 33 | import JSONEditor from '@json-editor/json-editor'; |
| 34 | |
| 35 | const ModalStyled = styled(Modal)` |
| 36 | background-color: transparent; |
| 37 | ` |
| 38 | |
| 39 | export default class PolicyModal extends React.Component { |
| 40 | |
| 41 | state = { |
| 42 | show: true, |
| 43 | loopCache: this.props.loopCache, |
| 44 | jsonEditor: null, |
| 45 | policyName: this.props.match.params.policyName, |
| 46 | // 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] | 47 | policyInstanceType: this.props.match.params.policyInstanceType, |
| 48 | pdpGroup: null, |
| 49 | pdpGroupList: [], |
| 50 | pdpSubgroupList: [], |
| 51 | chosenPdpGroup: '', |
| 52 | chosenPdpSubgroup: '' |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | constructor(props, context) { |
| 56 | super(props, context); |
| 57 | this.handleClose = this.handleClose.bind(this); |
| 58 | this.handleSave = this.handleSave.bind(this); |
| 59 | this.renderJsonEditor = this.renderJsonEditor.bind(this); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 60 | this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this); |
| 61 | this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 62 | this.createJsonEditor = this.createJsonEditor.bind(this); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | handleSave() { |
| 66 | var errors = this.state.jsonEditor.validate(); |
| 67 | var editorData = this.state.jsonEditor.getValue(); |
| 68 | |
| 69 | if (errors.length !== 0) { |
| 70 | console.error("Errors detected during policy data validation ", errors); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 71 | return; |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 72 | } |
| 73 | else { |
| 74 | console.info("NO validation errors found in policy data"); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 75 | if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') { |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 76 | this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 77 | this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 78 | LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => { |
| 79 | this.setState({ show: false }); |
| 80 | this.props.history.push('/'); |
| 81 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
| 82 | }); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 83 | } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') { |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 84 | this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 85 | this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup); |
| 86 | LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => { |
| 87 | this.setState({ show: false }); |
| 88 | this.props.history.push('/'); |
| 89 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
| 90 | }); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | handleClose() { |
| 96 | this.setState({ show: false }); |
| 97 | this.props.history.push('/'); |
| 98 | } |
| 99 | |
| 100 | componentDidMount() { |
| 101 | this.renderJsonEditor(); |
| 102 | } |
| 103 | |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 104 | createJsonEditor(toscaModel, editorData) { |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 105 | JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({ |
| 106 | getTab: function(text,tabId) { |
| 107 | var liel = document.createElement('li'); |
| 108 | liel.classList.add('nav-item'); |
| 109 | var ael = document.createElement("a"); |
| 110 | ael.classList.add("nav-link"); |
| 111 | ael.setAttribute("style",'padding:10px;max-width:160px;'); |
| 112 | ael.setAttribute("href", "#" + tabId); |
| 113 | ael.setAttribute('data-toggle', 'tab'); |
| 114 | text.setAttribute("style",'word-wrap:break-word;'); |
| 115 | ael.appendChild(text); |
| 116 | liel.appendChild(ael); |
| 117 | return liel; |
| 118 | } |
| 119 | }); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 120 | return new JSONEditor(document.getElementById("editor"), |
| 121 | { schema: toscaModel, |
| 122 | startval: editorData, |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 123 | theme: 'myBootstrap4', |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 124 | object_layout: 'grid', |
| 125 | disable_properties: true, |
| 126 | disable_edit_json: false, |
| 127 | disable_array_reorder: true, |
| 128 | disable_array_delete_last_row: true, |
| 129 | disable_array_delete_all_rows: false, |
sebdet | 49ab84a | 2020-03-13 15:27:41 -0700 | [diff] [blame] | 130 | array_controls_top: true, |
| 131 | keep_oneof_values: false, |
| 132 | collapsed:true, |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 133 | show_errors: 'always', |
| 134 | display_required_only: false, |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 135 | show_opt_in: false, |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 136 | prompt_before_delete: true, |
sebdet | 8277572 | 2020-03-16 07:07:18 -0700 | [diff] [blame] | 137 | required_by_default: false |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 138 | }) |
| 139 | } |
| 140 | |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 141 | renderJsonEditor() { |
| 142 | console.debug("Rendering PolicyModal ", this.state.policyName); |
| 143 | var toscaModel = {}; |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 144 | var editorData = {}; |
| 145 | var pdpGroupValues = {}; |
| 146 | var chosenPdpGroupValue, chosenPdpSubgroupValue; |
| 147 | if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') { |
| 148 | toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName); |
| 149 | editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 150 | pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 151 | chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName); |
| 152 | chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName); |
| 153 | } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') { |
| 154 | toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName); |
| 155 | editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 156 | pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 157 | chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName); |
| 158 | chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName); |
| 159 | } |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 160 | |
| 161 | if (toscaModel == null) { |
| 162 | return; |
| 163 | } |
| 164 | |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 165 | var pdpSubgroupValues = []; |
| 166 | if (typeof(chosenPdpGroupValue) !== "undefined") { |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 167 | var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue)); |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 168 | pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } }); |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 169 | } |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 170 | this.setState({ |
| 171 | jsonEditor: this.createJsonEditor(toscaModel,editorData), |
| 172 | pdpGroup: pdpGroupValues, |
| 173 | pdpGroupList: pdpGroupValues.map(entry => { |
| 174 | return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] }; |
| 175 | }), |
| 176 | pdpSubgroupList: pdpSubgroupValues, |
| 177 | chosenPdpGroup: chosenPdpGroupValue, |
| 178 | chosenPdpSubgroup: chosenPdpSubgroupValue |
| 179 | }) |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | handlePdpGroupChange(e) { |
| 183 | var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value)); |
| 184 | const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } }); |
| 185 | if (this.state.chosenPdpGroup !== e.value) { |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 186 | this.setState({ |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 187 | chosenPdpGroup: e.value, |
| 188 | chosenPdpSubgroup: '', |
| 189 | pdpSubgroupList: pdpSubgroupValues |
| 190 | }); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | handlePdpSubgroupChange(e) { |
| 195 | this.setState({ chosenPdpSubgroup: e.value }); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | render() { |
| 199 | return ( |
| 200 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}> |
| 201 | <Modal.Header closeButton> |
sebdet | 2dd4e99 | 2020-03-04 15:47:39 -0800 | [diff] [blame] | 202 | <Modal.Title>Edit the policy</Modal.Title> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 203 | </Modal.Header> |
| 204 | <Modal.Body> |
| 205 | <div id="editor" /> |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 206 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
| 207 | <Form.Label column sm="2">Pdp Group Info</Form.Label> |
| 208 | <Col sm="3"> |
| 209 | <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} /> |
| 210 | </Col> |
| 211 | <Col sm="3"> |
| 212 | <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} /> |
| 213 | </Col> |
| 214 | </Form.Group> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 215 | </Modal.Body> |
| 216 | <Modal.Footer> |
| 217 | <Button variant="secondary" onClick={this.handleClose}> |
| 218 | Close |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 219 | </Button> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 220 | <Button variant="primary" onClick={this.handleSave}> |
| 221 | Save Changes |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 222 | </Button> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 223 | </Modal.Footer> |
| 224 | </ModalStyled> |
| 225 | |
| 226 | ); |
| 227 | } |
| 228 | } |