daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | OpenStack Networking UI Service |
| 19 | |
| 20 | Provides a mechanism to highlight hosts, devices and links according to |
| 21 | a virtual network. Also provides trace functionality to prove that |
| 22 | flow rules for the specific vm are installed appropriately. |
| 23 | */ |
| 24 | |
| 25 | (function () { |
| 26 | 'use strict'; |
| 27 | |
| 28 | // injected refs |
| 29 | var $log, fs, flash, wss, ds; |
| 30 | |
| 31 | // constants |
| 32 | var displayStart = 'openstackNetworkingUiStart', |
| 33 | displayUpdate = 'openstackNetworkingUiUpdate', |
| 34 | displayStop = 'openstackNetworkingUiStop', |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame^] | 35 | flowStatsAddRequest = 'flowStatsAddRequest', |
| 36 | flowStatsRemoveRequest = 'flowStatsRemoveRequest', |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 37 | flowTraceRequest = 'flowTraceRequest'; |
| 38 | |
| 39 | // internal state |
| 40 | var currentMode = null; |
| 41 | |
| 42 | // === --------------------------- |
| 43 | // === Helper functions |
| 44 | |
| 45 | function sendDisplayStart(mode) { |
| 46 | wss.sendEvent(displayStart, { |
| 47 | mode: mode |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | function sendDisplayUpdate(what) { |
| 52 | wss.sendEvent(displayUpdate, { |
| 53 | id: what ? what.id : '' |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | function sendDisplayStop() { |
| 58 | wss.sendEvent(displayStop); |
| 59 | } |
| 60 | |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame^] | 61 | function sendFlowStatsAddRequest(src, dst) { |
| 62 | wss.sendEvent(flowStatsAddRequest, { |
| 63 | srcIp: src, |
| 64 | dstIp: dst, |
| 65 | }); |
| 66 | $log.info('FlowStatsAdd request message is sent.'); |
| 67 | flash.flash('sendFlowStatsAddRequest called'); |
| 68 | } |
| 69 | |
| 70 | function sendFlowStatsRemoveRequest(src, dst) { |
| 71 | wss.sendEvent(flowStatsRemoveRequest, { |
| 72 | srcIp: src, |
| 73 | dstIp: dst, |
| 74 | }); |
| 75 | $log.info('FlowStatsRemove request message is sent.'); |
| 76 | flash.flash('sendFlowStatsRemoveRequest called'); |
| 77 | } |
| 78 | |
Daniel Park | 577b69c | 2018-07-16 17:29:34 +0900 | [diff] [blame] | 79 | function sendFlowTraceRequest(src, dst, srcDeviceId, dstDeviceId) { |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 80 | wss.sendEvent(flowTraceRequest, { |
| 81 | srcIp: src, |
Daniel Park | 577b69c | 2018-07-16 17:29:34 +0900 | [diff] [blame] | 82 | dstIp: dst, |
| 83 | srcDeviceId: srcDeviceId, |
| 84 | dstDeviceId: dstDeviceId, |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 85 | }); |
| 86 | flash.flash('sendFlowTraceRequest called'); |
| 87 | } |
| 88 | |
| 89 | // === --------------------------- |
| 90 | // === Main API functions |
| 91 | |
| 92 | function startDisplay(mode) { |
| 93 | if (currentMode === mode) { |
| 94 | $log.debug('(in mode', mode, 'already)'); |
| 95 | } else { |
| 96 | currentMode = mode; |
| 97 | sendDisplayStart(mode); |
| 98 | |
| 99 | flash.flash('Starting Openstack Networking UI mode'); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | function updateDisplay(m) { |
| 104 | if (currentMode) { |
| 105 | sendDisplayUpdate(m); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | function stopDisplay() { |
| 110 | if (currentMode) { |
| 111 | currentMode = null; |
| 112 | sendDisplayStop(); |
| 113 | flash.flash('Canceling Openstack Networking UI Overlay mode'); |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 119 | function dOk() { |
| 120 | ds.closeDialog(); |
| 121 | } |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame^] | 122 | |
| 123 | function openFlowStatsAddResultDialog(data) { |
| 124 | $log.info('Addition Result message: ', data); |
| 125 | var flowStatsAddResultDialogId = 'flowStatsAddResultDialogId', |
| 126 | flowStatsAddResultDialogOpt = { |
| 127 | width: 650, |
| 128 | edge: 'left', |
| 129 | margin: 20, |
| 130 | hideMargin: -20 |
| 131 | } |
| 132 | var statsSuccess = data.statsSuccess == true ? "SUCCESS" : "FALSE"; |
| 133 | ds.openDialog(flowStatsAddResultDialogId, flowStatsAddResultDialogOpt) |
| 134 | .setTitle('Flow Stats Addition Result: ' + statsSuccess) |
| 135 | .addContent(createStatsAddResultInfoDiv(data)) |
| 136 | .addOk(dOk, 'Close') |
| 137 | .bindKeys(); |
| 138 | } |
| 139 | |
| 140 | function createStatsAddResultInfoDiv(data) { |
| 141 | var texts = ds.createDiv('flowStatsAddResult'); |
| 142 | texts.append('p').text('Result of addition: ' + data.statsSuccess); |
| 143 | return texts; |
| 144 | } |
| 145 | |
| 146 | function openFlowStatsRemoveResultDialog(data) { |
| 147 | $log.info('Removal Result message: ', data); |
| 148 | var flowStatsRemoveResultDialogId = 'flowStatsRemoveResultDialogId', |
| 149 | flowStatsRemoveResultDialogOpt = { |
| 150 | width: 650, |
| 151 | edge: 'left', |
| 152 | margin: 20, |
| 153 | hideMargin: -20 |
| 154 | } |
| 155 | var statsSuccess = data.statsSuccess == true ? "SUCCESS" : "FALSE"; |
| 156 | ds.openDialog(flowStatsRemoveResultDialogId, flowStatsRemoveResultDialogOpt) |
| 157 | .setTitle('Flow Stats Removal Result: ' + statsSuccess) |
| 158 | .addContent(createStatsRemoveResultInfoDiv(data)) |
| 159 | .addOk(dOk, 'Close') |
| 160 | .bindKeys(); |
| 161 | } |
| 162 | |
| 163 | function createStatsRemoveResultInfoDiv(data) { |
| 164 | var texts = ds.createDiv('flowStatsRemoveResult'); |
| 165 | texts.append('p').text('Result of removal: ' + data.statsSuccess); |
| 166 | return texts; |
| 167 | } |
| 168 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 169 | function openFlowTraceResultDialog(data) { |
| 170 | var flowTraceResultDialogId = 'flowTraceResultDialogId', |
| 171 | flowTraceResultDialogOpt = { |
| 172 | width: 650, |
| 173 | edge: 'left', |
| 174 | margin: 20, |
| 175 | hideMargin: -20 |
| 176 | } |
Daniel Park | 577b69c | 2018-07-16 17:29:34 +0900 | [diff] [blame] | 177 | var traceSuccess = data.traceSuccess == true ? "SUCCESS" : "FALSE"; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 178 | ds.openDialog(flowTraceResultDialogId, flowTraceResultDialogOpt) |
| 179 | .setTitle('Flow Trace Result: ' + traceSuccess) |
| 180 | .addContent(createTraceResultInfoDiv(data)) |
| 181 | .addOk(dOk, 'Close') |
| 182 | .bindKeys(); |
| 183 | } |
| 184 | |
| 185 | function createTraceResultInfoDiv(data) { |
| 186 | var texts = ds.createDiv('flowTraceResult'); |
| 187 | |
| 188 | texts.append('div').attr("class", "table-header"); |
| 189 | texts.append('div').attr("class", "table-body"); |
| 190 | |
| 191 | texts.select('.table-header').append('table').append('tbody').append('tr'); |
| 192 | texts.select('.table-body').append('table').append('tbody'); |
| 193 | |
| 194 | |
| 195 | var theaderSelection = texts.select('.table-header') |
| 196 | .select('table').select('tbody').select('tr'); |
| 197 | |
| 198 | theaderSelection.append('td').text('Node'); |
| 199 | theaderSelection.append('td').text('Table Id'); |
| 200 | theaderSelection.append('td').text('Priority'); |
| 201 | theaderSelection.append('td').text('Selector'); |
| 202 | theaderSelection.append('td').text('Action'); |
| 203 | |
| 204 | var tbodySelection = texts.select('.table-body').select('table').select('tbody'); |
| 205 | var rowNum = 1; |
| 206 | |
Daniel Park | 577b69c | 2018-07-16 17:29:34 +0900 | [diff] [blame] | 207 | data.traceResult.forEach(function(result) { |
| 208 | result.flowRules.forEach(function(flowRule) { |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 209 | tbodySelection.append('tr'); |
| 210 | var tbodyTrSelection = tbodySelection.select('tr:nth-child(' + rowNum + ')'); |
Daniel Park | 577b69c | 2018-07-16 17:29:34 +0900 | [diff] [blame] | 211 | tbodyTrSelection.append('td').text(result.traceNodeName); |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 212 | tbodyTrSelection.append('td').text(flowRule.table); |
| 213 | tbodyTrSelection.append('td').text(flowRule.priority); |
Daniel Park | 577b69c | 2018-07-16 17:29:34 +0900 | [diff] [blame] | 214 | tbodyTrSelection.append('td').text(flowRule.selector); |
| 215 | tbodyTrSelection.append('td').text(flowRule.actions); |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 216 | if (jsonToSring(flowRule.actions).includes("drop")) { |
| 217 | tbodyTrSelection.attr("class", "drop"); |
| 218 | } |
| 219 | rowNum++; |
| 220 | }); |
| 221 | |
| 222 | }); |
| 223 | |
| 224 | return texts; |
| 225 | } |
| 226 | |
| 227 | function jsonToSring(jsonData) { |
| 228 | var result = []; |
| 229 | for (var key in jsonData) { |
| 230 | result.push(key + ':' + jsonData[key]); |
| 231 | } |
| 232 | |
| 233 | return result.join('/'); |
| 234 | |
| 235 | } |
| 236 | |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame^] | 237 | function flowStatsAddResult(data) { |
| 238 | flash.flash('flowStatsAddResult called'); |
| 239 | $log.debug(data); |
| 240 | |
| 241 | openFlowStatsAddResultDialog(data) |
| 242 | } |
| 243 | |
| 244 | function flowStatsRemoveResult(data) { |
| 245 | flash.flash('flowStatsRemoveResult called'); |
| 246 | $log.debug(data); |
| 247 | |
| 248 | openFlowStatsRemoveResultDialog(data) |
| 249 | } |
| 250 | |
| 251 | |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 252 | function flowTraceResult(data) { |
| 253 | flash.flash('flowTraceResult called'); |
| 254 | $log.debug(data); |
| 255 | |
| 256 | openFlowTraceResultDialog(data) |
| 257 | } |
| 258 | |
| 259 | // === --------------------------- |
| 260 | // === Module Factory Definition |
| 261 | |
| 262 | angular.module('ovSonaTopov', []) |
| 263 | .factory('SonaTopovService', |
| 264 | ['$log', 'FnService', 'FlashService', 'WebSocketService', 'DialogService', |
| 265 | |
| 266 | function (_$log_, _fs_, _flash_, _wss_, _ds_) { |
| 267 | $log = _$log_; |
| 268 | fs = _fs_; |
| 269 | flash = _flash_; |
| 270 | wss = _wss_; |
| 271 | ds = _ds_; |
| 272 | |
| 273 | return { |
| 274 | startDisplay: startDisplay, |
| 275 | updateDisplay: updateDisplay, |
| 276 | stopDisplay: stopDisplay, |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame^] | 277 | flowStatsAddResult: flowStatsAddResult, |
| 278 | flowStatsRemoveResult: flowStatsRemoveResult, |
| 279 | sendFlowStatsAddRequest: sendFlowStatsAddRequest, |
| 280 | sendFlowStatsRemoveRequest: sendFlowStatsRemoveRequest, |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 281 | flowTraceResult: flowTraceResult, |
| 282 | sendFlowTraceRequest: sendFlowTraceRequest, |
| 283 | }; |
| 284 | }]); |
| 285 | }()); |