Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 { Component, OnDestroy, OnInit } from '@angular/core'; |
| 17 | import { |
| 18 | FnService, |
| 19 | LogService, |
| 20 | PrefsService, |
| 21 | WebSocketService, |
Andrea Campanella | 7cbeb97 | 2019-07-25 15:05:59 +0200 | [diff] [blame] | 22 | IconService, |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 23 | SortDir, TableBaseImpl, TableResponse |
Sean Condon | 3dd062f | 2020-04-14 09:25:00 +0100 | [diff] [blame] | 24 | } from 'org_onosproject_onos/web/gui2-fw-lib/public_api'; |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 25 | import { ActivatedRoute } from '@angular/router'; |
| 26 | import {FormGroup, FormControl} from '@angular/forms'; |
| 27 | |
| 28 | /** |
| 29 | * Model of the response from WebSocket |
| 30 | */ |
| 31 | interface RoadmPortTableResponse extends TableResponse { |
| 32 | roadmPorts: RoadmPort[]; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Model of the roadm ports returned from the WebSocket |
| 37 | */ |
| 38 | interface RoadmPort { |
| 39 | id: string; |
| 40 | reversePort: string; |
| 41 | name: string; |
| 42 | type: string; |
| 43 | enabled: string; |
| 44 | minFreq: string; |
| 45 | maxFreq: string; |
| 46 | grid: string; |
Andrea Campanella | 02e2eb4e | 2019-08-29 11:46:57 -0700 | [diff] [blame] | 47 | currFreq: string; |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 48 | powerRange: string; |
| 49 | currentPower: string; |
| 50 | targetPower: string; |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 51 | modulation: string; |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 52 | hasTargetPower: string; |
| 53 | serviceState: string; |
| 54 | } |
| 55 | |
| 56 | interface FilterToggleState { |
| 57 | devId: string; |
| 58 | nzFilter: boolean; |
| 59 | showDelta: boolean; |
| 60 | } |
| 61 | |
| 62 | const defaultPortPrefsState = { |
| 63 | nzFilter: 1, |
| 64 | showDelta: 0, |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * ONOS GUI -- Port View Component |
| 69 | */ |
| 70 | @Component({ |
| 71 | selector: 'roadm-port', |
| 72 | templateUrl: './port.component.html', |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 73 | styleUrls: ['./port.component.css', |
| 74 | '../../../../../../web/gui2-fw-lib/lib/widget/table.theme.css', |
| 75 | '../../../../../../web/gui2-fw-lib/lib/widget/table.css', |
| 76 | ] |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 77 | }) |
| 78 | export class RoadmPortComponent extends TableBaseImpl implements OnInit, OnDestroy { |
| 79 | devId: string; |
| 80 | nzFilter: boolean = true; |
| 81 | showDelta: boolean = false; |
| 82 | prefsState = {}; |
| 83 | toggleState: FilterToggleState; |
| 84 | |
| 85 | powerForm: FormGroup; |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 86 | modulationForm: FormGroup; |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 87 | freqForm: FormGroup; |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 88 | SET_POWER_REQ = 'roadmSetTargetPowerRequest'; |
| 89 | SET_POWER_RESP = 'roadmSetTargetPowerResponse'; |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 90 | SET_MODULATION = 'roadmSetModulationRequest'; |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 91 | SET_FREQUENCY = 'roadmSetFrequencyRequest'; |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 92 | |
| 93 | restorePrefsConfig; // Function |
| 94 | |
| 95 | deviceTip = 'Show device table'; |
| 96 | |
| 97 | constructor(protected fs: FnService, |
| 98 | protected log: LogService, |
| 99 | protected ar: ActivatedRoute, |
| 100 | protected wss: WebSocketService, |
Andrea Campanella | 7cbeb97 | 2019-07-25 15:05:59 +0200 | [diff] [blame] | 101 | protected prefs: PrefsService, |
| 102 | protected is: IconService, |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 103 | ) { |
| 104 | super(fs, log, wss, 'roadmPort'); |
| 105 | this.ar.queryParams.subscribe(params => { |
| 106 | this.devId = params['devId']; |
| 107 | |
| 108 | }); |
| 109 | |
| 110 | this.payloadParams = { |
| 111 | devId: this.devId |
| 112 | }; |
| 113 | |
| 114 | this.responseCallback = this.portResponseCb; |
| 115 | this.restorePrefsConfig = this.restoreConfigFromPrefs; |
| 116 | |
| 117 | this.sortParams = { |
| 118 | firstCol: 'id', |
| 119 | firstDir: SortDir.desc, |
| 120 | secondCol: 'type', |
| 121 | secondDir: SortDir.asc, |
| 122 | }; |
Andrea Campanella | 7cbeb97 | 2019-07-25 15:05:59 +0200 | [diff] [blame] | 123 | |
| 124 | this.is.loadIconDef('switch'); |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | ngOnInit() { |
| 128 | this.init(); |
| 129 | this.createForm(); |
| 130 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 131 | [this.SET_POWER_RESP, (data) => this.powerConfigCb(data)] |
| 132 | ])); |
| 133 | this.log.debug('RoadmPortComponent initialized'); |
| 134 | } |
| 135 | |
| 136 | createForm() { |
| 137 | this.powerForm = new FormGroup({ |
| 138 | newPower: new FormControl(''), |
| 139 | }); |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 140 | this.modulationForm = new FormGroup({ |
| 141 | newModulation: new FormControl(''), |
| 142 | }); |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 143 | this.freqForm = new FormGroup({ |
| 144 | newFreq: new FormControl(''), |
| 145 | }); |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 146 | this.log.debug('Create Forms'); |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | ngOnDestroy() { |
| 150 | this.destroy(); |
| 151 | this.log.debug('RoadmPortComponent destroyed'); |
| 152 | } |
| 153 | |
| 154 | portResponseCb(data: RoadmPortTableResponse) { |
| 155 | this.log.debug('Roadm Port response received for ', data.roadmPorts.length, 'port'); |
| 156 | } |
| 157 | |
| 158 | isNz(): boolean { |
| 159 | return this.nzFilter; |
| 160 | } |
| 161 | |
| 162 | isDelta(): boolean { |
| 163 | return this.showDelta; |
| 164 | } |
| 165 | |
| 166 | toggleNZState(b?: any) { |
| 167 | if (b === undefined) { |
| 168 | this.nzFilter = !this.nzFilter; |
| 169 | } else { |
| 170 | this.nzFilter = b; |
| 171 | } |
| 172 | this.payloadParams = this.filterToggleState(); |
| 173 | this.updatePrefsState('nzFilter', this.nzFilter); |
| 174 | this.forceRefesh(); |
| 175 | } |
| 176 | |
| 177 | toggleDeltaState(b?: any) { |
| 178 | if (b === undefined) { |
| 179 | this.showDelta = !this.showDelta; |
| 180 | } else { |
| 181 | this.showDelta = b; |
| 182 | } |
| 183 | |
| 184 | this.payloadParams = this.filterToggleState(); |
| 185 | this.updatePrefsState('showDelta', this.showDelta); |
| 186 | this.forceRefesh(); |
| 187 | } |
| 188 | |
| 189 | updatePrefsState(what: any, b: any) { |
| 190 | this.prefsState[what] = b ? 1 : 0; |
| 191 | this.prefs.setPrefs('port_prefs', this.prefsState); |
| 192 | } |
| 193 | |
| 194 | filterToggleState(): FilterToggleState { |
| 195 | return this.toggleState = { |
| 196 | devId: this.devId, |
| 197 | nzFilter: this.nzFilter, |
| 198 | showDelta: this.showDelta, |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | forceRefesh() { |
| 203 | this.requestTableData(); |
| 204 | } |
| 205 | |
| 206 | restoreConfigFromPrefs() { |
| 207 | this.prefsState = this.prefs.asNumbers( |
| 208 | this.prefs.getPrefs('port_prefs', defaultPortPrefsState, ) |
| 209 | ); |
| 210 | |
| 211 | this.log.debug('Port - Prefs State:', this.prefsState); |
| 212 | this.toggleDeltaState(this.prefsState['showDelta']); |
| 213 | this.toggleNZState(this.prefsState['nzFilter']); |
| 214 | } |
| 215 | |
| 216 | submitPower(devId, port) { |
| 217 | this.log.debug('Set power of port ', port, 'in device ', devId, 'as value ', this.powerForm.value['newPower'], 'dBm.'); |
| 218 | this.wss.sendEvent(this.SET_POWER_REQ, { |
| 219 | 'targetPower': this.powerForm.value['newPower'], |
| 220 | 'devId': devId, |
| 221 | 'id': port, |
| 222 | }); |
| 223 | } |
| 224 | |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 225 | submitModulation(devId, port) { |
| 226 | this.log.debug('Set Modulation of port ', port, 'in device ', devId, 'as value ', this.modulationForm.value['newModulation']); |
| 227 | this.wss.sendEvent(this.SET_MODULATION, { |
| 228 | 'modulation': this.modulationForm.value['newModulation'], |
| 229 | 'devId': devId, |
| 230 | 'id': port, |
| 231 | }); |
| 232 | } |
| 233 | |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 234 | submitFrequency(devId, port) { |
| 235 | this.log.debug('Set Frequency of port ', port, 'in device ', devId, 'as value ', this.freqForm.value['newFreq']); |
| 236 | this.wss.sendEvent(this.SET_FREQUENCY, { |
| 237 | 'currFreq': this.freqForm.value['newFreq'], |
| 238 | 'devId': devId, |
| 239 | 'id': port, |
| 240 | }); |
| 241 | } |
| 242 | |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 243 | powerConfigCb(data) { |
| 244 | if (!data.valid) { |
| 245 | const info = 'The power config operation is failed. The reason is: \n' + data.message; |
| 246 | alert(info); |
| 247 | } else { |
| 248 | this.log.debug('The power config operation is successful!'); |
| 249 | } |
| 250 | } |
Sean Condon | f20b8ef | 2019-08-13 16:45:52 +0100 | [diff] [blame] | 251 | |
| 252 | convertNumber(str: string): number { |
| 253 | return Number(str); |
| 254 | } |
Boyuan Yan | 6b23b38 | 2019-06-04 11:59:35 -0700 | [diff] [blame] | 255 | } |