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