blob: 56bd85cf09fb34d112c0cad0b679551bc4f88628 [file] [log] [blame]
Boyuan Yan6b23b382019-06-04 11:59:35 -07001/*
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 */
16import { Component, OnDestroy, OnInit } from '@angular/core';
17import {
18 FnService,
19 LogService,
20 PrefsService,
21 WebSocketService,
Andrea Campanella7cbeb972019-07-25 15:05:59 +020022 IconService,
Boyuan Yan6b23b382019-06-04 11:59:35 -070023 SortDir, TableBaseImpl, TableResponse
24} from 'gui2-fw-lib';
25import { ActivatedRoute } from '@angular/router';
26import {FormGroup, FormControl} from '@angular/forms';
27
28/**
29 * Model of the response from WebSocket
30 */
31interface RoadmPortTableResponse extends TableResponse {
32 roadmPorts: RoadmPort[];
33}
34
35/**
36 * Model of the roadm ports returned from the WebSocket
37 */
38interface 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 Campanella7ebfe322019-08-29 11:46:57 -070047 currFreq: string;
Boyuan Yan6b23b382019-06-04 11:59:35 -070048 powerRange: string;
49 currentPower: string;
50 targetPower: string;
Andrea Campanellabdeeda12019-08-02 16:12:05 +020051 modulation: string;
Boyuan Yan6b23b382019-06-04 11:59:35 -070052 hasTargetPower: string;
53 serviceState: string;
54}
55
56interface FilterToggleState {
57 devId: string;
58 nzFilter: boolean;
59 showDelta: boolean;
60}
61
62const 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})
75export 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 Campanellabdeeda12019-08-02 16:12:05 +020083 modulationForm: FormGroup;
Boyuan Yan6b23b382019-06-04 11:59:35 -070084 SET_POWER_REQ = 'roadmSetTargetPowerRequest';
85 SET_POWER_RESP = 'roadmSetTargetPowerResponse';
Andrea Campanellabdeeda12019-08-02 16:12:05 +020086 SET_MODULATION = 'roadmSetModulationRequest';
Boyuan Yan6b23b382019-06-04 11:59:35 -070087
88 restorePrefsConfig; // Function
89
90 deviceTip = 'Show device table';
91
92 constructor(protected fs: FnService,
93 protected log: LogService,
94 protected ar: ActivatedRoute,
95 protected wss: WebSocketService,
Andrea Campanella7cbeb972019-07-25 15:05:59 +020096 protected prefs: PrefsService,
97 protected is: IconService,
Boyuan Yan6b23b382019-06-04 11:59:35 -070098 ) {
99 super(fs, log, wss, 'roadmPort');
100 this.ar.queryParams.subscribe(params => {
101 this.devId = params['devId'];
102
103 });
104
105 this.payloadParams = {
106 devId: this.devId
107 };
108
109 this.responseCallback = this.portResponseCb;
110 this.restorePrefsConfig = this.restoreConfigFromPrefs;
111
112 this.sortParams = {
113 firstCol: 'id',
114 firstDir: SortDir.desc,
115 secondCol: 'type',
116 secondDir: SortDir.asc,
117 };
Andrea Campanella7cbeb972019-07-25 15:05:59 +0200118
119 this.is.loadIconDef('switch');
Boyuan Yan6b23b382019-06-04 11:59:35 -0700120 }
121
122 ngOnInit() {
123 this.init();
124 this.createForm();
125 this.wss.bindHandlers(new Map<string, (data) => void>([
126 [this.SET_POWER_RESP, (data) => this.powerConfigCb(data)]
127 ]));
128 this.log.debug('RoadmPortComponent initialized');
129 }
130
131 createForm() {
132 this.powerForm = new FormGroup({
133 newPower: new FormControl(''),
134 });
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200135 this.modulationForm = new FormGroup({
136 newModulation: new FormControl(''),
137 });
138 this.log.debug('Create Forms');
Boyuan Yan6b23b382019-06-04 11:59:35 -0700139 }
140
141 ngOnDestroy() {
142 this.destroy();
143 this.log.debug('RoadmPortComponent destroyed');
144 }
145
146 portResponseCb(data: RoadmPortTableResponse) {
147 this.log.debug('Roadm Port response received for ', data.roadmPorts.length, 'port');
148 }
149
150 isNz(): boolean {
151 return this.nzFilter;
152 }
153
154 isDelta(): boolean {
155 return this.showDelta;
156 }
157
158 toggleNZState(b?: any) {
159 if (b === undefined) {
160 this.nzFilter = !this.nzFilter;
161 } else {
162 this.nzFilter = b;
163 }
164 this.payloadParams = this.filterToggleState();
165 this.updatePrefsState('nzFilter', this.nzFilter);
166 this.forceRefesh();
167 }
168
169 toggleDeltaState(b?: any) {
170 if (b === undefined) {
171 this.showDelta = !this.showDelta;
172 } else {
173 this.showDelta = b;
174 }
175
176 this.payloadParams = this.filterToggleState();
177 this.updatePrefsState('showDelta', this.showDelta);
178 this.forceRefesh();
179 }
180
181 updatePrefsState(what: any, b: any) {
182 this.prefsState[what] = b ? 1 : 0;
183 this.prefs.setPrefs('port_prefs', this.prefsState);
184 }
185
186 filterToggleState(): FilterToggleState {
187 return this.toggleState = {
188 devId: this.devId,
189 nzFilter: this.nzFilter,
190 showDelta: this.showDelta,
191 };
192 }
193
194 forceRefesh() {
195 this.requestTableData();
196 }
197
198 restoreConfigFromPrefs() {
199 this.prefsState = this.prefs.asNumbers(
200 this.prefs.getPrefs('port_prefs', defaultPortPrefsState, )
201 );
202
203 this.log.debug('Port - Prefs State:', this.prefsState);
204 this.toggleDeltaState(this.prefsState['showDelta']);
205 this.toggleNZState(this.prefsState['nzFilter']);
206 }
207
208 submitPower(devId, port) {
209 this.log.debug('Set power of port ', port, 'in device ', devId, 'as value ', this.powerForm.value['newPower'], 'dBm.');
210 this.wss.sendEvent(this.SET_POWER_REQ, {
211 'targetPower': this.powerForm.value['newPower'],
212 'devId': devId,
213 'id': port,
214 });
215 }
216
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200217 submitModulation(devId, port) {
218 this.log.debug('Set Modulation of port ', port, 'in device ', devId, 'as value ', this.modulationForm.value['newModulation']);
219 this.wss.sendEvent(this.SET_MODULATION, {
220 'modulation': this.modulationForm.value['newModulation'],
221 'devId': devId,
222 'id': port,
223 });
224 }
225
Boyuan Yan6b23b382019-06-04 11:59:35 -0700226 powerConfigCb(data) {
227 if (!data.valid) {
228 const info = 'The power config operation is failed. The reason is: \n' + data.message;
229 alert(info);
230 } else {
231 this.log.debug('The power config operation is successful!');
232 }
233 }
Sean Condonf20b8ef2019-08-13 16:45:52 +0100234
235 convertNumber(str: string): number {
236 return Number(str);
237 }
Boyuan Yan6b23b382019-06-04 11:59:35 -0700238}