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