blob: 1d85a085e78bedd1960661a8f5ca0a7ef4cd32d9 [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 Condonf4f54a12018-10-10 23:25:46 +010016import { Injectable } from '@angular/core';
17import {
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 Condon83fc39f2018-04-19 18:56:13 +010023
24/**
Sean Condonf4f54a12018-10-10 23:25:46 +010025 * ONOS GUI -- Topology Service Module.
Sean Condon83fc39f2018-04-19 18:56:13 +010026 */
Sean Condonf4f54a12018-10-10 23:25:46 +010027@Injectable()
28export class TopologyService {
Sean Condon83fc39f2018-04-19 18:56:13 +010029
Sean Condonaa4366d2018-11-02 14:29:01 +000030 private handlers: string[] = [];
31 private openListener: any;
32
Sean Condonf4f54a12018-10-10 23:25:46 +010033 constructor(
34 protected log: LogService,
Sean Condonaa4366d2018-11-02 14:29:01 +000035 protected wss: WebSocketService
Sean Condonf4f54a12018-10-10 23:25:46 +010036 ) {
Sean Condonaa4366d2018-11-02 14:29:01 +000037 this.log.debug('TopologyService constructed');
Sean Condonf4f54a12018-10-10 23:25:46 +010038 }
39
Sean Condonaa4366d2018-11-02 14:29:01 +000040 /**
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 Condonf4f54a12018-10-10 23:25:46 +010071
Sean Condonaa4366d2018-11-02 14:29:01 +000072 // in case we fail over to a new server,
73 // listen for wsock-open events
74 this.openListener = this.wss.addOpenListener(() => this.wsOpen);
Sean Condonf4f54a12018-10-10 23:25:46 +010075
Sean Condonaa4366d2018-11-02 14:29:01 +000076 // tell the server we are ready to receive topology events
77 this.wss.sendEvent('topo2Start', {});
78 this.log.debug('TopologyService initialized');
Sean Condonf4f54a12018-10-10 23:25:46 +010079 }
80
Sean Condonaa4366d2018-11-02 14:29:01 +000081 /**
82 * tell the server we no longer wish to receive topology events
83 */
Sean Condonf4f54a12018-10-10 23:25:46 +010084 destroy() {
Sean Condonaa4366d2018-11-02 14:29:01 +000085 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 Condonf4f54a12018-10-10 23:25:46 +010091
92
Sean Condonaa4366d2018-11-02 14:29:01 +000093 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 Condonf4f54a12018-10-10 23:25:46 +010097 }
98}