blob: 34a2bdfcb51d8369d85eb26e9f8a16e10657f690 [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,
22 SortDir, TableBaseImpl, TableResponse
23} from 'gui2-fw-lib';
24import { ActivatedRoute } from '@angular/router';
25import {FormGroup, FormControl} from '@angular/forms';
26
27/**
28 * Model of the response from WebSocket
29 */
30interface RoadmPortTableResponse extends TableResponse {
31 roadmPorts: RoadmPort[];
32}
33
34/**
35 * Model of the roadm ports returned from the WebSocket
36 */
37interface RoadmPort {
38 id: string;
39 reversePort: string;
40 name: string;
41 type: string;
42 enabled: string;
43 minFreq: string;
44 maxFreq: string;
45 grid: string;
46 powerRange: string;
47 currentPower: string;
48 targetPower: string;
49 hasTargetPower: string;
50 serviceState: string;
51}
52
53interface FilterToggleState {
54 devId: string;
55 nzFilter: boolean;
56 showDelta: boolean;
57}
58
59const defaultPortPrefsState = {
60 nzFilter: 1,
61 showDelta: 0,
62};
63
64/**
65 * ONOS GUI -- Port View Component
66 */
67@Component({
68 selector: 'roadm-port',
69 templateUrl: './port.component.html',
70 styleUrls: ['./port.component.css', '../../../fw/widget/table.css', '../../../fw/widget/table.theme.css']
71})
72export class RoadmPortComponent extends TableBaseImpl implements OnInit, OnDestroy {
73 devId: string;
74 nzFilter: boolean = true;
75 showDelta: boolean = false;
76 prefsState = {};
77 toggleState: FilterToggleState;
78
79 powerForm: FormGroup;
80 SET_POWER_REQ = 'roadmSetTargetPowerRequest';
81 SET_POWER_RESP = 'roadmSetTargetPowerResponse';
82
83 restorePrefsConfig; // Function
84
85 deviceTip = 'Show device table';
86
87 constructor(protected fs: FnService,
88 protected log: LogService,
89 protected ar: ActivatedRoute,
90 protected wss: WebSocketService,
91 protected prefs: PrefsService
92 ) {
93 super(fs, log, wss, 'roadmPort');
94 this.ar.queryParams.subscribe(params => {
95 this.devId = params['devId'];
96
97 });
98
99 this.payloadParams = {
100 devId: this.devId
101 };
102
103 this.responseCallback = this.portResponseCb;
104 this.restorePrefsConfig = this.restoreConfigFromPrefs;
105
106 this.sortParams = {
107 firstCol: 'id',
108 firstDir: SortDir.desc,
109 secondCol: 'type',
110 secondDir: SortDir.asc,
111 };
112 }
113
114 ngOnInit() {
115 this.init();
116 this.createForm();
117 this.wss.bindHandlers(new Map<string, (data) => void>([
118 [this.SET_POWER_RESP, (data) => this.powerConfigCb(data)]
119 ]));
120 this.log.debug('RoadmPortComponent initialized');
121 }
122
123 createForm() {
124 this.powerForm = new FormGroup({
125 newPower: new FormControl(''),
126 });
127 this.log.debug('Create Form');
128 }
129
130 ngOnDestroy() {
131 this.destroy();
132 this.log.debug('RoadmPortComponent destroyed');
133 }
134
135 portResponseCb(data: RoadmPortTableResponse) {
136 this.log.debug('Roadm Port response received for ', data.roadmPorts.length, 'port');
137 }
138
139 isNz(): boolean {
140 return this.nzFilter;
141 }
142
143 isDelta(): boolean {
144 return this.showDelta;
145 }
146
147 toggleNZState(b?: any) {
148 if (b === undefined) {
149 this.nzFilter = !this.nzFilter;
150 } else {
151 this.nzFilter = b;
152 }
153 this.payloadParams = this.filterToggleState();
154 this.updatePrefsState('nzFilter', this.nzFilter);
155 this.forceRefesh();
156 }
157
158 toggleDeltaState(b?: any) {
159 if (b === undefined) {
160 this.showDelta = !this.showDelta;
161 } else {
162 this.showDelta = b;
163 }
164
165 this.payloadParams = this.filterToggleState();
166 this.updatePrefsState('showDelta', this.showDelta);
167 this.forceRefesh();
168 }
169
170 updatePrefsState(what: any, b: any) {
171 this.prefsState[what] = b ? 1 : 0;
172 this.prefs.setPrefs('port_prefs', this.prefsState);
173 }
174
175 filterToggleState(): FilterToggleState {
176 return this.toggleState = {
177 devId: this.devId,
178 nzFilter: this.nzFilter,
179 showDelta: this.showDelta,
180 };
181 }
182
183 forceRefesh() {
184 this.requestTableData();
185 }
186
187 restoreConfigFromPrefs() {
188 this.prefsState = this.prefs.asNumbers(
189 this.prefs.getPrefs('port_prefs', defaultPortPrefsState, )
190 );
191
192 this.log.debug('Port - Prefs State:', this.prefsState);
193 this.toggleDeltaState(this.prefsState['showDelta']);
194 this.toggleNZState(this.prefsState['nzFilter']);
195 }
196
197 submitPower(devId, port) {
198 this.log.debug('Set power of port ', port, 'in device ', devId, 'as value ', this.powerForm.value['newPower'], 'dBm.');
199 this.wss.sendEvent(this.SET_POWER_REQ, {
200 'targetPower': this.powerForm.value['newPower'],
201 'devId': devId,
202 'id': port,
203 });
204 }
205
206 powerConfigCb(data) {
207 if (!data.valid) {
208 const info = 'The power config operation is failed. The reason is: \n' + data.message;
209 alert(info);
210 } else {
211 this.log.debug('The power config operation is successful!');
212 }
213 }
214}