Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +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 | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 16 | import {Injectable, SimpleChange} from '@angular/core'; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 17 | import { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 18 | LogService, WebSocketService, |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 19 | } from 'gui2-fw-lib'; |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 20 | import { InstanceComponent } from './panel/instance/instance.component'; |
| 21 | import { BackgroundSvgComponent } from './layer/backgroundsvg/backgroundsvg.component'; |
| 22 | import { ForceSvgComponent } from './layer/forcesvg/forcesvg.component'; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 23 | import { |
| 24 | ModelEventMemo, |
| 25 | ModelEventType, |
| 26 | Region |
| 27 | } from './layer/forcesvg/models'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 30 | * ONOS GUI -- Topology Service Module. |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 31 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 32 | @Injectable() |
| 33 | export class TopologyService { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 34 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 35 | private handlers: string[] = []; |
| 36 | private openListener: any; |
| 37 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 38 | constructor( |
| 39 | protected log: LogService, |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 40 | protected wss: WebSocketService |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 41 | ) { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 42 | this.log.debug('TopologyService constructed'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 45 | /** |
| 46 | * bind our event handlers to the web socket service, so that our |
| 47 | * callbacks get invoked for incoming events |
| 48 | */ |
| 49 | init(instance: InstanceComponent, background: BackgroundSvgComponent, force: ForceSvgComponent) { |
| 50 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 51 | ['topo2AllInstances', (data) => { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 52 | this.log.debug('Instances updated through WSS as topo2AllInstances', data); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 53 | instance.onosInstances = data.members; |
| 54 | } |
| 55 | ], |
| 56 | ['topo2CurrentLayout', (data) => { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 57 | this.log.debug('Background Data updated from WSS as topo2CurrentLayout', data); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 58 | if (background) { |
| 59 | background.layoutData = data; |
| 60 | } |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 61 | } |
| 62 | ], |
| 63 | ['topo2CurrentRegion', (data) => { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 64 | force.regionData = data; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 65 | force.ngOnChanges({ |
| 66 | 'regionData' : new SimpleChange(<Region>{}, data, true) |
| 67 | }); |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 68 | this.log.debug('Region Data replaced from WSS as topo2CurrentRegion', force.regionData); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 69 | } |
| 70 | ], |
| 71 | ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ], |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 72 | ['topo2UiModelEvent', (event) => { |
| 73 | // this.log.debug('Handling', event); |
| 74 | force.handleModelEvent( |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 75 | <ModelEventType><unknown>(ModelEventType[event.type]), // Number based enum |
| 76 | <ModelEventMemo>(event.memo), // String based enum |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 77 | event.subject, event.data); |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 78 | this.log.debug('Region Data updated from WSS as topo2UiModelEvent', force.regionData); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 79 | } |
| 80 | ], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 81 | // topo2Highlights is handled by TrafficService |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 82 | ])); |
| 83 | this.handlers.push('topo2AllInstances'); |
| 84 | this.handlers.push('topo2CurrentLayout'); |
| 85 | this.handlers.push('topo2CurrentRegion'); |
| 86 | this.handlers.push('topo2PeerRegions'); |
| 87 | this.handlers.push('topo2UiModelEvent'); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 88 | // this.handlers.push('topo2Highlights'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 89 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 90 | // in case we fail over to a new server, |
| 91 | // listen for wsock-open events |
| 92 | this.openListener = this.wss.addOpenListener(() => this.wsOpen); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 93 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 94 | // tell the server we are ready to receive topology events |
| 95 | this.wss.sendEvent('topo2Start', {}); |
| 96 | this.log.debug('TopologyService initialized'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 99 | /** |
| 100 | * tell the server we no longer wish to receive topology events |
| 101 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 102 | destroy() { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 103 | this.wss.sendEvent('topo2Stop', {}); |
| 104 | this.wss.unbindHandlers(this.handlers); |
| 105 | this.wss.removeOpenListener(this.openListener); |
| 106 | this.openListener = null; |
| 107 | this.log.debug('TopologyService destroyed'); |
| 108 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 109 | |
| 110 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 111 | wsOpen(host: string, url: string) { |
| 112 | this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url); |
| 113 | // tell the server we are ready to receive topo events |
| 114 | this.wss.sendEvent('topo2Start', {}); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 115 | } |
| 116 | } |