blob: 7151725c3a0bed63d06992bb9d9b1909a9d17a55 [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';
33import JSONEditor from '@json-editor/json-editor';
34
35const ModalStyled = styled(Modal)`
36 background-color: transparent;
37`
38
39export default class PolicyModal extends React.Component {
40
41 state = {
42 show: true,
43 loopCache: this.props.loopCache,
44 jsonEditor: null,
45 policyName: this.props.match.params.policyName,
46 // This is to indicate whether it's an operational or config policy (in terms of loop instance)
xuegao635445a2020-03-02 11:37:20 +010047 policyInstanceType: this.props.match.params.policyInstanceType,
48 pdpGroup: null,
49 pdpGroupList: [],
50 pdpSubgroupList: [],
51 chosenPdpGroup: '',
52 chosenPdpSubgroup: ''
sebdetc11160e2020-02-25 15:13:31 -080053 };
54
55 constructor(props, context) {
56 super(props, context);
57 this.handleClose = this.handleClose.bind(this);
58 this.handleSave = this.handleSave.bind(this);
59 this.renderJsonEditor = this.renderJsonEditor.bind(this);
xuegao635445a2020-03-02 11:37:20 +010060 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
61 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
sebdet2dd4e992020-03-04 15:47:39 -080062 this.createJsonEditor = this.createJsonEditor.bind(this);
sebdetc11160e2020-02-25 15:13:31 -080063 }
64
65 handleSave() {
66 var errors = this.state.jsonEditor.validate();
67 var editorData = this.state.jsonEditor.getValue();
68
69 if (errors.length !== 0) {
70 console.error("Errors detected during policy data validation ", errors);
sebdet2dd4e992020-03-04 15:47:39 -080071 return;
sebdetc11160e2020-02-25 15:13:31 -080072 }
73 else {
74 console.info("NO validation errors found in policy data");
sebdetd2a4df02020-02-26 15:47:30 -080075 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
sebdet2dd4e992020-03-04 15:47:39 -080076 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
xuegao635445a2020-03-02 11:37:20 +010077 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
sebdetc11160e2020-02-25 15:13:31 -080078 LoopService.setMicroServiceProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getMicroServiceForName(this.state.policyName)).then(resp => {
79 this.setState({ show: false });
80 this.props.history.push('/');
81 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
82 });
sebdetd2a4df02020-02-26 15:47:30 -080083 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
sebdet2dd4e992020-03-04 15:47:39 -080084 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
xuegao635445a2020-03-02 11:37:20 +010085 this.state.loopCache.updateOperationalPolicyPdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
86 LoopService.setOperationalPolicyProperties(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()).then(resp => {
87 this.setState({ show: false });
88 this.props.history.push('/');
89 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
90 });
sebdetc11160e2020-02-25 15:13:31 -080091 }
92 }
93 }
94
95 handleClose() {
96 this.setState({ show: false });
97 this.props.history.push('/');
98 }
99
100 componentDidMount() {
101 this.renderJsonEditor();
102 }
103
sebdet2dd4e992020-03-04 15:47:39 -0800104 createJsonEditor(toscaModel, editorData) {
105 return new JSONEditor(document.getElementById("editor"),
106 { schema: toscaModel,
107 startval: editorData,
108 theme: 'bootstrap4',
109 object_layout: 'grid',
110 disable_properties: true,
111 disable_edit_json: false,
112 disable_array_reorder: true,
113 disable_array_delete_last_row: true,
114 disable_array_delete_all_rows: false,
sebdet49ab84a2020-03-13 15:27:41 -0700115 array_controls_top: true,
116 keep_oneof_values: false,
117 collapsed:true,
sebdet2dd4e992020-03-04 15:47:39 -0800118 show_errors: 'always',
119 display_required_only: false,
120 show_opt_in: true,
121 prompt_before_delete: true,
sebdet49ab84a2020-03-13 15:27:41 -0700122 required_by_default: false,
123 array_controls_top: true
sebdet2dd4e992020-03-04 15:47:39 -0800124 })
125 }
126
sebdetc11160e2020-02-25 15:13:31 -0800127 renderJsonEditor() {
128 console.debug("Rendering PolicyModal ", this.state.policyName);
129 var toscaModel = {};
xuegao635445a2020-03-02 11:37:20 +0100130 var editorData = {};
131 var pdpGroupValues = {};
132 var chosenPdpGroupValue, chosenPdpSubgroupValue;
133 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
134 toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
135 editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName);
sebdet2dd4e992020-03-04 15:47:39 -0800136 pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
xuegao635445a2020-03-02 11:37:20 +0100137 chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName);
138 chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName);
139 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
140 toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
141 editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
sebdet2dd4e992020-03-04 15:47:39 -0800142 pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
xuegao635445a2020-03-02 11:37:20 +0100143 chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
144 chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
145 }
sebdetc11160e2020-02-25 15:13:31 -0800146
147 if (toscaModel == null) {
148 return;
149 }
150
sebdet2dd4e992020-03-04 15:47:39 -0800151 var pdpSubgroupValues = [];
152 if (typeof(chosenPdpGroupValue) !== "undefined") {
xuegao635445a2020-03-02 11:37:20 +0100153 var selectedPdpGroup = pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
sebdet2dd4e992020-03-04 15:47:39 -0800154 pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
xuegao635445a2020-03-02 11:37:20 +0100155 }
sebdet2dd4e992020-03-04 15:47:39 -0800156 this.setState({
157 jsonEditor: this.createJsonEditor(toscaModel,editorData),
158 pdpGroup: pdpGroupValues,
159 pdpGroupList: pdpGroupValues.map(entry => {
160 return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] };
161 }),
162 pdpSubgroupList: pdpSubgroupValues,
163 chosenPdpGroup: chosenPdpGroupValue,
164 chosenPdpSubgroup: chosenPdpSubgroupValue
165 })
xuegao635445a2020-03-02 11:37:20 +0100166 }
167
168 handlePdpGroupChange(e) {
169 var selectedPdpGroup = this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value));
170 const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
171 if (this.state.chosenPdpGroup !== e.value) {
sebdet2dd4e992020-03-04 15:47:39 -0800172 this.setState({
xuegao635445a2020-03-02 11:37:20 +0100173 chosenPdpGroup: e.value,
174 chosenPdpSubgroup: '',
175 pdpSubgroupList: pdpSubgroupValues
176 });
177 }
178 }
179
180 handlePdpSubgroupChange(e) {
181 this.setState({ chosenPdpSubgroup: e.value });
sebdetc11160e2020-02-25 15:13:31 -0800182 }
183
184 render() {
185 return (
186 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
187 <Modal.Header closeButton>
sebdet2dd4e992020-03-04 15:47:39 -0800188 <Modal.Title>Edit the policy</Modal.Title>
sebdetc11160e2020-02-25 15:13:31 -0800189 </Modal.Header>
190 <Modal.Body>
191 <div id="editor" />
xuegao635445a2020-03-02 11:37:20 +0100192 <Form.Group as={Row} controlId="formPlaintextEmail">
193 <Form.Label column sm="2">Pdp Group Info</Form.Label>
194 <Col sm="3">
195 <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
196 </Col>
197 <Col sm="3">
198 <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
199 </Col>
200 </Form.Group>
sebdetc11160e2020-02-25 15:13:31 -0800201 </Modal.Body>
202 <Modal.Footer>
203 <Button variant="secondary" onClick={this.handleClose}>
204 Close
xuegao635445a2020-03-02 11:37:20 +0100205 </Button>
sebdetc11160e2020-02-25 15:13:31 -0800206 <Button variant="primary" onClick={this.handleSave}>
207 Save Changes
xuegao635445a2020-03-02 11:37:20 +0100208 </Button>
sebdetc11160e2020-02-25 15:13:31 -0800209 </Modal.Footer>
210 </ModalStyled>
211
212 );
213 }
214}