blob: 2fa0b5432c39c6c2781da633a4c23c13c2db7d83 [file] [log] [blame]
sebdetc11160e2020-02-25 15:13:31 -08001/*-
2 * ============LICENSE_START=======================================================
sebdetb8831e52021-03-12 19:30:22 +01003 * ONAP POLICY-CLAMP
sebdetc11160e2020-02-25 15:13:31 -08004 * ================================================================================
sebdetb8831e52021-03-12 19:30:22 +01005 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights
sebdetc11160e2020-02-25 15:13:31 -08006 * 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';
sebdetb8831e52021-03-12 19:30:22 +010034import { JSONEditor } from '@json-editor/json-editor/dist/jsoneditor.js';
35import "@fortawesome/fontawesome-free/css/all.css"
xuegaoe18ed702020-03-17 10:46:39 +010036import Alert from 'react-bootstrap/Alert';
sebdetc0ec0fc2020-05-18 12:31:11 +020037import OnapConstant from '../../../utils/OnapConstants';
Ted Humphrey9dfd03b2020-07-07 03:37:37 -040038import OnapUtils from '../../../utils/OnapUtils';
sebdetc11160e2020-02-25 15:13:31 -080039
40const ModalStyled = styled(Modal)`
sebdetb8831e52021-03-12 19:30:22 +010041 background-color: transparent;
sebdetc11160e2020-02-25 15:13:31 -080042`
43
Ted Humphrey9dfd03b2020-07-07 03:37:37 -040044const DivWhiteSpaceStyled = styled.div`
sebdetb8831e52021-03-12 19:30:22 +010045 white-space: pre;
Ted Humphrey9dfd03b2020-07-07 03:37:37 -040046`
47
sebdetc11160e2020-02-25 15:13:31 -080048export default class PolicyModal extends React.Component {
49
sebdetb8831e52021-03-12 19:30:22 +010050 state = {
51 show: true,
52 loopCache: this.props.loopCache,
53 jsonEditor: null,
54 policyName: this.props.match.params.policyName,
55 // This is to indicate whether it's an operational or config policy (in terms of loop instance)
56 policyInstanceType: this.props.match.params.policyInstanceType,
57 pdpGroup: null,
58 pdpGroupList: [],
59 pdpSubgroupList: [],
60 chosenPdpGroup: '',
61 chosenPdpSubgroup: '',
62 showSucAlert: false,
63 showFailAlert: false
64 };
sebdetc11160e2020-02-25 15:13:31 -080065
sebdetb8831e52021-03-12 19:30:22 +010066 constructor(props, context) {
67 super(props, context);
68 this.handleClose = this.handleClose.bind(this);
69 this.handleSave = this.handleSave.bind(this);
70 this.renderJsonEditor = this.renderJsonEditor.bind(this);
71 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
72 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
73 this.createJsonEditor = this.createJsonEditor.bind(this);
74 this.handleRefresh = this.handleRefresh.bind(this);
75 this.disableAlert = this.disableAlert.bind(this);
76 this.renderPdpGroupDropDown = this.renderPdpGroupDropDown.bind(this);
77 this.renderOpenLoopMessage = this.renderOpenLoopMessage.bind(this);
78 this.renderModalTitle = this.renderModalTitle.bind(this);
79 this.readOnly = props.readOnly !== undefined ? props.readOnly : false;
80 }
sebdetc11160e2020-02-25 15:13:31 -080081
sebdetb8831e52021-03-12 19:30:22 +010082 handleSave() {
83 var editorData = this.state.jsonEditor.getValue();
84 var errors = this.state.jsonEditor.validate();
85 errors = errors.concat(this.customValidation(editorData, this.state.loopCache.getTemplateName()));
sebdetc11160e2020-02-25 15:13:31 -080086
sebdetb8831e52021-03-12 19:30:22 +010087 if (errors.length !== 0) {
88 console.error("Errors detected during policy data validation ", errors);
89 this.setState({
90 showFailAlert: true,
91 showMessage: 'Errors detected during policy data validation:\n' + OnapUtils.jsonEditorErrorFormatter(errors)
92 });
93 return;
94 }
95 else {
96 console.info("NO validation errors found in policy data");
97 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
98 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
99 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
100 LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => {
101 this.setState({ show: false });
102 this.props.history.push('/');
103 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
104 });
105 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
106 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
107 this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
108 LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => {
109 this.setState({ show: false });
110 this.props.history.push('/');
111 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
112 });
113 }
114 }
115 }
sebdetc11160e2020-02-25 15:13:31 -0800116
sebdetb8831e52021-03-12 19:30:22 +0100117 customValidation(editorData, templateName) {
118 // method for sub-classes to override with customized validation
119 return [];
120 }
Ted Humphrey9dfd03b2020-07-07 03:37:37 -0400121
sebdetb8831e52021-03-12 19:30:22 +0100122 handleClose() {
123 this.setState({ show: false });
124 this.props.history.push('/');
125 }
sebdetc11160e2020-02-25 15:13:31 -0800126
sebdetb8831e52021-03-12 19:30:22 +0100127 componentDidMount() {
128 this.renderJsonEditor();
129 }
sebdetc11160e2020-02-25 15:13:31 -0800130
sebdetb8831e52021-03-12 19:30:22 +0100131 componentDidUpdate() {
132 if (this.state.showSucAlert === true || this.state.showFailAlert === true) {
133 let modalElement = document.getElementById("policyModal")
134 if (modalElement) {
135 modalElement.scrollTo(0, 0);
136 }
137 }
138 }
Ted Humphrey9dfd03b2020-07-07 03:37:37 -0400139
sebdet2dd4e992020-03-04 15:47:39 -0800140 createJsonEditor(toscaModel, editorData) {
sebdetb8831e52021-03-12 19:30:22 +0100141 /*JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({
142 getTab: function(text,tabId) {
143 var liel = document.createElement('li');
144 liel.classList.add('nav-item');
145 var ael = document.createElement("a");
146 ael.classList.add("nav-link");
147 ael.setAttribute("style",'padding:10px;max-width:160px;');
148 ael.setAttribute("href", "#" + tabId);
149 ael.setAttribute('data-toggle', 'tab');
150 text.setAttribute("style",'word-wrap:break-word;');
151 ael.appendChild(text);
152 liel.appendChild(ael);
153 return liel;
154 }
155 });*/
sebdet2dd4e992020-03-04 15:47:39 -0800156 return new JSONEditor(document.getElementById("editor"),
157 { schema: toscaModel,
158 startval: editorData,
sebdetb8831e52021-03-12 19:30:22 +0100159 theme: 'bootstrap4',
160 iconlib: 'fontawesome5',
sebdet2dd4e992020-03-04 15:47:39 -0800161 object_layout: 'grid',
sebdet1b4f20d2020-03-17 07:47:03 -0700162 disable_properties: false,
sebdet2dd4e992020-03-04 15:47:39 -0800163 disable_edit_json: false,
164 disable_array_reorder: true,
165 disable_array_delete_last_row: true,
166 disable_array_delete_all_rows: false,
sebdet49ab84a2020-03-13 15:27:41 -0700167 array_controls_top: true,
168 keep_oneof_values: false,
169 collapsed:true,
sebdet2dd4e992020-03-04 15:47:39 -0800170 show_errors: 'always',
171 display_required_only: false,
sebdet82775722020-03-16 07:07:18 -0700172 show_opt_in: false,
sebdet2dd4e992020-03-04 15:47:39 -0800173 prompt_before_delete: true,
sebdet82775722020-03-16 07:07:18 -0700174 required_by_default: false
sebdet2dd4e992020-03-04 15:47:39 -0800175 })
176 }
177
sebdetb8831e52021-03-12 19:30:22 +0100178 renderJsonEditor() {
179 console.debug("Rendering PolicyModal ", this.state.policyName);
180 var toscaModel = {};
181 var editorData = {};
182 var pdpGroupValues = {};
183 var chosenPdpGroupValue, chosenPdpSubgroupValue;
184 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
185 toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
186 editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName);
187 pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
188 chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName);
189 chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName);
190 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
191 toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
192 editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
193 pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
194 chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
195 chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
196 }
sebdetc11160e2020-02-25 15:13:31 -0800197
sebdetb8831e52021-03-12 19:30:22 +0100198 if (toscaModel == null) {
199 return;
200 }
sebdetc11160e2020-02-25 15:13:31 -0800201
sebdet2dd4e992020-03-04 15:47:39 -0800202 var pdpSubgroupValues = [];
sebdetb8831e52021-03-12 19:30:22 +0100203 if (typeof(chosenPdpGroupValue) !== "undefined") {
204 var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
205 pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
206 }
207 this.setState({
208 jsonEditor: this.createJsonEditor(toscaModel,editorData),
209 pdpGroup: pdpGroupValues,
210 pdpGroupList: pdpGroupValues.map(entry => {
211 return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] };
212 }),
213 pdpSubgroupList: pdpSubgroupValues,
214 chosenPdpGroup: chosenPdpGroupValue,
215 chosenPdpSubgroup: chosenPdpSubgroupValue
216 })
sebdetc0ec0fc2020-05-18 12:31:11 +0200217 }
218
sebdetb8831e52021-03-12 19:30:22 +0100219 handlePdpGroupChange(e) {
220 var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value));
221 const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
222 if (this.state.chosenPdpGroup !== e.value) {
223 this.setState({
224 chosenPdpGroup: e.value,
225 chosenPdpSubgroup: '',
226 pdpSubgroupList: pdpSubgroupValues
227 });
228 }
229 }
sebdetc0ec0fc2020-05-18 12:31:11 +0200230
sebdetb8831e52021-03-12 19:30:22 +0100231 handlePdpSubgroupChange(e) {
232 this.setState({ chosenPdpSubgroup: e.value });
233 }
234
235 handleRefresh() {
236 var newLoopCache, toscaModel, editorData;
237 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
238 LoopService.refreshMicroServicePolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
239 newLoopCache = new LoopCache(data);
240 toscaModel = newLoopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
241 editorData = newLoopCache.getMicroServicePropertiesForName(this.state.policyName);
242 document.getElementById("editor").innerHTML = "";
243 this.setState({
244 loopCache: newLoopCache,
245 jsonEditor: this.createJsonEditor(toscaModel,editorData),
246 showSucAlert: true,
247 showMessage: "Successfully refreshed"
248 });
249 })
250 .catch(error => {
251 console.error("Error while refreshing the Operational Policy Json Representation");
252 this.setState({
253 showFailAlert: true,
254 showMessage: "Refreshing of UI failed"
255 });
256 });
257 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
258 LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
259 var newLoopCache = new LoopCache(data);
260 toscaModel = newLoopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
261 editorData = newLoopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
262 document.getElementById("editor").innerHTML = "";
263 this.setState({
264 loopCache: newLoopCache,
265 jsonEditor: this.createJsonEditor(toscaModel,editorData),
266 showSucAlert: true,
267 showMessage: "Successfully refreshed"
268 });
269 })
270 .catch(error => {
271 console.error("Error while refreshing the Operational Policy Json Representation");
272 this.setState({
273 showFailAlert: true,
274 showMessage: "Refreshing of UI failed"
275 });
276 });
277 }
278 }
279
280 disableAlert() {
281 this.setState ({ showSucAlert: false, showFailAlert: false });
282 }
283
284 renderPdpGroupDropDown() {
285 if(this.state.policyInstanceType !== OnapConstant.operationalPolicyType || !this.state.loopCache.isOpenLoopTemplate()) {
286 return (
287 <Form.Group as={Row} controlId="formPlaintextEmail">
288 <Form.Label column sm="2">Pdp Group Info</Form.Label>
289 <Col sm="3">
290 <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
291 </Col>
292 <Col sm="3">
293 <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
294 </Col>
295 </Form.Group>
296 );
297 }
298 }
299
300 renderOpenLoopMessage() {
301 if(this.state.policyInstanceType === OnapConstant.operationalPolicyType && this.state.loopCache.isOpenLoopTemplate()) {
302 return (
303 "Operational Policy cannot be configured as only Open Loop is supported for this Template!"
304 );
305 }
306 }
307
308 renderModalTitle() {
309 return (
310 <Modal.Title>Edit the policy</Modal.Title>
311 );
312 }
313
314 renderButton() {
315 var allElement = [(<Button variant="secondary" onClick={this.handleClose}>
316 Close
sebdetc0ec0fc2020-05-18 12:31:11 +0200317 </Button>)];
sebdetb8831e52021-03-12 19:30:22 +0100318 if(this.state.policyInstanceType !== OnapConstant.operationalPolicyType || !this.state.loopCache.isOpenLoopTemplate()) {
sebdetc0ec0fc2020-05-18 12:31:11 +0200319 allElement.push((
320 <Button variant="primary" disabled={this.readOnly} onClick={this.handleSave}>
321 Save Changes
322 </Button>
323 ));
324 allElement.push((
325 <Button variant="primary" disabled={this.readOnly} onClick={this.handleRefresh}>
326 Refresh
327 </Button>
328 ));
sebdetb8831e52021-03-12 19:30:22 +0100329 }
330 return allElement;
331 }
sebdetc0ec0fc2020-05-18 12:31:11 +0200332
sebdetb8831e52021-03-12 19:30:22 +0100333 render() {
334 return (
335 <ModalStyled size="xl" backdrop="static" keyboard={false} show={this.state.show} onHide={this.handleClose}>
336 <Modal.Header closeButton>
337 {this.renderModalTitle()}
338 </Modal.Header>
Ted Humphrey9dfd03b2020-07-07 03:37:37 -0400339 <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible>
340 <DivWhiteSpaceStyled>
341 {this.state.showMessage}
342 </DivWhiteSpaceStyled>
343 </Alert>
344 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
345 <DivWhiteSpaceStyled>
346 {this.state.showMessage}
347 </DivWhiteSpaceStyled>
348 </Alert>
sebdetb8831e52021-03-12 19:30:22 +0100349 <Modal.Body>
350 {this.renderOpenLoopMessage()}
351 <div id="editor" />
352 {this.renderPdpGroupDropDown()}
353 </Modal.Body>
354 <Modal.Footer>
355 {this.renderButton()}
356 </Modal.Footer>
357 </ModalStyled>
358 );
359 }
Ted Humphrey9dfd03b2020-07-07 03:37:37 -0400360}