634c5ac5750a3e67ac8baeb828965ef0b31d4add
[clamp.git] / ui-react / src / components / dialogs / Policy / PolicyModal.js
1 /*-
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
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 JSONEditor from '@json-editor/json-editor';
34
35 const ModalStyled = styled(Modal)`
36         background-color: transparent;
37 `
38
39 export 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)
47                 policyInstanceType: this.props.match.params.policyInstanceType,
48                 pdpGroup: null,
49                 pdpGroupList: [],
50                 pdpSubgroupList: [],
51                 chosenPdpGroup: '',
52                 chosenPdpSubgroup: ''
53         };
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);
60                 this.handlePdpGroupChange = this.handlePdpGroupChange.bind(this);
61                 this.handlePdpSubgroupChange = this.handlePdpSubgroupChange.bind(this);
62                 this.createJsonEditor = this.createJsonEditor.bind(this);
63         }
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);
71                         return;
72                 }
73                 else {
74                         console.info("NO validation errors found in policy data");
75                         if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
76                 this.state.loopCache.updateMicroServiceProperties(this.state.policyName, editorData);
77                 this.state.loopCache.updateMicroServicePdpGroup(this.state.policyName, this.state.chosenPdpGroup, this.state.chosenPdpSubgroup);
78                 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                 });
83                         } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
84                                 this.state.loopCache.updateOperationalPolicyProperties(this.state.policyName, editorData);
85                                 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                                 });
91                         }
92                 }
93         }
94
95         handleClose() {
96                 this.setState({ show: false });
97                 this.props.history.push('/');
98         }
99
100         componentDidMount() {
101                 this.renderJsonEditor();
102         }
103
104     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,
115               no_additional_properties: true,
116               show_errors: 'always',
117               display_required_only: false,
118               show_opt_in: true,
119               prompt_before_delete: true,
120               required_by_default: true
121         })
122     }
123
124         renderJsonEditor() {
125                 console.debug("Rendering PolicyModal ", this.state.policyName);
126                 var toscaModel = {};
127                 var editorData = {};
128                 var pdpGroupValues = {};
129                 var chosenPdpGroupValue, chosenPdpSubgroupValue;
130                 if (this.state.policyInstanceType === 'MICRO-SERVICE-POLICY') {
131                         toscaModel = this.state.loopCache.getMicroServiceJsonRepresentationForName(this.state.policyName);
132                         editorData = this.state.loopCache.getMicroServicePropertiesForName(this.state.policyName);
133                         pdpGroupValues = this.state.loopCache.getMicroServiceSupportedPdpGroup(this.state.policyName);
134                         chosenPdpGroupValue = this.state.loopCache.getMicroServicePdpGroup(this.state.policyName);
135                         chosenPdpSubgroupValue = this.state.loopCache.getMicroServicePdpSubgroup(this.state.policyName);
136                 } else if (this.state.policyInstanceType === 'OPERATIONAL-POLICY') {
137                         toscaModel = this.state.loopCache.getOperationalPolicyJsonRepresentationForName(this.state.policyName);
138                         editorData = this.state.loopCache.getOperationalPolicyPropertiesForName(this.state.policyName);
139                         pdpGroupValues = this.state.loopCache.getOperationalPolicySupportedPdpGroup(this.state.policyName);
140                         chosenPdpGroupValue = this.state.loopCache.getOperationalPolicyPdpGroup(this.state.policyName);
141                         chosenPdpSubgroupValue = this.state.loopCache.getOperationalPolicyPdpSubgroup(this.state.policyName);
142                 }
143
144                 if (toscaModel == null) {
145                         return;
146                 }
147
148         var pdpSubgroupValues = [];
149                 if (typeof(chosenPdpGroupValue) !== "undefined") {
150                         var selectedPdpGroup =  pdpGroupValues.filter(entry => (Object.keys(entry)[0] === chosenPdpGroupValue));
151                         pdpSubgroupValues = selectedPdpGroup[0][chosenPdpGroupValue].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
152                 }
153                 this.setState({
154                                         jsonEditor: this.createJsonEditor(toscaModel,editorData),
155                                         pdpGroup: pdpGroupValues,
156                                         pdpGroupList: pdpGroupValues.map(entry => {
157                                                                 return { label: Object.keys(entry)[0], value: Object.keys(entry)[0] };
158                                                 }),
159                                         pdpSubgroupList: pdpSubgroupValues,
160                                         chosenPdpGroup: chosenPdpGroupValue,
161                                         chosenPdpSubgroup: chosenPdpSubgroupValue
162                                 })
163         }
164
165         handlePdpGroupChange(e) {
166                 var selectedPdpGroup =  this.state.pdpGroup.filter(entry => (Object.keys(entry)[0] === e.value));
167                 const pdpSubgroupValues = selectedPdpGroup[0][e.value].map((pdpSubgroup) => { return { label: pdpSubgroup, value: pdpSubgroup } });
168                 if (this.state.chosenPdpGroup !== e.value) {
169                         this.setState({
170                                 chosenPdpGroup: e.value,
171                                 chosenPdpSubgroup: '',
172                                 pdpSubgroupList: pdpSubgroupValues
173                         });
174                 }
175         }
176
177         handlePdpSubgroupChange(e) {
178                 this.setState({ chosenPdpSubgroup: e.value });
179         }
180
181         render() {
182                 return (
183                         <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
184                                 <Modal.Header closeButton>
185                                         <Modal.Title>Edit the policy</Modal.Title>
186                                 </Modal.Header>
187                                 <Modal.Body>
188                                         <div id="editor" />
189                                         <Form.Group as={Row} controlId="formPlaintextEmail">
190                                                 <Form.Label column sm="2">Pdp Group Info</Form.Label>
191                                                 <Col sm="3">
192                                                         <Select value={{ label: this.state.chosenPdpGroup, value: this.state.chosenPdpGroup }} onChange={this.handlePdpGroupChange} options={this.state.pdpGroupList} />
193                                                 </Col>
194                                                 <Col sm="3">
195                                                         <Select value={{ label: this.state.chosenPdpSubgroup, value: this.state.chosenPdpSubgroup }} onChange={this.handlePdpSubgroupChange} options={this.state.pdpSubgroupList} />
196                                                 </Col>
197                                         </Form.Group>
198                                 </Modal.Body>
199                                 <Modal.Footer>
200                                         <Button variant="secondary" onClick={this.handleClose}>
201                                                 Close
202                                         </Button>
203                                         <Button variant="primary" onClick={this.handleSave}>
204                                                 Save Changes
205                                         </Button>
206                                 </Modal.Footer>
207                         </ModalStyled>
208
209                 );
210         }
211 }