aboutsummaryrefslogtreecommitdiffstats
path: root/demo-ui/app/controllers/CosController.js
blob: 69772a24450b9a6f72e911ee3aad89bcd5b07a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*******************************************************************************
* Copyright (c) 2015 CableLabs Inc. and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution, and is available at
* http://www.apache.org/licenses/LICENSE-2.0
*******************************************************************************/

(function() {

var CosController = function($scope, $http, $interval, $log, cosServices, model, dbg ) {

	//
	// Controller Set Up
	//

	dbg.p("---> in CosController()");

	actionState = {
		IDLE : 1, ADD : 2, UPDATE : 3, DEL : 4
	}

	// $scope variables
	$scope.cosList = null;
	$scope.selectedCosIdx = -1;
	$scope.cosToEdit =  null;
	$scope.cosActionButtonText =  "unset";

	$scope.availableBWs = [
		{
			name : "10M",
			cir  : 10000
		},
		{
			name : "100M",
			cir  : 100000
		},
		{
			name : "1G",
			cir  : 1000000
		},
		{
			name : "10G",
			cir  : 10000000
		},
	];

	dbg.p("bandwidths avaialble:")
	dbg.pj($scope.availableBWs);

	// congroller variables
	var cosActionState = actionState.IDLE;
	var newCos =  null;
	var uniqueId = true;
	var nameNum  = 1;


	// cos constructor
	var Cos = function(){
		if ( uniqueId )
			this.id = "new"+ nameNum;
		else
			this.id = "new";
    	this.commitedInfoRate = 50;
    	this.availbility = 95.5;
    	this.frameDelay = 2.2;
    	this.jitter = 3.3;
    	this.frameLoss = 4.4 ;
	}

	//
	// utility fxns
	//

	var validCosIdx = function(idx) {
		return ( idx >= 0 && idx < $scope.cosList.length )
	}

	var cosIdxReset = function() {
		return ( $scope.cosList.length > 0 ? 0 : -1);
	}

	var cosIdxAfter = function( action ) {
		switch ( action ) {
		    case actionState.DEL:
			    if ( validCosIdx($scope.selectedCosIdx) )
			    	return $scope.selectedCosIdx;
			    else if ( validCosIdx($scope.selectedCosIdx-1) )
			    	return $scope.selectedCosIdx-1;
				else
					return cosIdxReset();
		        break;
		    case actionState.ADD:
		    	return ( $scope.cosList.length >= 0 ?
		    		     $scope.cosList.length-1 :  cosIdxReset() );
		        break;
		    case actionState.UPDATE:
		    	return ( validCosIdx($scope.selectedCosIdx) ?
		    		     $scope.selectedCosIdx : cosIdxReset() );
		        break;
		    default:
		        dbg.e("invalid action state");
				break;
		}
	}

	$scope.cosExists = function(cosId) {
		var nameCheck = function (cosElm) { return cosElm.id == cosId; }
		return ( $scope.cosList &&
			     $scope.cosList.filter(nameCheck).length > 0 );
	}

	$scope.bwText = function(cos) {
		if (cos) {
			switch ( cos.commitedInfoRate ) {
				case 10000    : return "10M";
				case 100000   : return "100M";
				case 1000000  : return "1G";
				case 10000000 : return "10G";
			}
			return "invalid BW";
		}
		else {
			return "null cos";
		}
	}

	// return index of avilable BW based cir (-1 if not found)
	var availableBwIdx = function (cir) {
		for ( var i = 0; i < $scope.availableBWs.length; i++ )  {
			if ($scope.availableBWs[i].cir == cir)  {
				return i;
			}
		}
		return -1;
	}

	// return index of cos in current list (-1 if not found)
	var cosNameToIdx = function ( cosName ) {
		if ( !cosName || !$scope.cosList )
			return -1;

		for ( var i = 0; i < $scope.cosList.length; i++ )  {
			if ($scope.cosList[i].id == cosName )  {
				return i;
			}
		}
		return -1;
	}

	//
	// HTTP Repsponse Handlers
	//

	var onRequestError = function(reason){
		dbg.e("HTTP Request Problem\n" + JSON.stringify(reason));
	};
	var onCosListResp = function(data) {
		dbg.p("in onCosListResp()");
		$scope.cosList = data;

		if ( $scope.cosList && $scope.cosList.length > 0   )
			$scope.selectedCosIdx = 0;
		else
			$scope.selectedCosIdx = -1;

		dbg.p("selectedCosIdx = " + $scope.selectedCosIdx,1);
	};
	var onCosCreateResp = function(data) {
		dbg.p("in onCosCreateResp()");
		dbg.p("following cos returned from create cmd", 1);
		dbg.pj(data);
		$scope.cosList.push(newCos);
		$scope.selectedCosIdx = cosIdxAfter(actionState.ADD);
		nameNum++;
		newCos =  null;
		$scope.cosToEdit =  null;
		dbg.p("selectedCosIdx = " + $scope.selectedCosIdx,1);
	};
	var onCosUpdateResp = function(data) {
		dbg.p("in onCosUpdateResp()");
		dbg.p("following cos returned after update", 1);
		dbg.pj(data);
		$scope.selectedCosIdx = cosIdxAfter(actionState.UPDATE);
		$scope.cosToEdit =  null;
		dbg.p("selectedCosIdx = " + $scope.selectedCosIdx,1);
	};
	var onCosDeleteResp = function(data) {
		dbg.p("in onCosDeleteResp()");
		dbg.p("following returned after delete", 1);
		dbg.pj(data);
		if ( validCosIdx($scope.selectedCosIdx) )
			$scope.cosList.splice($scope.selectedCosIdx,1);
		$scope.selectedCosIdx = cosIdxAfter(actionState.DEL);
		dbg.p("selectedCosIdx = " + $scope.selectedCosIdx,1);
	};

	//
 	// CoS Event Handlers & Utils
	//

	$scope.onCosClick = function (i) {
		dbg.p("in onCosClick(): clicked ("+i+")");
		$scope.selectedCosIdx = i;
		cosActionState = actionState.IDLE;
		dbg.p("selectedCosIdx = "+ $scope.selectedCosIdx,1);
	}
	$scope.onAddCos = function () {
		dbg.p("in onAddCos()");
		cosActionState = actionState.ADD;
		$scope.cosActionButtonText =  "Create";
		newCos =  new Cos();
		$scope.cosToEdit = newCos;

		if ($scope.availableBWs.length >=3 )
			$scope.cosToEdit.bwSelected = $scope.availableBWs[2];
		else
			$scope.cosToEdit.bwSelected = $scope.availableBWs[0];

		dbg.pj(newCos);
	}
	$scope.onUpdateCos = function () {
		dbg.p("in onUpdateCos()");
		if ( $scope.cosList.length <= 0 )
		{
			dbg.p("nos CoS's exist, no action being taken");
			return;
		}
		cosActionState = actionState.UPDATE;
		$scope.cosActionButtonText =  "Update";
		$scope.cosToEdit = $scope.cosList[$scope.selectedCosIdx];

		dbg.p("looking for dd list bw: " + $scope.cosToEdit.commitedInfoRate);
		var availBwIdx = availableBwIdx($scope.cosToEdit.commitedInfoRate);
		if ( availBwIdx < 0 || availBwIdx > $scope.availableBWs.length-1 ) {
			dbg.e("invalid selected BW idx: "+availBwIdx+"(setting to 0)");
			$scope.cosToEdit.bwSelected = $scope.availableBWs[0];
		}
		else {
			$scope.cosToEdit.bwSelected = $scope.availableBWs[availBwIdx];
		}

		dbg.p("about to edit cos:");
		dbg.pj($scope.cosToEdit);


	}
	$scope.onDelCos = function () {
		dbg.p("in onDelCos()");
		if ( $scope.cosList.length <= 0 )
		{
			dbg.p("nos CoS's exist, no action being taken");
			return;
		}
		cosActionState = actionState.DEL;
		$scope.cosActionButtonText =  "Delete";
	}

	$scope.onCosInputSubmit = function () {
		dbg.p("in onCosInputSubmit()");

		var cosToOperateOn = null;
		if ( cosActionState === actionState.ADD)
			cosToOperateOn = newCos;
		else
			cosToOperateOn = $scope.cosList[$scope.selectedCosIdx];

		switch(cosActionState) {

		    case actionState.DEL:
		        dbg.p("about to delete " + cosToOperateOn.id,1);
				cosServices.deleteCos(cosToOperateOn)
            				.then(onCosDeleteResp, onRequestError);
		        break;

		    case actionState.ADD:
		    	cosToOperateOn.commitedInfoRate = cosToOperateOn.bwSelected.cir;
				delete cosToOperateOn.bwSelected;
		        dbg.p("about to add " + cosToOperateOn.id, 1);
				cosServices.createCos(cosToOperateOn)
            				.then(onCosCreateResp, onRequestError);
		        break;

		    case actionState.UPDATE:
		    	cosToOperateOn.commitedInfoRate = cosToOperateOn.bwSelected.cir;
				delete cosToOperateOn.bwSelected;
		        dbg.p("about to update " + cosToOperateOn.id, 1);
				cosServices.updateCos(cosToOperateOn)
            				.then(onCosUpdateResp, onRequestError);
		        break;

		    default:
		        dbg.e("invalid action state");
		        break;
		}

		cosActionState = actionState.IDLE;
	}

	//
	// State query fxns
	//

	$scope.cosActionInProgress = function () {
		return ( cosActionState != actionState.IDLE  )
	}
	$scope.cosEditInProgress = function () {
		return ( cosActionState === actionState.ADD  ||
			     cosActionState === actionState.UPDATE  );
	}
	$scope.cosUpdateInProgress = function () {
		return (  cosActionState === actionState.UPDATE  );
	}
	$scope.cosAddInProgress = function () {
		return (  cosActionState === actionState.ADD  );
	}
	$scope.cosDelInProgress = function () {
		return (  cosActionState === actionState.DEL  );
	}
	$scope.cosNameConflict = function () {
		return ( $scope.cosAddInProgress() &&
				 $scope.cosExists($scope.cosToEdit.id));
	}
	$scope.showCosInputs = function () {
		return $scope.cosEditInProgress();
	}
	$scope.showCosValues = function () {
		return (!$scope.showCosInputs() &&
		         validCosIdx($scope.selectedCosIdx));
	}
	$scope.showCosNameInput = function () {
		return $scope.cosAddInProgress();
	}
	$scope.showCosName = function () {
		return (! $scope.showCosNameInput());
	}
	$scope.showCosActionButton = function () {
		return ( $scope.cosAddInProgress()   ||
			   ($scope.cosActionInProgress() &&
				  $scope.cosList.length > 0  ));
	}
	$scope.activateCosActionButton = function () {
		return ( $scope.cosActionInProgress() &&
				!$scope.cosNameConflict()     )
	}

	//
	// Watchers
	//

    // we can't get our initial CoS list until 
    // the URL has been set by config file read in
    cosUrlWatcher = function () { return cosServices.getCosUrl(); }
    onCosUrlChange = function (newCosUrl, oldCosUrl) {
		if (newCosUrl !== oldCosUrl) {
			dbg.p("detected cos url change, getting cos list");
			cosServices.getCosList()
					   .then(onCosListResp, onRequestError);
		}
	}
    $scope.$watch( cosUrlWatcher, onCosUrlChange );

    // Watch for change in selected EPL so tht 
    // we can set the appropraite CoS as selected
    eplWatcher = function () { return model.getCurrentEpl(); }
    onEplChange = function (newEpl, oldEpl) {
        dbg.p("in CosController:onEplChange");
        //dbg.p("oldEpl:"); dbg.pj(oldEpl);
        //dbg.p("newEpl:"); dbg.pj(newEpl);

        // only make a chance if we have an
        if ( newEpl ) {
        	var cosIdx = cosNameToIdx(newEpl.cos);
            dbg.p("changing selected CoS to: " + newEpl.cos + 
            	  " [idx="+cosIdx+"]", 1);
            if ( validCosIdx(cosIdx) ) {
				cosActionState = actionState.IDLE
				$scope.selectedCosIdx = cosIdx;
            }
        }
    }
    $scope.$watch( eplWatcher, onEplChange );

};

// register controller in the module
app.controller("CosController", CosController);

}());