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