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