Merge "Rework tosca converter"
[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 LoopSvg from './components/loop_viewer/svg/LoopSvg';
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                 showAlert: false
113         };
114
115         constructor() {
116                 super();
117                 this.getUser = this.getUser.bind(this);
118                 this.logout = this.logout.bind(this);
119                 this.updateLoopCache = this.updateLoopCache.bind(this);
120                 this.loadLoop = this.loadLoop.bind(this);
121                 this.closeLoop = this.closeLoop.bind(this);
122                 this.showAlert =  this.showAlert.bind(this);
123                 this.disableAlert =  this.disableAlert.bind(this);
124         }
125
126         componentWillMount() {
127                 this.getUser();
128         }
129
130         getUser() {
131                 UserService.login().then(user => {
132                         this.setState({ userName: user })
133                 });
134         }
135         
136         logout() {
137                 UserService.logout().then(user => {
138                         this.setState({ userName: user });
139                         window.location.reload();
140                 });
141                 
142         }
143
144         renderMenuNavBar() {
145                 return (
146                         <MenuBar loopName={this.state.loopName}/>
147                 );
148         }
149
150         renderUserLoggedNavBar() {
151                 return (
152                         <Navbar.Text>
153                         <StyledLoginInfo>Signed in as: </StyledLoginInfo>
154                                 <StyledRouterLink to="/userInfo">{this.state.userName}</StyledRouterLink>
155                                 <StyledRouterLink to="/logout/"> (logout)</StyledRouterLink>
156                         </Navbar.Text>
157                 );
158         }
159
160         renderLogoNavBar() {
161                 return (
162                         <Navbar.Brand>
163                                 <img height="50px" width="234px" src={logo} alt="" />
164                                 <ProjectNameStyled>CLAMP</ProjectNameStyled>
165                         </Navbar.Brand>
166                 );
167         }
168
169         renderAlertBar() {
170                 return (
171                                 <Alert variant="danger" show={this.state.showAlert} onClose={this.disableAlert} dismissible>
172                                         {this.state.showMessage}
173                                 </Alert>
174                 );
175         }
176
177         renderNavBar() {
178                 return (
179                         <Navbar >
180                                 {this.renderLogoNavBar()}
181                                 <Navbar.Toggle aria-controls="responsive-navbar-nav" />
182                                 {this.renderMenuNavBar()}
183                                 {this.renderUserLoggedNavBar()}
184                         </Navbar>
185                 );
186         }
187
188         renderLoopViewHeader() {
189                 return (
190                         <LoopViewHeaderDivStyled>
191                                 Loop Viewer - {this.state.loopName}
192                         </LoopViewHeaderDivStyled>
193                 );
194         }
195
196         renderLoopViewBody() {
197                 return (
198                         <LoopViewBodyDivStyled>
199                                 <LoopSvg loopCache={this.state.loopCache} />
200                                 <LoopStatus loopCache={this.state.loopCache}/>
201                                 <LoopLogs loopCache={this.state.loopCache} />
202                         </LoopViewBodyDivStyled>
203                 );
204         }
205
206         getLoopCache() {
207                 return this.state.loopCache;
208
209         }
210
211         renderLoopViewer() {
212                 return (
213                         <LoopViewDivStyled>
214                                 {this.renderLoopViewHeader()}
215                                 {this.renderLoopViewBody()}
216                         </LoopViewDivStyled>
217                 );
218         }
219
220         updateLoopCache(loopJson) {
221                 this.setState({ loopCache: new LoopCache(loopJson) });
222                 this.setState({ loopName: this.state.loopCache.getLoopName() });
223                 console.info(this.state.loopName+" loop loaded successfully");
224         }
225
226         showAlert(message) {
227                 this.setState ({ showAlert: true, showMessage:message });
228         }
229
230         disableAlert() {
231                 this.setState ({ showAlert: false });
232         }
233
234         loadLoop(loopName) {
235                 LoopService.getLoop(loopName).then(loop => {
236                         console.debug("Updating loopCache");
237                         LoopActionService.refreshStatus(loopName).then(data => {
238                                 this.updateLoopCache(data);
239                                 this.props.history.push('/');
240                         })
241                         .catch(error => {
242                                 this.updateLoopCache(loop);
243                                 this.props.history.push('/');
244                         });
245                 });
246         }
247
248         closeLoop() {
249                 this.setState({ loopCache: new LoopCache({}), loopName: OnapConstants.defaultLoopName });
250                 this.props.history.push('/');
251         }
252
253         render() {
254                 return (
255                                 <StyledMainDiv id="main_div">
256                                 <Route path="/uploadToscaPolicyModal" render={(routeProps) => (<UploadToscaPolicyModal {...routeProps} />)} />
257                                 <Route path="/viewToscaPolicyModal" render={(routeProps) => (<ViewToscaPolicyModal {...routeProps} />)} />
258                                 <Route path="/ViewLoopTemplatesModal" render={(routeProps) => (<ViewLoopTemplatesModal {...routeProps} />)} />
259                                 <Route path="/ManageDictionaries" render={(routeProps) => (<ManageDictionaries {...routeProps} />)} />
260                                 <Route path="/operationalPolicyModal"
261                                         render={(routeProps) => (<OperationalPolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
262                                 <Route path="/policyModal/:policyInstanceType/:policyName" render={(routeProps) => (<PolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
263                                 <Route path="/configurationPolicyModal/:policyName" render={(routeProps) => (<ConfigurationPolicyModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
264                                 <Route path="/createLoop" render={(routeProps) => (<CreateLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
265                                 <Route path="/openLoop" render={(routeProps) => (<OpenLoopModal {...routeProps} loadLoopFunction={this.loadLoop} />)} />
266                                 <Route path="/loopProperties" render={(routeProps) => (<LoopPropertiesModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
267                                 <Route path="/modifyLoop" render={(routeProps) => (<ModifyLoopModal {...routeProps} loopCache={this.getLoopCache()} loadLoopFunction={this.loadLoop}/>)} />
268
269                                 <Route path="/userInfo" render={(routeProps) => (<UserInfoModal {...routeProps} />)} />
270                                 <Route path="/closeLoop" render={this.closeLoop} />
271                                 <Route path="/submit" render={(routeProps) => (<PerformAction {...routeProps} loopAction="submit" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
272                                 <Route path="/stop" render={(routeProps) => (<PerformAction {...routeProps} loopAction="stop" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
273                                 <Route path="/restart" render={(routeProps) => (<PerformAction {...routeProps} loopAction="restart" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
274                                 <Route path="/delete" render={(routeProps) => (<PerformAction {...routeProps} loopAction="delete" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
275                                 <Route path="/undeploy" render={(routeProps) => (<PerformAction {...routeProps} loopAction="undeploy" loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
276                                 <Route path="/deploy" render={(routeProps) => (<DeployLoopModal {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
277                                 <Route path="/refreshStatus" render={(routeProps) => (<RefreshStatus {...routeProps} loopCache={this.getLoopCache()} updateLoopFunction={this.updateLoopCache} showAlert={this.showAlert}/>)} />
278                                 <Route path="/logout" render={this.logout} />
279                                 <GlobalClampStyle />
280                                         {this.renderAlertBar()}
281                                         {this.renderNavBar()}
282                                         {this.renderLoopViewer()}
283                                 </StyledMainDiv>
284                 );
285         }
286 }