blob: df94ed1e19db1bff0a97952625d30b8e1ec0177c [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';
17import { LogService } from '../../../log.service';
18import { FnService } from '../../../fw/util/fn.service';
19import { LoadingService } from '../../../fw/layer/loading.service';
20import { DialogService } from '../../../fw/layer/dialog.service';
21import { WebSocketService } from '../../../fw/remote/websocket.service';
22import { TableResponse, TableBaseImpl, SortDir } from '../../../fw/widget/table.base';
23
24const RESUBMITINTENT = 'resubmitIntent';
25const REMOVEINTENT = 'removeIntent';
26const REMOVEINTENTS = 'removeIntents';
27const INTENTRESUBMITTED = 'Intent resubmitted';
28const INTENTWITHDRAWN = 'Intent withdrawn';
29const INTENTPURGE = 'Intents purged';
30
31/**
32 * Model of the response from WebSocket
33 */
34interface IntentTableResponse extends TableResponse {
35 intents: Intent[];
36}
37
38/**
39 * Model of the Intents returned from the WebSocket
40 */
41interface Intent {
42 appId: string;
43 appName: string;
44 key: string;
45 type: string;
46 priority: string;
47 state: string;
48 purge: boolean;
49}
50
51export enum IntentAction {
52 NONE = 0,
53 RESUBMIT = 1,
54 WITHDRAWN = 2,
55 PURGE = 3,
56 PURGEWITHDRAWN = 4,
57}
58
59/**
60* ONOS GUI -- Intents View Component extends TableBaseImpl
61*/
62@Component({
63 selector: 'onos-intent',
64 templateUrl: './intent.component.html',
65 styleUrls: ['./intent.component.css', './intent-theme.css', '../../../fw/widget/table.css', '../../../fw/widget/table.theme.css']
66})
67export class IntentComponent extends TableBaseImpl implements OnInit, OnDestroy {
68
69 brief: boolean;
70 withdrwanIntent: boolean;
71 intentData: Intent;
72 warnMsg: string = '';
73 intentActionMsg: string;
74 intentTableData: IntentTableResponse;
75 IntentActionEnum: any = IntentAction;
76 intentAction: IntentAction = IntentAction.NONE;
77
78 topoTip = 'Show selected intent on topology view';
79 resubmitTip = 'Resubmit selected intent';
80 deactivateTip = 'Remove selected intent';
81 purgeTip = 'Purge selected intent';
82 purgeAllTip = 'Purge withdrawn intents';
83
84 briefTip = 'Switch to brief view';
85 detailTip = 'Switch to detailed view';
86
87 constructor(
88 protected log: LogService,
89 protected fs: FnService,
90 private ds: DialogService,
91 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}