Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / launchpad / src / topologyL2View / topologyL2Store.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 import TopologyL2Actions from './topologyL2Actions.js';
19 import TopologyL2Source from './topologyL2Source.js';
20 import Alt from '../alt';
21 let rw = require('utils/rw.js');
22 class TopologyL2Store {
23 constructor() {
24 var self = this;
25 // initial state
26 this.isLoading = true;
27 this.topologyData = {
28 nodes: [],
29 links: [],
30 network_ids: []
31 };
32 this.errorMessage = null;
33 this.socket = null;
34 this.detailView = null;
35
36 this.bindActions(TopologyL2Actions);
37 // bind source listeners
38 this.exportAsync(TopologyL2Source);
39 this.exportPublicMethods({
40 closeSocket: this.closeSocket,
41 getTopologyData: this.getTopologyData
42 });
43 this.ajax_mode = rw.getSearchParams(window.location).ajax_mode || false;
44 }
45
46 getTopologyData = (id) => {
47 if (this.ajax_mode) {
48 this.getInstance().fetchTopology();
49 } else {
50 this.getInstance().openTopologyApiSocket(id);
51 }
52 }
53 openTopologyApiSocketLoading() {}
54 openTopologyApiSocketSuccess = (connection) => {
55 let self = this;
56
57 let connectionManager = (type, connection) => {
58 let ws = window.multiplexer.channel(connection);
59 if (!connection) {
60 console.warn('There was an issue connecting to the ' + type + ' socket');
61 return;
62 }
63 if (self.socket) {
64 self.closeSocket();
65 }
66 self.setState({
67 socket: ws.ws,
68 channelId: connection
69 });
70 ws.onmessage = function(data) {
71 self.setState({
72 topologyData: JSON.parse(data.data),
73 isLoading: false,
74 });
75 };
76 }
77
78 connectionManager('foo-type', connection);
79
80 }
81 openTopologyApiSocketError() {}
82
83 handleLogout = () => {
84 this.closeSocket();
85 }
86
87 closeSocket = () => {
88 if (this.socket) {
89 window.multiplexer.channel(this.channelId).close();
90 }
91 this.setState({
92 socket: null
93 });
94
95 this.detailView = null;
96 this.hasSelected = false;
97
98 this.bindListeners({
99 getTopologyApiSuccess: TopologyL2Actions.GET_TOPOLOGY_API_SUCCESS,
100 getTopologyApiLoading: TopologyL2Actions.GET_TOPOLOGY_API_LOADING,
101 getTopologyApiError: TopologyL2Actions.GET_TOPOLOGY_API_ERROR
102 });
103 // bind source listeners
104 this.exportAsync(TopologyL2Source);
105 }
106
107 getTopologyApiSuccess = (data) => {
108 this.setState({
109 topologyData: data,
110 errorMessage: null
111 });
112 }
113
114 getTopologyApiLoading = () => {}
115
116 getTopologyApiError = (errorMessage) => {
117 this.errorMessage = errorMessage;
118 }
119
120 getNodeData = (node_id) => {
121 // find node in thisa.topologyData.nodes
122 var node_data = this.topologyData.nodes.find(
123 function(element, index, array) {
124 return (element.id == node_id);
125 });
126 return node_data;
127 }
128
129 nodeClicked = (node_id) => {
130 this.setState({
131 detailData: this.getNodeData(node_id)
132 });
133 }
134 }
135 export default Alt.createStore(TopologyL2Store);