8624726bed0f491253c983f75fc2db52a81dcab8
[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 SvgGenerator from './components/loop_viewer/svg/SvgGenerator';
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 PolicyModal from './components/dialogs/Policy/PolicyModal';
44 import LoopPropertiesModal from './components/dialogs/Loop/LoopPropertiesModal';
45 import UserInfoModal from './components/dialogs/UserInfoModal';
46 import LoopService from './api/LoopService';
47 import UploadToscaPolicyModal from './components/dialogs/Tosca/UploadToscaPolicyModal';
48 import ViewToscaPolicyModal from './components/dialogs/Tosca/ViewToscaPolicyModal';
49 import ViewLoopTemplatesModal from './components/dialogs/Tosca/ViewLoopTemplatesModal';
50 import ManageDictionaries from './components/dialogs/ManageDictionaries/ManageDictionaries';
51 import PerformAction from './components/dialogs/PerformActions';
52 import RefreshStatus from './components/dialogs/RefreshStatus';
53 import DeployLoopModal from './components/dialogs/Loop/DeployLoopModal';
54 import Alert from 'react-bootstrap/Alert';
55
56 import { Link } from 'react-router-dom';
57
58 const StyledMainDiv = styled.div`
59         background-color: ${props => props.theme.backgroundColor};
60 `
61
62 const ProjectNameStyled = styled.a`
63         vertical-align: middle;
64         padding-left: 30px;
65         font-size: 36px;
66         font-weight: bold;
67 `
68
69 const StyledRouterLink = styled(Link)`
70         color: ${props => props.theme.menuFontColor};
71         background-color: ${props => props.theme.backgroundColor};
72 `
73
74 const StyledLoginInfo = styled.a`
75         color: ${props => props.theme.menuFontColor};
76         background-color: ${props => props.theme.backgroundColor};
77 `
78
79 const LoopViewDivStyled = styled.div`
80         height: 100%;
81         overflow: hidden;
82         margin-left: 10px;
83         margin-right: 10px;
84         margin-bottom: 10px;
85         color: ${props => props.theme.loopViewerFontColor};
86         background-color: ${props => props.theme.loopViewerBackgroundColor};
87         border: 1px solid transparent;
88         border-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
89 `
90
91 const LoopViewHeaderDivStyled = styled.div`
92         background-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
93         padding: 10px 10px;
94         color: ${props => props.theme.loopViewerHeaderFontColor};
95 `
96
97 const LoopViewBodyDivStyled = styled.div`
98         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
99         padding: 10px 10px;
100         color: ${props => (props.theme.loopViewerHeaderFontColor)};
101         height: 95%;
102 `
103
104 export default class LoopUI extends React.Component {
105
106         state = {
107                 userName: null,
108                 loopName: OnapConstants.defaultLoopName,
109                 loopCache: new LoopCache({}),
110                 showSucAlert: false,
111                 showFailAlert: false
112         };
113
114         constructor() {
115                 super();
116                 this.getUser = this.getUser.bind(this);
117                 this.updateLoopCache = this.updateLoopCache.bind(this);
118                 this.loadLoop = this.loadLoop.bind(this);
119                 this.closeLoop = this.closeLoop.bind(this);
120                 this.showSucAlert =  this.showSucAlert.bind(this);
121                 this.showFailAlert =  this.showFailAlert.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         renderMenuNavBar() {
136                 return (
137                         <MenuBar loopName={this.state.loopName}/>
138                 );
139         }
140
141         renderUserLoggedNavBar() {
142                 return (
143                         <Navbar.Text>
144                         <StyledLoginInfo>Signed in as: </StyledLoginInfo>
145                                 <StyledRouterLink to="/userInfo">{this.state.userName}</StyledRouterLink>
146                         </Navbar.Text>
147                 );
148         }
149
150         renderLogoNavBar() {
151                 return (
152                         <Navbar.Brand>
153                                 <img height="50px" width="234px" src={logo} alt="" />
154                                 <ProjectNameStyled>CLAMP</ProjectNameStyled>
155                         </Navbar.Brand>
156                 );
157         }
158
159         renderAlertBar() {
160                 return (
161                         <div>
162                                 <Alert variant="success" show={this.state.showSucAlert} onClose={this.disableAlert} dismissible>
163                                         {this.state.showMessage}
164                                 </Alert>
165                                 <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
166                                         {this.state.showMessage}
167                                 </Alert>
168                         </div>
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} - ({this.state.loopCache.getTemplateName()})
187                         </LoopViewHeaderDivStyled>
188                 );
189         }
190
191         renderLoopViewBody() {
192                 return (
193                         <LoopViewBodyDivStyled>
194                                 <SvgGenerator loopCache={this.state.loopCache} clickable={true} generatedFrom={SvgGenerator.GENERATED_FROM_INSTANCE}/>
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         showSucAlert(message) {
222                 this.setState ({ showSucAlert: true, showMessage:message });
223         }
224
225         showFailAlert(message) {
226                 this.setState ({ showFailAlert: true, showMessage:message });
227         }
228  
229         disableAlert() {
230                 this.setState ({ showSucAlert: false, showFailAlert: 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="/ManageDictionaries" render={(routeProps) => (<ManageDictionaries {...routeProps} />)} />
259                                 <Route path="/policyModal/:policyInstanceType/:policyName" render={(routeProps) => (<PolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
260                                 <Route path="/createLoop" render={(routeProps) => (<CreateLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
261                                 <Route path="/openLoop" render={(routeProps) => (<OpenLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
262                                 <Route path="/loopProperties" render={(routeProps) => (<LoopPropertiesModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
263                                 <Route path="/modifyLoop" render={(routeProps) => (<ModifyLoopModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
264
265                                 <Route path="/userInfo" render={(routeProps) => (<UserInfoModal {...routeProps} />)} />
266                                 <Route path="/closeLoop" render={this.closeLoop} />
267                                 <Route path="/submit" render={(routeProps) => (<PerformAction {...routeProps} loopAction="submit" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
268                                 <Route path="/stop" render={(routeProps) => (<PerformAction {...routeProps} loopAction="stop" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
269                                 <Route path="/restart" render={(routeProps) => (<PerformAction {...routeProps} loopAction="restart" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
270                                 <Route path="/delete" render={(routeProps) => (<PerformAction {...routeProps} loopAction="delete" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
271                                 <Route path="/undeploy" render={(routeProps) => (<PerformAction {...routeProps} loopAction="undeploy" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
272                                 <Route path="/deploy" render={(routeProps) => (<DeployLoopModal {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
273                                 <Route path="/refreshStatus" render={(routeProps) => (<RefreshStatus {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showSucAlert={this.showSucAlert} showFailAlert={this.showFailAlert}/>)} />
274                                 <GlobalClampStyle />
275                                         {this.renderAlertBar()}
276                                         {this.renderNavBar()}
277                                         {this.renderLoopViewer()}
278                                 </StyledMainDiv>
279                 );
280         }
281 }