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'; |
| 17 | import {LogService, WebSocketService} from 'gui2-fw-lib'; |
| 18 | import {ForceSvgComponent} from './layer/forcesvg/forcesvg.component'; |
| 19 | |
| 20 | export enum TrafficType { |
| 21 | IDLE, |
| 22 | FLOWSTATSBYTES = 'flowStatsBytes', |
| 23 | PORTSTATSBITSEC = 'portStatsBitSec', |
| 24 | PORTSTATSPKTSEC = 'portStatsPktSec', |
| 25 | } |
| 26 | |
| 27 | const ALL_TRAFFIC_TYPES = [ |
| 28 | TrafficType.FLOWSTATSBYTES, |
| 29 | TrafficType.PORTSTATSBITSEC, |
| 30 | TrafficType.PORTSTATSPKTSEC |
| 31 | ]; |
| 32 | |
| 33 | const ALL_TRAFFIC_MSGS = [ |
| 34 | 'Flow Stats (bytes)', |
| 35 | 'Port Stats (bits / second)', |
| 36 | 'Port Stats (packets / second)', |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * ONOS GUI -- Traffic Service Module. |
| 41 | */ |
| 42 | @Injectable({ |
| 43 | providedIn: 'root' |
| 44 | }) |
| 45 | export class TrafficService { |
| 46 | private handlers: string[] = []; |
| 47 | private openListener: any; |
| 48 | |
| 49 | constructor( |
| 50 | protected log: LogService, |
| 51 | protected wss: WebSocketService |
| 52 | ) { |
| 53 | this.log.debug('TrafficService constructed'); |
| 54 | } |
| 55 | |
| 56 | init(force: ForceSvgComponent) { |
| 57 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 58 | ['topo2Highlights', (data) => { |
| 59 | force.handleHighlights(data.devices, data.hosts, data.links); |
| 60 | } |
| 61 | ] |
| 62 | ])); |
| 63 | |
| 64 | this.handlers.push('topo2Highlights'); |
| 65 | |
| 66 | // in case we fail over to a new server, |
| 67 | // listen for wsock-open events |
| 68 | this.openListener = this.wss.addOpenListener(() => this.wsOpen); |
| 69 | |
| 70 | // tell the server we are ready to receive topology events |
| 71 | this.wss.sendEvent('topo2RequestAllTraffic', { |
| 72 | trafficType: TrafficType.FLOWSTATSBYTES |
| 73 | }); |
| 74 | this.log.debug('Topo2Traffic: Show All Traffic'); |
| 75 | } |
| 76 | |
| 77 | destroy() { |
| 78 | this.wss.sendEvent('topo2CancelTraffic', {}); |
| 79 | this.wss.unbindHandlers(this.handlers); |
| 80 | this.log.debug('Traffic monitoring canceled'); |
| 81 | } |
| 82 | |
| 83 | wsOpen(host: string, url: string) { |
| 84 | this.log.debug('topo2RequestAllTraffic: WSopen - cluster node:', host, 'URL:', url); |
| 85 | // tell the server we are ready to receive topo events |
| 86 | this.wss.sendEvent('topo2RequestAllTraffic', { |
| 87 | trafficType: TrafficType.FLOWSTATSBYTES |
| 88 | }); |
| 89 | } |
| 90 | } |