Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import { Injectable } from '@angular/core'; |
Sean Condon | a3ad779 | 2020-01-04 19:26:34 +0000 | [diff] [blame] | 17 | import {LogService, WebSocketService} from 'gui2-fw-lib/public_api'; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 18 | import {ForceSvgComponent} from './layer/forcesvg/forcesvg.component'; |
| 19 | |
Sean Condon | 19e8367 | 2019-04-13 16:21:52 +0100 | [diff] [blame] | 20 | export 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 56 | /** |
| 57 | * ONOS GUI -- Traffic Service Module. |
| 58 | */ |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 59 | @Injectable() |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 60 | export class TrafficService { |
| 61 | private handlers: string[] = []; |
| 62 | private openListener: any; |
Sean Condon | 19e8367 | 2019-04-13 16:21:52 +0100 | [diff] [blame] | 63 | private trafficType: TrafficType.Enum; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 64 | |
| 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 Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 75 | force.handleHighlights(data.devices, data.hosts, data.links, 5000); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 76 | } |
| 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | destroy() { |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 88 | this.wss.unbindHandlers(this.handlers); |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 89 | this.handlers.pop(); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 90 | } |
| 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 Condon | 19e8367 | 2019-04-13 16:21:52 +0100 | [diff] [blame] | 96 | trafficType: TrafficType.literal(this.trafficType) |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 97 | }); |
| 98 | } |
Sean Condon | 19e8367 | 2019-04-13 16:21:52 +0100 | [diff] [blame] | 99 | |
| 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 112 | } |