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 | a3ad779 | 2020-01-04 19:26:34 +0000 | [diff] [blame] | 19 | } from 'gui2-fw-lib/public_api'; |
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 | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 30 | * Model of the Intent to be displayed |
| 31 | */ |
| 32 | export interface Intent { |
| 33 | appId: string; |
| 34 | appName: string; |
| 35 | key: string; |
| 36 | type: string; |
| 37 | } |
| 38 | |
| 39 | export interface RelatedIntent { |
| 40 | ids: string[]; |
| 41 | hover: string; |
| 42 | } |
| 43 | |
| 44 | /** |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 45 | * ONOS GUI -- Topology Service Module. |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 46 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 47 | @Injectable() |
| 48 | export class TopologyService { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 49 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 50 | private handlers: string[] = []; |
| 51 | private openListener: any; |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 52 | public instancesIndex: Map<string, number>; |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 53 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 54 | constructor( |
| 55 | protected log: LogService, |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 56 | protected wss: WebSocketService |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 57 | ) { |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 58 | this.instancesIndex = new Map(); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 59 | this.log.debug('TopologyService constructed'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 62 | /** |
| 63 | * bind our event handlers to the web socket service, so that our |
| 64 | * callbacks get invoked for incoming events |
| 65 | */ |
| 66 | init(instance: InstanceComponent, background: BackgroundSvgComponent, force: ForceSvgComponent) { |
| 67 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 68 | ['topo2AllInstances', (data) => { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 69 | this.log.debug('Instances updated through WSS as topo2AllInstances', data); |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 70 | instance.ngOnChanges( |
| 71 | {'onosInstances': new SimpleChange({}, data.members, true)}); |
| 72 | |
| 73 | // Also generate an index locally of the instances |
| 74 | // needed so that devices can be coloured by instance |
| 75 | this.instancesIndex.clear(); |
| 76 | (<Instance[]>data.members).forEach((inst, idx) => this.instancesIndex.set(inst.id, idx)); |
| 77 | this.log.debug('Created local index of instances', this.instancesIndex); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 78 | } |
| 79 | ], |
| 80 | ['topo2CurrentLayout', (data) => { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 81 | this.log.debug('Background Data updated from WSS as topo2CurrentLayout', data); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 82 | if (background) { |
| 83 | background.layoutData = data; |
| 84 | } |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 85 | } |
| 86 | ], |
| 87 | ['topo2CurrentRegion', (data) => { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 88 | force.regionData = data; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 89 | force.ngOnChanges({ |
| 90 | 'regionData' : new SimpleChange(<Region>{}, data, true) |
| 91 | }); |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 92 | this.log.debug('Region Data replaced from WSS as topo2CurrentRegion', force.regionData); |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 93 | } |
| 94 | ], |
| 95 | ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ], |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 96 | ['topo2UiModelEvent', (event) => { |
| 97 | // this.log.debug('Handling', event); |
| 98 | force.handleModelEvent( |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 99 | <ModelEventType><unknown>(ModelEventType[event.type]), // Number based enum |
| 100 | <ModelEventMemo>(event.memo), // String based enum |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 101 | event.subject, event.data); |
| 102 | } |
| 103 | ], |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 104 | ['showHighlights', (event) => { |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 105 | this.log.debug('Handling showHighlights', event); |
| 106 | force.handleHighlights(event.devices, event.hosts, event.links, 5000); |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 107 | }] |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 108 | // topo2Highlights is handled by TrafficService |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 109 | ])); |
| 110 | this.handlers.push('topo2AllInstances'); |
| 111 | this.handlers.push('topo2CurrentLayout'); |
| 112 | this.handlers.push('topo2CurrentRegion'); |
| 113 | this.handlers.push('topo2PeerRegions'); |
| 114 | this.handlers.push('topo2UiModelEvent'); |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 115 | this.handlers.push('showHighlights'); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 116 | // this.handlers.push('topo2Highlights'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 117 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 118 | // in case we fail over to a new server, |
| 119 | // listen for wsock-open events |
| 120 | this.openListener = this.wss.addOpenListener(() => this.wsOpen); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 121 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 122 | // tell the server we are ready to receive topology events |
| 123 | this.wss.sendEvent('topo2Start', {}); |
| 124 | this.log.debug('TopologyService initialized'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 127 | /** |
| 128 | * tell the server we no longer wish to receive topology events |
| 129 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 130 | destroy() { |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 131 | this.wss.sendEvent('topo2Stop', {}); |
| 132 | this.wss.unbindHandlers(this.handlers); |
| 133 | this.wss.removeOpenListener(this.openListener); |
| 134 | this.openListener = null; |
| 135 | this.log.debug('TopologyService destroyed'); |
| 136 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 137 | |
| 138 | |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 139 | wsOpen(host: string, url: string) { |
| 140 | this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url); |
| 141 | // tell the server we are ready to receive topo events |
| 142 | this.wss.sendEvent('topo2Start', {}); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 143 | } |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Result will be handled by showHighlights handler (set up in topology service) |
| 147 | * which will call handleHighlights() in Force Component |
| 148 | */ |
| 149 | setSelectedIntent(selectedIntent: Intent): void { |
| 150 | this.log.debug('Selected intent changed to', selectedIntent); |
| 151 | this.wss.sendEvent('selectIntent', selectedIntent); |
| 152 | } |
| 153 | |
| 154 | selectRelatedIntent(ids: string[]): void { |
| 155 | this.log.debug('Select next intent'); |
| 156 | this.wss.sendEvent('requestNextRelatedIntent', <RelatedIntent>{ |
| 157 | ids: ids, |
| 158 | hover: undefined, |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Tell the backend to stop sending highlights - any present will fade after 5 seconds |
| 164 | * There is also a cancel traffic for Topo 2 in Traffic Service |
| 165 | */ |
| 166 | cancelHighlights(): void { |
| 167 | this.wss.sendEvent('cancelTraffic', {}); |
| 168 | this.log.debug('Highlights canceled'); |
| 169 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 170 | } |