2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 import React from 'react'
25 import Button from 'react-bootstrap/Button';
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';
30 import Modal from 'react-bootstrap/Modal';
31 import styled from 'styled-components';
32 import LoopService from '../../../api/LoopService';
33 import LoopCache from '../../../api/LoopCache';
34 import JSONEditor from '@json-editor/json-editor';
35 import Alert from 'react-bootstrap/Alert';
36 import OnapConstant from '../../../utils/OnapConstants';
37 import OnapUtils from '../../../utils/OnapUtils';
39 const ModalStyled = styled(Modal)`
40 background-color: transparent;
43 const DivWhiteSpaceStyled = styled.div`
47 export default class PolicyModal extends React.Component {
51 loopCache: this.props.loopCache,
53 policyName: this.props.match.params.policyName,
54 // This is to indicate whether it's an operational or config policy (in terms of loop instance)
55 policyInstanceType: this.props.match.params.policyInstanceType,
60 chosenPdpSubgroup: '',
65 constructor(props, context) {
66 super(props, context);
67 this.handleClose = this.handleClose.bind(this);
68 this.handleSave = this.handleSave.bind(this);
69 this.renderJsonEditor = this.renderJsonEditor.bind(this);
70 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
71 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
72 this.createJsonEditor = this.createJsonEditor.bind(this);
73 this.handleRefresh = this.handleRefresh.bind(this);
74 this.disableAlert = this.disableAlert.bind(this);
75 this.renderPdpGroupDropDown = this.renderPdpGroupDropDown.bind(this);
76 this.renderOpenLoopMessage = this.renderOpenLoopMessage.bind(this);
77 this.renderModalTitle = this.renderModalTitle.bind(this);
78 this.readOnly = props.readOnly !== undefined ? props.readOnly : false;
82 var editorData = this.state.jsonEditor.getValue();
83 var errors = this.state.jsonEditor.validate();
84 errors = errors.concat(this.customValidation(editorData, this.state.loopCache.getTemplateName()));
86 if (errors.length !== 0) {
87 console.error("Errors detected during policy data validation ", errors);
90 showMessage: 'Errors detected during policy data validation:\n' + OnapUtils.jsonEditorErrorFormatter(errors)
95 console.info("NO validation errors found in policy data");
96 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
97 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
98 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
99 LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => {
100 this.setState({ show: false });
101 this.props.history.push('/');
102 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
104 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
105 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
106 this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
107 LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => {
108 this.setState({ show: false });
109 this.props.history.push('/');
110 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
116 customValidation(editorData, templateName) {
117 // method for sub-classes to override with customized validation
122 this.setState({ show: false });
123 this.props.history.push('/');
126 componentDidMount() {
127 this.renderJsonEditor();
130 componentDidUpdate() {
131 if (this.state.showSucAlert === true || this.state.showFailAlert === true) {
132 let modalElement = document.getElementById("policyModal")
134 modalElement.scrollTo(0, 0);
139 createJsonEditor(toscaModel, editorData) {
140 JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({
141 getTab: function(text,tabId) {
142 var liel = document.createElement('li');
143 liel.classList.add('nav-item');
144 var ael = document.createElement("a");
145 ael.classList.add("nav-link");
146 ael.setAttribute("style",'padding:10px;max-width:160px;');
147 ael.setAttribute("href", "#" + tabId);
148 ael.setAttribute('data-toggle', 'tab');
149 text.setAttribute("style",'word-wrap:break-word;');
150 ael.appendChild(text);
151 liel.appendChild(ael);
155 return new JSONEditor(document.getElementById("editor"),
156 { schema: toscaModel,
157 startval: editorData,
158 theme: 'myBootstrap4',
159 object_layout: 'grid',
160 disable_properties: false,
161 disable_edit_json: false,
162 disable_array_reorder: true,
163 disable_array_delete_last_row: true,
164 disable_array_delete_all_rows: false,
165 array_controls_top: true,
166 keep_oneof_values: false,
168 show_errors: 'always',
169 display_required_only: false,
171 prompt_before_delete: true,
172 required_by_default: false
177 console.debug("Rendering PolicyModal ", this.state.policyName);
180 var pdpGroupValues = {};
181 var chosenPdpGroupValue, chosenPdpSubgroupValue;
182 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
183 toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
184 editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName);
185 pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
186 chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName);
187 chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName);
188 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
189 toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
190 editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
191 pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
192 chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
193 chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
196 if (toscaModel == null) {
200 var pdpSubgroupValues = [];
201 if (typeof(chosenPdpGroupValue) !== "undefined") {
202 var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
203 pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
206 jsonEditor: this.createJsonEditor(toscaModel,editorData),
207 pdpGroup: pdpGroupValues,
208 pdpGroupList: pdpGroupValues.map(entry => {
209 return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] };
211 pdpSubgroupList: pdpSubgroupValues,
212 chosenPdpGroup: chosenPdpGroupValue,
213 chosenPdpSubgroup: chosenPdpSubgroupValue
217 handlePdpGroupChange(e) {
218 var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value));
219 const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
220 if (this.state.chosenPdpGroup !== e.value) {
222 chosenPdpGroup: e.value,
223 chosenPdpSubgroup: '',
224 pdpSubgroupList: pdpSubgroupValues
229 handlePdpSubgroupChange(e) {
230 this.setState({ chosenPdpSubgroup: e.value });
234 var newLoopCache, toscaModel, editorData;
235 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
236 LoopService.refreshMicroServicePolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
237 newLoopCache = new LoopCache(data);
238 toscaModel = newLoopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
239 editorData = newLoopCache.getMicroServicePropertiesForName(this.state.policyName);
240 document.getElementById("editor").innerHTML = "";
242 loopCache: newLoopCache,
243 jsonEditor: this.createJsonEditor(toscaModel,editorData),
245 showMessage: "Successfully refreshed"
249 console.error("Error while refreshing the Operational Policy Json Representation");
252 showMessage: "Refreshing of UI failed"
255 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
256 LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
257 var newLoopCache = new LoopCache(data);
258 toscaModel = newLoopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
259 editorData = newLoopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
260 document.getElementById("editor").innerHTML = "";
262 loopCache: newLoopCache,
263 jsonEditor: this.createJsonEditor(toscaModel,editorData),
265 showMessage: "Successfully refreshed"
269 console.error("Error while refreshing the Operational Policy Json Representation");
272 showMessage: "Refreshing of UI failed"
279 this.setState ({ showSucAlert: false, showFailAlert: false });
282 renderPdpGroupDropDown() {
283 if(this.state.policyInstanceType !== OnapConstant.operationalPolicyType || !this.state.loopCache.isOpenLoopTemplate()) {
285 <Form.Group as={Row} controlId="formPlaintextEmail">
286 <Form.Label column sm="2">Pdp Group Info</Form.Label>
288 <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
291 <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
298 renderOpenLoopMessage() {
299 if(this.state.policyInstanceType === OnapConstant.operationalPolicyType && this.state.loopCache.isOpenLoopTemplate()) {
301 "Operational Policy cannot be configured as only Open Loop is supported for this Template!"
308 <Modal.Title>Edit the policy</Modal.Title>
313 var allElement = [(<Button variant="secondary" onClick={this.handleClose}>
316 if(this.state.policyInstanceType !== OnapConstant.operationalPolicyType || !this.state.loopCache.isOpenLoopTemplate()) {
318 <Button variant="primary" disabled={this.readOnly} onClick={this.handleSave}>
323 <Button variant="primary" disabled={this.readOnly} onClick={this.handleRefresh}>
333 <ModalStyled size="xl" backdrop="static" keyboard={false} show={this.state.show} onHide={this.handleClose}>
334 <Modal.Header closeButton>
335 {this.renderModalTitle()}
337 <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible>
338 <DivWhiteSpaceStyled>
339 {this.state.showMessage}
340 </DivWhiteSpaceStyled>
342 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
343 <DivWhiteSpaceStyled>
344 {this.state.showMessage}
345 </DivWhiteSpaceStyled>
348 {this.renderOpenLoopMessage()}
350 {this.renderPdpGroupDropDown()}
353 {this.renderButton()}