blob: f2a1f6041e33718fc7611f6225a828474ee8377c [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 Condon058804c2019-04-16 09:41:52 +010020import {Instance, InstanceComponent} from './panel/instance/instance.component';
Sean Condonaa4366d2018-11-02 14:29:01 +000021import { 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;
Sean Condon058804c2019-04-16 09:41:52 +010037 public instancesIndex: Map<string, number>;
Sean Condonaa4366d2018-11-02 14:29:01 +000038
Sean Condonf4f54a12018-10-10 23:25:46 +010039 constructor(
40 protected log: LogService,
Sean Condonaa4366d2018-11-02 14:29:01 +000041 protected wss: WebSocketService
Sean Condonf4f54a12018-10-10 23:25:46 +010042 ) {
Sean Condon058804c2019-04-16 09:41:52 +010043 this.instancesIndex = new Map();
Sean Condonaa4366d2018-11-02 14:29:01 +000044 this.log.debug('TopologyService constructed');
Sean Condonf4f54a12018-10-10 23:25:46 +010045 }
46
Sean Condonaa4366d2018-11-02 14:29:01 +000047 /**
48 * bind our event handlers to the web socket service, so that our
49 * callbacks get invoked for incoming events
50 */
51 init(instance: InstanceComponent, background: BackgroundSvgComponent, force: ForceSvgComponent) {
52 this.wss.bindHandlers(new Map<string, (data) => void>([
53 ['topo2AllInstances', (data) => {
Sean Condon91481822019-01-01 13:56:14 +000054 this.log.debug('Instances updated through WSS as topo2AllInstances', data);
Sean Condon058804c2019-04-16 09:41:52 +010055 instance.ngOnChanges(
56 {'onosInstances': new SimpleChange({}, data.members, true)});
57
58 // Also generate an index locally of the instances
59 // needed so that devices can be coloured by instance
60 this.instancesIndex.clear();
61 (<Instance[]>data.members).forEach((inst, idx) => this.instancesIndex.set(inst.id, idx));
62 this.log.debug('Created local index of instances', this.instancesIndex);
Sean Condonaa4366d2018-11-02 14:29:01 +000063 }
64 ],
65 ['topo2CurrentLayout', (data) => {
Sean Condon91481822019-01-01 13:56:14 +000066 this.log.debug('Background Data updated from WSS as topo2CurrentLayout', data);
Sean Condon50855cf2018-12-23 15:37:42 +000067 if (background) {
68 background.layoutData = data;
69 }
Sean Condonaa4366d2018-11-02 14:29:01 +000070 }
71 ],
72 ['topo2CurrentRegion', (data) => {
Sean Condonaa4366d2018-11-02 14:29:01 +000073 force.regionData = data;
Sean Condon0c577f62018-11-18 22:40:05 +000074 force.ngOnChanges({
75 'regionData' : new SimpleChange(<Region>{}, data, true)
76 });
Sean Condon91481822019-01-01 13:56:14 +000077 this.log.debug('Region Data replaced from WSS as topo2CurrentRegion', force.regionData);
Sean Condonaa4366d2018-11-02 14:29:01 +000078 }
79 ],
80 ['topo2PeerRegions', (data) => { this.log.warn('Add fn for topo2PeerRegions callback', data); } ],
Sean Condon50855cf2018-12-23 15:37:42 +000081 ['topo2UiModelEvent', (event) => {
82 // this.log.debug('Handling', event);
83 force.handleModelEvent(
Sean Condon91481822019-01-01 13:56:14 +000084 <ModelEventType><unknown>(ModelEventType[event.type]), // Number based enum
85 <ModelEventMemo>(event.memo), // String based enum
Sean Condon50855cf2018-12-23 15:37:42 +000086 event.subject, event.data);
87 }
88 ],
Sean Condon64ea7d22019-04-12 19:39:13 +010089 ['showHighlights', (event) => {
90 force.handleHighlights(event.devices, event.hosts, event.links);
91 }]
Sean Condon91481822019-01-01 13:56:14 +000092 // topo2Highlights is handled by TrafficService
Sean Condonaa4366d2018-11-02 14:29:01 +000093 ]));
94 this.handlers.push('topo2AllInstances');
95 this.handlers.push('topo2CurrentLayout');
96 this.handlers.push('topo2CurrentRegion');
97 this.handlers.push('topo2PeerRegions');
98 this.handlers.push('topo2UiModelEvent');
Sean Condon50855cf2018-12-23 15:37:42 +000099 // this.handlers.push('topo2Highlights');
Sean Condonf4f54a12018-10-10 23:25:46 +0100100
Sean Condonaa4366d2018-11-02 14:29:01 +0000101 // in case we fail over to a new server,
102 // listen for wsock-open events
103 this.openListener = this.wss.addOpenListener(() => this.wsOpen);
Sean Condonf4f54a12018-10-10 23:25:46 +0100104
Sean Condonaa4366d2018-11-02 14:29:01 +0000105 // tell the server we are ready to receive topology events
106 this.wss.sendEvent('topo2Start', {});
107 this.log.debug('TopologyService initialized');
Sean Condonf4f54a12018-10-10 23:25:46 +0100108 }
109
Sean Condonaa4366d2018-11-02 14:29:01 +0000110 /**
111 * tell the server we no longer wish to receive topology events
112 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100113 destroy() {
Sean Condonaa4366d2018-11-02 14:29:01 +0000114 this.wss.sendEvent('topo2Stop', {});
115 this.wss.unbindHandlers(this.handlers);
116 this.wss.removeOpenListener(this.openListener);
117 this.openListener = null;
118 this.log.debug('TopologyService destroyed');
119 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100120
121
Sean Condonaa4366d2018-11-02 14:29:01 +0000122 wsOpen(host: string, url: string) {
123 this.log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url);
124 // tell the server we are ready to receive topo events
125 this.wss.sendEvent('topo2Start', {});
Sean Condonf4f54a12018-10-10 23:25:46 +0100126 }
127}