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, { forwardRef } from 'react'
25 import MaterialTable from "material-table";
26 import Button from 'react-bootstrap/Button';
27 import Modal from 'react-bootstrap/Modal';
28 import styled from 'styled-components';
29 import PolicyToscaService from '../../../api/PolicyToscaService';
30 import ArrowUpward from '@material-ui/icons/ArrowUpward';
31 import ChevronLeft from '@material-ui/icons/ChevronLeft';
32 import ChevronRight from '@material-ui/icons/ChevronRight';
33 import Clear from '@material-ui/icons/Clear';
34 import FirstPage from '@material-ui/icons/FirstPage';
35 import LastPage from '@material-ui/icons/LastPage';
36 import Search from '@material-ui/icons/Search';
37 import LoopService from '../../../api/LoopService';
38 import Tabs from 'react-bootstrap/Tabs';
39 import Tab from 'react-bootstrap/Tab';
40 import Alert from 'react-bootstrap/Alert';
42 const ModalStyled = styled(Modal)`
43 background-color: transparent;
45 const TextModal = styled.textarea`
48 background-color: ${props => props.theme.toscaTextareaBackgroundColor};
50 font-size: ${props => props.theme.toscaTextareaFontSize};
54 const cellStyle = { border: '1px solid black' };
55 const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' };
56 const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'};
58 export default class ModifyLoopModal extends React.Component {
62 loopCache: this.props.loopCache,
63 content: 'Please select Tosca model to view the details',
65 toscaPolicyModelsData: [],
66 selectedPolicyModelsData: [],
70 { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
72 headerStyle: headerStyle
74 { title: "Policy Model Type", field: "policyModelType",
76 headerStyle: headerStyle
78 { title: "Policy Acronym", field: "policyAcronym",
80 headerStyle: headerStyle
82 { title: "Policy Name", field: "policyName",
84 headerStyle: headerStyle
86 { title: "Version", field: "version",
88 headerStyle: headerStyle
90 { title: "Uploaded By", field: "updatedBy",
92 headerStyle: headerStyle
94 { title: "Uploaded Date", field: "updatedDate", editable: 'never',
96 headerStyle: headerStyle
98 { title: "Created Date", field: "createdDate", editable: 'never',
100 headerStyle: headerStyle
104 FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
105 LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
106 NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
107 PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
108 ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
109 Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
110 SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
114 constructor(props, context) {
115 super(props, context);
116 this.handleClose = this.handleClose.bind(this);
117 this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this);
118 this.handleYamlContent = this.handleYamlContent.bind(this);
119 this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this);
120 this.handleAdd = this.handleAdd.bind(this);
121 this.handleRemove = this.handleRemove.bind(this);
122 this.initializeToscaPolicyModelsInfo();
125 componentWillReceiveProps(newProps) {
127 loopCache: newProps.loopCache,
128 temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties()))
132 initializeToscaPolicyModelsInfo() {
133 var operationalPolicies = this.state.loopCache.getOperationalPolicies();
134 var selectedPolicyModels = [];
135 for (var policy in operationalPolicies) {
136 var newRow = operationalPolicies[policy]["policyModel"];
137 newRow["policyName"] = operationalPolicies[policy].name;
138 selectedPolicyModels.push(newRow);
141 PolicyToscaService.getToscaPolicyModels().then(allToscaModels => {
142 this.setState({ toscaPolicyModelsData: allToscaModels,
143 selectedPolicyModelsData: selectedPolicyModels});
147 getToscaPolicyModelYaml(policyModelType, policyModelVersion) {
148 if (typeof policyModelType !== "undefined") {
149 PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => {
150 if (toscaYaml.length !== 0) {
151 this.setState({content: toscaYaml})
153 this.setState({ content: 'No Tosca model Yaml available' })
157 this.setState({ content: 'Please select Tosca model to view the details' })
161 handleYamlContent(event) {
162 this.setState({ content: event.target.value });
166 this.setState({ show: false });
167 this.props.history.push('/');
173 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
174 {this.state.showMessage}
181 LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version)
183 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
187 this.setState({ showFailAlert: true, showMessage: "Adding failed with error: " + error.message});
192 LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version,this.state.selectedRowData.policyName);
193 this.props.loadLoopFunction(this.state.loopCache.getLoopName());
199 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
200 <Modal.Header closeButton>
201 <Modal.Title>Modify Loop Operational Policies</Modal.Title>
203 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}>
204 <Tab eventKey="add" title="Add Operational Policies">
207 title={"View Tosca Policy Models"}
208 data={this.state.toscaPolicyModelsData}
209 columns={this.state.toscaColumns}
210 icons={this.state.tableIcons}
211 onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version);this.setState({selectedRowData: rowData})}}
213 headerStyle: rowHeaderStyle,
214 rowStyle: rowData => ({
215 backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
216 && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
221 <TextModal value={this.state.content} onChange={this.handleYamlContent}/>
226 <Tab eventKey="remove" title="Remove Operational Policies">
229 title={"Tosca Policy Models already added"}
230 data={this.state.selectedPolicyModelsData}
231 columns={this.state.toscaColumns}
232 icons={this.state.tableIcons}
233 onRowClick={(event, rowData) => {this.setState({selectedRowData: rowData})}}
235 headerStyle: rowHeaderStyle,
236 rowStyle: rowData => ({
237 backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
238 && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
246 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
247 <Button variant="primary" disabled={(this.state.key === "remove")} type="submit" onClick={this.handleAdd}>Add</Button>
248 <Button variant="primary" disabled={(this.state.key === "add")} type="submit" onClick={this.handleRemove}>Remove</Button>