sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP CLAMP |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2019 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'; |
| 26 | import Modal from 'react-bootstrap/Modal'; |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 27 | import styled from 'styled-components'; |
xuegao | 2f5b2cd | 2020-01-06 16:13:46 +0100 | [diff] [blame] | 28 | import LoopCache from '../../../api/LoopCache'; |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 29 | import LoopService from '../../../api/LoopService'; |
| 30 | import JSONEditor from '@json-editor/json-editor'; |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 31 | |
| 32 | const ModalStyled = styled(Modal)` |
| 33 | background-color: transparent; |
| 34 | ` |
| 35 | |
| 36 | export default class OperationalPolicyModal extends React.Component { |
| 37 | |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 38 | state = { |
| 39 | show: true, |
| 40 | loopCache: this.props.loopCache, |
sebdet | 337f366 | 2019-09-06 18:11:51 +0200 | [diff] [blame] | 41 | jsonEditor: null |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 44 | constructor(props, context) { |
| 45 | super(props, context); |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 46 | this.handleClose = this.handleClose.bind(this); |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 47 | this.handleSave = this.handleSave.bind(this); |
| 48 | this.renderJsonEditor = this.renderJsonEditor.bind(this); |
xuegao | 2f5b2cd | 2020-01-06 16:13:46 +0100 | [diff] [blame] | 49 | this.handleRefresh = this.handleRefresh.bind(this); |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 50 | this.setDefaultJsonEditorOptions(); |
| 51 | } |
| 52 | |
| 53 | handleSave() { |
| 54 | var errors = this.state.jsonEditor.validate(); |
| 55 | var editorData = this.state.jsonEditor.getValue(); |
| 56 | |
| 57 | if (errors.length !== 0) { |
| 58 | console.error("Errors detected during config policy data validation ", errors); |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 59 | this.props.showAlert(errors); |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 60 | } |
| 61 | else { |
| 62 | console.info("NO validation errors found in config policy data"); |
| 63 | this.state.loopCache.updateOperationalPolicyProperties(editorData); |
| 64 | LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => { |
| 65 | this.setState({ show: false }); |
| 66 | this.props.history.push('/'); |
| 67 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
| 68 | }); |
| 69 | } |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | handleClose() { |
| 73 | this.setState({ show: false }); |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 74 | this.props.history.push('/'); |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 75 | } |
| 76 | |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 77 | componentDidMount() { |
| 78 | this.renderJsonEditor(); |
| 79 | } |
| 80 | |
| 81 | setDefaultJsonEditorOptions() { |
xuegao | 83fe2b0 | 2019-09-25 11:23:18 +0200 | [diff] [blame] | 82 | JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({ |
| 83 | getTab: function(text,tabId) { |
| 84 | var liel = document.createElement('li'); |
| 85 | liel.classList.add('nav-item'); |
| 86 | var ael = document.createElement("a"); |
| 87 | ael.classList.add("nav-link"); |
| 88 | ael.setAttribute("style",'padding:10px;max-width:160px;'); |
| 89 | ael.setAttribute("href", "#" + tabId); |
| 90 | ael.setAttribute('data-toggle', 'tab'); |
| 91 | text.setAttribute("style",'word-wrap:break-word;'); |
| 92 | ael.appendChild(text); |
| 93 | liel.appendChild(ael); |
| 94 | return liel; |
| 95 | } |
| 96 | }); |
| 97 | JSONEditor.defaults.options.theme = 'myBootstrap4'; |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 98 | JSONEditor.defaults.options.object_layout = 'grid'; |
| 99 | JSONEditor.defaults.options.disable_properties = true; |
| 100 | JSONEditor.defaults.options.disable_edit_json = false; |
| 101 | JSONEditor.defaults.options.disable_array_reorder = true; |
| 102 | JSONEditor.defaults.options.disable_array_delete_last_row = true; |
| 103 | JSONEditor.defaults.options.disable_array_delete_all_rows = false; |
| 104 | JSONEditor.defaults.options.array_controls_top=true; |
| 105 | JSONEditor.defaults.options.show_errors = 'always'; |
| 106 | JSONEditor.defaults.options.keep_oneof_values=false; |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 107 | JSONEditor.defaults.options.collapsed=true; |
| 108 | //JSONEditor.defaults.options.template = 'default'; |
| 109 | } |
| 110 | |
| 111 | renderJsonEditor() { |
| 112 | console.debug("Rendering OperationalPolicyModal"); |
| 113 | var schema_json = this.state.loopCache.getOperationalPolicyJsonSchema(); |
| 114 | |
| 115 | if (schema_json == null) { |
| 116 | console.error("NO Operational policy schema found"); |
| 117 | return; |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 118 | } |
xuegao | c488fed | 2019-12-19 11:58:45 +0100 | [diff] [blame] | 119 | var operationalPoliciesData = this.state.loopCache.getOperationalPoliciesNoJsonSchema(); |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 120 | |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 121 | this.setState({ |
| 122 | jsonEditor: new JSONEditor(document.getElementById("editor"), |
| 123 | { schema: schema_json.schema, startval: operationalPoliciesData }) |
| 124 | }) |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 125 | } |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 126 | |
xuegao | 2f5b2cd | 2020-01-06 16:13:46 +0100 | [diff] [blame] | 127 | handleRefresh() { |
| 128 | LoopService.refreshOpPolicyJson(this.state.loopCache.getLoopName()).then(data => { |
| 129 | var newLoopCache = new LoopCache(data); |
| 130 | var schema_json = newLoopCache.getOperationalPolicyJsonSchema(); |
| 131 | var operationalPoliciesData = newLoopCache.getOperationalPoliciesNoJsonSchema(); |
| 132 | document.getElementById("editor").innerHTML = ""; |
| 133 | this.setState({ |
| 134 | loopCache: newLoopCache, |
| 135 | jsonEditor: new JSONEditor(document.getElementById("editor"), |
| 136 | { schema: schema_json.schema, startval: operationalPoliciesData }) |
| 137 | }) |
| 138 | this.props.updateLoopFunction(data); |
| 139 | |
| 140 | }) |
| 141 | .catch(error => { |
| 142 | console.error("Error while refreshing the Operational Policy Json Representation"); |
| 143 | }); |
| 144 | } |
| 145 | |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 146 | render() { |
| 147 | return ( |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 148 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}> |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 149 | <Modal.Header closeButton> |
| 150 | <Modal.Title>Operational policies</Modal.Title> |
| 151 | </Modal.Header> |
| 152 | <Modal.Body> |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 153 | <div id="editor" /> |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 154 | |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 155 | </Modal.Body> |
| 156 | <Modal.Footer> |
| 157 | <Button variant="secondary" onClick={this.handleClose}> |
| 158 | Close |
xuegao | 2f5b2cd | 2020-01-06 16:13:46 +0100 | [diff] [blame] | 159 | </Button> |
| 160 | <Button variant="secondary" onClick={this.handleRefresh}> |
| 161 | Refresh |
| 162 | </Button> |
sebdet | f9e2cee | 2019-08-09 18:36:09 +0200 | [diff] [blame] | 163 | <Button variant="primary" onClick={this.handleSave}> |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 164 | Save Changes |
xuegao | 2f5b2cd | 2020-01-06 16:13:46 +0100 | [diff] [blame] | 165 | </Button> |
sebdet | 4946e5b | 2019-07-10 12:32:36 +0200 | [diff] [blame] | 166 | </Modal.Footer> |
| 167 | </ModalStyled> |
| 168 | |
| 169 | ); |
| 170 | } |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 171 | } |