Max Chu | 6a4bb65 | 2017-09-29 17:15:56 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | import * as _ from 'lodash'; |
| 19 | import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg; |
| 20 | |
| 21 | export interface IMicrosemiStatsBinData { |
| 22 | lowerLimit: string; |
| 23 | count: number; |
| 24 | } |
| 25 | |
| 26 | export interface IMicrosemiStatsBin { |
| 27 | bins: IMicrosemiStatsBinData[]; |
| 28 | } |
| 29 | |
| 30 | export interface IMicrosemiStatsFormat { |
| 31 | interFrameDelayVariationTwoWayAvg: string; |
| 32 | interFrameDelayVariationTwoWayMax: string; |
| 33 | interFrameDelayVariationTwoWayMin: string; |
| 34 | interFrameDelayVariationTwoWayBins: IMicrosemiStatsBin; |
| 35 | FrameDelayTwoWayAvg: string; |
| 36 | FrameDelayTwoWayMax: string; |
| 37 | FrameDelayTwoWayMin: string; |
| 38 | FrameDelayTwoWayBins: IMicrosemiStatsBin; |
| 39 | [x: string]: any; |
| 40 | } |
| 41 | |
| 42 | export interface IMicrosemiStatsData { |
| 43 | dm: { |
| 44 | dmId: any; |
| 45 | current: IMicrosemiStatsFormat; |
| 46 | historic: IMicrosemiStatsFormat[]; |
| 47 | [x: string]: any; |
| 48 | }; |
| 49 | dmId?: any; |
| 50 | current?: IMicrosemiStatsFormat; |
| 51 | historic?: IMicrosemiStatsFormat[]; |
| 52 | [x: string]: any; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | export interface IMicrosemiChartData { |
| 57 | labels: string[]; |
| 58 | data: [number[]]; |
| 59 | } |
| 60 | |
| 61 | export interface IMicrosemiStats { |
| 62 | getMds(): any; |
| 63 | getMeps(mdName: string, maName: string): any; |
| 64 | getDms(mdName: string, maName: string, mepId: string): any; |
| 65 | get(mdName: string, maName: string, mepId: string, dmId: string): ng.IPromise<any>; |
| 66 | getAllData(res: IMicrosemiStatsData): any; |
| 67 | } |
| 68 | |
| 69 | export interface IMicrosemiMdData { |
| 70 | mds?: [IMicrosemiMdFormat[]]; |
| 71 | md?: IMicrosemiMdFormat; |
| 72 | } |
| 73 | |
| 74 | export interface IMicrosemiMdFormat { |
| 75 | mdName: string; |
| 76 | mdNameType: string; |
| 77 | mdLevel: string; |
| 78 | mdNumericId: number; |
| 79 | maList: any[]; |
| 80 | } |
| 81 | |
| 82 | export interface IMicrosemiMaData { |
| 83 | ma: IMicrosemiMaFormat[]; |
| 84 | } |
| 85 | |
| 86 | export interface IMicrosemiMaFormat { |
| 87 | maName: string; |
| 88 | maNameType: string; |
| 89 | maNumericId: number; |
| 90 | 'ccm-interval': string; |
| 91 | componentList: any[]; |
| 92 | 'rmep-list': any[]; |
| 93 | } |
| 94 | |
| 95 | export interface IMicrosemiMepData { |
| 96 | meps: [IMicrosemiMepFormat[]]; |
| 97 | } |
| 98 | |
| 99 | export interface IMicrosemiMepFormat { |
| 100 | mepId: number; |
| 101 | deviceId: string; |
| 102 | port: string; |
| 103 | direction: string; |
| 104 | mdName: string; |
| 105 | maName: string; |
| 106 | 'administrative-state': boolean; |
| 107 | 'cci-enabled': boolean; |
| 108 | 'ccm-ltm-priority': number; |
| 109 | macAddress: string; |
| 110 | loopback: any; |
| 111 | remoteMeps: any[]; |
| 112 | activeMacStatusDefect: boolean; |
| 113 | activeRdiCcmDefect: boolean; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | export interface IMicrosemiDmList { |
| 118 | dms: [IMicrosemiStatsData[]]; |
| 119 | } |
| 120 | |
| 121 | export class MicrosemiStats implements IMicrosemiStats { |
| 122 | |
| 123 | static $inject = [ |
| 124 | '$q', |
| 125 | '$http', |
| 126 | '$log', |
| 127 | '$base64' |
| 128 | ]; |
| 129 | |
| 130 | private auth = this.$base64.encode('onos:rocks'); |
| 131 | private headers = { |
| 132 | 'Authorization': `Basic ${this.auth}` |
| 133 | }; |
| 134 | |
| 135 | constructor ( |
| 136 | private $q: ng.IQService, |
| 137 | private $http: ng.IHttpService, |
| 138 | private $log: ng.ILogService, |
| 139 | private $base64: any |
| 140 | ) { |
| 141 | |
| 142 | } |
| 143 | |
| 144 | public getMds() { |
| 145 | const d = this.$q.defer(); |
| 146 | this.$http.get(`/vtn/onos/cfm/md/`, {'headers': this.headers}) |
| 147 | .then((res: IHttpPromiseCallbackArg<IMicrosemiMdData>) => { |
| 148 | d.resolve(res.data.mds[0]); |
| 149 | }) |
| 150 | .catch(err => { |
| 151 | this.$log.error(`[CFMStatService] Error in GET md: `, err); |
| 152 | }); |
| 153 | return d.promise; |
| 154 | } |
| 155 | |
| 156 | public getMeps(mdName: string, maName: string) { |
| 157 | const d = this.$q.defer(); |
| 158 | this.$http.get(`/vtn/onos/cfm/md/${mdName}/ma/${maName}/mep/`, {'headers': this.headers}) |
| 159 | .then((res: IHttpPromiseCallbackArg<IMicrosemiMepData>) => { |
| 160 | d.resolve(res.data.meps[0]); |
| 161 | }) |
| 162 | .catch(err => { |
| 163 | this.$log.error(`[CFMStatService] Error in GET mep: `, err); |
| 164 | }); |
| 165 | return d.promise; |
| 166 | } |
| 167 | |
| 168 | public getDms(mdName : string, maName: string, mepId: string) { |
| 169 | const d = this.$q.defer(); |
| 170 | this.$http.get(`/vtn/onos/cfm/md/${mdName}/ma/${maName}/mep/${mepId}/dm/`, {'headers': this.headers}) |
| 171 | .then((res: IHttpPromiseCallbackArg<IMicrosemiDmList>) => { |
| 172 | d.resolve(res.data.dms[0]); |
| 173 | }) |
| 174 | .catch(err => { |
| 175 | this.$log.error(`[CFMStatService] Error in GET dm list: `, err); |
| 176 | }); |
| 177 | return d.promise; |
| 178 | } |
| 179 | |
| 180 | public get(mdName : string, maName: string, mepId: string, dmId: string) { |
| 181 | const d = this.$q.defer(); |
| 182 | |
| 183 | this.$http.get(`/vtn/onos/cfm/md/${mdName}/ma/${maName}/mep/${mepId}/dm/${dmId}`, {'headers': this.headers}) |
| 184 | .then((res) => { |
| 185 | d.resolve(res); |
| 186 | }) |
| 187 | .catch(err => { |
| 188 | this.$log.error(`[CFMStatService] Error in GET: `, err); |
| 189 | }); |
| 190 | return d.promise; |
| 191 | } |
| 192 | |
| 193 | public getAllData(res: IMicrosemiStatsData) { |
| 194 | const data = res['data']; |
| 195 | const line = this.parseDataForLine(data); |
| 196 | const delay = this.parseDelayBin(data); |
| 197 | const jitter = this.parseJitterBin(data); |
| 198 | return { |
| 199 | line, |
| 200 | delay, |
| 201 | jitter |
| 202 | }; |
| 203 | } |
| 204 | |
| 205 | private getNumericVal(val: string): number { |
| 206 | if (!val) { |
| 207 | return 0; |
| 208 | } |
| 209 | val = val.replace('PT', ''); |
| 210 | val = val.replace('S', ''); |
| 211 | return parseFloat(val) * 1000; |
| 212 | } |
| 213 | |
| 214 | private formatDate(string: string): string { |
| 215 | const date = new Date(string); |
| 216 | return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`; |
| 217 | } |
| 218 | |
| 219 | private parseDelayBin(stats: IMicrosemiStatsData) { |
| 220 | const labels: number[] = []; |
| 221 | const data: number[] = []; |
| 222 | _.forEach(stats.dm.current.frameDelayTwoWayBins.bins, (bin) => { |
| 223 | labels.push(this.getNumericVal(bin.lowerLimit)); |
| 224 | data.push(bin.count); |
| 225 | }); |
| 226 | return { |
| 227 | labels, |
| 228 | data |
| 229 | }; |
| 230 | } |
| 231 | |
| 232 | private parseJitterBin(stats: IMicrosemiStatsData) { |
| 233 | const labels: number[] = []; |
| 234 | const data: number[] = []; |
| 235 | _.forEach(stats.dm.current.interFrameDelayVariationTwoWayBins.bins, (bin) => { |
| 236 | labels.push(this.getNumericVal(bin.lowerLimit)); |
| 237 | data.push(bin.count); |
| 238 | }); |
| 239 | return { |
| 240 | labels, |
| 241 | data |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | private parseDataForLine(data: IMicrosemiStatsData): IMicrosemiChartData { |
| 246 | let stats = angular.copy(data.dm.historic); |
| 247 | stats.unshift(data.dm.current); |
| 248 | |
| 249 | const jitterAvg: number[] = []; |
| 250 | const jitterMax: number[] = []; |
| 251 | const jitterMin: number[] = []; |
| 252 | const delayAvg: number[] = []; |
| 253 | const delayMax: number[] = []; |
| 254 | const delayMin: number[] = []; |
| 255 | const labels: string[] = []; |
| 256 | |
| 257 | _.forEach(stats, (s: IMicrosemiStatsFormat) => { |
| 258 | const j_avg = this.getNumericVal(s.interFrameDelayVariationTwoWayAvg); |
| 259 | const j_max = this.getNumericVal(s.interFrameDelayVariationTwoWayMax); |
| 260 | const j_min = this.getNumericVal(s.interFrameDelayVariationTwoWayMin); |
| 261 | const d_avg = this.getNumericVal(s.frameDelayTwoWayAvg); |
| 262 | const d_max = this.getNumericVal(s.frameDelayTwoWayMax); |
| 263 | const d_min = this.getNumericVal(s.frameDelayTwoWayMin); |
| 264 | jitterAvg.push(j_avg); |
| 265 | jitterMax.push(j_max); |
| 266 | jitterMin.push(j_min); |
| 267 | delayAvg.push(d_avg); |
| 268 | delayMax.push(d_max); |
| 269 | delayMin.push(d_min); |
| 270 | labels.push(s.endTime ? this.formatDate(s.endTime) : 'current'); |
| 271 | }); |
| 272 | |
| 273 | return { |
| 274 | labels: labels.reverse(), |
| 275 | data: [ |
| 276 | jitterAvg.reverse(), |
| 277 | jitterMax.reverse(), |
| 278 | jitterMin.reverse(), |
| 279 | delayAvg.reverse(), |
| 280 | delayMax.reverse(), |
| 281 | delayMin.reverse(), |
| 282 | ] |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | } |