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', |
| 35 | flowTraceRequest = 'flowTraceRequest'; |
| 36 | |
| 37 | // internal state |
| 38 | var currentMode = null; |
| 39 | |
| 40 | // === --------------------------- |
| 41 | // === Helper functions |
| 42 | |
| 43 | function sendDisplayStart(mode) { |
| 44 | wss.sendEvent(displayStart, { |
| 45 | mode: mode |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | function sendDisplayUpdate(what) { |
| 50 | wss.sendEvent(displayUpdate, { |
| 51 | id: what ? what.id : '' |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | function sendDisplayStop() { |
| 56 | wss.sendEvent(displayStop); |
| 57 | } |
| 58 | |
| 59 | function sendFlowTraceRequest(src, dst) { |
| 60 | wss.sendEvent(flowTraceRequest, { |
| 61 | srcIp: src, |
| 62 | dstIp: dst |
| 63 | }); |
| 64 | flash.flash('sendFlowTraceRequest called'); |
| 65 | } |
| 66 | |
| 67 | // === --------------------------- |
| 68 | // === Main API functions |
| 69 | |
| 70 | function startDisplay(mode) { |
| 71 | if (currentMode === mode) { |
| 72 | $log.debug('(in mode', mode, 'already)'); |
| 73 | } else { |
| 74 | currentMode = mode; |
| 75 | sendDisplayStart(mode); |
| 76 | |
| 77 | flash.flash('Starting Openstack Networking UI mode'); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | function updateDisplay(m) { |
| 82 | if (currentMode) { |
| 83 | sendDisplayUpdate(m); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function stopDisplay() { |
| 88 | if (currentMode) { |
| 89 | currentMode = null; |
| 90 | sendDisplayStop(); |
| 91 | flash.flash('Canceling Openstack Networking UI Overlay mode'); |
| 92 | return true; |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | function dOk() { |
| 99 | ds.closeDialog(); |
| 100 | } |
| 101 | function openFlowTraceResultDialog(data) { |
| 102 | var flowTraceResultDialogId = 'flowTraceResultDialogId', |
| 103 | flowTraceResultDialogOpt = { |
| 104 | width: 650, |
| 105 | edge: 'left', |
| 106 | margin: 20, |
| 107 | hideMargin: -20 |
| 108 | } |
| 109 | var traceSuccess = data.trace_success == true ? "SUCCESS" : "FALSE"; |
| 110 | ds.openDialog(flowTraceResultDialogId, flowTraceResultDialogOpt) |
| 111 | .setTitle('Flow Trace Result: ' + traceSuccess) |
| 112 | .addContent(createTraceResultInfoDiv(data)) |
| 113 | .addOk(dOk, 'Close') |
| 114 | .bindKeys(); |
| 115 | } |
| 116 | |
| 117 | function createTraceResultInfoDiv(data) { |
| 118 | var texts = ds.createDiv('flowTraceResult'); |
| 119 | |
| 120 | texts.append('div').attr("class", "table-header"); |
| 121 | texts.append('div').attr("class", "table-body"); |
| 122 | |
| 123 | texts.select('.table-header').append('table').append('tbody').append('tr'); |
| 124 | texts.select('.table-body').append('table').append('tbody'); |
| 125 | |
| 126 | |
| 127 | var theaderSelection = texts.select('.table-header') |
| 128 | .select('table').select('tbody').select('tr'); |
| 129 | |
| 130 | theaderSelection.append('td').text('Node'); |
| 131 | theaderSelection.append('td').text('Table Id'); |
| 132 | theaderSelection.append('td').text('Priority'); |
| 133 | theaderSelection.append('td').text('Selector'); |
| 134 | theaderSelection.append('td').text('Action'); |
| 135 | |
| 136 | var tbodySelection = texts.select('.table-body').select('table').select('tbody'); |
| 137 | var rowNum = 1; |
| 138 | |
| 139 | data.trace_result.forEach(function(result) { |
| 140 | result.flow_rules.forEach(function(flowRule) { |
| 141 | tbodySelection.append('tr'); |
| 142 | var tbodyTrSelection = tbodySelection.select('tr:nth-child(' + rowNum + ')'); |
| 143 | tbodyTrSelection.append('td').text(result.trace_node_name); |
| 144 | tbodyTrSelection.append('td').text(flowRule.table); |
| 145 | tbodyTrSelection.append('td').text(flowRule.priority); |
| 146 | tbodyTrSelection.append('td').text(jsonToSring(flowRule.selector)); |
| 147 | tbodyTrSelection.append('td').text(jsonToSring(flowRule.actions)); |
| 148 | if (jsonToSring(flowRule.actions).includes("drop")) { |
| 149 | tbodyTrSelection.attr("class", "drop"); |
| 150 | } |
| 151 | rowNum++; |
| 152 | }); |
| 153 | |
| 154 | }); |
| 155 | |
| 156 | return texts; |
| 157 | } |
| 158 | |
| 159 | function jsonToSring(jsonData) { |
| 160 | var result = []; |
| 161 | for (var key in jsonData) { |
| 162 | result.push(key + ':' + jsonData[key]); |
| 163 | } |
| 164 | |
| 165 | return result.join('/'); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | function flowTraceResult(data) { |
| 170 | flash.flash('flowTraceResult called'); |
| 171 | $log.debug(data); |
| 172 | |
| 173 | openFlowTraceResultDialog(data) |
| 174 | } |
| 175 | |
| 176 | // === --------------------------- |
| 177 | // === Module Factory Definition |
| 178 | |
| 179 | angular.module('ovSonaTopov', []) |
| 180 | .factory('SonaTopovService', |
| 181 | ['$log', 'FnService', 'FlashService', 'WebSocketService', 'DialogService', |
| 182 | |
| 183 | function (_$log_, _fs_, _flash_, _wss_, _ds_) { |
| 184 | $log = _$log_; |
| 185 | fs = _fs_; |
| 186 | flash = _flash_; |
| 187 | wss = _wss_; |
| 188 | ds = _ds_; |
| 189 | |
| 190 | return { |
| 191 | startDisplay: startDisplay, |
| 192 | updateDisplay: updateDisplay, |
| 193 | stopDisplay: stopDisplay, |
| 194 | flowTraceResult: flowTraceResult, |
| 195 | sendFlowTraceRequest: sendFlowTraceRequest, |
| 196 | }; |
| 197 | }]); |
| 198 | }()); |