blob: 719703af41527e980aa5938456c5d13c6dd0abba [file] [log] [blame]
daniel park128c52c2017-09-04 13:15:51 +09001/*
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
Daniel Park577b69c2018-07-16 17:29:34 +090059 function sendFlowTraceRequest(src, dst, srcDeviceId, dstDeviceId) {
daniel park128c52c2017-09-04 13:15:51 +090060 wss.sendEvent(flowTraceRequest, {
61 srcIp: src,
Daniel Park577b69c2018-07-16 17:29:34 +090062 dstIp: dst,
63 srcDeviceId: srcDeviceId,
64 dstDeviceId: dstDeviceId,
daniel park128c52c2017-09-04 13:15:51 +090065 });
66 flash.flash('sendFlowTraceRequest called');
67 }
68
69 // === ---------------------------
70 // === Main API functions
71
72 function startDisplay(mode) {
73 if (currentMode === mode) {
74 $log.debug('(in mode', mode, 'already)');
75 } else {
76 currentMode = mode;
77 sendDisplayStart(mode);
78
79 flash.flash('Starting Openstack Networking UI mode');
80 }
81 }
82
83 function updateDisplay(m) {
84 if (currentMode) {
85 sendDisplayUpdate(m);
86 }
87 }
88
89 function stopDisplay() {
90 if (currentMode) {
91 currentMode = null;
92 sendDisplayStop();
93 flash.flash('Canceling Openstack Networking UI Overlay mode');
94 return true;
95 }
96 return false;
97 }
98
99
100 function dOk() {
101 ds.closeDialog();
102 }
103 function openFlowTraceResultDialog(data) {
104 var flowTraceResultDialogId = 'flowTraceResultDialogId',
105 flowTraceResultDialogOpt = {
106 width: 650,
107 edge: 'left',
108 margin: 20,
109 hideMargin: -20
110 }
Daniel Park577b69c2018-07-16 17:29:34 +0900111 var traceSuccess = data.traceSuccess == true ? "SUCCESS" : "FALSE";
daniel park128c52c2017-09-04 13:15:51 +0900112 ds.openDialog(flowTraceResultDialogId, flowTraceResultDialogOpt)
113 .setTitle('Flow Trace Result: ' + traceSuccess)
114 .addContent(createTraceResultInfoDiv(data))
115 .addOk(dOk, 'Close')
116 .bindKeys();
117 }
118
119 function createTraceResultInfoDiv(data) {
120 var texts = ds.createDiv('flowTraceResult');
121
122 texts.append('div').attr("class", "table-header");
123 texts.append('div').attr("class", "table-body");
124
125 texts.select('.table-header').append('table').append('tbody').append('tr');
126 texts.select('.table-body').append('table').append('tbody');
127
128
129 var theaderSelection = texts.select('.table-header')
130 .select('table').select('tbody').select('tr');
131
132 theaderSelection.append('td').text('Node');
133 theaderSelection.append('td').text('Table Id');
134 theaderSelection.append('td').text('Priority');
135 theaderSelection.append('td').text('Selector');
136 theaderSelection.append('td').text('Action');
137
138 var tbodySelection = texts.select('.table-body').select('table').select('tbody');
139 var rowNum = 1;
140
Daniel Park577b69c2018-07-16 17:29:34 +0900141 data.traceResult.forEach(function(result) {
142 result.flowRules.forEach(function(flowRule) {
daniel park128c52c2017-09-04 13:15:51 +0900143 tbodySelection.append('tr');
144 var tbodyTrSelection = tbodySelection.select('tr:nth-child(' + rowNum + ')');
Daniel Park577b69c2018-07-16 17:29:34 +0900145 tbodyTrSelection.append('td').text(result.traceNodeName);
daniel park128c52c2017-09-04 13:15:51 +0900146 tbodyTrSelection.append('td').text(flowRule.table);
147 tbodyTrSelection.append('td').text(flowRule.priority);
Daniel Park577b69c2018-07-16 17:29:34 +0900148 tbodyTrSelection.append('td').text(flowRule.selector);
149 tbodyTrSelection.append('td').text(flowRule.actions);
daniel park128c52c2017-09-04 13:15:51 +0900150 if (jsonToSring(flowRule.actions).includes("drop")) {
151 tbodyTrSelection.attr("class", "drop");
152 }
153 rowNum++;
154 });
155
156 });
157
158 return texts;
159 }
160
161 function jsonToSring(jsonData) {
162 var result = [];
163 for (var key in jsonData) {
164 result.push(key + ':' + jsonData[key]);
165 }
166
167 return result.join('/');
168
169 }
170
171 function flowTraceResult(data) {
172 flash.flash('flowTraceResult called');
173 $log.debug(data);
174
175 openFlowTraceResultDialog(data)
176 }
177
178 // === ---------------------------
179 // === Module Factory Definition
180
181 angular.module('ovSonaTopov', [])
182 .factory('SonaTopovService',
183 ['$log', 'FnService', 'FlashService', 'WebSocketService', 'DialogService',
184
185 function (_$log_, _fs_, _flash_, _wss_, _ds_) {
186 $log = _$log_;
187 fs = _fs_;
188 flash = _flash_;
189 wss = _wss_;
190 ds = _ds_;
191
192 return {
193 startDisplay: startDisplay,
194 updateDisplay: updateDisplay,
195 stopDisplay: stopDisplay,
196 flowTraceResult: flowTraceResult,
197 sendFlowTraceRequest: sendFlowTraceRequest,
198 };
199 }]);
200}());