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