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 //Screen Loader default
39 this.displayScreenLoader
= false;
40 this.bindActions(SkyquakeContainerActions
);
41 this.exportAsync(SkyquakeContainerSource
);
44 this.exportPublicMethods({
45 // getNav: this.getNav
49 getSkyquakeNavSuccess
= (data
) => {
52 nav
: decorateAndTransformNav(data
, self
.currentPlugin
)
58 window
.multiplexer
.channel(this.channelId
).close();
64 //Remove once logging plugin is implemented
65 getSysLogViewerURLSuccess(data
){
66 window
.open(data
.url
);
68 getSysLogViewerURLError(data
){
69 console
.log('failed', data
)
72 openNotificationsSocketLoading
= () => {
78 openNotificationsSocketSuccess
= (data
) => {
81 let connection
= data
.connection
;
82 let streamSource
= data
.streamSource
;
83 console
.log('Success opening notification socket for stream ', streamSource
);
85 let ws
= window
.multiplexer
.channel(connection
);
87 if (!connection
) return;
94 ws
.onmessage
= (socket
) => {
96 var data
= JSON
.parse(socket
.data
);
97 if (!data
.notification
) {
98 console
.warn('No notification in the received payload: ', data
);
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
);
107 newNotificationEvent
: true,
108 newNotificationMsg
: data
.notification
,
109 notifications
: self
.notifications
,
115 console
.log('Error in parsing data on notification socket');
124 openNotificationsSocketError
= (data
) => {
125 console
.log('Error opening notification socket', data
);
128 getEventStreamsLoading
= () => {
134 getEventStreamsSuccess
= (streams
) => {
135 console
.log('Found streams: ', streams
);
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') {
144 self
.getInstance().openNotificationsSocket(streamLocation
['location'], stream
['name']);
156 getEventStreamsError
= (error
) => {
157 console
.log('Failed to get streams object');
164 showNotification
= (data
) => {
165 if(typeof(data
) == 'string') {
167 displayNotification
: true,
168 notificationMessage
: data
171 if(data
.type
== 'error') {
173 displayNotification
: true,
174 notificationMessage
: data
.msg
,
175 displayScreenLoader
: false
180 hideNotification
= () => {
182 displayNotification
: false
186 showScreenLoader
= () => {
188 displayScreenLoader
: true
191 hideScreenLoader
= () => {
193 displayScreenLoader
: false
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.
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
) {
211 route
.route
= '/' + k
+ '/index.html?api_server=' + API_SERVER
+ '&upload_server=' + UPLOAD_SERVER
+ '#' + route
.route
;
213 route
.route
= '/' + k
+ '/#' + route
.route
;
215 route
.isExternal
= true;
222 function getCurrentPlugin() {
223 var paths
= window
.location
.pathname
.split('/');
224 var currentPath
= null;
225 if (paths
[0] != "") {
226 currentPath
= paths
[0]
228 currentPath
= paths
[1];
230 if (currentPath
!= null) {
233 console
.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
237 export default Alt
.createStore(SkyquakeContainerStore
);