54c45e4a9f31a142963c9305a68debb0b5de82eb
[osm/UI.git] / skyquake / plugins / launchpad / src / launchpadFleetStore.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 import Alt from './alt';
20
21 var FleetSource = require('./launchpadFleetSource.js');
22 var FleetActions = require('./launchpadFleetActions.js');
23 import CardActions from './launchpad_card/launchpadCardActions.js';
24 var Utils = require('utils/utils.js');
25 import LaunchNetworkServiceSource from './instantiate/launchNetworkServiceSource.js';
26 import LaunchNetworkServiceActions from './instantiate/launchNetworkServiceActions.js';
27
28 import {LaunchpadSettings} from './settings.js';
29
30
31 var FleetStore;
32 var _ = require('underscore');
33 // _.debounce(function(){});
34 function FleetStoreConstructor() {
35 var self = this;
36 this.fleets = [];
37 this.descriptorCount = 0;
38 this.socket = null;
39 this.selectedSlaParam = '';
40 this.launchpads = [];
41 this.nsrs = [];
42 this.exportAsync(FleetSource);
43 this.exportAsync(LaunchNetworkServiceSource);
44 this.slideno = 0;
45 this.dropdownSlide = ['', 0];
46 this.slideChange = -1;
47 this.validateErrorEvent = 0;
48 this.launchpadSettings = new LaunchpadSettings();
49 this.openedNsrIDs = this.launchpadSettings.openedNSRs();
50 this.isNsListPanelVisible = true;
51 this.bindListeners({
52 //NEW
53 //Socket Actions
54 openNSRSocketLoading: FleetActions.openNSRSocketLoading,
55 openNSRSocketSuccess: FleetActions.openNSRSocketSuccess,
56 //Card Actions
57 handleUpdateControlInput: CardActions.updateControlInput,
58 //Source Actions
59 handleNsrControlSuccess: FleetActions.nsrControlSuccess,
60 handleNsrControlError: FleetActions.nsrControlError,
61 handleSlideNoStateChange: FleetActions.slideNoStateChange,
62 handleSlideNoStateChangeSuccess: FleetActions.slideNoStateChangeSuccess,
63 getNsrInstancesSuccess: FleetActions.getNsrInstancesSuccess,
64 getNsrInstancesError: FleetActions.getNsrInstancesError,
65 deleteNsrInstanceSuccess: FleetActions.deleteNsrInstanceSuccess,
66 deletingNSR: FleetActions.deletingNSR,
67 deleteNsrInstanceError: FleetActions.deleteNsrInstanceError,
68 setNSRStatusSuccess: FleetActions.setNSRStatusSuccess,
69 setNSRStatusError: FleetActions.setNSRStatusError,
70 validateReset: FleetActions.validateReset,
71 validateError: FleetActions.validateError,
72 //Launch Network Service Source Actions
73 getCatalogSuccess: LaunchNetworkServiceActions.getCatalogSuccess,
74 // Card management actions
75 openNsrCard: FleetActions.openNsrCard,
76 closeNsrCard: FleetActions.closeNsrCard,
77 instantiateNetworkService: FleetActions.instantiateNetworkService,
78 setNsListPanelVisible: FleetActions.setNsListPanelVisible,
79
80 });
81 this.bindAction(LaunchNetworkServiceActions.launchNSRSuccess, responseData => {
82 try {
83 this.openNsrCard(responseData.data.nsr_id);
84 } catch (e) {
85 console.log("Unable to open NS Card for response data: ", responseData);
86 }
87 });
88 this.exportPublicMethods({
89 getFleets: function() {
90 return this.getState().fleets;
91 },
92 closeSocket: this.closeSocket.bind(self)
93 });
94 }
95
96 FleetStoreConstructor.prototype.handleLogout = function() {
97 this.closeSocket();
98 }
99
100 FleetStoreConstructor.prototype.closeSocket = function() {
101 if(this.socket) {
102 window.multiplexer.channel(this.channelId).close();
103 }
104 this.setState({
105 socket:null
106 })
107 }
108
109 //NEW
110 FleetStoreConstructor.prototype.openNSRSocketLoading = function(connection) {
111 console.log('open socketloading')
112 Alt.actions.global.showScreenLoader.defer();
113 };
114 FleetStoreConstructor.prototype.openNSRSocketSuccess = function(connection) {
115 var self = this;
116 var isLoading = true;
117 var ws = window.multiplexer.channel(connection);
118 if (!connection) return;
119 self.setState({
120 socket: ws.ws,
121 channelId: connection
122 });
123 ws.onmessage = function(socket) {
124 try {
125 var data = JSON.parse(socket.data);
126 if (!data.nsrs) {
127 console.warn('NSRS property not present on the payload, check that the api server is functioning correct and that the LP is fully launched. Received: ', data);
128 data.nsrs = [];
129 }
130 Utils.checkAuthentication(data.statusCode, function() {
131 self.closeSocket();
132 });
133 let deletingNSRs = [];
134
135 if (self.nsrs) {
136 deletingNSRs = _.filter(self.nsrs, function(nsr) {
137 return nsr.deleting == true;
138 });
139 };
140
141 deletingNSRs.forEach(function(deletingNSR) {
142 data.nsrs.map(nsr => {
143 if (nsr.id == deletingNSR.id) {
144 _.extend(nsr, deletingNSR);
145 }
146 });
147 });
148 if(isLoading) {
149 isLoading = false;
150 Alt.actions.global.hideScreenLoader.defer();
151 }
152 self.setState({
153 nsrs: data.nsrs
154 });
155 } catch(e) {
156 console.log('HIT an exception in openNSRSocketSuccess', e);
157 }
158 };
159 }
160 FleetStoreConstructor.prototype.getNsrInstancesSuccess = function(data) {
161 this.setState({
162 nsrs: data.nsrs
163 });
164 };
165 FleetStoreConstructor.prototype.deleteNsrInstanceSuccess = function(data) {
166 console.log('deleted', data)
167 };
168
169 FleetStoreConstructor.prototype.deletingNSR = function(id) {
170 console.log('deleting NSR', id);
171 let nsrs = [];
172 let self = this;
173 try {
174 nsrs = this.nsrs.map(nsr => {
175 if (nsr.id == id) {
176 nsr.deleting = true;
177 self.closedNsrCard(id);
178 }
179 return nsr;
180 });
181 this.setState({
182 nsrs: nsrs
183 })
184 } catch (e) {
185 console.log('No NSR\'s found. Should never get here');
186 }
187 };
188
189 FleetStoreConstructor.prototype.deleteNsrInstanceError = function(data) {};
190 FleetStoreConstructor.prototype.getNsrInstancesError = function(data) {
191 console.log('ERROR', data)
192 };
193 FleetStoreConstructor.prototype.handleUpdateControlInput = _.debounce(function(data) {
194 var opt = data[0];
195 FleetStore.nsrControl(opt.operation, opt.url, data[1])
196 }, 500).bind(null);
197 FleetStoreConstructor.prototype.handleNsrControlSuccess = function(data) {
198 console.log(data)
199 };
200 FleetStoreConstructor.prototype.handleNsrControlError = function() {};
201 FleetStoreConstructor.prototype.handleSlideNoStateChange = function(data) {
202 this.setState({
203 dropdownSlide: data.pane,
204 slideno: data.no,
205 slideChange: data.slideChange
206 });
207 };
208 FleetStoreConstructor.prototype.handleSlideNoStateChangeSuccess = function() {
209 this.setState({
210 slideChange: this.slideChange - 1
211 });
212 };
213 FleetStoreConstructor.prototype.setNSRStatusSuccess = function() {};
214 FleetStoreConstructor.prototype.setNSRStatusError = function(data) {
215 console.log('Error changing NSR State', data)
216 };
217
218 FleetStoreConstructor.prototype.getCatalogSuccess = function(data) {
219 var self = this;
220 var descriptorCount = 0;
221 data.forEach(function(catalog) {
222 descriptorCount += catalog.descriptors.length;
223 });
224
225 self.setState({
226 descriptorCount: descriptorCount
227 });
228 };
229
230 FleetStoreConstructor.prototype.validateError = function(msg) {
231 this.setState({
232 validateErrorEvent: true,
233 validateErrorMsg: msg
234 });
235 };
236 FleetStoreConstructor.prototype.validateReset = function() {
237 this.setState({
238 validateErrorEvent: false
239 });
240 };
241
242 // Card management
243 FleetStoreConstructor.prototype.openNsrCard = function(id) {
244 //console.log("*** *** FleetStore.openNsrCard with nsr id:", id);
245 const openedNsrIDs = this.openedNsrIDs.slice(0);
246 // Only add if card is not there
247 if (id) {
248 if (!openedNsrIDs.includes(id)) {
249 openedNsrIDs.unshift(id);
250 this.launchpadSettings.addOpenNSR(id);
251 this.setState({
252 openedNsrIDs: openedNsrIDs
253 });
254 } else {
255 console.log("NSR already open, id:%s", id);
256 }
257 } else {
258 console.log("null nsr id.");
259 }
260 }
261 FleetStoreConstructor.prototype.closeNsrCard = function(id) {
262 this.launchpadSettings.removeOpenNSR(id);
263 this.setState({
264 openedNsrIDs: this.openedNsrIDs.filter(nsr_id => nsr_id !== id)
265 });
266 }
267 FleetStoreConstructor.prototype.instantiateNetworkService = function(id) {
268 window.location.hash = window.location.hash + '/launch';
269 }
270
271 FleetStoreConstructor.prototype.setNsListPanelVisible = function(isVisible) {
272 this.setState({
273 isNsListPanelVisible: isVisible
274 })
275 }
276
277 FleetStore = Alt.createStore(FleetStoreConstructor);
278 module.exports = FleetStore;