Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 3 | * |
| 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 Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 24 | // constants and configuration |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 25 | var dialogId = 'remove-intent-dialog', |
| 26 | dialogOpts = { |
| 27 | edge: 'right' |
Simon Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 28 | }; |
| 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 KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 145 | |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 146 | angular.module('ovIntent', []) |
| 147 | .controller('OvIntentCtrl', |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 148 | ['$log', '$scope', 'TableBuilderService', 'NavService', |
Simon Hunt | c5489c9 | 2017-01-30 17:50:48 -0800 | [diff] [blame] | 149 | 'TopoOverlayService', 'TopoTrafficService', 'DialogService', |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 150 | |
Simon Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 151 | 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 Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 160 | |
| 161 | dropdown = d3.select('div.show-intent-btn .dropdown'); |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 162 | |
Simon Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 163 | // set up scope function references... |
| 164 | $scope.showIntent = showIntent; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 165 | |
Simon Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 166 | $scope.canShowIntent = function() { |
| 167 | var d = $scope.intentData; |
| 168 | return d && tov.overlaysAcceptingIntents(d.intentType).length > 0; |
| 169 | }; |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 170 | |
Simon Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 171 | // build the table |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 172 | tbs.buildTable({ |
| 173 | scope: $scope, |
| 174 | tag: 'intent', |
| 175 | selCb: selCb, |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 176 | respCb: respCb, |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 177 | idKey: 'key' |
| 178 | }); |
| 179 | |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 180 | |
| 181 | // TODO: clean up the following code... |
| 182 | |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 183 | $scope.isIntentInstalled = function () { |
| 184 | return $scope.intentState === 'Installed'; |
| 185 | }; |
| 186 | |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 187 | $scope.isIntentWithdrawn = function () { |
| 188 | return $scope.intentState === 'Withdrawn'; |
| 189 | }; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 190 | |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 191 | $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 KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 201 | function executeAction(action) { |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 202 | var content = ds.createDiv(), |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 203 | txt, |
| 204 | bPurge = action === 'purge'; |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 205 | |
| 206 | $scope.intentData.intentPurge = bPurge; |
| 207 | |
| 208 | content.append('p'). |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 209 | text('Are you sure you want to '+ action + |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 210 | ' the selected intent?'); |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 211 | |
| 212 | function dOk() { |
| 213 | var d = $scope.intentData; |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 214 | $log.debug(d); |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 215 | d && (action === 'resubmit' ? tts.resubmitIntent(d) : |
| 216 | tts.removeIntent(d)); |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 217 | $scope.fired = true; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 218 | } |
| 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 KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 231 | } |
Simon Hunt | 441c9ae | 2017-02-03 18:22:31 -0800 | [diff] [blame] | 232 | |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 233 | 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 KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 256 | |
| 257 | $scope.deactivateIntent = function () { |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 258 | executeAction("withdraw"); |
| 259 | }; |
| 260 | |
| 261 | $scope.resubmitIntent = function () { |
| 262 | executeAction("resubmit"); |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | $scope.purgeIntent = function () { |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 266 | executeAction("purge"); |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 267 | }; |
| 268 | |
Kavitha Alagesan | 98c0006 | 2016-08-23 18:20:42 -0700 | [diff] [blame] | 269 | $scope.briefToggle = function () { |
| 270 | $scope.brief = !$scope.brief; |
| 271 | }; |
| 272 | |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 273 | $scope.$on('$destroy', function () { |
| 274 | ds.closeDialog(); |
| 275 | $log.debug('OvIntentCtrl has been destroyed'); |
| 276 | }); |
| 277 | |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 278 | $scope.purgeIntents = function () { |
| 279 | executeActions("purgeIntents"); |
| 280 | }; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 281 | $log.debug('OvIntentCtrl has been created'); |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 282 | }]); |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 283 | }()); |