blob: 6bd490e8440b236f8f4309ac43f143a553f22cad [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;
Sean Condon4e55c802019-12-03 22:13:34 +0000132 this.intentData.type = selRow.type;
prai9d445962018-07-27 13:27:43 +0530133 }
134
135 briefToggle() {
136 this.brief = !this.brief;
137 }
138
139 intentState() {
140 return this.intentData.state;
141 }
142
143 isHavingWithdrawn() {
144 if (this.intentTableData !== undefined) {
145 for (let i = 0; i < this.intentTableData.intents.length; i++) {
146 if (this.intentTableData.intents[i].state === 'Withdrawn') {
147 this.withdrwanIntent = true;
148 return this.withdrwanIntent;
149 }
150 }
151
152 }
153 return this.withdrwanIntent;
154 }
155
156 /**
prai9d445962018-07-27 13:27:43 +0530157 * Perform one of the intent actions - resubmit, remove, purge or purge withdrawn
158 * Raises a dialog which calls back the dOk() below
159 */
160 confirmAction(action: IntentAction): void {
161 this.intentActionMsg = '';
162 this.intentAction = action;
163 const intentActionLc = (<string>IntentAction[this.intentAction]).toLowerCase();
164
165 if (this.intentAction === IntentAction.PURGE) {
166 this.intentData.purge = true;
167 } else {
168 this.intentData.purge = false;
169 }
170
171 if (this.intentAction === IntentAction.PURGEWITHDRAWN) {
172 this.warnMsg = 'Are you sure you want to purge all the withdrawn intents?';
173 } else {
174 this.warnMsg = 'Are you sure you want to ' + intentActionLc + ' the selected intent?';
175 }
176 this.log.debug('Initiating', this.intentAction, 'of', this.selId);
177 }
178
179 /**
180 * Callback when the Confirm dialog is shown and a choice is made
181 */
182 dOk(choice: boolean) {
183 const intentActionLc = (<string>IntentAction[this.intentAction]).toLowerCase();
184 if (choice) {
185
186 if (this.intentAction === IntentAction.RESUBMIT) {
187 this.wss.sendEvent(RESUBMITINTENT, {
188 appId: this.intentData.appId,
189 appName: this.intentData.appName,
190 key: this.intentData.key,
191 purge: this.intentData.purge,
192 });
193 this.selId = '';
194 this.intentActionMsg = INTENTRESUBMITTED;
195 } else if ((this.intentAction === IntentAction.PURGE) || (this.intentAction === IntentAction.WITHDRAWN)) {
196 this.wss.sendEvent(REMOVEINTENT, {
197 appId: this.intentData.appId,
198 appName: this.intentData.appName,
199 key: this.intentData.key,
200 purge: this.intentData.purge,
201 });
202 this.selId = '';
203 if (this.intentData.purge) {
204 this.intentActionMsg = INTENTPURGE;
205 } else {
206 this.intentActionMsg = INTENTWITHDRAWN;
207 }
208 } else if (this.intentAction === IntentAction.PURGEWITHDRAWN) {
209 this.wss.sendEvent(REMOVEINTENTS, {});
210 this.intentActionMsg = INTENTPURGE;
211 } else {
212 this.log.debug('Some worng input provided');
213 }
214
215 } else {
216 this.log.debug('Cancelled', intentActionLc, 'on', this.selId);
217 }
218 this.warnMsg = '';
219 }
220}