blob: 3572b7c93070423a018c30000e72c6c651796992 [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 Condon0c577f62018-11-18 22:40:05 +000016import {Injectable, SimpleChanges, 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 Condon0c577f62018-11-18 22:40:05 +000023import {Region} from './layer/forcesvg/models';
Sean Condon83fc39f2018-04-19 18:56:13 +010024
25/**
Sean Condonf4f54a12018-10-10 23:25:46 +010026 * ONOS GUI -- Topology Service Module.
Sean Condon83fc39f2018-04-19 18:56:13 +010027 */
Sean Condonf4f54a12018-10-10 23:25:46 +010028@Injectable()
29export class TopologyService {
Sean Condon83fc39f2018-04-19 18:56:13 +010030
Sean Condonaa4366d2018-11-02 14:29:01 +000031 private handlers: string[] = [];
32 private openListener: any;
33
Sean Condonf4f54a12018-10-10 23:25:46 +010034 constructor(
35 protected log: LogService,
Sean Condonaa4366d2018-11-02 14:29:01 +000036 protected wss: WebSocketService
Sean Condonf4f54a12018-10-10 23:25:46 +010037 ) {
Sean Condonaa4366d2018-11-02 14:29:01 +000038 this.log.debug('TopologyService constructed');
Sean Condonf4f54a12018-10-10 23:25:46 +010039 }
40
Sean Condonaa4366d2018-11-02 14:29:01 +000041 /**
42 * bind our event handlers to the web socket service, so that our
43 * callbacks get invoked for incoming events
44 */
45 init(instance: InstanceComponent, background: BackgroundSvgComponent, force: ForceSvgComponent) {
46 this.wss.bindHandlers(new Map<string, (data) => void>([
47 ['topo2AllInstances', (data) => {
48 this.log.warn('Add fn for topo2AllInstances callback', data);
49 instance.onosInstances = data.members;
50 }
51 ],
52 ['topo2CurrentLayout', (data) => {
53 this.log.warn('Add fn for topo2CurrentLayout callback', data);
54 background.layoutData = data;
55 }
56 ],
57 ['topo2CurrentRegion', (data) => {
Sean Condonaa4366d2018-11-02 14:29:01 +000058 force.regionData = data;
Sean Condon0c577f62018-11-18 22:40:05 +000059 force.ngOnChanges({
60 'regionData' : new SimpleChange(<Region>{}, data, true)
61 });
62 this.log.warn('Add fn for topo2CurrentRegion callback', force.regionData);
Sean Condonaa4366d2018-11-02 14:29:01 +000063 }
64 ],
65 ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ],
66 ['topo2UiModelEvent', (data) => { this.log.warn('Add fn for topo2UiModelEvent callback', data); } ],
67 ['topo2Highlights', (data) => { this.log.warn('Add fn for topo2Highlights callback', data); } ],
68 ]));
69 this.handlers.push('topo2AllInstances');
70 this.handlers.push('topo2CurrentLayout');
71 this.handlers.push('topo2CurrentRegion');
72 this.handlers.push('topo2PeerRegions');
73 this.handlers.push('topo2UiModelEvent');
74 this.handlers.push('topo2Highlights');
Sean Condonf4f54a12018-10-10 23:25:46 +010075
Sean Condonaa4366d2018-11-02 14:29:01 +000076 // in case we fail over to a new server,
77 // listen for wsock-open events
78 this.openListener = this.wss.addOpenListener(() => this.wsOpen);
Sean Condonf4f54a12018-10-10 23:25:46 +010079
Sean Condonaa4366d2018-11-02 14:29:01 +000080 // tell the server we are ready to receive topology events
81 this.wss.sendEvent('topo2Start', {});
82 this.log.debug('TopologyService initialized');
Sean Condonf4f54a12018-10-10 23:25:46 +010083 }
84
Sean Condonaa4366d2018-11-02 14:29:01 +000085 /**
86 * tell the server we no longer wish to receive topology events
87 */
Sean Condonf4f54a12018-10-10 23:25:46 +010088 destroy() {
Sean Condonaa4366d2018-11-02 14:29:01 +000089 this.wss.sendEvent('topo2Stop', {});
90 this.wss.unbindHandlers(this.handlers);
91 this.wss.removeOpenListener(this.openListener);
92 this.openListener = null;
93 this.log.debug('TopologyService destroyed');
94 }
Sean Condonf4f54a12018-10-10 23:25:46 +010095
96
Sean Condonaa4366d2018-11-02 14:29:01 +000097 wsOpen(host: string, url: string) {
98 this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url);
99 // tell the server we are ready to receive topo events
100 this.wss.sendEvent('topo2Start', {});
Sean Condonf4f54a12018-10-10 23:25:46 +0100101 }
102}