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