blob: 2c5d777d7430e8ca4e64a69f34d1a1296e742141 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon91481822019-01-01 13:56:14 +00002 * Copyright 2019-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 Condon50855cf2018-12-23 15:37:42 +000016import {Injectable, SimpleChange} from '@angular/core';
Sean Condonf4f54a12018-10-10 23:25:46 +010017import {
Sean Condonaa4366d2018-11-02 14:29:01 +000018 LogService, WebSocketService,
Sean Condonf4f54a12018-10-10 23:25:46 +010019} from 'gui2-fw-lib';
Sean Condonaa4366d2018-11-02 14:29:01 +000020import { InstanceComponent } from './panel/instance/instance.component';
21import { BackgroundSvgComponent } from './layer/backgroundsvg/backgroundsvg.component';
22import { ForceSvgComponent } from './layer/forcesvg/forcesvg.component';
Sean Condon50855cf2018-12-23 15:37:42 +000023import {
24 ModelEventMemo,
25 ModelEventType,
26 Region
27} from './layer/forcesvg/models';
Sean Condon83fc39f2018-04-19 18:56:13 +010028
29/**
Sean Condonf4f54a12018-10-10 23:25:46 +010030 * ONOS GUI -- Topology Service Module.
Sean Condon83fc39f2018-04-19 18:56:13 +010031 */
Sean Condonf4f54a12018-10-10 23:25:46 +010032@Injectable()
33export class TopologyService {
Sean Condon83fc39f2018-04-19 18:56:13 +010034
Sean Condonaa4366d2018-11-02 14:29:01 +000035 private handlers: string[] = [];
36 private openListener: any;
37
Sean Condonf4f54a12018-10-10 23:25:46 +010038 constructor(
39 protected log: LogService,
Sean Condonaa4366d2018-11-02 14:29:01 +000040 protected wss: WebSocketService
Sean Condonf4f54a12018-10-10 23:25:46 +010041 ) {
Sean Condonaa4366d2018-11-02 14:29:01 +000042 this.log.debug('TopologyService constructed');
Sean Condonf4f54a12018-10-10 23:25:46 +010043 }
44
Sean Condonaa4366d2018-11-02 14:29:01 +000045 /**
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 Condon91481822019-01-01 13:56:14 +000052 this.log.debug('Instances updated through WSS as topo2AllInstances', data);
Sean Condonaa4366d2018-11-02 14:29:01 +000053 instance.onosInstances = data.members;
54 }
55 ],
56 ['topo2CurrentLayout', (data) => {
Sean Condon91481822019-01-01 13:56:14 +000057 this.log.debug('Background Data updated from WSS as topo2CurrentLayout', data);
Sean Condon50855cf2018-12-23 15:37:42 +000058 if (background) {
59 background.layoutData = data;
60 }
Sean Condonaa4366d2018-11-02 14:29:01 +000061 }
62 ],
63 ['topo2CurrentRegion', (data) => {
Sean Condonaa4366d2018-11-02 14:29:01 +000064 force.regionData = data;
Sean Condon0c577f62018-11-18 22:40:05 +000065 force.ngOnChanges({
66 'regionData' : new SimpleChange(<Region>{}, data, true)
67 });
Sean Condon91481822019-01-01 13:56:14 +000068 this.log.debug('Region Data replaced from WSS as topo2CurrentRegion', force.regionData);
Sean Condonaa4366d2018-11-02 14:29:01 +000069 }
70 ],
71 ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ],
Sean Condon50855cf2018-12-23 15:37:42 +000072 ['topo2UiModelEvent', (event) => {
73 // this.log.debug('Handling', event);
74 force.handleModelEvent(
Sean Condon91481822019-01-01 13:56:14 +000075 <ModelEventType><unknown>(ModelEventType[event.type]), // Number based enum
76 <ModelEventMemo>(event.memo), // String based enum
Sean Condon50855cf2018-12-23 15:37:42 +000077 event.subject, event.data);
Sean Condon91481822019-01-01 13:56:14 +000078 this.log.debug('Region Data updated from WSS as topo2UiModelEvent', force.regionData);
Sean Condon50855cf2018-12-23 15:37:42 +000079 }
80 ],
Sean Condon91481822019-01-01 13:56:14 +000081 // topo2Highlights is handled by TrafficService
Sean Condonaa4366d2018-11-02 14:29:01 +000082 ]));
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 Condon50855cf2018-12-23 15:37:42 +000088 // this.handlers.push('topo2Highlights');
Sean Condonf4f54a12018-10-10 23:25:46 +010089
Sean Condonaa4366d2018-11-02 14:29:01 +000090 // in case we fail over to a new server,
91 // listen for wsock-open events
92 this.openListener = this.wss.addOpenListener(() => this.wsOpen);
Sean Condonf4f54a12018-10-10 23:25:46 +010093
Sean Condonaa4366d2018-11-02 14:29:01 +000094 // tell the server we are ready to receive topology events
95 this.wss.sendEvent('topo2Start', {});
96 this.log.debug('TopologyService initialized');
Sean Condonf4f54a12018-10-10 23:25:46 +010097 }
98
Sean Condonaa4366d2018-11-02 14:29:01 +000099 /**
100 * tell the server we no longer wish to receive topology events
101 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100102 destroy() {
Sean Condonaa4366d2018-11-02 14:29:01 +0000103 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 Condonf4f54a12018-10-10 23:25:46 +0100109
110
Sean Condonaa4366d2018-11-02 14:29:01 +0000111 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 Condonf4f54a12018-10-10 23:25:46 +0100115 }
116}