Rift-15726 compress code in production environment
[osm/UI.git] / skyquake / plugins / launchpad / src / topologyView / topologyStore.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 TopologyActions from './topologyActions.js';
19 import TopologySource from './topologySource.js';
20 // import source
21 import Alt from '../alt';
22 let rw = require('utils/rw.js');
23 class TopologyStore {
24 constructor() {
25 var self = this;
26 // initial state
27 this.isLoading = true;
28 this.topologyData = {};
29 this.socket = null;
30 this.detailView = null;
31 this.hasSelected = false;
32 // bind action listeners
33 this.bindActions(TopologyActions);
34
35 // bind source listeners
36 this.exportAsync(TopologySource);
37 this.exportPublicMethods({
38 selectNode: this.selectNode,
39 closeSocket: this.closeSocket,
40 getTopologyData: this.getTopologyData
41 })
42 this.ajax_mode = rw.getSearchParams(window.location).ajax_mode || false;
43 }
44 selectNode = (node) => {
45 var apiType = {
46 'nsr' : 'getRawNSR',
47 'vdur' : 'getRawVDUR',
48 'vnfr': 'getRawVNFR'
49 }
50 // TODO: VISIT
51 apiType[node.type] && this.getInstance()[apiType[node.type]](node.id, node.parent ? node.parent.id : undefined);
52 }
53 getRawSuccess = (data) => {
54 this.setState({
55 detailView: data
56 });
57 }
58 getRawLoading = () => {
59
60 }
61 getRawError = () => {
62
63 }
64
65 getTopologyData = (nsr_id) => {
66 if (this.ajax_mode) {
67 this.getInstance().getNSRTopology(nsr_id);
68 } else {
69 this.getInstance().openNSRTopologySocket(nsr_id);
70 }
71 }
72
73 openNSRTopologySocketLoading = () => {
74 console.log('loading')
75 }
76 openNSRTopologySocketSuccess = (connection) => {
77 let self = this;
78
79 let connectionManager = (type, connection) => {
80 let ws = window.multiplexer.channel(connection);
81 if (!connection) {
82 console.warn('There was an issue connecting to the ' + type + ' socket');
83 return;
84 }
85 if (self.socket) {
86 self.closeSocket();
87 }
88
89 self.setState({
90 socket: ws.ws,
91 channelId: connection
92 });
93 ws.onmessage = function(data) {
94 var tData = JSON.parse(data.data);
95 var newState = {
96 topologyData: tData,
97 isLoading: false
98 };
99 if(!self.hasSelected) {
100 newState.hasSelected = true;
101 self.selectNode(tData);
102 }
103 self.setState(newState);
104
105 };
106 }
107
108 connectionManager('nsr', connection);
109 }
110 openNSRTopologySocketError = () => {
111 console.log('error')
112 }
113 handleLogout = () => {
114 this.closeSocket();
115 }
116 closeSocket = () => {
117 if (this.socket) {
118 window.multiplexer.channel(this.channelId).close();
119 }
120 this.setState({
121 socket: null
122 })
123 }
124 getNSRTopologySuccess = (data) => {
125 this.setState({
126 topologyData: data,
127 errorMessage: null,
128 isLoading: false
129 });
130 }
131 getNSRTopologyLoading = () => {}
132 getNSRTopologyError = (errorMessage) => {
133 console.log('error', errorMessage)
134 //this.errorMessage = errorMessage;
135 }
136
137 }
138 export default Alt.createStore(TopologyStore, 'TopologyStore');