fe4a7b059c16be508b1ee75d6ef9b601df10668a
[osm/UI.git] / skyquake / framework / widgets / skyquake_container / skyquakeContainerStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 //This will reach out to the global routes endpoint
19
20 import Alt from './skyquakeAltInstance.js';
21 import SkyquakeContainerSource from './skyquakeContainerSource.js';
22 import SkyquakeContainerActions from './skyquakeContainerActions';
23 import _ from 'lodash';
24 //Temporary, until api server is on same port as webserver
25 var rw = require('utils/rw.js');
26 var API_SERVER = rw.getSearchParams(window.location).api_server;
27 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
28
29 class SkyquakeContainerStore {
30 constructor() {
31 this.currentPlugin = getCurrentPlugin();
32 this.nav = {};
33 this.notifications = [];
34 this.socket = null;
35 //Notification defaults
36 this.notificationMessage = '';
37 this.displayNotification = false;
38 //Screen Loader default
39 this.displayScreenLoader = false;
40 this.bindActions(SkyquakeContainerActions);
41 this.exportAsync(SkyquakeContainerSource);
42
43
44 this.exportPublicMethods({
45 // getNav: this.getNav
46 });
47
48 }
49 getSkyquakeNavSuccess = (data) => {
50 var self = this;
51 this.setState({
52 nav: decorateAndTransformNav(data, self.currentPlugin)
53 })
54 }
55
56 closeSocket = () => {
57 if (this.socket) {
58 window.multiplexer.channel(this.channelId).close();
59 }
60 this.setState({
61 socket: null
62 });
63 }
64 //Remove once logging plugin is implemented
65 getSysLogViewerURLSuccess(data){
66 window.open(data.url);
67 }
68 getSysLogViewerURLError(data){
69 console.log('failed', data)
70 }
71
72 openNotificationsSocketLoading = () => {
73 this.setState({
74 isLoading: true
75 })
76 }
77
78 openNotificationsSocketSuccess = (data) => {
79 var self = this;
80
81 let connection = data.connection;
82 let streamSource = data.streamSource;
83 console.log('Success opening notification socket for stream ', streamSource);
84
85 let ws = window.multiplexer.channel(connection);
86
87 if (!connection) return;
88 self.setState({
89 socket: ws.ws,
90 isLoading: false,
91 channelId: connection
92 });
93
94 ws.onmessage = (socket) => {
95 try {
96 var data = JSON.parse(socket.data);
97 if (!data.notification) {
98 console.warn('No notification in the received payload: ', data);
99 } else {
100 // Temp to test before adding multi-sources
101 data.notification.source = streamSource;
102 if (_.indexOf(self.notifications, data.notification) == -1) {
103 // newly appreared event.
104 // Add to the notifications list and setState
105 self.notifications.unshift(data.notification);
106 self.setState({
107 newNotificationEvent: true,
108 newNotificationMsg: data.notification,
109 notifications: self.notifications,
110 isLoading: false
111 });
112 }
113 }
114 } catch(e) {
115 console.log('Error in parsing data on notification socket');
116 }
117 };
118
119 ws.onclose = () => {
120 self.closeSocket();
121 };
122 }
123
124 openNotificationsSocketError = (data) => {
125 console.log('Error opening notification socket', data);
126 }
127
128 getEventStreamsLoading = () => {
129 this.setState({
130 isLoading: true
131 });
132 }
133
134 getEventStreamsSuccess = (streams) => {
135 console.log('Found streams: ', streams);
136 let self = this;
137
138 streams['ietf-restconf-monitoring:streams'] &&
139 streams['ietf-restconf-monitoring:streams']['stream'] &&
140 streams['ietf-restconf-monitoring:streams']['stream'].map((stream) => {
141 stream['access'] && stream['access'].map((streamLocation) => {
142 if (streamLocation['encoding'] == 'ws_json') {
143 setTimeout(() => {
144 self.getInstance().openNotificationsSocket(streamLocation['location'], stream['name']);
145 }, 0);
146 }
147 })
148 })
149
150 this.setState({
151 isLoading: true,
152 streams: streams
153 })
154 }
155
156 getEventStreamsError = (error) => {
157 console.log('Failed to get streams object');
158 this.setState({
159 isLoading: false
160 })
161 }
162
163 //Notifications
164 showNotification = (data) => {
165 if(typeof(data) == 'string') {
166 this.setState({
167 displayNotification: true,
168 notificationMessage: data
169 });
170 } else {
171 if(data.type == 'error') {
172 this.setState({
173 displayNotification: true,
174 notificationMessage: data.msg,
175 displayScreenLoader: false
176 });
177 }
178 }
179 }
180 hideNotification = () => {
181 this.setState({
182 displayNotification: false
183 })
184 }
185 //ScreenLoader
186 showScreenLoader = () => {
187 this.setState({
188 displayScreenLoader: true
189 });
190 }
191 hideScreenLoader = () => {
192 this.setState({
193 displayScreenLoader: false
194 })
195 }
196
197 }
198
199 /**
200 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
201 * @param {object} nav Nav item from /nav endoingpoint
202 * @param {string} currentPlugin Current plugin name taken from url path.
203 * @return {array} Returns list of constructed nav items.
204 */
205 function decorateAndTransformNav(nav, currentPlugin) {
206 for ( let k in nav) {
207 nav[k].pluginName = k;
208 if (k != currentPlugin) {
209 nav[k].routes.map(function(route, i) {
210 if (API_SERVER) {
211 route.route = '/' + k + '/index.html?api_server=' + API_SERVER + '&upload_server=' + UPLOAD_SERVER + '#' + route.route;
212 } else {
213 route.route = '/' + k + '/#' + route.route;
214 }
215 route.isExternal = true;
216 })
217 }
218 }
219 return nav;
220 }
221
222 function getCurrentPlugin() {
223 var paths = window.location.pathname.split('/');
224 var currentPath = null;
225 if (paths[0] != "") {
226 currentPath = paths[0]
227 } else {
228 currentPath = paths[1];
229 }
230 if (currentPath != null) {
231 return currentPath;
232 } else {
233 console.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
234 }
235 }
236
237 export default Alt.createStore(SkyquakeContainerStore);