3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 //This will reach out to the global routes endpoint
20 import Alt
from './skyquakeAltInstance.js';
21 import SkyquakeContainerSource
from './skyquakeContainerSource.js';
22 import SkyquakeContainerActions
from './skyquakeContainerActions';
23 import _indexOf
from 'lodash/indexOf';
24 //Temporary, until api server is on same port as webserver
25 import rw
from 'utils/rw.js';
27 var API_SERVER
= rw
.getSearchParams(window
.location
).api_server
;
28 var UPLOAD_SERVER
= rw
.getSearchParams(window
.location
).upload_server
;
30 class SkyquakeContainerStore
{
32 this.currentPlugin
= getCurrentPlugin();
34 this.notifications
= [];
36 //Notification defaults
37 this.notificationMessage
= '';
38 this.displayNotification
= false;
39 this.notificationType
= 'error';
40 //Screen Loader default
41 this.displayScreenLoader
= false;
42 this.bindActions(SkyquakeContainerActions
);
43 this.exportAsync(SkyquakeContainerSource
);
46 this.exportPublicMethods({
47 // getNav: this.getNav
51 getSkyquakeNavSuccess
= (data
) => {
54 nav
: decorateAndTransformNav(data
, self
.currentPlugin
)
60 window
.multiplexer
.channel(this.channelId
).close();
66 //Remove once logging plugin is implemented
67 getSysLogViewerURLSuccess(data
){
68 window
.open(data
.url
);
70 getSysLogViewerURLError(data
){
71 console
.log('failed', data
)
74 openNotificationsSocketLoading
= () => {
80 openNotificationsSocketSuccess
= (data
) => {
83 let connection
= data
.connection
;
84 let streamSource
= data
.streamSource
;
85 console
.log('Success opening notification socket for stream ', streamSource
);
87 let ws
= window
.multiplexer
.channel(connection
);
89 if (!connection
) return;
96 ws
.onmessage
= (socket
) => {
98 var data
= JSON
.parse(socket
.data
);
99 if (!data
.notification
) {
100 console
.warn('No notification in the received payload: ', data
);
102 // Temp to test before adding multi-sources
103 data
.notification
.source
= streamSource
;
104 if (_indexOf(self
.notifications
, data
.notification
) == -1) {
105 // newly appreared event.
106 // Add to the notifications list and setState
107 self
.notifications
.unshift(data
.notification
);
109 newNotificationEvent
: true,
110 newNotificationMsg
: data
.notification
,
111 notifications
: self
.notifications
,
117 console
.log('Error in parsing data on notification socket');
126 openNotificationsSocketError
= (data
) => {
127 console
.log('Error opening notification socket', data
);
130 getEventStreamsLoading
= () => {
136 getEventStreamsSuccess
= (streams
) => {
137 console
.log('Found streams: ', streams
);
140 streams
['ietf-restconf-monitoring:streams'] &&
141 streams
['ietf-restconf-monitoring:streams']['stream'] &&
142 streams
['ietf-restconf-monitoring:streams']['stream'].map((stream
) => {
143 stream
['access'] && stream
['access'].map((streamLocation
) => {
144 if (streamLocation
['encoding'] == 'ws_json') {
146 self
.getInstance().openNotificationsSocket(streamLocation
['location'], stream
['name']);
158 getEventStreamsError
= (error
) => {
159 console
.log('Failed to get streams object');
166 showNotification
= (data
) => {
168 displayNotification
: true,
169 notificationMessage
: data
,
170 notificationType
: 'error',
171 displayScreenLoader
: false
173 if(typeof(data
) == 'string') {
176 state
.notificationMessage
= data
.msg
;
178 state
.notificationType
= data
.type
;
181 this.setState(state
);
183 hideNotification
= () => {
185 displayNotification
: false
189 showScreenLoader
= () => {
191 displayScreenLoader
: true
194 hideScreenLoader
= () => {
196 displayScreenLoader
: false
203 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
204 * @param {object} nav Nav item from /nav endoingpoint
205 * @param {string} currentPlugin Current plugin name taken from url path.
206 * @return {array} Returns list of constructed nav items.
208 function decorateAndTransformNav(nav
, currentPlugin
) {
209 for ( let k
in nav
) {
210 nav
[k
].pluginName
= k
;
211 if (k
!= currentPlugin
) {
212 nav
[k
].routes
.map(function(route
, i
) {
214 route
.route
= '/' + k
+ '/index.html?api_server=' + API_SERVER
+ '&upload_server=' + UPLOAD_SERVER
+ '#' + route
.route
;
216 route
.route
= '/' + k
+ '/#' + route
.route
;
218 route
.isExternal
= true;
225 function getCurrentPlugin() {
226 var paths
= window
.location
.pathname
.split('/');
227 var currentPath
= null;
228 if (paths
[0] != "") {
229 currentPath
= paths
[0]
231 currentPath
= paths
[1];
233 if (currentPath
!= null) {
236 console
.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
240 export default Alt
.createStore(SkyquakeContainerStore
, 'SkyquakeContainerStore');