Add Create loop dialog
[clamp.git] / ui-react / src / LoopUI.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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 styled from 'styled-components';
26 import MenuBar from './components/menu/MenuBar';
27 import Navbar from 'react-bootstrap/Navbar';
28 import logo from './logo.png';
29 import { GlobalClampStyle } from './theme/globalStyle.js';
30 import OnapConstants from './utils/OnapConstants';
31
32 import LoopSvg from './components/loop_viewer/svg/LoopSvg';
33 import LoopLogs from './components/loop_viewer/logs/LoopLogs';
34 import LoopStatus from './components/loop_viewer/status/LoopStatus';
35 import UserService from './api/UserService';
36 import LoopCache from './api/LoopCache';
37 import LoopActionService from './api/LoopActionService';
38
39 import { Route } from 'react-router-dom'
40 import CreateLoopModal from './components/dialogs/Loop/CreateLoopModal';
41 import OpenLoopModal from './components/dialogs/Loop/OpenLoopModal';
42 import ModifyLoopModal from './components/dialogs/Loop/ModifyLoopModal';
43 import OperationalPolicyModal from './components/dialogs/OperationalPolicy/OperationalPolicyModal';
44 import ConfigurationPolicyModal from './components/dialogs/ConfigurationPolicy/ConfigurationPolicyModal';
45 import PolicyModal from './components/dialogs/Policy/PolicyModal';
46 import LoopPropertiesModal from './components/dialogs/Loop/LoopPropertiesModal';
47 import UserInfoModal from './components/dialogs/UserInfoModal';
48 import LoopService from './api/LoopService';
49 import UploadToscaPolicyModal from './components/dialogs/Tosca/UploadToscaPolicyModal';
50 import ViewToscaPolicyModal from './components/dialogs/Tosca/ViewToscaPolicyModal';
51 import ViewLoopTemplatesModal from './components/dialogs/Tosca/ViewLoopTemplatesModal';
52 import PerformAction from './components/dialogs/PerformActions';
53 import RefreshStatus from './components/dialogs/RefreshStatus';
54 import DeployLoopModal from './components/dialogs/Loop/DeployLoopModal';
55 import Alert from 'react-bootstrap/Alert';
56
57 import { Link } from 'react-router-dom';
58
59 const StyledMainDiv = styled.div`
60         background-color: ${props => props.theme.backgroundColor};
61 `
62
63 const ProjectNameStyled = styled.a`
64         vertical-align: middle;
65         padding-left: 30px;
66         font-size: 36px;
67         font-weight: bold;
68 `
69
70 const StyledRouterLink = styled(Link)`
71         color: ${props => props.theme.menuFontColor};
72         background-color: ${props => props.theme.backgroundColor};
73 `
74
75 const StyledLoginInfo = styled.a`
76         color: ${props => props.theme.menuFontColor};
77         background-color: ${props => props.theme.backgroundColor};
78 `
79
80 const LoopViewDivStyled = styled.div`
81         height: 100%;
82         overflow: hidden;
83         margin-left: 10px;
84         margin-right: 10px;
85         margin-bottom: 10px;
86         color: ${props => props.theme.loopViewerFontColor};
87         background-color: ${props => props.theme.loopViewerBackgroundColor};
88         border: 1px solid transparent;
89         border-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
90 `
91
92 const LoopViewHeaderDivStyled = styled.div`
93         background-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
94         padding: 10px 10px;
95         color: ${props => props.theme.loopViewerHeaderFontColor};
96 `
97
98 const LoopViewBodyDivStyled = styled.div`
99         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
100         padding: 10px 10px;
101         color: ${props => (props.theme.loopViewerHeaderFontColor)};
102         height: 95%;
103 `
104
105 export default class LoopUI extends React.Component {
106
107         state = {
108                 userName: null,
109                 loopName: OnapConstants.defaultLoopName,
110                 loopCache: new LoopCache({}),
111                 showAlert: false
112         };
113
114         constructor() {
115                 super();
116                 this.getUser = this.getUser.bind(this);
117                 this.logout = this.logout.bind(this);
118                 this.updateLoopCache = this.updateLoopCache.bind(this);
119                 this.loadLoop = this.loadLoop.bind(this);
120                 this.closeLoop = this.closeLoop.bind(this);
121                 this.showAlert =  this.showAlert.bind(this);
122                 this.disableAlert =  this.disableAlert.bind(this);
123         }
124
125         componentWillMount() {
126                 this.getUser();
127         }
128
129         getUser() {
130                 UserService.login().then(user => {
131                         this.setState({ userName: user })
132                 });
133         }
134         
135         logout() {
136                 UserService.logout().then(user => {
137                         this.setState({ userName: user });
138                         window.location.reload();
139                 });
140                 
141         }
142
143         renderMenuNavBar() {
144                 return (
145                         <MenuBar loopName={this.state.loopName}/>
146                 );
147         }
148
149         renderUserLoggedNavBar() {
150                 return (
151                         <Navbar.Text>
152                         <StyledLoginInfo>Signed in as: </StyledLoginInfo>
153                                 <StyledRouterLink to="/userInfo">{this.state.userName}</StyledRouterLink>
154                                 <StyledRouterLink to="/logout/"> (logout)</StyledRouterLink>
155                         </Navbar.Text>
156                 );
157         }
158
159         renderLogoNavBar() {
160                 return (
161                         <Navbar.Brand>
162                                 <img height="50px" width="234px" src={logo} alt="" />
163                                 <ProjectNameStyled>CLAMP</ProjectNameStyled>
164                         </Navbar.Brand>
165                 );
166         }
167
168         renderAlertBar() {
169                 return (
170                                 <Alert variant="danger" show={this.state.showAlert} onClose={this.disableAlert} dismissible>
171                                         {this.state.showMessage}
172                                 </Alert>
173                 );
174         }
175
176         renderNavBar() {
177                 return (
178                         <Navbar >
179                                 {this.renderLogoNavBar()}
180                                 <Navbar.Toggle aria-controls="responsive-navbar-nav" />
181                                 {this.renderMenuNavBar()}
182                                 {this.renderUserLoggedNavBar()}
183                         </Navbar>
184                 );
185         }
186
187         renderLoopViewHeader() {
188                 return (
189                         <LoopViewHeaderDivStyled>
190                                 Loop Viewer - {this.state.loopName}
191                         </LoopViewHeaderDivStyled>
192                 );
193         }
194
195         renderLoopViewBody() {
196                 return (
197                         <LoopViewBodyDivStyled>
198                                 <LoopSvg loopCache={this.state.loopCache} />
199                                 <LoopStatus loopCache={this.state.loopCache}/>
200                                 <LoopLogs loopCache={this.state.loopCache} />
201                         </LoopViewBodyDivStyled>
202                 );
203         }
204
205         getLoopCache() {
206                 return this.state.loopCache;
207
208         }
209
210         renderLoopViewer() {
211                 return (
212                         <LoopViewDivStyled>
213                                 {this.renderLoopViewHeader()}
214                                 {this.renderLoopViewBody()}
215                         </LoopViewDivStyled>
216                 );
217         }
218
219         updateLoopCache(loopJson) {
220                 this.setState({ loopCache: new LoopCache(loopJson) });
221                 this.setState({ loopName: this.state.loopCache.getLoopName() });
222                 console.info(this.state.loopName+" loop loaded successfully");
223         }
224
225         showAlert(message) {
226                 this.setState ({ showAlert: true, showMessage:message });
227         }
228
229         disableAlert() {
230                 this.setState ({ showAlert: false });
231         }
232
233         loadLoop(loopName) {
234                 LoopService.getLoop(loopName).then(loop => {
235                         console.debug("Updating loopCache");
236                         LoopActionService.refreshStatus(loopName).then(data => {
237                                 this.updateLoopCache(data);
238                                 this.props.history.push('/');
239                         })
240                         .catch(error => {
241                                 this.updateLoopCache(loop);
242                                 this.props.history.push('/');
243                         });
244                 });
245         }
246
247         closeLoop() {
248                 this.setState({ loopCache: new LoopCache({}), loopName: OnapConstants.defaultLoopName });
249                 this.props.history.push('/');
250         }
251
252         render() {
253                 return (
254                                 <StyledMainDiv id="main_div">
255                                 <Route path="/uploadToscaPolicyModal" render={(routeProps) => (<UploadToscaPolicyModal {...routeProps} />)} />
256                                 <Route path="/viewToscaPolicyModal" render={(routeProps) => (<ViewToscaPolicyModal {...routeProps} />)} />
257                                 <Route path="/ViewLoopTemplatesModal" render={(routeProps) => (<ViewLoopTemplatesModal {...routeProps} />)} />
258                                 <Route path="/operationalPolicyModal"
259                                         render={(routeProps) => (<OperationalPolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
260                                 <Route path="/policyModal/:policyInstanceType/:policyName" render={(routeProps) => (<PolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
261                                 <Route path="/configurationPolicyModal/:policyName" render={(routeProps) => (<ConfigurationPolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
262                                 <Route path="/createLoop" render={(routeProps) => (<CreateLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
263                                 <Route path="/openLoop" render={(routeProps) => (<OpenLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
264                                 <Route path="/loopProperties" render={(routeProps) => (<LoopPropertiesModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
265                                 <Route path="/modifyLoop" render={(routeProps) => (<ModifyLoopModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
266
267                                 <Route path="/userInfo" render={(routeProps) => (<UserInfoModal {...routeProps} />)} />
268                                 <Route path="/closeLoop" render={this.closeLoop} />
269                                 <Route path="/submit" render={(routeProps) => (<PerformAction {...routeProps} loopAction="submit" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
270                                 <Route path="/stop" render={(routeProps) => (<PerformAction {...routeProps} loopAction="stop" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
271                                 <Route path="/restart" render={(routeProps) => (<PerformAction {...routeProps} loopAction="restart" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
272                                 <Route path="/delete" render={(routeProps) => (<PerformAction {...routeProps} loopAction="delete" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
273                                 <Route path="/undeploy" render={(routeProps) => (<PerformAction {...routeProps} loopAction="undeploy" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
274                                 <Route path="/deploy" render={(routeProps) => (<DeployLoopModal {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
275                                 <Route path="/refreshStatus" render={(routeProps) => (<RefreshStatus {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
276                                 <Route path="/logout" render={this.logout} />
277                                 <GlobalClampStyle />
278                                         {this.renderAlertBar()}
279                                         {this.renderNavBar()}
280                                         {this.renderLoopViewer()}
281                                 </StyledMainDiv>
282                 );
283         }
284 }