blob: 89e70795e30f0878886f80ae7e50a05790e61622 [file] [log] [blame]
sebdet4946e5b2019-07-10 12:32:36 +02001/*-
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
24import React from 'react'
25import Button from 'react-bootstrap/Button';
26import Modal from 'react-bootstrap/Modal';
sebdet4946e5b2019-07-10 12:32:36 +020027import styled from 'styled-components';
xuegao2f5b2cd2020-01-06 16:13:46 +010028import LoopCache from '../../../api/LoopCache';
sebdetf9e2cee2019-08-09 18:36:09 +020029import LoopService from '../../../api/LoopService';
30import JSONEditor from '@json-editor/json-editor';
sebdet4946e5b2019-07-10 12:32:36 +020031
32const ModalStyled = styled(Modal)`
33 background-color: transparent;
34`
35
36export default class OperationalPolicyModal extends React.Component {
37
sebdet493c3832019-07-15 17:26:18 +020038 state = {
39 show: true,
40 loopCache: this.props.loopCache,
sebdet337f3662019-09-06 18:11:51 +020041 jsonEditor: null
sebdet493c3832019-07-15 17:26:18 +020042 };
43
sebdet4946e5b2019-07-10 12:32:36 +020044 constructor(props, context) {
45 super(props, context);
sebdet4946e5b2019-07-10 12:32:36 +020046 this.handleClose = this.handleClose.bind(this);
sebdetf9e2cee2019-08-09 18:36:09 +020047 this.handleSave = this.handleSave.bind(this);
48 this.renderJsonEditor = this.renderJsonEditor.bind(this);
xuegao2f5b2cd2020-01-06 16:13:46 +010049 this.handleRefresh = this.handleRefresh.bind(this);
sebdetf9e2cee2019-08-09 18:36:09 +020050 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);
xuegao0efeb6b2019-10-07 14:36:34 +020059 this.props.showAlert(errors);
sebdetf9e2cee2019-08-09 18:36:09 +020060 }
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 }
sebdet4946e5b2019-07-10 12:32:36 +020070 }
71
72 handleClose() {
73 this.setState({ show: false });
sebdetf9e2cee2019-08-09 18:36:09 +020074 this.props.history.push('/');
sebdet4946e5b2019-07-10 12:32:36 +020075 }
76
sebdetf9e2cee2019-08-09 18:36:09 +020077 componentDidMount() {
78 this.renderJsonEditor();
79 }
80
81 setDefaultJsonEditorOptions() {
xuegao83fe2b02019-09-25 11:23:18 +020082 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 });
sebdetf9e2cee2019-08-09 18:36:09 +020097 }
sebdet2dd4e992020-03-04 15:47:39 -080098
sebdetf9e2cee2019-08-09 18:36:09 +020099 renderJsonEditor() {
100 console.debug("Rendering OperationalPolicyModal");
101 var schema_json = this.state.loopCache.getOperationalPolicyJsonSchema();
102
103 if (schema_json == null) {
104 console.error("NO Operational policy schema found");
105 return;
sebdet4946e5b2019-07-10 12:32:36 +0200106 }
xuegaoc488fed2019-12-19 11:58:45 +0100107 var operationalPoliciesData = this.state.loopCache.getOperationalPoliciesNoJsonSchema();
sebdet4946e5b2019-07-10 12:32:36 +0200108
sebdetf9e2cee2019-08-09 18:36:09 +0200109 this.setState({
110 jsonEditor: new JSONEditor(document.getElementById("editor"),
sebdet2dd4e992020-03-04 15:47:39 -0800111 {
112 schema: schema_json.schema,
113 startval: operationalPoliciesData,
114 theme: 'myBootstrap4',
115 object_layout: 'grid',
116 disable_properties: true,
117 disable_edit_json: false,
118 disable_array_reorder: true,
119 disable_array_delete_last_row: true,
120 disable_array_delete_all_rows: false,
121 array_controls_top: true,
122 show_errors: 'always',
123 keep_oneof_values: false,
124 collapsed:true
125 })
sebdetf9e2cee2019-08-09 18:36:09 +0200126 })
sebdet493c3832019-07-15 17:26:18 +0200127 }
sebdet4946e5b2019-07-10 12:32:36 +0200128
xuegao2f5b2cd2020-01-06 16:13:46 +0100129 handleRefresh() {
sebdete916ac22020-03-16 11:04:34 -0700130 LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()[0]).then(data => {
xuegao2f5b2cd2020-01-06 16:13:46 +0100131 var newLoopCache = new LoopCache(data);
132 var schema_json = newLoopCache.getOperationalPolicyJsonSchema();
133 var operationalPoliciesData = newLoopCache.getOperationalPoliciesNoJsonSchema();
134 document.getElementById("editor").innerHTML = "";
135 this.setState({
136 loopCache: newLoopCache,
137 jsonEditor: new JSONEditor(document.getElementById("editor"),
138 { schema: schema_json.schema, startval: operationalPoliciesData })
139 })
140 this.props.updateLoopFunction(data);
141
142 })
143 .catch(error => {
144 console.error("Error while refreshing the Operational Policy Json Representation");
145 });
146 }
147
sebdet4946e5b2019-07-10 12:32:36 +0200148 render() {
149 return (
sebdetf9e2cee2019-08-09 18:36:09 +0200150 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
sebdet4946e5b2019-07-10 12:32:36 +0200151 <Modal.Header closeButton>
152 <Modal.Title>Operational policies</Modal.Title>
153 </Modal.Header>
154 <Modal.Body>
sebdetf9e2cee2019-08-09 18:36:09 +0200155 <div id="editor" />
xuegaof248df62019-07-15 15:16:18 +0200156
sebdet4946e5b2019-07-10 12:32:36 +0200157 </Modal.Body>
158 <Modal.Footer>
159 <Button variant="secondary" onClick={this.handleClose}>
160 Close
xuegao2f5b2cd2020-01-06 16:13:46 +0100161 </Button>
162 <Button variant="secondary" onClick={this.handleRefresh}>
163 Refresh
164 </Button>
sebdetf9e2cee2019-08-09 18:36:09 +0200165 <Button variant="primary" onClick={this.handleSave}>
sebdet4946e5b2019-07-10 12:32:36 +0200166 Save Changes
xuegao2f5b2cd2020-01-06 16:13:46 +0100167 </Button>
sebdet4946e5b2019-07-10 12:32:36 +0200168 </Modal.Footer>
169 </ModalStyled>
170
171 );
172 }
sebdet493c3832019-07-15 17:26:18 +0200173}