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