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 | |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 24 | var dialogId = 'remove-intent-dialog', |
| 25 | dialogOpts = { |
| 26 | edge: 'right' |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 27 | }, |
| 28 | dropdown; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 29 | |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 30 | angular.module('ovIntent', []) |
| 31 | .controller('OvIntentCtrl', |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 32 | ['$log', '$scope', 'TableBuilderService', 'NavService', |
Simon Hunt | c5489c9 | 2017-01-30 17:50:48 -0800 | [diff] [blame] | 33 | 'TopoOverlayService', 'TopoTrafficService', 'DialogService', |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 34 | |
Simon Hunt | c5489c9 | 2017-01-30 17:50:48 -0800 | [diff] [blame] | 35 | function ($log, $scope, tbs, ns, tov, tts, ds) { |
Kavitha Alagesan | 98c0006 | 2016-08-23 18:20:42 -0700 | [diff] [blame] | 36 | $scope.briefTip = 'Switch to brief view'; |
| 37 | $scope.detailTip = 'Switch to detailed view'; |
| 38 | $scope.brief = true; |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 39 | $scope.intentState = 'NA'; |
| 40 | $scope.fired = false; |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 41 | $scope.showOverlays = false; |
| 42 | |
| 43 | dropdown = d3.select('div.show-intent-btn .dropdown'); |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 44 | |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 45 | function selCb($event, row) { |
| 46 | $log.debug('Got a click on:', row); |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 47 | var m = /(\d+)\s:\s(.*)/.exec(row.appId), |
| 48 | id = m ? m[1] : null, |
| 49 | name = m ? m[2] : null; |
| 50 | |
Simon Hunt | 144bd79 | 2015-10-28 11:46:36 -0700 | [diff] [blame] | 51 | $scope.intentData = ($scope.selId && m) ? { |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 52 | appId: id, |
| 53 | appName: name, |
| 54 | key: row.key |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 55 | } : null; |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 56 | |
| 57 | $scope.intentState = row.state; |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 58 | showDropdown(false); |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 59 | } |
| 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 Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | tbs.buildTable({ |
| 71 | scope: $scope, |
| 72 | tag: 'intent', |
| 73 | selCb: selCb, |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 74 | respCb: respCb, |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 75 | idKey: 'key' |
| 76 | }); |
| 77 | |
| 78 | $scope.topoTip = 'Show selected intent on topology view'; |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 79 | $scope.resubmitTip = 'Resubmit selected intent'; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 80 | $scope.deactivateTip = 'Remove selected intent'; |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 81 | $scope.purgeTip = 'Purge selected intent'; |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 82 | $scope.purgeAllTip = 'Purge withdrawn intents'; |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 83 | |
Simon Hunt | c5489c9 | 2017-01-30 17:50:48 -0800 | [diff] [blame] | 84 | |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 85 | 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 Hunt | c5489c9 | 2017-01-30 17:50:48 -0800 | [diff] [blame] | 101 | ns.navTo('topo', d); |
| 102 | } |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 103 | |
| 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 Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 141 | |
| 142 | |
| 143 | // TODO: clean up the following code... |
| 144 | |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 145 | $scope.isIntentInstalled = function () { |
| 146 | return $scope.intentState === 'Installed'; |
| 147 | }; |
| 148 | |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 149 | $scope.isIntentWithdrawn = function () { |
| 150 | return $scope.intentState === 'Withdrawn'; |
| 151 | }; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 152 | |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 153 | $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 KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 163 | function executeAction(action) { |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 164 | var content = ds.createDiv(), |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 165 | txt, |
| 166 | bPurge = action === 'purge'; |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 167 | |
| 168 | $scope.intentData.intentPurge = bPurge; |
| 169 | |
| 170 | content.append('p'). |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 171 | text('Are you sure you want to '+ action + |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 172 | ' the selected intent?'); |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 173 | |
| 174 | function dOk() { |
| 175 | var d = $scope.intentData; |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 176 | $log.debug(d); |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 177 | d && (action === 'resubmit' ? tts.resubmitIntent(d) : |
| 178 | tts.removeIntent(d)); |
Viswanath KSP | 317f329 | 2016-09-04 14:13:22 +0530 | [diff] [blame] | 179 | $scope.fired = true; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 180 | } |
| 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 KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 193 | } |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 194 | 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 KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 217 | |
| 218 | $scope.deactivateIntent = function () { |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 219 | executeAction("withdraw"); |
| 220 | }; |
| 221 | |
| 222 | $scope.resubmitIntent = function () { |
| 223 | executeAction("resubmit"); |
Viswanath KSP | 813a20d | 2016-09-13 04:25:41 +0530 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | $scope.purgeIntent = function () { |
Viswanath KSP | 14aee09 | 2016-10-02 01:47:40 +0530 | [diff] [blame] | 227 | executeAction("purge"); |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 228 | }; |
| 229 | |
Kavitha Alagesan | 98c0006 | 2016-08-23 18:20:42 -0700 | [diff] [blame] | 230 | $scope.briefToggle = function () { |
| 231 | $scope.brief = !$scope.brief; |
| 232 | }; |
| 233 | |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 234 | $scope.$on('$destroy', function () { |
| 235 | ds.closeDialog(); |
| 236 | $log.debug('OvIntentCtrl has been destroyed'); |
| 237 | }); |
| 238 | |
Deepa Vaddireddy | 6334092 | 2017-01-19 08:15:31 +0530 | [diff] [blame] | 239 | $scope.purgeIntents = function () { |
| 240 | executeActions("purgeIntents"); |
| 241 | }; |
Viswanath KSP | 0f29770 | 2016-08-13 18:02:43 +0530 | [diff] [blame] | 242 | $log.debug('OvIntentCtrl has been created'); |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 243 | }]); |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 244 | }()); |