blob: a2375baffc48f36c3f3788c2043c39fa585286e9 [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
Viswanath KSP0f297702016-08-13 18:02:43 +053024 var dialogId = 'remove-intent-dialog',
25 dialogOpts = {
26 edge: 'right'
Simon Huntfc5c5842017-02-01 23:32:18 -080027 },
28 dropdown;
Viswanath KSP0f297702016-08-13 18:02:43 +053029
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070030 angular.module('ovIntent', [])
31 .controller('OvIntentCtrl',
Simon Hunt4a6b54b2015-10-27 22:08:25 -070032 ['$log', '$scope', 'TableBuilderService', 'NavService',
Simon Huntc5489c92017-01-30 17:50:48 -080033 'TopoOverlayService', 'TopoTrafficService', 'DialogService',
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070034
Simon Huntc5489c92017-01-30 17:50:48 -080035 function ($log, $scope, tbs, ns, tov, tts, ds) {
Kavitha Alagesan98c00062016-08-23 18:20:42 -070036 $scope.briefTip = 'Switch to brief view';
37 $scope.detailTip = 'Switch to detailed view';
38 $scope.brief = true;
Viswanath KSP317f3292016-09-04 14:13:22 +053039 $scope.intentState = 'NA';
40 $scope.fired = false;
Simon Huntfc5c5842017-02-01 23:32:18 -080041 $scope.showOverlays = false;
42
43 dropdown = d3.select('div.show-intent-btn .dropdown');
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070044
Simon Hunt4e412732015-10-27 15:25:39 -070045 function selCb($event, row) {
46 $log.debug('Got a click on:', row);
Simon Hunt4a6b54b2015-10-27 22:08:25 -070047 var m = /(\d+)\s:\s(.*)/.exec(row.appId),
48 id = m ? m[1] : null,
49 name = m ? m[2] : null;
50
Simon Hunt144bd792015-10-28 11:46:36 -070051 $scope.intentData = ($scope.selId && m) ? {
Viswanath KSP0f297702016-08-13 18:02:43 +053052 appId: id,
53 appName: name,
54 key: row.key
Simon Hunt4a6b54b2015-10-27 22:08:25 -070055 } : null;
Viswanath KSP317f3292016-09-04 14:13:22 +053056
57 $scope.intentState = row.state;
Simon Huntfc5c5842017-02-01 23:32:18 -080058 showDropdown(false);
Viswanath KSP317f3292016-09-04 14:13:22 +053059 }
60
61 function respCb() {
62 if ($scope.fired) {
63 if ($scope.changedData) {
64 $scope.intentState = $scope.changedData.state;
65 }
66 $scope.fired = false;
67 }
Simon Hunt4e412732015-10-27 15:25:39 -070068 }
69
70 tbs.buildTable({
71 scope: $scope,
72 tag: 'intent',
73 selCb: selCb,
Viswanath KSP317f3292016-09-04 14:13:22 +053074 respCb: respCb,
Simon Hunt4e412732015-10-27 15:25:39 -070075 idKey: 'key'
76 });
77
78 $scope.topoTip = 'Show selected intent on topology view';
Viswanath KSP14aee092016-10-02 01:47:40 +053079 $scope.resubmitTip = 'Resubmit selected intent';
Viswanath KSP0f297702016-08-13 18:02:43 +053080 $scope.deactivateTip = 'Remove selected intent';
Viswanath KSP813a20d2016-09-13 04:25:41 +053081 $scope.purgeTip = 'Purge selected intent';
Deepa Vaddireddy63340922017-01-19 08:15:31 +053082 $scope.purgeAllTip = 'Purge withdrawn intents';
Simon Hunt4e412732015-10-27 15:25:39 -070083
Simon Huntc5489c92017-01-30 17:50:48 -080084
Simon Huntfc5c5842017-02-01 23:32:18 -080085 function showDropdown(b) {
86 dropdown.style('display', b ? 'block' : 'none');
87 }
88
89 $scope.showIntent = function () {
90 var d = $scope.intentData,
91 tovData,
92 ncb;
93
94 if (!d) {
95 // no intent selected - nothing to do
96 return;
97 }
98
99 function setOvAndNavigate(info) {
100 d.overlayId = info.id;
Simon Huntc5489c92017-01-30 17:50:48 -0800101 ns.navTo('topo', d);
102 }
Simon Huntfc5c5842017-02-01 23:32:18 -0800103
104 function clickMe(data) {
105 showDropdown(false);
106 setOvAndNavigate(data);
107 }
108
109 function setUpSelection(tovData) {
110 dropdown.text(null);
111
112 tovData.forEach(function (data) {
113 var div = dropdown.append('div');
114 div.classed('overlay-choice', true);
115 div.text(data.tt);
116 div.on('click', function () {
117 clickMe(data);
118 });
119 });
120
121 showDropdown(true);
122 }
123
124 tovData = tov.listOverlaysThatShowIntents();
125 ncb = tovData.length;
126 // NOTE: ncb should be at least 1, (traffic overlay)
127
128 if (ncb === 1) {
129 setOvAndNavigate(tovData[0]);
130
131 } else if (ncb > 1) {
132 // let the user choose which overlay to invoke...
133 setUpSelection(tovData);
134
135 } else {
136 $log.error('Internal Error - no overlay configured',
137 'to show selected intent on topology view');
138 }
Simon Hunt4e412732015-10-27 15:25:39 -0700139 };
140
Simon Huntfc5c5842017-02-01 23:32:18 -0800141
142
143 // TODO: clean up the following code...
144
Viswanath KSP317f3292016-09-04 14:13:22 +0530145 $scope.isIntentInstalled = function () {
146 return $scope.intentState === 'Installed';
147 };
148
Viswanath KSP813a20d2016-09-13 04:25:41 +0530149 $scope.isIntentWithdrawn = function () {
150 return $scope.intentState === 'Withdrawn';
151 };
Viswanath KSP0f297702016-08-13 18:02:43 +0530152
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530153 $scope.isHavingWithdrawn = function () {
154 var isWithdrawn = false;
155 $scope.tableData.forEach(function (intent) {
156 if (intent.state ==='Withdrawn') {
157 isWithdrawn = true;
158 }
159 });
160 return isWithdrawn;
161 };
162
Viswanath KSP14aee092016-10-02 01:47:40 +0530163 function executeAction(action) {
Viswanath KSP813a20d2016-09-13 04:25:41 +0530164 var content = ds.createDiv(),
Viswanath KSP14aee092016-10-02 01:47:40 +0530165 txt,
166 bPurge = action === 'purge';
Viswanath KSP813a20d2016-09-13 04:25:41 +0530167
168 $scope.intentData.intentPurge = bPurge;
169
170 content.append('p').
Viswanath KSP14aee092016-10-02 01:47:40 +0530171 text('Are you sure you want to '+ action +
Viswanath KSP813a20d2016-09-13 04:25:41 +0530172 ' the selected intent?');
Viswanath KSP0f297702016-08-13 18:02:43 +0530173
174 function dOk() {
175 var d = $scope.intentData;
Viswanath KSP813a20d2016-09-13 04:25:41 +0530176 $log.debug(d);
Viswanath KSP14aee092016-10-02 01:47:40 +0530177 d && (action === 'resubmit' ? tts.resubmitIntent(d) :
178 tts.removeIntent(d));
Viswanath KSP317f3292016-09-04 14:13:22 +0530179 $scope.fired = true;
Viswanath KSP0f297702016-08-13 18:02:43 +0530180 }
181
182 function dCancel() {
183 ds.closeDialog();
184 $log.debug('Canceling remove-intent action');
185 }
186
187 ds.openDialog(dialogId, dialogOpts)
188 .setTitle('Confirm Action')
189 .addContent(content)
190 .addOk(dOk)
191 .addCancel(dCancel)
192 .bindKeys();
Viswanath KSP813a20d2016-09-13 04:25:41 +0530193 }
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530194 function executeActions(action) {
195 var content = ds.createDiv(),
196 txt='purgeIntents';
197 content.append('p').
198 text('Are you sure you want to purge all the withdrawn intents?');
199
200 function dOk() {
201 tts.removeIntents();
202 $scope.fired = true;
203 }
204
205 function dCancel() {
206 ds.closeDialog();
207 $log.debug('Canceling remove-intents action');
208 }
209
210 ds.openDialog(dialogId, dialogOpts)
211 .setTitle('Confirm Action')
212 .addContent(content)
213 .addOk(dOk)
214 .addCancel(dCancel)
215 .bindKeys();
216 }
Viswanath KSP813a20d2016-09-13 04:25:41 +0530217
218 $scope.deactivateIntent = function () {
Viswanath KSP14aee092016-10-02 01:47:40 +0530219 executeAction("withdraw");
220 };
221
222 $scope.resubmitIntent = function () {
223 executeAction("resubmit");
Viswanath KSP813a20d2016-09-13 04:25:41 +0530224 };
225
226 $scope.purgeIntent = function () {
Viswanath KSP14aee092016-10-02 01:47:40 +0530227 executeAction("purge");
Viswanath KSP0f297702016-08-13 18:02:43 +0530228 };
229
Kavitha Alagesan98c00062016-08-23 18:20:42 -0700230 $scope.briefToggle = function () {
231 $scope.brief = !$scope.brief;
232 };
233
Viswanath KSP0f297702016-08-13 18:02:43 +0530234 $scope.$on('$destroy', function () {
235 ds.closeDialog();
236 $log.debug('OvIntentCtrl has been destroyed');
237 });
238
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530239 $scope.purgeIntents = function () {
240 executeActions("purgeIntents");
241 };
Viswanath KSP0f297702016-08-13 18:02:43 +0530242 $log.debug('OvIntentCtrl has been created');
Simon Hunt4e412732015-10-27 15:25:39 -0700243 }]);
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700244}());