RIFT-15726 - optimize download size -> lodash usage in UI
[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 import _filter from 'lodash/filter';
21 import _extend from 'lodash/extend';
22 import _debounce from 'lodash/debounce';
23 var FleetSource = require('./launchpadFleetSource.js');
24 var FleetActions = require('./launchpadFleetActions.js');
25 import CardActions from './launchpad_card/launchpadCardActions.js';
26 var Utils = require('utils/utils.js');
27 import LaunchNetworkServiceSource from './instantiate/launchNetworkServiceSource.js';
28 import LaunchNetworkServiceActions from './instantiate/launchNetworkServiceActions.js';
29
30 import {LaunchpadSettings} from './settings.js';
31
32
33 var FleetStore;
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 getVDUConsoleLinkSuccess: FleetActions.getVDUConsoleLinkSuccess
80
81 });
82 this.bindAction(LaunchNetworkServiceActions.launchNSRSuccess, responseData => {
83 try {
84 this.openNsrCard(responseData.data.nsr_id);
85 } catch (e) {
86 console.log("Unable to open NS Card for response data: ", responseData);
87 }
88 });
89 this.exportPublicMethods({
90 getFleets: function() {
91 return this.getState().fleets;
92 },
93 closeSocket: this.closeSocket.bind(self)
94 });
95 }
96
97 FleetStoreConstructor.prototype.handleLogout = function() {
98 this.closeSocket();
99 }
100
101 FleetStoreConstructor.prototype.closeSocket = function() {
102 if(this.socket) {
103 window.multiplexer.channel(this.channelId).close();
104 }
105 this.setState({
106 socket:null
107 })
108 }
109
110 //NEW
111 FleetStoreConstructor.prototype.openNSRSocketLoading = function(connection) {
112 console.log('open socketloading')
113 Alt.actions.global.showScreenLoader.defer();
114 };
115 FleetStoreConstructor.prototype.openNSRSocketSuccess = function(connection) {
116 var self = this;
117 var isLoading = true;
118 var ws = window.multiplexer.channel(connection);
119 if (!connection) return;
120 self.setState({
121 socket: ws.ws,
122 channelId: connection
123 });
124 ws.onmessage = function(socket) {
125 try {
126 var data = JSON.parse(socket.data);
127 if (!data.nsrs) {
128 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);
129 data.nsrs = [];
130 }
131 Utils.checkAuthentication(data.statusCode, function() {
132 self.closeSocket();
133 });
134 let deletingNSRs = [];
135
136 if (self.nsrs) {
137 deletingNSRs = _filter(self.nsrs, function(nsr) {
138 return nsr.deleting == true;
139 });
140 };
141
142 deletingNSRs.forEach(function(deletingNSR) {
143 data.nsrs.map(nsr => {
144 if (nsr.id == deletingNSR.id) {
145 _extend(nsr, deletingNSR);
146 }
147 });
148 });
149 if(isLoading) {
150 isLoading = false;
151 Alt.actions.global.hideScreenLoader.defer();
152 }
153 self.setState({
154 nsrs: data.nsrs
155 });
156 } catch(e) {
157 console.log('HIT an exception in openNSRSocketSuccess', e);
158 }
159 };
160 }
161 FleetStoreConstructor.prototype.getNsrInstancesSuccess = function(data) {
162 this.setState({
163 nsrs: data.nsrs
164 });
165 };
166 FleetStoreConstructor.prototype.deleteNsrInstanceSuccess = function(data) {
167 console.log('deleted', data)
168 };
169
170 FleetStoreConstructor.prototype.deletingNSR = function(id) {
171 console.log('deleting NSR', id);
172 let nsrs = [];
173 let self = this;
174 try {
175 nsrs = this.nsrs.map(nsr => {
176 if (nsr.id == id) {
177 nsr.deleting = true;
178 self.closedNsrCard(id);
179 }
180 return nsr;
181 });
182 this.setState({
183 nsrs: nsrs
184 })
185 } catch (e) {
186 console.log('No NSR\'s found. Should never get here');
187 }
188 };
189
190 FleetStoreConstructor.prototype.deleteNsrInstanceError = function(data) {};
191 FleetStoreConstructor.prototype.getNsrInstancesError = function(data) {
192 console.log('ERROR', data)
193 };
194 FleetStoreConstructor.prototype.handleUpdateControlInput = _debounce(function(data) {
195 var opt = data[0];
196 FleetStore.nsrControl(opt.operation, opt.url, data[1])
197 }, 500).bind(null);
198 FleetStoreConstructor.prototype.handleNsrControlSuccess = function(data) {
199 console.log(data)
200 };
201 FleetStoreConstructor.prototype.handleNsrControlError = function() {};
202 FleetStoreConstructor.prototype.handleSlideNoStateChange = function(data) {
203 this.setState({
204 dropdownSlide: data.pane,
205 slideno: data.no,
206 slideChange: data.slideChange
207 });
208 };
209 FleetStoreConstructor.prototype.handleSlideNoStateChangeSuccess = function() {
210 this.setState({
211 slideChange: this.slideChange - 1
212 });
213 };
214 FleetStoreConstructor.prototype.setNSRStatusSuccess = function() {};
215 FleetStoreConstructor.prototype.setNSRStatusError = function(data) {
216 console.log('Error changing NSR State', data)
217 };
218
219 FleetStoreConstructor.prototype.getCatalogSuccess = function(data) {
220 var self = this;
221 var descriptorCount = 0;
222 data.forEach(function(catalog) {
223 descriptorCount += catalog.descriptors.length;
224 });
225
226 self.setState({
227 descriptorCount: descriptorCount
228 });
229 };
230
231 FleetStoreConstructor.prototype.validateError = function(msg) {
232 this.setState({
233 validateErrorEvent: true,
234 validateErrorMsg: msg
235 });
236 };
237 FleetStoreConstructor.prototype.validateReset = function() {
238 this.setState({
239 validateErrorEvent: false
240 });
241 };
242
243 // Card management
244 FleetStoreConstructor.prototype.openNsrCard = function(id) {
245 //console.log("*** *** FleetStore.openNsrCard with nsr id:", id);
246 const openedNsrIDs = this.openedNsrIDs.slice(0);
247 // Only add if card is not there
248 if (id) {
249 if (!openedNsrIDs.includes(id)) {
250 openedNsrIDs.unshift(id);
251 this.launchpadSettings.addOpenNSR(id);
252 this.setState({
253 openedNsrIDs: openedNsrIDs
254 });
255 } else {
256 console.log("NSR already open, id:%s", id);
257 }
258 } else {
259 console.log("null nsr id.");
260 }
261 }
262 FleetStoreConstructor.prototype.closeNsrCard = function(id) {
263 this.launchpadSettings.removeOpenNSR(id);
264 this.setState({
265 openedNsrIDs: this.openedNsrIDs.filter(nsr_id => nsr_id !== id)
266 });
267 }
268 FleetStoreConstructor.prototype.instantiateNetworkService = function(id) {
269 window.location.hash = window.location.hash + '/launch';
270 }
271
272 FleetStoreConstructor.prototype.setNsListPanelVisible = function(isVisible) {
273 this.setState({
274 isNsListPanelVisible: isVisible
275 })
276 }
277
278 FleetStoreConstructor.prototype.getVDUConsoleLinkSuccess = function(data) {
279 data['console-url'] && window.open(data['console-url']);
280 }
281
282 FleetStore = Alt.createStore(FleetStoreConstructor, 'FleetStore');
283 module.exports = FleetStore;