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';
37 const ModalStyled = styled(Modal)`
38 background-color: transparent;
41 export default class PolicyModal extends React.Component {
45 loopCache: this.props.loopCache,
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)
49 policyInstanceType: this.props.match.params.policyInstanceType,
54 chosenPdpSubgroup: '',
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);
64 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
65 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
66 this.createJsonEditor = this.createJsonEditor.bind(this);
67 this.handleRefresh = this.handleRefresh.bind(this);
68 this.disableAlert = this.disableAlert.bind(this);
72 var errors = this.state.jsonEditor.validate();
73 var editorData = this.state.jsonEditor.getValue();
75 if (errors.length !== 0) {
76 console.error("Errors detected during policy data validation ", errors);
79 showMessage: "Errors detected during policy data validation " + errors
84 console.info("NO validation errors found in policy data");
85 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
86 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
87 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
88 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());
93 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
94 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
95 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());
106 this.setState({ show: false });
107 this.props.history.push('/');
110 componentDidMount() {
111 this.renderJsonEditor();
114 createJsonEditor(toscaModel, editorData) {
115 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);
130 return new JSONEditor(document.getElementById("editor"),
131 { schema: toscaModel,
132 startval: editorData,
133 theme: 'myBootstrap4',
134 object_layout: 'grid',
135 disable_properties: false,
136 disable_edit_json: false,
137 disable_array_reorder: true,
138 disable_array_delete_last_row: true,
139 disable_array_delete_all_rows: false,
140 array_controls_top: true,
141 keep_oneof_values: false,
143 show_errors: 'always',
144 display_required_only: false,
146 prompt_before_delete: true,
147 required_by_default: false
152 console.debug("Rendering PolicyModal ", this.state.policyName);
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);
160 pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
161 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);
166 pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
167 chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
168 chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
171 if (toscaModel == null) {
175 var pdpSubgroupValues = [];
176 if (typeof(chosenPdpGroupValue) !== "undefined") {
177 var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
178 pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
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] };
186 pdpSubgroupList: pdpSubgroupValues,
187 chosenPdpGroup: chosenPdpGroupValue,
188 chosenPdpSubgroup: chosenPdpSubgroupValue
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) {
197 chosenPdpGroup: e.value,
198 chosenPdpSubgroup: '',
199 pdpSubgroupList: pdpSubgroupValues
204 handlePdpSubgroupChange(e) {
205 this.setState({ chosenPdpSubgroup: e.value });
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 = "";
217 loopCache: newLoopCache,
218 jsonEditor: this.createJsonEditor(toscaModel,editorData),
220 showMessage: "Successfully refreshed"
224 console.error("Error while refreshing the Operational Policy Json Representation");
227 showMessage: "Refreshing of UI failed"
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 = "";
237 loopCache: newLoopCache,
238 jsonEditor: this.createJsonEditor(toscaModel,editorData),
240 showMessage: "Successfully refreshed"
244 console.error("Error while refreshing the Operational Policy Json Representation");
247 showMessage: "Refreshing of UI failed"
254 this.setState ({ showSucAlert: false, showFailAlert: false });
259 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose } backdrop="static">
260 <Modal.Header closeButton>
261 <Modal.Title>Edit the policy</Modal.Title>
263 <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible>
264 {this.state.showMessage}
266 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
267 {this.state.showMessage}
271 <Form.Group as={Row} controlId="formPlaintextEmail">
272 <Form.Label column sm="2">Pdp Group Info</Form.Label>
274 <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
277 <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
282 <Button variant="secondary" onClick={this.handleClose}>
285 <Button variant="primary" onClick={this.handleSave}>
288 <Button variant="primary" onClick={this.handleRefresh}>