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