blob: 5930386c262f5262ed7d2fe4348490d3aede6fae [file] [log] [blame]
sebdetc11160e2020-02-25 15:13:31 -08001/*-
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
24import React from 'react'
25import Button from 'react-bootstrap/Button';
xuegao635445a2020-03-02 11:37:20 +010026import Form from 'react-bootstrap/Form';
27import Col from 'react-bootstrap/Col';
28import Row from 'react-bootstrap/Row';
29import Select from 'react-select';
sebdetc11160e2020-02-25 15:13:31 -080030import Modal from 'react-bootstrap/Modal';
31import styled from 'styled-components';
32import LoopService from '../../../api/LoopService';
xuegaoe18ed702020-03-17 10:46:39 +010033import LoopCache from '../../../api/LoopCache';
sebdetc11160e2020-02-25 15:13:31 -080034import JSONEditor from '@json-editor/json-editor';
xuegaoe18ed702020-03-17 10:46:39 +010035import Alert from 'react-bootstrap/Alert';
sebdetc11160e2020-02-25 15:13:31 -080036
37const ModalStyled = styled(Modal)`
38 background-color: transparent;
39`
40
41export 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)
xuegao635445a2020-03-02 11:37:20 +010049 policyInstanceType: this.props.match.params.policyInstanceType,
50 pdpGroup: null,
51 pdpGroupList: [],
52 pdpSubgroupList: [],
53 chosenPdpGroup: '',
xuegaoe18ed702020-03-17 10:46:39 +010054 chosenPdpSubgroup: '',
55 showSucAlert: false,
56 showFailAlert: false
sebdetc11160e2020-02-25 15:13:31 -080057 };
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);
xuegao635445a2020-03-02 11:37:20 +010064 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
65 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
sebdet2dd4e992020-03-04 15:47:39 -080066 this.createJsonEditor = this.createJsonEditor.bind(this);
xuegaoe18ed702020-03-17 10:46:39 +010067 this.handleRefresh = this.handleRefresh.bind(this);
68 this.disableAlert = this.disableAlert.bind(this);
sebdetc11160e2020-02-25 15:13:31 -080069 }
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);
sebdet6e7d0482020-04-16 16:14:45 +020077 this.setState({
78 showFailAlert: true,
79 showMessage: "Errors detected during policy data validation " + errors
80 });
sebdet2dd4e992020-03-04 15:47:39 -080081 return;
sebdetc11160e2020-02-25 15:13:31 -080082 }
83 else {
84 console.info("NO validation errors found in policy data");
sebdetd2a4df02020-02-26 15:47:30 -080085 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
sebdet2dd4e992020-03-04 15:47:39 -080086 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
xuegao635445a2020-03-02 11:37:20 +010087 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
sebdetc11160e2020-02-25 15:13:31 -080088 LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => {
89 this.setState({ show: false });
90 this.props.history.push('/');
91 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
92 });
sebdetd2a4df02020-02-26 15:47:30 -080093 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
sebdet2dd4e992020-03-04 15:47:39 -080094 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
xuegao635445a2020-03-02 11:37:20 +010095 this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
96 LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => {
97 this.setState({ show: false });
98 this.props.history.push('/');
99 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
100 });
sebdetc11160e2020-02-25 15:13:31 -0800101 }
102 }
103 }
104
105 handleClose() {
106 this.setState({ show: false });
107 this.props.history.push('/');
108 }
109
110 componentDidMount() {
111 this.renderJsonEditor();
112 }
113
sebdet2dd4e992020-03-04 15:47:39 -0800114 createJsonEditor(toscaModel, editorData) {
sebdet82775722020-03-16 07:07:18 -0700115 JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({
116 getTab: function(text,tabId) {
117 var liel = document.createElement('li');
118 liel.classList.add('nav-item');
119 var ael = document.createElement("a");
120 ael.classList.add("nav-link");
121 ael.setAttribute("style",'padding:10px;max-width:160px;');
122 ael.setAttribute("href", "#" + tabId);
123 ael.setAttribute('data-toggle', 'tab');
124 text.setAttribute("style",'word-wrap:break-word;');
125 ael.appendChild(text);
126 liel.appendChild(ael);
127 return liel;
128 }
129 });
sebdet2dd4e992020-03-04 15:47:39 -0800130 return new JSONEditor(document.getElementById("editor"),
131 { schema: toscaModel,
132 startval: editorData,
sebdet82775722020-03-16 07:07:18 -0700133 theme: 'myBootstrap4',
sebdet2dd4e992020-03-04 15:47:39 -0800134 object_layout: 'grid',
sebdet1b4f20d2020-03-17 07:47:03 -0700135 disable_properties: false,
sebdet2dd4e992020-03-04 15:47:39 -0800136 disable_edit_json: false,
137 disable_array_reorder: true,
138 disable_array_delete_last_row: true,
139 disable_array_delete_all_rows: false,
sebdet49ab84a2020-03-13 15:27:41 -0700140 array_controls_top: true,
141 keep_oneof_values: false,
142 collapsed:true,
sebdet2dd4e992020-03-04 15:47:39 -0800143 show_errors: 'always',
144 display_required_only: false,
sebdet82775722020-03-16 07:07:18 -0700145 show_opt_in: false,
sebdet2dd4e992020-03-04 15:47:39 -0800146 prompt_before_delete: true,
sebdet82775722020-03-16 07:07:18 -0700147 required_by_default: false
sebdet2dd4e992020-03-04 15:47:39 -0800148 })
149 }
150
sebdetc11160e2020-02-25 15:13:31 -0800151 renderJsonEditor() {
152 console.debug("Rendering PolicyModal ", this.state.policyName);
153 var toscaModel = {};
xuegao635445a2020-03-02 11:37:20 +0100154 var editorData = {};
155 var pdpGroupValues = {};
156 var chosenPdpGroupValue, chosenPdpSubgroupValue;
157 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
158 toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
159 editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName);
sebdet2dd4e992020-03-04 15:47:39 -0800160 pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
xuegao635445a2020-03-02 11:37:20 +0100161 chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName);
162 chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName);
163 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
164 toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
165 editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
sebdet2dd4e992020-03-04 15:47:39 -0800166 pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
xuegao635445a2020-03-02 11:37:20 +0100167 chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
168 chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
169 }
sebdetc11160e2020-02-25 15:13:31 -0800170
171 if (toscaModel == null) {
172 return;
173 }
174
sebdet2dd4e992020-03-04 15:47:39 -0800175 var pdpSubgroupValues = [];
176 if (typeof(chosenPdpGroupValue) !== "undefined") {
xuegao635445a2020-03-02 11:37:20 +0100177 var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
sebdet2dd4e992020-03-04 15:47:39 -0800178 pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
xuegao635445a2020-03-02 11:37:20 +0100179 }
sebdet2dd4e992020-03-04 15:47:39 -0800180 this.setState({
181 jsonEditor: this.createJsonEditor(toscaModel,editorData),
182 pdpGroup: pdpGroupValues,
183 pdpGroupList: pdpGroupValues.map(entry => {
184 return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] };
185 }),
186 pdpSubgroupList: pdpSubgroupValues,
187 chosenPdpGroup: chosenPdpGroupValue,
188 chosenPdpSubgroup: chosenPdpSubgroupValue
189 })
xuegao635445a2020-03-02 11:37:20 +0100190 }
191
192 handlePdpGroupChange(e) {
193 var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value));
194 const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
195 if (this.state.chosenPdpGroup !== e.value) {
sebdet2dd4e992020-03-04 15:47:39 -0800196 this.setState({
xuegao635445a2020-03-02 11:37:20 +0100197 chosenPdpGroup: e.value,
198 chosenPdpSubgroup: '',
199 pdpSubgroupList: pdpSubgroupValues
200 });
201 }
202 }
203
204 handlePdpSubgroupChange(e) {
205 this.setState({ chosenPdpSubgroup: e.value });
sebdetc11160e2020-02-25 15:13:31 -0800206 }
207
xuegaoe18ed702020-03-17 10:46:39 +0100208 handleRefresh() {
209 var newLoopCache, toscaModel, editorData;
210 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
211 LoopService.refreshMicroServicePolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
212 newLoopCache = new LoopCache(data);
213 toscaModel = newLoopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
214 editorData = newLoopCache.getMicroServicePropertiesForName(this.state.policyName);
215 document.getElementById("editor").innerHTML = "";
216 this.setState({
217 loopCache: newLoopCache,
218 jsonEditor: this.createJsonEditor(toscaModel,editorData),
219 showSucAlert: true,
220 showMessage: "Successfully refreshed"
221 });
222 })
223 .catch(error => {
224 console.error("Error while refreshing the Operational Policy Json Representation");
225 this.setState({
226 showFailAlert: true,
227 showMessage: "Refreshing of UI failed"
228 });
229 });
230 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
231 LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
232 var newLoopCache = new LoopCache(data);
233 toscaModel = newLoopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
234 editorData = newLoopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
235 document.getElementById("editor").innerHTML = "";
236 this.setState({
237 loopCache: newLoopCache,
238 jsonEditor: this.createJsonEditor(toscaModel,editorData),
239 showSucAlert: true,
240 showMessage: "Successfully refreshed"
241 });
242 })
243 .catch(error => {
244 console.error("Error while refreshing the Operational Policy Json Representation");
245 this.setState({
246 showFailAlert: true,
247 showMessage: "Refreshing of UI failed"
248 });
249 });
250 }
251 }
252
253 disableAlert() {
254 this.setState ({ showSucAlert: false, showFailAlert: false });
255 }
256
sebdetc11160e2020-02-25 15:13:31 -0800257 render() {
258 return (
sebdetedaf4f92020-04-16 00:43:48 +0200259 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose } backdrop="static">
sebdetc11160e2020-02-25 15:13:31 -0800260 <Modal.Header closeButton>
sebdet2dd4e992020-03-04 15:47:39 -0800261 <Modal.Title>Edit the policy</Modal.Title>
sebdetc11160e2020-02-25 15:13:31 -0800262 </Modal.Header>
xuegaoe18ed702020-03-17 10:46:39 +0100263 <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible>
264 {this.state.showMessage}
265 </Alert>
266 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
267 {this.state.showMessage}
268 </Alert>
sebdetc11160e2020-02-25 15:13:31 -0800269 <Modal.Body>
270 <div id="editor" />
xuegao635445a2020-03-02 11:37:20 +0100271 <Form.Group as={Row} controlId="formPlaintextEmail">
272 <Form.Label column sm="2">Pdp Group Info</Form.Label>
273 <Col sm="3">
274 <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
275 </Col>
276 <Col sm="3">
277 <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
278 </Col>
279 </Form.Group>
sebdetc11160e2020-02-25 15:13:31 -0800280 </Modal.Body>
281 <Modal.Footer>
282 <Button variant="secondary" onClick={this.handleClose}>
283 Close
xuegao635445a2020-03-02 11:37:20 +0100284 </Button>
sebdetc11160e2020-02-25 15:13:31 -0800285 <Button variant="primary" onClick={this.handleSave}>
286 Save Changes
xuegao635445a2020-03-02 11:37:20 +0100287 </Button>
xuegaoe18ed702020-03-17 10:46:39 +0100288 <Button variant="primary" onClick={this.handleRefresh}>
289 Refresh
290 </Button>
sebdetc11160e2020-02-25 15:13:31 -0800291 </Modal.Footer>
292 </ModalStyled>
293
294 );
295 }
296}