b64cfbaa384713269e7362ade7985db98d4fffe2
[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
37 import { Route } from 'react-router-dom'
38 import OpenLoopModal from './components/dialogs/OpenLoop/OpenLoopModal';
39 import OperationalPolicyModal from './components/dialogs/OperationalPolicy/OperationalPolicyModal';
40 import ConfigurationPolicyModal from './components/dialogs/ConfigurationPolicy/ConfigurationPolicyModal';
41 import LoopProperties from './components/dialogs/LoopProperties';
42 import UserInfo from './components/dialogs/UserInfo';
43 import LoopService from './api/LoopService';
44 import PerformAction from './components/dialogs/PerformActions';
45 import RefreshStatus from './components/dialogs/RefreshStatus';
46 import DeployLoop from './components/dialogs/DeployLoop';
47
48 const ProjectNameStyled = styled.a`
49         vertical-align: middle;
50         padding-left: 30px;
51         font-size: 30px;
52
53 `
54 const LoopViewDivStyled = styled.div`
55         height: 100%;
56         overflow: hidden;
57         margin-left: 10px;
58         margin-right: 10px;
59         margin-bottom: 10px;
60         color: ${props => props.theme.loopViewerFontColor};
61         background-color: ${props => props.theme.loopViewerBackgroundColor};
62         border: 1px solid transparent;
63         border-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
64 `
65
66 const LoopViewHeaderDivStyled = styled.div`
67         background-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
68         padding: 10px 10px;
69         color: ${props => props.theme.loopViewerHeaderFontColor};
70 `
71
72 const LoopViewBodyDivStyled = styled.div`
73         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
74         padding: 10px 10px;
75         color: ${props => (props.theme.loopViewerHeaderFontColor)};
76         height: 95%;
77 `
78
79 export default class LoopUI extends React.Component {
80
81         static defaultLoopName="Empty (NO loop loaded yet)";
82
83         state = {
84                 userName: null,
85                 loopName: LoopUI.defaultLoopName,
86                 loopCache: new LoopCache({})
87         };
88
89         constructor() {
90                 super();
91                 this.getUser = this.getUser.bind(this);
92                 this.updateLoopCache = this.updateLoopCache.bind(this);
93                 this.loadLoop = this.loadLoop.bind(this);
94                 this.closeLoop = this.closeLoop.bind(this);
95         }
96
97         componentWillMount() {
98                 this.getUser();
99         }
100
101         getUser() {
102                 UserService.login().then(user => {
103                         this.setState({ userName: user })
104                 });
105         }
106
107         renderMenuNavBar() {
108                 return (
109                         <MenuBar loopName={this.state.loopName}/>
110                 );
111         }
112
113         renderUserLoggedNavBar() {
114                 return (
115                         <Navbar.Text>
116                                 Signed in as: <a href="/login">{this.state.userName}</a>
117                         </Navbar.Text>
118                 );
119         }
120
121         renderLogoNavBar() {
122                 return (
123                         <Navbar.Brand>
124                                 <img height="50px" width="234px" src={logo} alt="" />
125                                 <ProjectNameStyled>CLAMP</ProjectNameStyled>
126                         </Navbar.Brand>
127                 );
128         }
129
130         renderNavBar() {
131                 return (
132                         <Navbar expand="lg">
133                                 {this.renderLogoNavBar()}
134                                 {this.renderMenuNavBar()}
135                                 {this.renderUserLoggedNavBar()}
136                         </Navbar>
137                 );
138         }
139
140         renderLoopViewHeader() {
141                 return (
142                         <LoopViewHeaderDivStyled>
143                                 Loop Viewer - {this.state.loopName}
144                         </LoopViewHeaderDivStyled>
145                 );
146         }
147
148         renderLoopViewBody() {
149                 return (
150                         <LoopViewBodyDivStyled>
151                                 <LoopSvg loopCache={this.state.loopCache} />
152                                 <LoopStatus loopCache={this.state.loopCache}/>
153                                 <LoopLogs loopCache={this.state.loopCache} />
154                         </LoopViewBodyDivStyled>
155                 );
156         }
157
158         getLoopCache() {
159                 return this.state.loopCache;
160
161         }
162
163         renderLoopViewer() {
164                 return (
165                         <LoopViewDivStyled>
166                                 {this.renderLoopViewHeader()}
167                                 {this.renderLoopViewBody()}
168                         </LoopViewDivStyled>
169                 );
170         }
171
172         updateLoopCache(loopJson) {
173                 this.setState({ loopCache: new LoopCache(loopJson), loopName: this.state.loopCache.getLoopName() });
174                 console.info(this.state.loopName+" loop loaded successfully");
175         }
176
177         loadLoop(loopName) {
178                 LoopService.getLoop(loopName).then(loop => {
179                         console.debug("Updating loopCache");
180                         this.updateLoopCache(loop);
181                 });
182         }
183
184         closeLoop() {
185                 this.setState({ loopCache: new LoopCache({}), loopName: LoopUI.defaultLoopName });
186                 this.props.history.push('/');
187         }
188         
189  render() {
190                 return (
191                                 <div id="main_div">
192                                 <Route path="/operationalPolicyModal"
193                                         render={(routeProps) => (<OperationalPolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
194                                 <Route path="/configurationPolicyModal/:componentName" render={(routeProps) => (<ConfigurationPolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
195                                 <Route path="/openLoop" render={(routeProps) => (<OpenLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
196                                 <Route path="/loopProperties" render={(routeProps) => (<LoopProperties {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
197                                 <Route path="/userInfo" render={(routeProps) => (<UserInfo {...routeProps} />)} />
198                                 <Route path="/closeLoop" render={this.closeLoop} />
199                                 <Route path="/submit" render={(routeProps) => (<PerformAction {...routeProps} loopAction="submit" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
200                                 <Route path="/stop" render={(routeProps) => (<PerformAction {...routeProps} loopAction="stop" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
201                                 <Route path="/restart" render={(routeProps) => (<PerformAction {...routeProps} loopAction="restart" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
202                                 <Route path="/delete" render={(routeProps) => (<PerformAction {...routeProps} loopAction="delete" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
203                                 <Route path="/undeploy" render={(routeProps) => (<PerformAction {...routeProps} loopAction="undeploy" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
204                                 <Route path="/deploy" render={(routeProps) => (<DeployLoop {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
205                                 <Route path="/refreshStatus" render={(routeProps) => (<RefreshStatus {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache}/>)} />
206                                         <GlobalClampStyle />
207                                         {this.renderNavBar()}
208                                         {this.renderLoopViewer()}
209                                 </div>
210                 );
211         }
212 }