blob: 8cc6070964e0a377b9600e743a2c7a00e563287e [file] [log] [blame]
Sean Condon50855cf2018-12-23 15:37:42 +00001/*
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 */
16import { Injectable } from '@angular/core';
Sean Condona3ad7792020-01-04 19:26:34 +000017import {LogService, WebSocketService} from 'gui2-fw-lib/public_api';
Sean Condon50855cf2018-12-23 15:37:42 +000018import {ForceSvgComponent} from './layer/forcesvg/forcesvg.component';
19
Sean Condon19e83672019-04-13 16:21:52 +010020export namespace TrafficType {
21 /**
22 * Toggle state for how the traffic should be displayed
23 */
24 export enum Enum { // Do not add an alias - they need to be number indexed
25 FLOWSTATSBYTES, // 0 flowStatsBytes
26 PORTSTATSBITSEC, // 1 portStatsBitSec
27 PORTSTATSPKTSEC // 2 portStatsPktSec
28 }
29
30 /**
31 * Add the method 'next()' to the TrafficType enum above
32 */
33 export function next(current: Enum) {
34 if (current === Enum.FLOWSTATSBYTES) {
35 return Enum.PORTSTATSBITSEC;
36 } else if (current === Enum.PORTSTATSBITSEC) {
37 return Enum.PORTSTATSPKTSEC;
38 } else if (current === Enum.PORTSTATSPKTSEC) {
39 return Enum.FLOWSTATSBYTES;
40 } else { // e.g. undefined
41 return Enum.PORTSTATSBITSEC;
42 }
43 }
44
45 export function literal(type: Enum) {
46 if (type === Enum.FLOWSTATSBYTES) {
47 return 'flowStatsBytes';
48 } else if (type === Enum.PORTSTATSBITSEC) {
49 return 'portStatsBitSec';
50 } else if (type === Enum.PORTSTATSPKTSEC) {
51 return 'portStatsPktSec';
52 }
53 }
Sean Condon50855cf2018-12-23 15:37:42 +000054}
55
Sean Condon50855cf2018-12-23 15:37:42 +000056/**
57 * ONOS GUI -- Traffic Service Module.
58 */
Sean Condon28884332019-03-21 14:07:00 +000059@Injectable()
Sean Condon50855cf2018-12-23 15:37:42 +000060export class TrafficService {
61 private handlers: string[] = [];
62 private openListener: any;
Sean Condon19e83672019-04-13 16:21:52 +010063 private trafficType: TrafficType.Enum;
Sean Condon50855cf2018-12-23 15:37:42 +000064
65 constructor(
66 protected log: LogService,
67 protected wss: WebSocketService
68 ) {
69 this.log.debug('TrafficService constructed');
70 }
71
72 init(force: ForceSvgComponent) {
73 this.wss.bindHandlers(new Map<string, (data) => void>([
74 ['topo2Highlights', (data) => {
Sean Condon64ea7d22019-04-12 19:39:13 +010075 force.handleHighlights(data.devices, data.hosts, data.links, 5000);
Sean Condon50855cf2018-12-23 15:37:42 +000076 }
77 ]
78 ]));
79
80 this.handlers.push('topo2Highlights');
81
82 // in case we fail over to a new server,
83 // listen for wsock-open events
84 this.openListener = this.wss.addOpenListener(() => this.wsOpen);
Sean Condon50855cf2018-12-23 15:37:42 +000085 }
86
87 destroy() {
Sean Condon50855cf2018-12-23 15:37:42 +000088 this.wss.unbindHandlers(this.handlers);
Sean Condon64ea7d22019-04-12 19:39:13 +010089 this.handlers.pop();
Sean Condon50855cf2018-12-23 15:37:42 +000090 }
91
92 wsOpen(host: string, url: string) {
93 this.log.debug('topo2RequestAllTraffic: WSopen - cluster node:', host, 'URL:', url);
94 // tell the server we are ready to receive topo events
95 this.wss.sendEvent('topo2RequestAllTraffic', {
Sean Condon19e83672019-04-13 16:21:52 +010096 trafficType: TrafficType.literal(this.trafficType)
Sean Condon50855cf2018-12-23 15:37:42 +000097 });
98 }
Sean Condon19e83672019-04-13 16:21:52 +010099
100 requestTraffic(trafficType: TrafficType.Enum) {
101 // tell the server we are ready to receive topology events
102 this.wss.sendEvent('topo2RequestAllTraffic', {
103 trafficType: TrafficType.literal(trafficType)
104 });
105 this.log.debug('Topo2Traffic: Show', trafficType);
106 }
107
108 cancelTraffic() {
109 this.wss.sendEvent('topo2CancelTraffic', {});
110 this.log.debug('Traffic monitoring canceled');
111 }
Sean Condon50855cf2018-12-23 15:37:42 +0000112}