blob: 585514da3e9ef79c5fb29cadee56ad18ef3c31be [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,
Sean Condon5ca00262018-09-06 17:55:25 +010019 LogService,
20 WebSocketService,
21 SortDir, TableBaseImpl, TableResponse
22} from 'gui2-fw-lib';
prai9d445962018-07-27 13:27:43 +053023
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,
prai9d445962018-07-27 13:27:43 +053090 protected wss: WebSocketService,
91 ) {
Sean Condon95fb5742019-04-02 12:16:55 +010092 super(fs, log, wss, 'intent', 'key');
prai9d445962018-07-27 13:27:43 +053093 this.responseCallback = this.intentResponseCb;
94 this.parentSelCb = this.rowSelection;
95 this.brief = true;
96 this.sortParams = {
97 firstCol: 'appId',
98 firstDir: SortDir.desc,
99 secondCol: 'key',
100 secondDir: SortDir.asc,
101 };
102 this.intentData = <Intent>{};
103 }
104
105 ngOnInit() {
106 this.init();
107 this.log.info('IntentComponent initialized');
108 }
109
110 ngOnDestroy() {
111 this.destroy();
112 this.log.info('IntentComponent distroyed');
113 }
114
115 intentResponseCb(data: IntentTableResponse) {
116 this.withdrwanIntent = false;
117 this.log.debug('Intent response received for ', data.intents.length, 'intent');
118 this.intentTableData = data;
119 this.isHavingWithdrawn();
120 }
121
122 /**
123 * called when a row is selected - sets the state of control icons
124 */
125 rowSelection(event: any, selRow: any) {
126 this.intentData.state = selRow.state;
127 const selRowAppId = selRow.appId;
128 const splittedRowAppId = selRowAppId.split(':');
129 this.intentData.appId = splittedRowAppId[0].trim();
130 this.intentData.appName = splittedRowAppId[1].trim();
131 this.intentData.key = this.selId;
132 }
133
134 briefToggle() {
135 this.brief = !this.brief;
136 }
137
138 intentState() {
139 return this.intentData.state;
140 }
141
142 isHavingWithdrawn() {
143 if (this.intentTableData !== undefined) {
144 for (let i = 0; i < this.intentTableData.intents.length; i++) {
145 if (this.intentTableData.intents[i].state === 'Withdrawn') {
146 this.withdrwanIntent = true;
147 return this.withdrwanIntent;
148 }
149 }
150
151 }
152 return this.withdrwanIntent;
153 }
154
155 /**
156 * TO-DO intent view related function need to implement once
157 * topology page will be available
158 */
159 showIntent() {
160 }
161
162 /**
163 * TO-DO intent view related function need to implement once
164 * topology page will be available
165 */
166 canShowIntent() {
167 }
168
169 /**
170 * Perform one of the intent actions - resubmit, remove, purge or purge withdrawn
171 * Raises a dialog which calls back the dOk() below
172 */
173 confirmAction(action: IntentAction): void {
174 this.intentActionMsg = '';
175 this.intentAction = action;
176 const intentActionLc = (<string>IntentAction[this.intentAction]).toLowerCase();
177
178 if (this.intentAction === IntentAction.PURGE) {
179 this.intentData.purge = true;
180 } else {
181 this.intentData.purge = false;
182 }
183
184 if (this.intentAction === IntentAction.PURGEWITHDRAWN) {
185 this.warnMsg = 'Are you sure you want to purge all the withdrawn intents?';
186 } else {
187 this.warnMsg = 'Are you sure you want to ' + intentActionLc + ' the selected intent?';
188 }
189 this.log.debug('Initiating', this.intentAction, 'of', this.selId);
190 }
191
192 /**
193 * Callback when the Confirm dialog is shown and a choice is made
194 */
195 dOk(choice: boolean) {
196 const intentActionLc = (<string>IntentAction[this.intentAction]).toLowerCase();
197 if (choice) {
198
199 if (this.intentAction === IntentAction.RESUBMIT) {
200 this.wss.sendEvent(RESUBMITINTENT, {
201 appId: this.intentData.appId,
202 appName: this.intentData.appName,
203 key: this.intentData.key,
204 purge: this.intentData.purge,
205 });
206 this.selId = '';
207 this.intentActionMsg = INTENTRESUBMITTED;
208 } else if ((this.intentAction === IntentAction.PURGE) || (this.intentAction === IntentAction.WITHDRAWN)) {
209 this.wss.sendEvent(REMOVEINTENT, {
210 appId: this.intentData.appId,
211 appName: this.intentData.appName,
212 key: this.intentData.key,
213 purge: this.intentData.purge,
214 });
215 this.selId = '';
216 if (this.intentData.purge) {
217 this.intentActionMsg = INTENTPURGE;
218 } else {
219 this.intentActionMsg = INTENTWITHDRAWN;
220 }
221 } else if (this.intentAction === IntentAction.PURGEWITHDRAWN) {
222 this.wss.sendEvent(REMOVEINTENTS, {});
223 this.intentActionMsg = INTENTPURGE;
224 } else {
225 this.log.debug('Some worng input provided');
226 }
227
228 } else {
229 this.log.debug('Cancelled', intentActionLc, 'on', this.selId);
230 }
231 this.warnMsg = '';
232 }
233}