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';
38 const ModalStyled = styled(Modal)`
39 background-color: transparent;
42 export default class PolicyModal extends React.Component {
46 loopCache: this.props.loopCache,
48 policyName: this.props.match.params.policyName,
49 // This is to indicate whether it's an operational or config policy (in terms of loop instance)
50 policyInstanceType: this.props.match.params.policyInstanceType,
55 chosenPdpSubgroup: '',
60 constructor(props, context) {
61 super(props, context);
62 this.handleClose = this.handleClose.bind(this);
63 this.handleSave = this.handleSave.bind(this);
64 this.renderJsonEditor = this.renderJsonEditor.bind(this);
65 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
66 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
67 this.createJsonEditor = this.createJsonEditor.bind(this);
68 this.handleRefresh = this.handleRefresh.bind(this);
69 this.disableAlert = this.disableAlert.bind(this);
70 this.renderPdpGroupDropDown = this.renderPdpGroupDropDown.bind(this);
71 this.renderOpenLoopMessage = this.renderOpenLoopMessage.bind(this);
72 this.renderModalTitle = this.renderModalTitle.bind(this);
76 var errors = this.state.jsonEditor.validate();
77 var editorData = this.state.jsonEditor.getValue();
79 if (errors.length !== 0) {
80 console.error("Errors detected during policy data validation ", errors);
83 showMessage: "Errors detected during policy data validation " + errors
88 console.info("NO validation errors found in policy data");
89 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
90 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
91 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
92 LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => {
93 this.setState({ show: false });
94 this.props.history.push('/');
95 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
97 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
98 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
99 this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
100 LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => {
101 this.setState({ show: false });
102 this.props.history.push('/');
103 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
110 this.setState({ show: false });
111 this.props.history.push('/');
114 componentDidMount() {
115 this.renderJsonEditor();
118 createJsonEditor(toscaModel, editorData) {
119 JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({
120 getTab: function(text,tabId) {
121 var liel = document.createElement('li');
122 liel.classList.add('nav-item');
123 var ael = document.createElement("a");
124 ael.classList.add("nav-link");
125 ael.setAttribute("style",'padding:10px;max-width:160px;');
126 ael.setAttribute("href", "#" + tabId);
127 ael.setAttribute('data-toggle', 'tab');
128 text.setAttribute("style",'word-wrap:break-word;');
129 ael.appendChild(text);
130 liel.appendChild(ael);
134 return new JSONEditor(document.getElementById("editor"),
135 { schema: toscaModel,
136 startval: editorData,
137 theme: 'myBootstrap4',
138 object_layout: 'grid',
139 disable_properties: false,
140 disable_edit_json: false,
141 disable_array_reorder: true,
142 disable_array_delete_last_row: true,
143 disable_array_delete_all_rows: false,
144 array_controls_top: true,
145 keep_oneof_values: false,
147 show_errors: 'always',
148 display_required_only: false,
150 prompt_before_delete: true,
151 required_by_default: false
156 console.debug("Rendering PolicyModal ", this.state.policyName);
159 var pdpGroupValues = {};
160 var chosenPdpGroupValue, chosenPdpSubgroupValue;
161 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
162 toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
163 editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName);
164 pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
165 chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName);
166 chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName);
167 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
168 toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
169 editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
170 pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
171 chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
172 chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
175 if (toscaModel == null) {
179 var pdpSubgroupValues = [];
180 if (typeof(chosenPdpGroupValue) !== "undefined") {
181 var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
182 pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
185 jsonEditor: this.createJsonEditor(toscaModel,editorData),
186 pdpGroup: pdpGroupValues,
187 pdpGroupList: pdpGroupValues.map(entry => {
188 return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] };
190 pdpSubgroupList: pdpSubgroupValues,
191 chosenPdpGroup: chosenPdpGroupValue,
192 chosenPdpSubgroup: chosenPdpSubgroupValue
196 handlePdpGroupChange(e) {
197 var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value));
198 const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
199 if (this.state.chosenPdpGroup !== e.value) {
201 chosenPdpGroup: e.value,
202 chosenPdpSubgroup: '',
203 pdpSubgroupList: pdpSubgroupValues
208 handlePdpSubgroupChange(e) {
209 this.setState({ chosenPdpSubgroup: e.value });
213 var newLoopCache, toscaModel, editorData;
214 if (this.state.policyInstanceType === OnapConstant.microServiceType) {
215 LoopService.refreshMicroServicePolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
216 newLoopCache = new LoopCache(data);
217 toscaModel = newLoopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
218 editorData = newLoopCache.getMicroServicePropertiesForName(this.state.policyName);
219 document.getElementById("editor").innerHTML = "";
221 loopCache: newLoopCache,
222 jsonEditor: this.createJsonEditor(toscaModel,editorData),
224 showMessage: "Successfully refreshed"
228 console.error("Error while refreshing the Operational Policy Json Representation");
231 showMessage: "Refreshing of UI failed"
234 } else if (this.state.policyInstanceType === OnapConstant.operationalPolicyType) {
235 LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(),this.state.policyName).then(data => {
236 var newLoopCache = new LoopCache(data);
237 toscaModel = newLoopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
238 editorData = newLoopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
239 document.getElementById("editor").innerHTML = "";
241 loopCache: newLoopCache,
242 jsonEditor: this.createJsonEditor(toscaModel,editorData),
244 showMessage: "Successfully refreshed"
248 console.error("Error while refreshing the Operational Policy Json Representation");
251 showMessage: "Refreshing of UI failed"
258 this.setState ({ showSucAlert: false, showFailAlert: false });
261 renderPdpGroupDropDown() {
262 if(this.state.policyInstanceType !== OnapConstant.operationalPolicyType || !this.state.loopCache.isOpenLoopTemplate()) {
264 <Form.Group as={Row} controlId="formPlaintextEmail">
265 <Form.Label column sm="2">Pdp Group Info</Form.Label>
267 <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
270 <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
277 renderOpenLoopMessage() {
278 if(this.state.policyInstanceType === OnapConstant.operationalPolicyType && this.state.loopCache.isOpenLoopTemplate()) {
280 "Operational Policy cannot be configured as only Open Loop is supported for this Template!"
287 <Modal.Title>Edit the policy</Modal.Title>
292 var allElement = [(<Button variant="secondary" onClick={this.handleClose}>
295 if(this.state.policyInstanceType !== OnapConstant.operationalPolicyType || !this.state.loopCache.isOpenLoopTemplate()) {
297 <Button variant="primary" disabled={this.readOnly} onClick={this.handleSave}>
302 <Button variant="primary" disabled={this.readOnly} onClick={this.handleRefresh}>
312 <ModalStyled size="xl" backdrop="static" keyboard={false} show={this.state.show} onHide={this.handleClose}>
313 <Modal.Header closeButton>
314 {this.renderModalTitle()}
316 <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible>
317 {this.state.showMessage}
319 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
320 {this.state.showMessage}
323 {this.renderOpenLoopMessage()}
325 {this.renderPdpGroupDropDown()}
328 {this.renderButton()}