blob: 921ba1806c3620c8f9c0716bb9efea47bbc6ed07 [file] [log] [blame]
xuegao691e2b72019-08-16 11:07:24 +02001/*-
2 * ============LICENSE_START=======================================================
sebdet4b6371f2021-04-15 19:43:27 +02003 * ONAP POLICY-CLAMP
xuegao691e2b72019-08-16 11:07:24 +02004 * ================================================================================
sebdet4b6371f2021-04-15 19:43:27 +02005 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights
xuegao691e2b72019-08-16 11:07:24 +02006 * 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 */
23import React from 'react';
sebdet2e9ae122019-11-15 16:28:55 +010024import LoopActionService from '../../../api/LoopActionService';
25import LoopService from '../../../api/LoopService';
xuegao691e2b72019-08-16 11:07:24 +020026import Button from 'react-bootstrap/Button';
27import Modal from 'react-bootstrap/Modal';
28import Form from 'react-bootstrap/Form';
xuegao9047def2019-12-13 11:50:24 +010029import Tabs from 'react-bootstrap/Tabs';
30import Tab from 'react-bootstrap/Tab';
xuegao691e2b72019-08-16 11:07:24 +020031import styled from 'styled-components';
xuegaoed700372020-04-08 08:46:25 +020032import Spinner from 'react-bootstrap/Spinner'
33
34const StyledSpinnerDiv = styled.div`
35 justify-content: center !important;
36 display: flex !important;
37`;
xuegao691e2b72019-08-16 11:07:24 +020038
39const ModalStyled = styled(Modal)`
40 background-color: transparent;
41`
42const FormStyled = styled(Form.Group)`
43 padding: .25rem 1.5rem;
44`
sebdet2e9ae122019-11-15 16:28:55 +010045export default class DeployLoopModal extends React.Component {
xuegao8c2ce602020-01-13 10:27:54 +010046
47
48
xuegao691e2b72019-08-16 11:07:24 +020049 constructor(props, context) {
50 super(props, context);
51
52 this.handleSave = this.handleSave.bind(this);
53 this.handleClose = this.handleClose.bind(this);
54 this.handleChange = this.handleChange.bind(this);
55 this.refreshStatus = this.refreshStatus.bind(this);
56 this.renderDeployParam = this.renderDeployParam.bind(this);
xuegaoed700372020-04-08 08:46:25 +020057 this.renderSpinner = this.renderSpinner.bind(this);
xuegao8c2ce602020-01-13 10:27:54 +010058
59 const propertiesJson = JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties()));
60 this.state = {
61 loopCache: this.props.loopCache,
62 temporaryPropertiesJson: propertiesJson,
63 show: true,
64 key: this.getInitialKeyValue(propertiesJson)
65 };
66 }
67 getInitialKeyValue(temporaryPropertiesJson) {
68 const deployJsonList = temporaryPropertiesJson["dcaeDeployParameters"];
sebdet4b6371f2021-04-15 19:43:27 +020069 return Object.keys(deployJsonList).find((obj) => Object.keys(deployJsonList).indexOf(obj) === 0);
xuegao691e2b72019-08-16 11:07:24 +020070 }
71 componentWillReceiveProps(newProps) {
72 this.setState({
73 loopName: newProps.loopCache.getLoopName(),
74 show: true
75 });
76 }
xuegao8c2ce602020-01-13 10:27:54 +010077
xuegao691e2b72019-08-16 11:07:24 +020078 handleClose(){
xuegaobe9a2a52020-03-24 16:38:01 +010079 this.setState({ show: false });
xuegao691e2b72019-08-16 11:07:24 +020080 this.props.history.push('/');
81 }
xuegaobe9a2a52020-03-24 16:38:01 +010082
xuegaoed700372020-04-08 08:46:25 +020083 renderSpinner() {
84 if (this.state.deploying) {
85 return (
86 <StyledSpinnerDiv>
87 <Spinner animation="border" role="status">
88 <span className="sr-only">Loading...</span>
89 </Spinner>
90 </StyledSpinnerDiv>
91 );
92 } else {
93 return (<div></div>);
94 }
95 }
96
xuegao841ddaa2019-09-19 14:23:14 +020097 handleSave() {
xuegao691e2b72019-08-16 11:07:24 +020098 const loopName = this.props.loopCache.getLoopName();
99 // save the global propserties
xuegaoed700372020-04-08 08:46:25 +0200100 this.setState({ deploying: true });
xuegao691e2b72019-08-16 11:07:24 +0200101 LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => {
xuegao691e2b72019-08-16 11:07:24 +0200102 LoopActionService.performAction(loopName, "deploy").then(pars => {
xuegaocc5fe512020-04-06 13:13:52 +0200103 this.props.showSucAlert("Action deploy successfully performed");
xuegao691e2b72019-08-16 11:07:24 +0200104 // refresh status and update loop logs
105 this.refreshStatus(loopName);
106 })
107 .catch(error => {
xuegaocc5fe512020-04-06 13:13:52 +0200108 this.props.showFailAlert("Action deploy failed");
xuegao691e2b72019-08-16 11:07:24 +0200109 // refresh status and update loop logs
110 this.refreshStatus(loopName);
111 });
112 });
113 }
114
115 refreshStatus(loopName) {
116 LoopActionService.refreshStatus(loopName).then(data => {
117 this.props.updateLoopFunction(data);
xuegaoed700372020-04-08 08:46:25 +0200118 this.setState({ show: false, deploying: false });
119 this.props.history.push('/');
xuegao691e2b72019-08-16 11:07:24 +0200120 })
121 .catch(error => {
xuegaocc5fe512020-04-06 13:13:52 +0200122 this.props.showFailAlert("Refresh status failed");
xuegaoed700372020-04-08 08:46:25 +0200123 this.setState({ show: false, deploying: false });
124 this.props.history.push('/');
xuegao691e2b72019-08-16 11:07:24 +0200125 });
126 }
127 handleChange(event) {
128 let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
xuegao9047def2019-12-13 11:50:24 +0100129 deploymentParam[this.state.key][event.target.name] = event.target.value;
xuegao691e2b72019-08-16 11:07:24 +0200130
131 this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}});
132 }
xuegao9047def2019-12-13 11:50:24 +0100133 renderDeployParamTabs() {
xuegao691e2b72019-08-16 11:07:24 +0200134 if (typeof (this.state.temporaryPropertiesJson) === "undefined") {
135 return "";
136 }
137
xuegao9047def2019-12-13 11:50:24 +0100138 const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
139 var indents = [];
sebdet4b6371f2021-04-15 19:43:27 +0200140 Object.keys(deployJsonList).forEach(item =>
xuegao9047def2019-12-13 11:50:24 +0100141 indents.push(<Tab eventKey={item} title={item}>
142 {this.renderDeployParam(deployJsonList[item])}
143 </Tab>)
144 );
145 return indents;
146 }
xuegao9047def2019-12-13 11:50:24 +0100147 renderDeployParam(deployJson) {
xuegao691e2b72019-08-16 11:07:24 +0200148 var indents = [];
sebdet4b6371f2021-04-15 19:43:27 +0200149 Object.keys(deployJson).forEach(item =>
xuegao691e2b72019-08-16 11:07:24 +0200150 indents.push(<FormStyled>
151 <Form.Label>{item}</Form.Label>
152 <Form.Control type="text" name={item} onChange={this.handleChange} defaultValue={deployJson[item]}></Form.Control>
153 </FormStyled>));
xuegao9047def2019-12-13 11:50:24 +0100154 return indents;
xuegao691e2b72019-08-16 11:07:24 +0200155 }
xuegao691e2b72019-08-16 11:07:24 +0200156 render() {
157 return (
Ted Humphreydf68db22020-04-16 17:36:17 +0000158 <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
xuegao691e2b72019-08-16 11:07:24 +0200159 <Modal.Header closeButton>
160 <Modal.Title>Deployment parameters</Modal.Title>
161 </Modal.Header>
xuegao9047def2019-12-13 11:50:24 +0100162 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}>
163 {this.renderDeployParamTabs()}
164 </Tabs>
xuegaoed700372020-04-08 08:46:25 +0200165 {this.renderSpinner()}
xuegao691e2b72019-08-16 11:07:24 +0200166 <Modal.Footer>
167 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
168 <Button variant="primary" type="submit" onClick={this.handleSave}>Deploy</Button>
169 </Modal.Footer>
170 </ModalStyled>
171 );
172 }
173}