blob: 6719564b5fb5beb5dd250329f87af8c151528760 [file] [log] [blame]
prai9d445962018-07-27 13:27:43 +05301/*
2 * Copyright 2018-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, OnInit, OnDestroy } from '@angular/core';
Sean Condon5ca00262018-09-06 17:55:25 +010017import {
18 FnService,
19 LoadingService,
20 LogService,
21 WebSocketService,
22 SortDir, TableBaseImpl, TableResponse
23} from 'gui2-fw-lib';
prai9d445962018-07-27 13:27:43 +053024
25const RESUBMITINTENT = 'resubmitIntent';
26const REMOVEINTENT = 'removeIntent';
27const REMOVEINTENTS = 'removeIntents';
28const INTENTRESUBMITTED = 'Intent resubmitted';
29const INTENTWITHDRAWN = 'Intent withdrawn';
30const INTENTPURGE = 'Intents purged';
31
32/**
33 * Model of the response from WebSocket
34 */
35interface IntentTableResponse extends TableResponse {
36 intents: Intent[];
37}
38
39/**
40 * Model of the Intents returned from the WebSocket
41 */
42interface Intent {
43 appId: string;
44 appName: string;
45 key: string;
46 type: string;
47 priority: string;
48 state: string;
49 purge: boolean;
50}
51
52export enum IntentAction {
53 NONE = 0,
54 RESUBMIT = 1,
55 WITHDRAWN = 2,
56 PURGE = 3,
57 PURGEWITHDRAWN = 4,
58}
59
60/**
61* ONOS GUI -- Intents View Component extends TableBaseImpl
62*/
63@Component({
64 selector: 'onos-intent',
65 templateUrl: './intent.component.html',
66 styleUrls: ['./intent.component.css', './intent-theme.css', '../../../fw/widget/table.css', '../../../fw/widget/table.theme.css']
67})
68export class IntentComponent extends TableBaseImpl implements OnInit, OnDestroy {
69
70 brief: boolean;
71 withdrwanIntent: boolean;
72 intentData: Intent;
73 warnMsg: string = '';
74 intentActionMsg: string;
75 intentTableData: IntentTableResponse;
76 IntentActionEnum: any = IntentAction;
77 intentAction: IntentAction = IntentAction.NONE;
78
79 topoTip = 'Show selected intent on topology view';
80 resubmitTip = 'Resubmit selected intent';
81 deactivateTip = 'Remove selected intent';
82 purgeTip = 'Purge selected intent';
83 purgeAllTip = 'Purge withdrawn intents';
84
85 briefTip = 'Switch to brief view';
86 detailTip = 'Switch to detailed view';
87
88 constructor(
89 protected log: LogService,
90 protected fs: FnService,
prai9d445962018-07-27 13:27:43 +053091 protected ls: LoadingService,
92 protected wss: WebSocketService,
93 ) {
94 super(fs, ls, log, wss, 'intent', 'key');
95 this.responseCallback = this.intentResponseCb;
96 this.parentSelCb = this.rowSelection;
97 this.brief = true;
98 this.sortParams = {
99 firstCol: 'appId',
100 firstDir: SortDir.desc,
101 secondCol: 'key',
102 secondDir: SortDir.asc,
103 };
104 this.intentData = <Intent>{};
105 }
106
107 ngOnInit() {
108 this.init();
109 this.log.info('IntentComponent initialized');
110 }
111
112 ngOnDestroy() {
113 this.destroy();
114 this.log.info('IntentComponent distroyed');
115 }
116
117 intentResponseCb(data: IntentTableResponse) {
118 this.withdrwanIntent = false;
119 this.log.debug('Intent response received for ', data.intents.length, 'intent');
120 this.intentTableData = data;
121 this.isHavingWithdrawn();
122 }
123
124 /**
125 * called when a row is selected - sets the state of control icons
126 */
127 rowSelection(event: any, selRow: any) {
128 this.intentData.state = selRow.state;
129 const selRowAppId = selRow.appId;
130 const splittedRowAppId = selRowAppId.split(':');
131 this.intentData.appId = splittedRowAppId[0].trim();
132 this.intentData.appName = splittedRowAppId[1].trim();
133 this.intentData.key = this.selId;
134 }
135
136 briefToggle() {
137 this.brief = !this.brief;
138 }
139
140 intentState() {
141 return this.intentData.state;
142 }
143
144 isHavingWithdrawn() {
145 if (this.intentTableData !== undefined) {
146 for (let i = 0; i < this.intentTableData.intents.length; i++) {
147 if (this.intentTableData.intents[i].state === 'Withdrawn') {
148 this.withdrwanIntent = true;
149 return this.withdrwanIntent;
150 }
151 }
152
153 }
154 return this.withdrwanIntent;
155 }
156
157 /**
158 * TO-DO intent view related function need to implement once
159 * topology page will be available
160 */
161 showIntent() {
162 }
163
164 /**
165 * TO-DO intent view related function need to implement once
166 * topology page will be available
167 */
168 canShowIntent() {
169 }
170
171 /**
172 * Perform one of the intent actions - resubmit, remove, purge or purge withdrawn
173 * Raises a dialog which calls back the dOk() below
174 */
175 confirmAction(action: IntentAction): void {
176 this.intentActionMsg = '';
177 this.intentAction = action;
178 const intentActionLc = (<string>IntentAction[this.intentAction]).toLowerCase();
179
180 if (this.intentAction === IntentAction.PURGE) {
181 this.intentData.purge = true;
182 } else {
183 this.intentData.purge = false;
184 }
185
186 if (this.intentAction === IntentAction.PURGEWITHDRAWN) {
187 this.warnMsg = 'Are you sure you want to purge all the withdrawn intents?';
188 } else {
189 this.warnMsg = 'Are you sure you want to ' + intentActionLc + ' the selected intent?';
190 }
191 this.log.debug('Initiating', this.intentAction, 'of', this.selId);
192 }
193
194 /**
195 * Callback when the Confirm dialog is shown and a choice is made
196 */
197 dOk(choice: boolean) {
198 const intentActionLc = (<string>IntentAction[this.intentAction]).toLowerCase();
199 if (choice) {
200
201 if (this.intentAction === IntentAction.RESUBMIT) {
202 this.wss.sendEvent(RESUBMITINTENT, {
203 appId: this.intentData.appId,
204 appName: this.intentData.appName,
205 key: this.intentData.key,
206 purge: this.intentData.purge,
207 });
208 this.selId = '';
209 this.intentActionMsg = INTENTRESUBMITTED;
210 } else if ((this.intentAction === IntentAction.PURGE) || (this.intentAction === IntentAction.WITHDRAWN)) {
211 this.wss.sendEvent(REMOVEINTENT, {
212 appId: this.intentData.appId,
213 appName: this.intentData.appName,
214 key: this.intentData.key,
215 purge: this.intentData.purge,
216 });
217 this.selId = '';
218 if (this.intentData.purge) {
219 this.intentActionMsg = INTENTPURGE;
220 } else {
221 this.intentActionMsg = INTENTWITHDRAWN;
222 }
223 } else if (this.intentAction === IntentAction.PURGEWITHDRAWN) {
224 this.wss.sendEvent(REMOVEINTENTS, {});
225 this.intentActionMsg = INTENTPURGE;
226 } else {
227 this.log.debug('Some worng input provided');
228 }
229
230 } else {
231 this.log.debug('Cancelled', intentActionLc, 'on', this.selId);
232 }
233 this.warnMsg = '';
234 }
235}