sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 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, { 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'; |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 38 | import Tabs from 'react-bootstrap/Tabs'; |
| 39 | import Tab from 'react-bootstrap/Tab'; |
xuegao | c656139 | 2020-04-21 13:39:50 +0200 | [diff] [blame] | 40 | import Alert from 'react-bootstrap/Alert'; |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 41 | |
| 42 | const ModalStyled = styled(Modal)` |
| 43 | background-color: transparent; |
| 44 | ` |
| 45 | const TextModal = styled.textarea` |
| 46 | margin-top: 20px; |
| 47 | white-space:pre; |
| 48 | background-color: ${props => props.theme.toscaTextareaBackgroundColor}; |
| 49 | text-align: justify; |
| 50 | font-size: ${props => props.theme.toscaTextareaFontSize}; |
| 51 | width: 100%; |
| 52 | height: 300px; |
| 53 | ` |
| 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'}; |
| 57 | |
| 58 | export default class ModifyLoopModal extends React.Component { |
| 59 | |
| 60 | state = { |
| 61 | show: true, |
| 62 | loopCache: this.props.loopCache, |
| 63 | content: 'Please select Tosca model to view the details', |
| 64 | selectedRowData: {}, |
| 65 | toscaPolicyModelsData: [], |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 66 | selectedPolicyModelsData: [], |
| 67 | key: 'add', |
xuegao | c656139 | 2020-04-21 13:39:50 +0200 | [diff] [blame] | 68 | showFailAlert: false, |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 69 | toscaColumns: [ |
| 70 | { title: "#", field: "index", render: rowData => rowData.tableData.id + 1, |
| 71 | cellStyle: cellStyle, |
| 72 | headerStyle: headerStyle |
| 73 | }, |
| 74 | { title: "Policy Model Type", field: "policyModelType", |
| 75 | cellStyle: cellStyle, |
| 76 | headerStyle: headerStyle |
| 77 | }, |
| 78 | { title: "Policy Acronym", field: "policyAcronym", |
| 79 | cellStyle: cellStyle, |
| 80 | headerStyle: headerStyle |
| 81 | }, |
sebdet | ab3eab6 | 2020-04-16 12:09:24 +0200 | [diff] [blame] | 82 | { title: "Policy Name", field: "policyName", |
| 83 | cellStyle: cellStyle, |
| 84 | headerStyle: headerStyle |
| 85 | }, |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 86 | { title: "Version", field: "version", |
| 87 | cellStyle: cellStyle, |
| 88 | headerStyle: headerStyle |
| 89 | }, |
| 90 | { title: "Uploaded By", field: "updatedBy", |
| 91 | cellStyle: cellStyle, |
| 92 | headerStyle: headerStyle |
| 93 | }, |
| 94 | { title: "Uploaded Date", field: "updatedDate", editable: 'never', |
| 95 | cellStyle: cellStyle, |
| 96 | headerStyle: headerStyle |
| 97 | }, |
sebdet | ab3eab6 | 2020-04-16 12:09:24 +0200 | [diff] [blame] | 98 | { title: "Created Date", field: "createdDate", editable: 'never', |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 99 | cellStyle: cellStyle, |
| 100 | headerStyle: headerStyle |
| 101 | } |
| 102 | ], |
| 103 | tableIcons: { |
| 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} />) |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | constructor(props, context) { |
| 115 | super(props, context); |
| 116 | this.handleClose = this.handleClose.bind(this); |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 117 | this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this); |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 118 | this.handleYamlContent = this.handleYamlContent.bind(this); |
| 119 | this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this); |
| 120 | this.handleAdd = this.handleAdd.bind(this); |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 121 | this.handleRemove = this.handleRemove.bind(this); |
| 122 | this.initializeToscaPolicyModelsInfo(); |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 123 | } |
| 124 | |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 125 | initializeToscaPolicyModelsInfo() { |
| 126 | var operationalPolicies = this.state.loopCache.getOperationalPolicies(); |
| 127 | var selectedPolicyModels = []; |
| 128 | for (var policy in operationalPolicies) { |
sebdet | ab3eab6 | 2020-04-16 12:09:24 +0200 | [diff] [blame] | 129 | var newRow = operationalPolicies[policy]["policyModel"]; |
sebdet | 6e7d048 | 2020-04-16 16:14:45 +0200 | [diff] [blame] | 130 | newRow["policyName"] = operationalPolicies[policy].name; |
sebdet | ab3eab6 | 2020-04-16 12:09:24 +0200 | [diff] [blame] | 131 | selectedPolicyModels.push(newRow); |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | PolicyToscaService.getToscaPolicyModels().then(allToscaModels => { |
| 135 | this.setState({ toscaPolicyModelsData: allToscaModels, |
| 136 | selectedPolicyModelsData: selectedPolicyModels}); |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 137 | }); |
| 138 | } |
| 139 | |
| 140 | getToscaPolicyModelYaml(policyModelType, policyModelVersion) { |
| 141 | if (typeof policyModelType !== "undefined") { |
| 142 | PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => { |
| 143 | if (toscaYaml.length !== 0) { |
| 144 | this.setState({content: toscaYaml}) |
| 145 | } else { |
| 146 | this.setState({ content: 'No Tosca model Yaml available' }) |
| 147 | } |
| 148 | }); |
| 149 | } else { |
| 150 | this.setState({ content: 'Please select Tosca model to view the details' }) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | handleYamlContent(event) { |
| 155 | this.setState({ content: event.target.value }); |
| 156 | } |
| 157 | |
| 158 | handleClose() { |
| 159 | this.setState({ show: false }); |
| 160 | this.props.history.push('/'); |
| 161 | } |
| 162 | |
xuegao | c656139 | 2020-04-21 13:39:50 +0200 | [diff] [blame] | 163 | renderAlert() { |
| 164 | return ( |
| 165 | <div> |
| 166 | <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible> |
| 167 | {this.state.showMessage} |
| 168 | </Alert> |
| 169 | </div> |
| 170 | ); |
| 171 | } |
| 172 | |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 173 | handleAdd() { |
xuegao | c656139 | 2020-04-21 13:39:50 +0200 | [diff] [blame] | 174 | LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version) |
| 175 | .then(pars => { |
| 176 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
| 177 | this.handleClose(); |
| 178 | }) |
| 179 | .catch(error => { |
| 180 | this.setState({ showFailAlert: true, showMessage: "Adding failed with error: " + error.message}); |
| 181 | }); |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | handleRemove() { |
sebdet | ab3eab6 | 2020-04-16 12:09:24 +0200 | [diff] [blame] | 185 | LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version,this.state.selectedRowData.policyName); |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 186 | this.props.loadLoopFunction(this.state.loopCache.getLoopName()); |
xuegao | a2625fe | 2020-03-18 12:55:22 +0100 | [diff] [blame] | 187 | this.handleClose(); |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | render() { |
| 191 | return ( |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 192 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 193 | <Modal.Header closeButton> |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 194 | <Modal.Title>Modify Loop Operational Policies</Modal.Title> |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 195 | </Modal.Header> |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 196 | <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}> |
| 197 | <Tab eventKey="add" title="Add Operational Policies"> |
| 198 | <Modal.Body> |
| 199 | <MaterialTable |
| 200 | title={"View Tosca Policy Models"} |
| 201 | data={this.state.toscaPolicyModelsData} |
| 202 | columns={this.state.toscaColumns} |
| 203 | icons={this.state.tableIcons} |
| 204 | onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version);this.setState({selectedRowData: rowData})}} |
| 205 | options={{ |
| 206 | headerStyle: rowHeaderStyle, |
| 207 | rowStyle: rowData => ({ |
| 208 | backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined |
| 209 | && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF' |
xuegao | c656139 | 2020-04-21 13:39:50 +0200 | [diff] [blame] | 210 | }) |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 211 | }} |
| 212 | /> |
| 213 | <div> |
| 214 | <TextModal value={this.state.content} onChange={this.handleYamlContent}/> |
| 215 | </div> |
| 216 | </Modal.Body> |
xuegao | c656139 | 2020-04-21 13:39:50 +0200 | [diff] [blame] | 217 | {this.renderAlert()} |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 218 | </Tab> |
| 219 | <Tab eventKey="remove" title="Remove Operational Policies"> |
| 220 | <Modal.Body> |
| 221 | <MaterialTable |
sebdet | ab3eab6 | 2020-04-16 12:09:24 +0200 | [diff] [blame] | 222 | title={"Tosca Policy Models already added"} |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 223 | data={this.state.selectedPolicyModelsData} |
| 224 | columns={this.state.toscaColumns} |
| 225 | icons={this.state.tableIcons} |
| 226 | onRowClick={(event, rowData) => {this.setState({selectedRowData: rowData})}} |
| 227 | options={{ |
| 228 | headerStyle: rowHeaderStyle, |
| 229 | rowStyle: rowData => ({ |
| 230 | backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined |
| 231 | && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF' |
| 232 | }) |
| 233 | }} |
| 234 | /> |
| 235 | </Modal.Body> |
| 236 | </Tab> |
| 237 | </Tabs> |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 238 | <Modal.Footer> |
| 239 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 240 | <Button variant="primary" disabled={(this.state.key === "remove")} type="submit" onClick={this.handleAdd}>Add</Button> |
| 241 | <Button variant="primary" disabled={(this.state.key === "add")} type="submit" onClick={this.handleRemove}>Remove</Button> |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 242 | </Modal.Footer> |
xuegao | fb4b25f | 2020-03-11 11:22:15 +0100 | [diff] [blame] | 243 | |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 244 | </ModalStyled> |
| 245 | ); |
| 246 | } |
| 247 | } |