Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 19 | import * as io from 'socket.io-client'; |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 20 | import * as _ from 'lodash'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 21 | import {Subject, Observable} from 'rxjs/Rx'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 22 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 23 | |
| 24 | export interface IWSEvent { |
| 25 | model: string; |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 26 | skip_notification?: boolean; |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 27 | deleted?: boolean; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 28 | msg: { |
| 29 | changed_fields: string[], |
| 30 | object?: any, |
| 31 | pk?: number |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | export interface IWSEventService { |
| 36 | list(): Observable<IWSEvent>; |
| 37 | } |
| 38 | |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 39 | export class WebSocketEvent implements IWSEventService { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 41 | static $inject = [ |
| 42 | 'AppConfig', |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 43 | 'SocketIo', |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 44 | '$log' |
| 45 | ]; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 46 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 47 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 48 | private _events: Subject<IWSEvent> = new Subject<IWSEvent>(); |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 49 | private socket; |
| 50 | constructor( |
| 51 | private AppConfig: IXosAppConfig, |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 52 | private SocketIo: any, |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 53 | private $log: ng.ILogService |
| 54 | ) { |
| 55 | // NOTE list of field that are not useful to the UI |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 56 | const ignoredFields: string[] = ['created', 'updated', 'backend_register', 'backend_status', 'policy_status']; |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 58 | this.socket = this.SocketIo.socket; |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 59 | |
| 60 | this.socket.on('remove', (data: IWSEvent): void => { |
| 61 | this.$log.info(`[WebSocket] Received Remove Event for: ${data.model} [${data.msg.pk}]`, data); |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 62 | |
| 63 | if (data.model.indexOf('_decl') > -1) { |
| 64 | // the GUI doesn't know about _decl models, |
| 65 | // send the event for the actual model |
| 66 | data.model = data.model.replace('_decl', ''); |
| 67 | } |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 68 | this._events.next(data); |
| 69 | |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 70 | // NOTE update observers of parent classes |
| 71 | this.updateParentClasses(data); |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 72 | }); |
| 73 | |
| 74 | this.socket.on('update', (data: IWSEvent): void => { |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 75 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 76 | if (data.msg.changed_fields.length === 0 || _.intersection(data.msg.changed_fields, ignoredFields).length === data.msg.changed_fields.length) { |
| 77 | // NOTE means that the only updated fields does not change anything in the UI, so don't send events around |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 81 | this.$log.info(`[WebSocket] Received Event for: ${data.model} [${data.msg.pk}]`, data); |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 83 | this._events.next(data); |
| 84 | |
| 85 | // NOTE update observers of parent classes |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 86 | this.updateParentClasses(data); |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 87 | |
| 88 | }); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 89 | } |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 90 | |
| 91 | public list() { |
Matteo Scandolo | b8d0637 | 2019-03-06 13:48:03 -0800 | [diff] [blame] | 92 | this.$log.debug(`[WebSocket] Active subscriptions: ${this._events.observers.length}`); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 93 | return this._events.asObservable(); |
| 94 | } |
Matteo Scandolo | 0654225 | 2018-01-30 13:35:55 -0800 | [diff] [blame] | 95 | |
| 96 | private updateParentClasses(data: IWSEvent) { |
| 97 | if (data.msg.object.class_names && angular.isString(data.msg.object.class_names)) { |
| 98 | const models = data.msg.object.class_names.split(','); |
| 99 | let event: IWSEvent = angular.copy(data); |
| 100 | _.forEach(models, (m: string) => { |
| 101 | |
| 102 | switch (m) { |
| 103 | case 'object': |
| 104 | case 'XOSBase': |
| 105 | case 'Model': |
| 106 | case 'PlModelMixIn': |
| 107 | case 'AttributeMixin': |
| 108 | // do not send events for classes that we don't care about |
| 109 | break; |
| 110 | default: |
| 111 | if (m.indexOf('_decl') > -1 || event.model === m) { |
| 112 | // do not send events for _decl classes |
| 113 | // or if the parent class is the same as the model class |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | event.model = m; |
| 118 | event.skip_notification = true; |
| 119 | this._events.next(event); |
| 120 | } |
| 121 | }); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | export class SocketIoService { |
| 127 | |
| 128 | static $inject = [ |
| 129 | 'AppConfig' |
| 130 | ]; |
| 131 | |
| 132 | public socket; |
| 133 | |
| 134 | constructor( |
| 135 | private AppConfig: IXosAppConfig, |
| 136 | private $log: ng.ILogService |
| 137 | ) { |
| 138 | this.socket = io(this.AppConfig.websocketClient); |
| 139 | } |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 140 | } |