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 | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | handleSave() { |
| 65 | var errors = this.state.jsonEditor.validate(); |
| 66 | var editorData = this.state.jsonEditor.getValue(); |
| 67 | |
| 68 | if (errors.length !== 0) { |
| 69 | console.error("Errors detected during policy data validation ", errors); |
| 70 | this.setState({ show: false }); |
| 71 | this.props.history.push('/'); |
| 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 | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 76 | this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData[0]); |
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') { |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 84 | this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData[0]); |
| 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 | |
| 104 | renderJsonEditor() { |
| 105 | console.debug("Rendering PolicyModal ", this.state.policyName); |
| 106 | var toscaModel = {}; |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 107 | var editorData = {}; |
| 108 | var pdpGroupValues = {}; |
| 109 | var chosenPdpGroupValue, chosenPdpSubgroupValue; |
| 110 | if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') { |
| 111 | toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName); |
| 112 | editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName); |
| 113 | pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpgroup(this.state.policyName); |
| 114 | chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName); |
| 115 | chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName); |
| 116 | } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') { |
| 117 | toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName); |
| 118 | editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName); |
| 119 | pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpgroup(this.state.policyName); |
| 120 | chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName); |
| 121 | chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName); |
| 122 | } |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 123 | |
| 124 | if (toscaModel == null) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | JSONEditor.defaults.options.theme = 'bootstrap4'; |
| 129 | //JSONEditor.defaults.options.iconlib = 'bootstrap2'; |
| 130 | JSONEditor.defaults.options.object_layout = 'grid'; |
| 131 | JSONEditor.defaults.options.disable_properties = true; |
| 132 | JSONEditor.defaults.options.disable_edit_json = false; |
| 133 | JSONEditor.defaults.options.disable_array_reorder = true; |
| 134 | JSONEditor.defaults.options.disable_array_delete_last_row = true; |
| 135 | JSONEditor.defaults.options.disable_array_delete_all_rows = false; |
| 136 | JSONEditor.defaults.options.show_errors = 'always'; |
| 137 | |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 138 | var pdpGroupListValues = pdpGroupValues.map(entry => { |
| 139 | return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] }; |
| 140 | }); |
| 141 | |
| 142 | if (typeof(chosenPdpGroupValue) === "undefined") { |
| 143 | this.setState({ |
| 144 | jsonEditor: new JSONEditor(document.getElementById("editor"), |
| 145 | { schema: toscaModel.schema, startval: editorData }), |
| 146 | pdpGroup: pdpGroupValues, |
| 147 | pdpGroupList: pdpGroupListValues, |
| 148 | chosenPdpGroup: chosenPdpGroupValue, |
| 149 | chosenPdpSubgroup: chosenPdpSubgroupValue |
| 150 | }) |
| 151 | } else { |
| 152 | var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue)); |
| 153 | const pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } }); |
| 154 | this.setState({ |
| 155 | jsonEditor: new JSONEditor(document.getElementById("editor"), |
| 156 | { schema: toscaModel.schema, startval: editorData }), |
| 157 | pdpGroup: pdpGroupValues, |
| 158 | pdpGroupList: pdpGroupListValues, |
| 159 | pdpSubgroupList: pdpSubgroupValues, |
| 160 | chosenPdpGroup: chosenPdpGroupValue, |
| 161 | chosenPdpSubgroup: chosenPdpSubgroupValue |
| 162 | }) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | handlePdpGroupChange(e) { |
| 167 | var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value)); |
| 168 | const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } }); |
| 169 | if (this.state.chosenPdpGroup !== e.value) { |
| 170 | this.setState({ |
| 171 | chosenPdpGroup: e.value, |
| 172 | chosenPdpSubgroup: '', |
| 173 | pdpSubgroupList: pdpSubgroupValues |
| 174 | }); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | handlePdpSubgroupChange(e) { |
| 179 | this.setState({ chosenPdpSubgroup: e.value }); |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | render() { |
| 183 | return ( |
| 184 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}> |
| 185 | <Modal.Header closeButton> |
| 186 | <Modal.Title>Configuration policies</Modal.Title> |
| 187 | </Modal.Header> |
| 188 | <Modal.Body> |
| 189 | <div id="editor" /> |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 190 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
| 191 | <Form.Label column sm="2">Pdp Group Info</Form.Label> |
| 192 | <Col sm="3"> |
| 193 | <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} /> |
| 194 | </Col> |
| 195 | <Col sm="3"> |
| 196 | <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} /> |
| 197 | </Col> |
| 198 | </Form.Group> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 199 | </Modal.Body> |
| 200 | <Modal.Footer> |
| 201 | <Button variant="secondary" onClick={this.handleClose}> |
| 202 | Close |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 203 | </Button> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 204 | <Button variant="primary" onClick={this.handleSave}> |
| 205 | Save Changes |
xuegao | 635445a | 2020-03-02 11:37:20 +0100 | [diff] [blame] | 206 | </Button> |
sebdet | c11160e | 2020-02-25 15:13:31 -0800 | [diff] [blame] | 207 | </Modal.Footer> |
| 208 | </ModalStyled> |
| 209 | |
| 210 | ); |
| 211 | } |
| 212 | } |