Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 16 | import { Injectable } from '@angular/core'; |
| 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 | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 23 | |
| 24 | /** |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 25 | * ONOS GUI -- Topology Service Module. |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 26 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 27 | @Injectable() |
| 28 | export class TopologyService { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 29 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 30 | private handlers: string[] = []; |
| 31 | private openListener: any; |
| 32 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 33 | constructor( |
| 34 | protected log: LogService, |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 35 | protected wss: WebSocketService |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 36 | ) { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 37 | this.log.debug('TopologyService constructed'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 40 | /** |
| 41 | * bind our event handlers to the web socket service, so that our |
| 42 | * callbacks get invoked for incoming events |
| 43 | */ |
| 44 | init(instance: InstanceComponent, background: BackgroundSvgComponent, force: ForceSvgComponent) { |
| 45 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 46 | ['topo2AllInstances', (data) => { |
| 47 | this.log.warn('Add fn for topo2AllInstances callback', data); |
| 48 | instance.onosInstances = data.members; |
| 49 | } |
| 50 | ], |
| 51 | ['topo2CurrentLayout', (data) => { |
| 52 | this.log.warn('Add fn for topo2CurrentLayout callback', data); |
| 53 | background.layoutData = data; |
| 54 | } |
| 55 | ], |
| 56 | ['topo2CurrentRegion', (data) => { |
| 57 | this.log.warn('Add fn for topo2CurrentRegion callback', data); |
| 58 | force.regionData = data; |
| 59 | } |
| 60 | ], |
| 61 | ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ], |
| 62 | ['topo2UiModelEvent', (data) => { this.log.warn('Add fn for topo2UiModelEvent callback', data); } ], |
| 63 | ['topo2Highlights', (data) => { this.log.warn('Add fn for topo2Highlights callback', data); } ], |
| 64 | ])); |
| 65 | this.handlers.push('topo2AllInstances'); |
| 66 | this.handlers.push('topo2CurrentLayout'); |
| 67 | this.handlers.push('topo2CurrentRegion'); |
| 68 | this.handlers.push('topo2PeerRegions'); |
| 69 | this.handlers.push('topo2UiModelEvent'); |
| 70 | this.handlers.push('topo2Highlights'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 71 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 72 | // in case we fail over to a new server, |
| 73 | // listen for wsock-open events |
| 74 | this.openListener = this.wss.addOpenListener(() => this.wsOpen); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 75 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 76 | // tell the server we are ready to receive topology events |
| 77 | this.wss.sendEvent('topo2Start', {}); |
| 78 | this.log.debug('TopologyService initialized'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 81 | /** |
| 82 | * tell the server we no longer wish to receive topology events |
| 83 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 84 | destroy() { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 85 | this.wss.sendEvent('topo2Stop', {}); |
| 86 | this.wss.unbindHandlers(this.handlers); |
| 87 | this.wss.removeOpenListener(this.openListener); |
| 88 | this.openListener = null; |
| 89 | this.log.debug('TopologyService destroyed'); |
| 90 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 91 | |
| 92 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame^] | 93 | wsOpen(host: string, url: string) { |
| 94 | this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url); |
| 95 | // tell the server we are ready to receive topo events |
| 96 | this.wss.sendEvent('topo2Start', {}); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 97 | } |
| 98 | } |