Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [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 | */ |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 16 | import { FnService } from '../util/fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 17 | import { LogService } from '../log.service'; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 18 | import { WebSocketService } from '../remote/websocket.service'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 19 | import { Observable, of } from 'rxjs'; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 20 | |
| 21 | const REFRESH_INTERVAL = 2000; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 22 | const SEARCH_REGEX = '\\W'; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 23 | |
| 24 | /** |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 25 | * Model of table annotations within this table base class |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 26 | */ |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 27 | export interface TableAnnots { |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 28 | noRowsMsg: string; |
| 29 | } |
| 30 | |
| 31 | /** |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 32 | * A model of data returned from Web Socket in a TableResponse |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 33 | * |
| 34 | * There is an interface extending from this one in the parent component |
| 35 | */ |
| 36 | export interface TableResponse { |
| 37 | annots: any; |
| 38 | // There will be other parts to the response depending on table type |
| 39 | // Expect one called tag+'s' e.g. devices or apps |
| 40 | } |
| 41 | |
| 42 | /** |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 43 | * A criteria for filtering the tableData |
| 44 | */ |
| 45 | export interface TableFilter { |
| 46 | queryStr: string; |
| 47 | queryBy: string; |
| 48 | sortBy: string; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Enumerated values for the sort dir |
| 53 | */ |
| 54 | export enum SortDir { |
| 55 | asc = 'asc', desc = 'desc' |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * A structure to format sort params for table |
| 60 | * This is sent to WebSocket as part of table request |
| 61 | */ |
| 62 | export interface SortParams { |
| 63 | firstCol: string; |
| 64 | firstDir: SortDir; |
| 65 | secondCol: string; |
| 66 | secondDir: SortDir; |
| 67 | } |
| 68 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 69 | export interface PayloadParams { |
| 70 | devId: string; |
| 71 | } |
| 72 | |
| 73 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 74 | /** |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 75 | * ONOS GUI -- Widget -- Table Base class |
| 76 | */ |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 77 | export abstract class TableBaseImpl { |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 78 | // attributes from the interface |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 79 | public annots: TableAnnots; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 80 | protected changedData: string[] = []; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 81 | protected payloadParams: PayloadParams; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 82 | protected sortParams: SortParams; |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 83 | public selectCallback; // Function |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 84 | protected parentSelCb = null; |
| 85 | protected responseCallback; // Function |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 86 | public loadingIconShown: boolean = false; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 87 | selId: string = undefined; |
| 88 | tableData: any[] = []; |
| 89 | tableDataFilter: TableFilter; |
| 90 | toggleRefresh; // Function |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 91 | autoRefresh: boolean = true; |
| 92 | autoRefreshTip: string = 'Toggle auto refresh'; // TODO: get LION string |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 93 | |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 94 | readonly root: string; |
| 95 | readonly req: string; |
| 96 | readonly resp: string; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 97 | private refreshPromise: any = null; |
| 98 | private handlers: string[] = []; |
| 99 | |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 100 | protected constructor( |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 101 | protected fs: FnService, |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 102 | protected log: LogService, |
| 103 | protected wss: WebSocketService, |
| 104 | protected tag: string, |
| 105 | protected idKey: string = 'id', |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 106 | protected selCb = () => ({}) // Function |
| 107 | ) { |
| 108 | this.root = tag + 's'; |
| 109 | this.req = tag + 'DataRequest'; |
| 110 | this.resp = tag + 'DataResponse'; |
| 111 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 112 | this.selectCallback = this.rowSelectionCb; |
| 113 | this.toggleRefresh = () => { |
| 114 | this.autoRefresh = !this.autoRefresh; |
| 115 | this.autoRefresh ? this.startRefresh() : this.stopRefresh(); |
| 116 | }; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 117 | |
| 118 | // Mapped to the search and searchBy inputs in template |
| 119 | // Changes are handled through TableFilterPipe |
| 120 | this.tableDataFilter = <TableFilter>{ |
| 121 | queryStr: '', |
| 122 | queryBy: '$', |
| 123 | }; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | init() { |
| 127 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 128 | [this.resp, (data) => this.tableDataResponseCb(data)] |
| 129 | ])); |
| 130 | this.handlers.push(this.resp); |
| 131 | |
| 132 | this.annots = <TableAnnots>{ |
| 133 | noRowsMsg: '' |
| 134 | }; |
| 135 | |
| 136 | // Now send the WebSocket request and make it repeat every 2 seconds |
| 137 | this.requestTableData(); |
| 138 | this.startRefresh(); |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 139 | this.log.debug('TableBase initialized. Calling ', this.req, |
| 140 | 'every', REFRESH_INTERVAL, 'ms'); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | destroy() { |
| 144 | this.wss.unbindHandlers(this.handlers); |
| 145 | this.stopRefresh(); |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 146 | this.loadingIconShown = false; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * A callback that executes when the table data that was requested |
| 151 | * on WebSocketService arrives. |
| 152 | * |
| 153 | * Happens every 2 seconds |
| 154 | */ |
| 155 | tableDataResponseCb(data: TableResponse) { |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 156 | this.loadingIconShown = false; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 157 | |
| 158 | const newTableData: any[] = Array.from(data[this.root]); |
| 159 | this.annots.noRowsMsg = data.annots.no_rows_msg; |
| 160 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 161 | // If the parents onResp() function is set then call it |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 162 | if (this.responseCallback) { |
| 163 | this.responseCallback(data); |
| 164 | } |
| 165 | this.changedData = []; |
| 166 | |
| 167 | // checks if data changed for row flashing |
| 168 | if (JSON.stringify(newTableData) !== JSON.stringify(this.tableData)) { |
| 169 | this.log.debug('table data has changed'); |
| 170 | const oldTableData: any[] = this.tableData; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 171 | this.tableData = [...newTableData]; // ES6 spread syntax |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 172 | // only flash the row if the data already exists |
| 173 | if (oldTableData.length > 0) { |
| 174 | for (const idx in newTableData) { |
| 175 | if (!this.fs.containsObj(oldTableData, newTableData[idx])) { |
| 176 | this.changedData.push(newTableData[idx][this.idKey]); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Table Data Request |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 185 | * Pass in sort parameters and the set will be returned sorted |
| 186 | * In the old GUI there was also a query parameter, but this was not |
| 187 | * implemented on the server end |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 188 | */ |
| 189 | requestTableData() { |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 190 | const p = (<any>Object).assign({}, this.sortParams, this.payloadParams); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 191 | |
| 192 | // Allow it to sit in pending events |
| 193 | if (this.wss.isConnected()) { |
| 194 | if (this.fs.debugOn('table')) { |
| 195 | this.log.debug('Table data REQUEST:', this.req, p); |
| 196 | } |
| 197 | this.wss.sendEvent(this.req, p); |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 198 | this.loadingIconShown = true; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Row Selected |
| 204 | */ |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 205 | rowSelectionCb(event: any, selRow: any): void { |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 206 | const selId: string = selRow[this.idKey]; |
| 207 | this.selId = (this.selId === selId) ? undefined : selId; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 208 | this.log.debug('Row', selId, 'selected'); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 209 | if (this.parentSelCb) { |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 210 | this.parentSelCb(event, selRow); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * autoRefresh functions |
| 216 | */ |
| 217 | startRefresh() { |
| 218 | this.refreshPromise = |
| 219 | setInterval(() => { |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 220 | if (!this.loadingIconShown) { |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 221 | if (this.fs.debugOn('table')) { |
| 222 | this.log.debug('Refreshing ' + this.root + ' page'); |
| 223 | } |
| 224 | this.requestTableData(); |
| 225 | } |
| 226 | }, REFRESH_INTERVAL); |
| 227 | } |
| 228 | |
| 229 | stopRefresh() { |
| 230 | if (this.refreshPromise) { |
| 231 | clearInterval(this.refreshPromise); |
| 232 | this.refreshPromise = null; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | isChanged(id: string): boolean { |
| 237 | return (this.fs.inArray(id, this.changedData) === -1) ? false : true; |
| 238 | } |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * A dummy implementation of the lionFn until the response is received and the LION |
| 242 | * bundle is received from the WebSocket |
| 243 | */ |
| 244 | dummyLion(key: string): string { |
| 245 | return '%' + key + '%'; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Change the sort order of the data returned |
| 250 | * |
| 251 | * sortParams are passed to the server by WebSocket and the data is |
| 252 | * returned sorted |
| 253 | * |
| 254 | * This is usually assigned to the (click) event on a column, and the column |
| 255 | * name passed in e.g. (click)="onSort('origin') |
| 256 | * If the column that is passed in is already the firstCol, then reverse its direction |
| 257 | * If a new column is passed in, then make the existing col the 2nd sort order |
| 258 | */ |
| 259 | onSort(colName: string) { |
| 260 | if (this.sortParams.firstCol === colName) { |
| 261 | if (this.sortParams.firstDir === SortDir.desc) { |
| 262 | this.sortParams.firstDir = SortDir.asc; |
| 263 | return; |
| 264 | } else { |
| 265 | this.sortParams.firstDir = SortDir.desc; |
| 266 | return; |
| 267 | } |
| 268 | } else { |
| 269 | this.sortParams.secondCol = this.sortParams.firstCol; |
| 270 | this.sortParams.secondDir = this.sortParams.firstDir; |
| 271 | this.sortParams.firstCol = colName; |
| 272 | this.sortParams.firstDir = SortDir.desc; |
| 273 | } |
| 274 | this.log.debug('Sort params', this.sortParams); |
| 275 | this.requestTableData(); |
| 276 | } |
| 277 | |
| 278 | sortIcon(column: string): string { |
| 279 | if (this.sortParams.firstCol === column) { |
| 280 | if (this.sortParams.firstDir === SortDir.asc) { |
| 281 | return 'upArrow'; |
| 282 | } else { |
| 283 | return 'downArrow'; |
| 284 | } |
| 285 | } else { |
| 286 | return ''; |
| 287 | } |
| 288 | } |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * De-selects the row |
| 292 | */ |
| 293 | deselectRow(event) { |
| 294 | this.log.debug('Details panel close event'); |
| 295 | this.selId = event; |
| 296 | } |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 297 | } |