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 | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 20 | import {Instance, InstanceComponent} from './panel/instance/instance.component'; |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 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; |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 37 | public instancesIndex: Map<string, number>; |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 38 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 39 | constructor( |
| 40 | protected log: LogService, |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 41 | protected wss: WebSocketService |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 42 | ) { |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 43 | this.instancesIndex = new Map(); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 44 | this.log.debug('TopologyService constructed'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 47 | /** |
| 48 | * bind our event handlers to the web socket service, so that our |
| 49 | * callbacks get invoked for incoming events |
| 50 | */ |
| 51 | init(instance: InstanceComponent, background: BackgroundSvgComponent, force: ForceSvgComponent) { |
| 52 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 53 | ['topo2AllInstances', (data) => { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 54 | this.log.debug('Instances updated through WSS as topo2AllInstances', data); |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 55 | instance.ngOnChanges( |
| 56 | {'onosInstances': new SimpleChange({}, data.members, true)}); |
| 57 | |
| 58 | // Also generate an index locally of the instances |
| 59 | // needed so that devices can be coloured by instance |
| 60 | this.instancesIndex.clear(); |
| 61 | (<Instance[]>data.members).forEach((inst, idx) => this.instancesIndex.set(inst.id, idx)); |
| 62 | this.log.debug('Created local index of instances', this.instancesIndex); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 63 | } |
| 64 | ], |
| 65 | ['topo2CurrentLayout', (data) => { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 66 | this.log.debug('Background Data updated from WSS as topo2CurrentLayout', data); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 67 | if (background) { |
| 68 | background.layoutData = data; |
| 69 | } |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 70 | } |
| 71 | ], |
| 72 | ['topo2CurrentRegion', (data) => { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 73 | force.regionData = data; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 74 | force.ngOnChanges({ |
| 75 | 'regionData' : new SimpleChange(<Region>{}, data, true) |
| 76 | }); |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 77 | this.log.debug('Region Data replaced from WSS as topo2CurrentRegion', force.regionData); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 78 | } |
| 79 | ], |
| 80 | ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ], |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 81 | ['topo2UiModelEvent', (event) => { |
| 82 | // this.log.debug('Handling', event); |
| 83 | force.handleModelEvent( |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 84 | <ModelEventType><unknown>(ModelEventType[event.type]), // Number based enum |
| 85 | <ModelEventMemo>(event.memo), // String based enum |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 86 | event.subject, event.data); |
| 87 | } |
| 88 | ], |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 89 | ['showHighlights', (event) => { |
| 90 | force.handleHighlights(event.devices, event.hosts, event.links); |
| 91 | }] |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 92 | // topo2Highlights is handled by TrafficService |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 93 | ])); |
| 94 | this.handlers.push('topo2AllInstances'); |
| 95 | this.handlers.push('topo2CurrentLayout'); |
| 96 | this.handlers.push('topo2CurrentRegion'); |
| 97 | this.handlers.push('topo2PeerRegions'); |
| 98 | this.handlers.push('topo2UiModelEvent'); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 99 | // this.handlers.push('topo2Highlights'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 100 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 101 | // in case we fail over to a new server, |
| 102 | // listen for wsock-open events |
| 103 | this.openListener = this.wss.addOpenListener(() => this.wsOpen); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 104 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 105 | // tell the server we are ready to receive topology events |
| 106 | this.wss.sendEvent('topo2Start', {}); |
| 107 | this.log.debug('TopologyService initialized'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 110 | /** |
| 111 | * tell the server we no longer wish to receive topology events |
| 112 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 113 | destroy() { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 114 | this.wss.sendEvent('topo2Stop', {}); |
| 115 | this.wss.unbindHandlers(this.handlers); |
| 116 | this.wss.removeOpenListener(this.openListener); |
| 117 | this.openListener = null; |
| 118 | this.log.debug('TopologyService destroyed'); |
| 119 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 120 | |
| 121 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 122 | wsOpen(host: string, url: string) { |
| 123 | this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url); |
| 124 | // tell the server we are ready to receive topo events |
| 125 | this.wss.sendEvent('topo2Start', {}); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 126 | } |
| 127 | } |