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 _
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
;
29 class SkyquakeContainerStore
{
31 this.currentPlugin
= getCurrentPlugin();
33 this.notifications
= [];
35 //Notification defaults
36 this.notificationMessage
= '';
37 this.displayNotification
= false;
38 this.notificationType
= 'error';
39 //Screen Loader default
40 this.displayScreenLoader
= false;
41 this.bindActions(SkyquakeContainerActions
);
42 this.exportAsync(SkyquakeContainerSource
);
45 this.exportPublicMethods({
46 // getNav: this.getNav
50 getSkyquakeNavSuccess
= (data
) => {
53 nav
: decorateAndTransformNav(data
, self
.currentPlugin
)
59 window
.multiplexer
.channel(this.channelId
).close();
65 //Remove once logging plugin is implemented
66 getSysLogViewerURLSuccess(data
){
67 window
.open(data
.url
);
69 getSysLogViewerURLError(data
){
70 console
.log('failed', data
)
73 openNotificationsSocketLoading
= () => {
79 openNotificationsSocketSuccess
= (data
) => {
82 let connection
= data
.connection
;
83 let streamSource
= data
.streamSource
;
84 console
.log('Success opening notification socket for stream ', streamSource
);
86 let ws
= window
.multiplexer
.channel(connection
);
88 if (!connection
) return;
95 ws
.onmessage
= (socket
) => {
97 var data
= JSON
.parse(socket
.data
);
98 if (!data
.notification
) {
99 console
.warn('No notification in the received payload: ', data
);
101 // Temp to test before adding multi-sources
102 data
.notification
.source
= streamSource
;
103 if (_
.indexOf(self
.notifications
, data
.notification
) == -1) {
104 // newly appreared event.
105 // Add to the notifications list and setState
106 self
.notifications
.unshift(data
.notification
);
108 newNotificationEvent
: true,
109 newNotificationMsg
: data
.notification
,
110 notifications
: self
.notifications
,
116 console
.log('Error in parsing data on notification socket');
125 openNotificationsSocketError
= (data
) => {
126 console
.log('Error opening notification socket', data
);
129 getEventStreamsLoading
= () => {
135 getEventStreamsSuccess
= (streams
) => {
136 console
.log('Found streams: ', streams
);
139 streams
['ietf-restconf-monitoring:streams'] &&
140 streams
['ietf-restconf-monitoring:streams']['stream'] &&
141 streams
['ietf-restconf-monitoring:streams']['stream'].map((stream
) => {
142 stream
['access'] && stream
['access'].map((streamLocation
) => {
143 if (streamLocation
['encoding'] == 'ws_json') {
145 self
.getInstance().openNotificationsSocket(streamLocation
['location'], stream
['name']);
157 getEventStreamsError
= (error
) => {
158 console
.log('Failed to get streams object');
165 showNotification
= (data
) => {
167 displayNotification
: true,
168 notificationMessage
: data
,
169 notificationType
: 'error',
170 displayScreenLoader
: false
172 if(typeof(data
) == 'string') {
175 state
.notificationMessage
= data
.msg
;
177 state
.notificationType
= data
.type
;
180 this.setState(state
);
182 hideNotification
= () => {
184 displayNotification
: false
188 showScreenLoader
= () => {
190 displayScreenLoader
: true
193 hideScreenLoader
= () => {
195 displayScreenLoader
: false
202 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
203 * @param {object} nav Nav item from /nav endoingpoint
204 * @param {string} currentPlugin Current plugin name taken from url path.
205 * @return {array} Returns list of constructed nav items.
207 function decorateAndTransformNav(nav
, currentPlugin
) {
208 for ( let k
in nav
) {
209 nav
[k
].pluginName
= k
;
210 if (k
!= currentPlugin
) {
211 nav
[k
].routes
.map(function(route
, i
) {
213 route
.route
= '/' + k
+ '/index.html?api_server=' + API_SERVER
+ '&upload_server=' + UPLOAD_SERVER
+ '#' + route
.route
;
215 route
.route
= '/' + k
+ '/#' + route
.route
;
217 route
.isExternal
= true;
224 function getCurrentPlugin() {
225 var paths
= window
.location
.pathname
.split('/');
226 var currentPath
= null;
227 if (paths
[0] != "") {
228 currentPath
= paths
[0]
230 currentPath
= paths
[1];
232 if (currentPath
!= null) {
235 console
.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
239 export default Alt
.createStore(SkyquakeContainerStore
);