blob: 9422a3fd5e248786a7b821dc5f2f25dfafbb580e [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
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 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) => {
52 this.log.warn('Add fn for topo2AllInstances callback', data);
53 instance.onosInstances = data.members;
54 }
55 ],
56 ['topo2CurrentLayout', (data) => {
57 this.log.warn('Add fn for topo2CurrentLayout callback', 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 });
68 this.log.warn('Add fn for topo2CurrentRegion callback', 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(
75 <ModelEventType><unknown>(ModelEventType[event.type]),
76 <ModelEventMemo>(event.memo),
77 event.subject, event.data);
78 }
79 ],
80 // ['topo2Highlights', (data) => { this.log.warn('Add fn for topo2Highlights callback', data); } ],
Sean Condonaa4366d2018-11-02 14:29:01 +000081 ]));
82 this.handlers.push('topo2AllInstances');
83 this.handlers.push('topo2CurrentLayout');
84 this.handlers.push('topo2CurrentRegion');
85 this.handlers.push('topo2PeerRegions');
86 this.handlers.push('topo2UiModelEvent');
Sean Condon50855cf2018-12-23 15:37:42 +000087 // this.handlers.push('topo2Highlights');
Sean Condonf4f54a12018-10-10 23:25:46 +010088
Sean Condonaa4366d2018-11-02 14:29:01 +000089 // in case we fail over to a new server,
90 // listen for wsock-open events
91 this.openListener = this.wss.addOpenListener(() => this.wsOpen);
Sean Condonf4f54a12018-10-10 23:25:46 +010092
Sean Condonaa4366d2018-11-02 14:29:01 +000093 // tell the server we are ready to receive topology events
94 this.wss.sendEvent('topo2Start', {});
95 this.log.debug('TopologyService initialized');
Sean Condonf4f54a12018-10-10 23:25:46 +010096 }
97
Sean Condonaa4366d2018-11-02 14:29:01 +000098 /**
99 * tell the server we no longer wish to receive topology events
100 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100101 destroy() {
Sean Condonaa4366d2018-11-02 14:29:01 +0000102 this.wss.sendEvent('topo2Stop', {});
103 this.wss.unbindHandlers(this.handlers);
104 this.wss.removeOpenListener(this.openListener);
105 this.openListener = null;
106 this.log.debug('TopologyService destroyed');
107 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100108
109
Sean Condonaa4366d2018-11-02 14:29:01 +0000110 wsOpen(host: string, url: string) {
111 this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url);
112 // tell the server we are ready to receive topo events
113 this.wss.sendEvent('topo2Start', {});
Sean Condonf4f54a12018-10-10 23:25:46 +0100114 }
115}