blob: 95671190e8905786c47be06e43c5c53975d51a14 [file] [log] [blame]
Bri Prebilic Cole96f26472015-03-31 13:07:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole96f26472015-03-31 13:07:05 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Intent View Module
19 */
20
21(function () {
22 'use strict';
23
Simon Hunt441c9ae2017-02-03 18:22:31 -080024 // constants and configuration
Viswanath KSP0f297702016-08-13 18:02:43 +053025 var dialogId = 'remove-intent-dialog',
26 dialogOpts = {
27 edge: 'right'
Simon Hunt441c9ae2017-02-03 18:22:31 -080028 };
29
30 // DOM elements
31 var dropdown;
32
33 // injected refs
34 var $log, $scope, ns, tov, tts, ds;
35
36
37 function initScope() {
38 $scope.topoTip = 'Show selected intent on topology view';
39 $scope.resubmitTip = 'Resubmit selected intent';
40 $scope.deactivateTip = 'Remove selected intent';
41 $scope.purgeTip = 'Purge selected intent';
42 $scope.purgeAllTip = 'Purge withdrawn intents';
43
44 $scope.briefTip = 'Switch to brief view';
45 $scope.detailTip = 'Switch to detailed view';
46
47 $scope.brief = true;
48 $scope.intentState = 'NA';
49 $scope.fired = false;
50 }
51
52 // === row selection and response callback functions:
53
54 function selCb($event, row) {
55 $log.debug('Got a click on:', row);
56 var m = /(\d+)\s:\s(.*)/.exec(row.appId),
57 id = m ? m[1] : null,
58 name = m ? m[2] : null;
59
60 $scope.intentData = ($scope.selId && m) ? {
61 appId: id,
62 appName: name,
63 key: row.key,
64 intentType: row.type
65 } : null;
66
67 $scope.intentState = row.state;
68 showDropdown(false);
69 }
70
71 function respCb() {
72 if ($scope.fired) {
73 if ($scope.changedData) {
74 $scope.intentState = $scope.changedData.state;
75 }
76 $scope.fired = false;
77 }
78 }
79
80
81 // === show-intent functions
82
83 function showDropdown(b) {
84 dropdown.style('display', b ? 'block' : 'none');
85 }
86
87 function showIntent () {
88 var d = $scope.intentData,
89 handlers,
90 nh;
91
92 if (!d) {
93 // no intent selected - nothing to do
94 return;
95 }
96
97 function setOvAndNavigate(info) {
98 d.overlayId = info.id;
99 ns.navTo('topo', d);
100 }
101
102 function clickMe(data) {
103 showDropdown(false);
104 setOvAndNavigate(data);
105 }
106
107 function setUpSelection(handlers) {
108 dropdown.text(null);
109
110 handlers.forEach(function (data) {
111 var div = dropdown.append('div');
112 div.classed('overlay-choice', true);
113 div.text(data.tt);
114 div.on('click', function () {
115 clickMe(data);
116 });
117 });
118
119 showDropdown(true);
120 }
121
122 handlers = tov.overlaysAcceptingIntents(d.intentType);
123 nh = handlers.length;
124
125 if (nh === 1) {
126 setOvAndNavigate(handlers[0]);
127
128 } else if (nh > 1) {
129 // let the user choose which overlay to invoke...
130 setUpSelection(handlers);
131
132 } else {
133 $log.warn('Sorry - no overlay configured to show',
134 'intents of type', d.intentType);
135 }
136 }
137
138
139 // === intent action functionality
140
141 // TODO: refactor to move functions below to here...
142
143
144 // === intent view controller
Viswanath KSP0f297702016-08-13 18:02:43 +0530145
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700146 angular.module('ovIntent', [])
147 .controller('OvIntentCtrl',
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700148 ['$log', '$scope', 'TableBuilderService', 'NavService',
Simon Huntc5489c92017-01-30 17:50:48 -0800149 'TopoOverlayService', 'TopoTrafficService', 'DialogService',
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700150
Simon Hunt441c9ae2017-02-03 18:22:31 -0800151 function (_$log_, _$scope_, tbs, _ns_, _tov_, _tts_, _ds_) {
152 $log = _$log_;
153 $scope = _$scope_;
154 ns = _ns_;
155 tov = _tov_;
156 tts = _tts_;
157 ds = _ds_;
158
159 initScope();
Simon Huntfc5c5842017-02-01 23:32:18 -0800160
161 dropdown = d3.select('div.show-intent-btn .dropdown');
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700162
Simon Hunt441c9ae2017-02-03 18:22:31 -0800163 // set up scope function references...
164 $scope.showIntent = showIntent;
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700165
Simon Hunt441c9ae2017-02-03 18:22:31 -0800166 $scope.canShowIntent = function() {
167 var d = $scope.intentData;
168 return d && tov.overlaysAcceptingIntents(d.intentType).length > 0;
169 };
Viswanath KSP317f3292016-09-04 14:13:22 +0530170
Simon Hunt441c9ae2017-02-03 18:22:31 -0800171 // build the table
Simon Hunt4e412732015-10-27 15:25:39 -0700172 tbs.buildTable({
173 scope: $scope,
174 tag: 'intent',
175 selCb: selCb,
Viswanath KSP317f3292016-09-04 14:13:22 +0530176 respCb: respCb,
Simon Hunt4e412732015-10-27 15:25:39 -0700177 idKey: 'key'
178 });
179
Simon Huntfc5c5842017-02-01 23:32:18 -0800180
181 // TODO: clean up the following code...
182
Viswanath KSP317f3292016-09-04 14:13:22 +0530183 $scope.isIntentInstalled = function () {
184 return $scope.intentState === 'Installed';
185 };
186
Viswanath KSP813a20d2016-09-13 04:25:41 +0530187 $scope.isIntentWithdrawn = function () {
188 return $scope.intentState === 'Withdrawn';
189 };
Viswanath KSP0f297702016-08-13 18:02:43 +0530190
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530191 $scope.isHavingWithdrawn = function () {
192 var isWithdrawn = false;
193 $scope.tableData.forEach(function (intent) {
194 if (intent.state ==='Withdrawn') {
195 isWithdrawn = true;
196 }
197 });
198 return isWithdrawn;
199 };
200
Viswanath KSP14aee092016-10-02 01:47:40 +0530201 function executeAction(action) {
Viswanath KSP813a20d2016-09-13 04:25:41 +0530202 var content = ds.createDiv(),
Viswanath KSP14aee092016-10-02 01:47:40 +0530203 txt,
204 bPurge = action === 'purge';
Viswanath KSP813a20d2016-09-13 04:25:41 +0530205
206 $scope.intentData.intentPurge = bPurge;
207
208 content.append('p').
Viswanath KSP14aee092016-10-02 01:47:40 +0530209 text('Are you sure you want to '+ action +
Viswanath KSP813a20d2016-09-13 04:25:41 +0530210 ' the selected intent?');
Viswanath KSP0f297702016-08-13 18:02:43 +0530211
212 function dOk() {
213 var d = $scope.intentData;
Viswanath KSP813a20d2016-09-13 04:25:41 +0530214 $log.debug(d);
Viswanath KSP14aee092016-10-02 01:47:40 +0530215 d && (action === 'resubmit' ? tts.resubmitIntent(d) :
216 tts.removeIntent(d));
Viswanath KSP317f3292016-09-04 14:13:22 +0530217 $scope.fired = true;
Viswanath KSP0f297702016-08-13 18:02:43 +0530218 }
219
220 function dCancel() {
221 ds.closeDialog();
222 $log.debug('Canceling remove-intent action');
223 }
224
225 ds.openDialog(dialogId, dialogOpts)
226 .setTitle('Confirm Action')
227 .addContent(content)
228 .addOk(dOk)
229 .addCancel(dCancel)
230 .bindKeys();
Viswanath KSP813a20d2016-09-13 04:25:41 +0530231 }
Simon Hunt441c9ae2017-02-03 18:22:31 -0800232
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530233 function executeActions(action) {
234 var content = ds.createDiv(),
235 txt='purgeIntents';
236 content.append('p').
237 text('Are you sure you want to purge all the withdrawn intents?');
238
239 function dOk() {
240 tts.removeIntents();
241 $scope.fired = true;
242 }
243
244 function dCancel() {
245 ds.closeDialog();
246 $log.debug('Canceling remove-intents action');
247 }
248
249 ds.openDialog(dialogId, dialogOpts)
250 .setTitle('Confirm Action')
251 .addContent(content)
252 .addOk(dOk)
253 .addCancel(dCancel)
254 .bindKeys();
255 }
Viswanath KSP813a20d2016-09-13 04:25:41 +0530256
257 $scope.deactivateIntent = function () {
Viswanath KSP14aee092016-10-02 01:47:40 +0530258 executeAction("withdraw");
259 };
260
261 $scope.resubmitIntent = function () {
262 executeAction("resubmit");
Viswanath KSP813a20d2016-09-13 04:25:41 +0530263 };
264
265 $scope.purgeIntent = function () {
Viswanath KSP14aee092016-10-02 01:47:40 +0530266 executeAction("purge");
Viswanath KSP0f297702016-08-13 18:02:43 +0530267 };
268
Kavitha Alagesan98c00062016-08-23 18:20:42 -0700269 $scope.briefToggle = function () {
270 $scope.brief = !$scope.brief;
271 };
272
Viswanath KSP0f297702016-08-13 18:02:43 +0530273 $scope.$on('$destroy', function () {
274 ds.closeDialog();
275 $log.debug('OvIntentCtrl has been destroyed');
276 });
277
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530278 $scope.purgeIntents = function () {
279 executeActions("purgeIntents");
280 };
Viswanath KSP0f297702016-08-13 18:02:43 +0530281 $log.debug('OvIntentCtrl has been created');
Simon Hunt4e412732015-10-27 15:25:39 -0700282 }]);
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700283}());