summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/views/services/slicing-management/slicing-task-management/slicing-task-model/subnet-params-model/subnet-params-model.component.ts
blob: 2d2dd7fa13a52895b6e42d09a7a6798425f28914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import {
	Component,
	OnInit,
	Input,
	Output,
	EventEmitter,
	ElementRef,
} from "@angular/core";
import { TRANSFRER_FORM_ITEMS, CORE_FORM_ITEMS } from "./constants";
import { NzMessageService } from "ng-zorro-antd";
import { stringify } from "@angular/core/src/util";
import { Util } from "../../../../../../shared/utils/utils";
import { SlicingTaskServices } from "@src/app/core/services/slicingTaskServices";

@Component({
	selector: "app-subnet-params-model",
	templateUrl: "./subnet-params-model.component.html",
	styleUrls: ["./subnet-params-model.component.less"],
})
export class SubnetParamsModelComponent implements OnInit {
	@Input() showModel: boolean;
	@Input() detailData: any;
	@Input() title: string;
	@Output() cancel = new EventEmitter<boolean>();
	@Output() paramsDataChange = new EventEmitter<any>();
	@Output() noPassParaChange = new EventEmitter<any>();

	transferFormItems: any[] = TRANSFRER_FORM_ITEMS;
	regxpIP = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/; // check for correct ip address
	formData: any;
	coreFormItems: any = [];
	areaList: any[] = [];
	areaLevel: number = 3;
	ANEndpointInputs: object = {};
	CNEndpointInputs: object = {};
	ANkeyList: string[] = [];
	CNkeyList: string[] = [];
	EndpointEnable: boolean = false; // Whether to enable the three parameters of Endpoint
	keyList: string[] = []; // keys of endPoint
	specialParaTN: string[] = [
		"Resource Sharing Level",
		"Connection Links",
		"AN Endpoint",
		"CN Endpoint",
	];
	// Parameters not passed to the back end
	notPassPara: string[] = ["sliceProfile_TN_connection_links"];
	connectionLinkTable: any[] = [];
	connectionTableHeader: string[] = [];
	pageSize: number = 2;
	recordNum: number = 0;
	// hasPageNo: number[] = [];
	loading = false;
	loadingDelay = 100;
	objectKeys = Object.keys;
	//  Comment: Above code

	constructor(
		private message: NzMessageService,
		private Util: Util,
		private http: SlicingTaskServices
	) {}

	ngOnInit() {}

	ngOnChanges() {
		// It is excuted every time you open it
		if (this.title) {
			this.formData = JSON.parse(JSON.stringify(this.detailData));
			if (this.title === "An" || this.title === "Cn") {
				this.coreFormItems =
					this.title === "An"
						? CORE_FORM_ITEMS.An
						: this.title === "Cn"
						? CORE_FORM_ITEMS.Cn
						: [];
			} else if (this.title === "Tn") {
				const pageNo = 1; // start from page 1
				const pageSize = this.pageSize;
				this.getConnectionLinkTable(pageNo, pageSize);
				this.ANkeyList = this.transferFormItems
					.find((item) => {
						return item.title === "AN Endpoint";
					})
					.options.map((val) => {
						return val.key;
					});
				this.CNkeyList = this.transferFormItems
					.find((item) => {
						return item.title === "CN Endpoint";
					})
					.options.map((val) => {
						return val.key;
					});
				this.keyList = this.ANkeyList.concat(this.CNkeyList);
				if (
					typeof this.formData !== "undefined" &&
					Object.keys(this.formData).length !== 0
				) {
					this.EndpointEnable = this.keyList.every((item) => {
						return this.formData.hasOwnProperty(item);
					});
				}
				if (this.EndpointEnable) {
					this.ANEndpointInputs = this.Util.pick(
						this.formData,
						this.ANkeyList
					);
					this.CNEndpointInputs = this.Util.pick(
						this.formData,
						this.CNkeyList
					);
				} else {
					this.transferFormItems.map((item, index) => {
						if (
							item.title === "AN Endpoint" ||
							item.title === "CN Endpoint"
						) {
							this.transferFormItems.splice(index, 1);
						}
					});
				}
			}
			// If the endpoint related parameters from the back end are incomplete, delete the endpoint item
			//-------> Comment: Above code
			if (this.title === "An") {
				this.AreaFormatting();
			}
		}
	}

	addCheckStatus() {
		if (this.connectionLinkTable && this.connectionLinkTable.length > 0) {
			this.connectionLinkTable.forEach((item) => {
				if (
					item.hasOwnProperty("linkId") &&
					typeof this.formData["sliceProfile_TN_connection_links"] !==
						"undefined" &&
					this.formData["sliceProfile_TN_connection_links"] !== "" &&
					this.formData["sliceProfile_TN_connection_links"] !==
						null &&
					item["linkId"] ===
						this.formData["sliceProfile_TN_connection_links"]
				) {
					item.checked = true;
				} else {
					item.checked = false;
				}
			});
		}
	}

	// changeResourceShare() {
	// 	this.judgeTn();
	// }

	isObject(val) {
		if (Object.prototype.toString.call(val) === "[object Object]") {
			return true;
		} else {
			return false;
		}
	}

	getConnectionLinkTable(pageNo, pageSize): void {
		const pageObj = {
			pageNo: pageNo,
			pageSize: pageSize,
		};
		this.loading = true;
		this.http
			.getConnectionLinkTable(pageObj, this.getConnetionFailed)
			.then((res) => {
				if (pageNo === 1) {
					// the first page
					this.loading = false;
					this.connectionLinkTable =
						res.result_body.connection_links_list;
					this.recordNum = res.result_body.record_number;
					// Use default value to occupy
					// let defaultItem: object = {};
					// if (this.connectionLinkTable.length !== 0) {
					// 	defaultItem = JSON.parse(
					// 		JSON.stringify(this.connectionLinkTable[0])
					// 	);
					// 	for (let key in defaultItem) {
					// 		if (!this.isObject(defaultItem[key])) {
					// 			defaultItem[key] = "";
					// 		}
					// 		// else {
					// 		// 	for (let i in defaultItem[key]) {
					// 		// 		defaultItem[key][i] = "";
					// 		// 	}
					// 		// }
					// 	}
					// 	for (let i = pageSize; i < this.recordNum; i++) {
					// 		this.connectionLinkTable.push(defaultItem);
					// 	}
					// }
					// this.hasPageNo = [1];
				} else if (pageNo > 1) {
					// delete the default page of the page and add the actual data of the page
					// const startIndex = pageSize * (pageNo - 1);
					// const endIndex = startIndex + pageSize - 1;
					// this.connectionLinkTable.splice(startIndex, pageSize);
					// const frontArr = this.connectionLinkTable.slice(
					// 	0,
					// 	startIndex
					// );
					// const backArr = this.connectionLinkTable.slice(startIndex);
					// this.connectionLinkTable = frontArr.concat(
					// 	res.result_body.connection_links_list,
					// 	backArr
					// );
					this.connectionLinkTable =
						res.result_body.connection_links_list;
					this.recordNum = res.result_body.record_number;
					this.loading = false;
				}
				// this.hasPageNo.push(pageNo);
				this.addCheckStatus(); // add init check status for connection link table
				this.judgeTn(); // init judge
				this.getTableHeader();
			});
	}

	getTableHeader(): void {
		// Find the common key of all data
		if (this.connectionLinkTable && this.connectionLinkTable.length > 0) {
			let keyList: any[] = this.connectionLinkTable.map((item) => {
				return Object.keys(item);
			});
			this.connectionTableHeader = this.Util.intersection(keyList).filter(
				(item) => {
					return item !== "checked";
				}
			);
			// Filter redundant items in table data
			this.connectionLinkTable.forEach((item) => {
				for (let key in item) {
					if (
						key !== "linkId" &&
						key !== "checked" &&
						this.connectionTableHeader.indexOf(key) === -1
					) {
						delete item[key];
					} else {
						// Filter out the null values in each item
						for (let i in item[key]) {
							if (
								i === "jitter" &&
								(item[key][i] === "" ||
									item[key][i] === "null" ||
									item[key][i] === null)
							) {
								delete item[key][i];
							}
						}
					}
				}
			});
		}
	}

	pageIndexChange(e) {
		// judge whether there is data of the page, if not, request it from the back end
		// if (this.hasPageNo.indexOf(e) === -1) {
		this.getConnectionLinkTable(e, this.pageSize);
		// }
	}
	getConnetionFailed() {
		console.log("failed");
	}

	judgeTn(): void {
		console.log("fprm", this.formData);
		if (
			this.formData["sliceProfile_TN_resourceSharingLevel"] ===
				"non-shared" ||
			this.formData["sliceProfile_TN_resourceSharingLevel"] == null
		) {
			//:todo clear??
			// this.connectionLinkTable.forEach((item) => {
			// 	item.checked = false;
			// });
			// this.formData["sliceProfile_TN_connection_links"] = null;
			this.notPassPara = ["sliceProfile_TN_connection_links"];
			this.transferFormItems.forEach((item) => {
				if (item.title === "Connection Links") {
					item.disable = true;
				} else if (
					item.title === "AN Endpoint" ||
					item.title === "CN Endpoint"
				) {
					item.required = true;
					item.disable = false;
				}
			});
		} else if (
			this.formData["sliceProfile_TN_resourceSharingLevel"] === "shared"
		) {
			this.transferFormItems.forEach((item) => {
				if (item.title === "Connection Links") {
					item.disable = false;
				} else if (
					item.title === "AN Endpoint" ||
					item.title === "CN Endpoint"
				) {
					if (
						typeof this.formData[
							"sliceProfile_TN_connection_links"
						] !== "undefined" &&
						this.formData["sliceProfile_TN_connection_links"] !==
							null &&
						this.formData["sliceProfile_TN_connection_links"] !== ""
					) {
						item.disable = true;
						item.required = false;
						this.notPassPara = [];
						this.notPassPara = this.notPassPara.concat(
							this.ANkeyList,
							this.CNkeyList
						);
					} else {
						//:todo
						this.formData["sliceProfile_TN_connection_links"] = "";
						item.disable = false;
						item.required = true;
						this.notPassPara = [];
					}
				}
			});
		}
	}

	validateEndPoint(key: string, value: any, required: boolean): string {
		if (required) {
			if (this.Util.isEmpty(value)) {
				return "can not be empty";
			}
		}
		if (key === "ip_address") {
			if (!this.regxpIP.test(value)) {
				if (value !== "") {
					return "xxx.xxx.xxx.xxx";
				} else {
					return "";
				}
			} else {
				return "";
			}
		} else if (key === "logical_link") {
			if (!this.Util.isInteger(value)) {
				if (value !== "") {
					return "integer only";
				} else {
					return "";
				}
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	changeLinkCheck(id: string): void {
		// update the selection state
		this.connectionLinkTable.forEach((item) => {
			if (item["linkId"] === id) {
				item.checked = true;
			} else {
				item.checked = false;
			}
		});
		this.formData["sliceProfile_TN_connection_links"] = id; //  get the selected id
		this.judgeTn();
	}

	AreaFormatting() {
		let areaList = [...this.formData.an_coverage_area_ta_list];
		this.areaList = areaList.map((item: any) => {
			let arr = item.split(";");
			item = arr.map((ite, index) => {
				let key: string;
				if (!index) {
					key = "province";
				} else if (index === 1) {
					key = "city";
				} else {
					key = "district";
				}
				const obj: any = {};
				obj.key = key;
				obj.selected = ite;
				obj.options = [{ name: ite, id: ite }];
				return obj;
			});
			return item;
		});
	}

	handleCancel(): void {
		this.noPassParaChange.emit(this.notPassPara);
		this.showModel = false;
		this.cancel.emit(this.showModel);
	}

	// special handling for address
	areaCheckBeforeSubmit(target: object): Boolean {
		for (const prop in target) {
			if (target.hasOwnProperty(prop)) {
				if (
					prop === "an_coverage_area_ta_list" ||
					prop === "cn_coverage_area_ta_list"
				) {
					// if the vlaue is "shanghai;shanghai;", the input is incomplete
					return target[prop].every((item) => {
						return this.Util.deepCheck(item.split(";"));
					});
				}
			}
		}
		return true;
	}

	endCheckBeforeSubmit(endpoint, required): Array<any> {
		// check params of Endpoint
		let result: Array<any> = [true, ""];
		let endPointList;
		endPointList = this.transferFormItems.find((item) => {
			return item.title === "AN Endpoint";
		}).options;
		let ipKey = "";
		let logicalKey = "";
		let nextKey = "";
		for (let item of endPointList) {
			if (item.title === "ip_address") {
				ipKey = item.key;
			} else if (item.title === "logical_link") {
				logicalKey = item.key;
			} else if (item.title === "nexthop_info") {
				nextKey = item.key;
			}
		}
		for (let prop in endpoint) {
			if (prop === ipKey) {
				if (required) {
					if (endpoint[prop] === "") {
						result = [false, "Endpoint can not be empty"];
					} else if (!this.regxpIP.test(endpoint[prop])) {
						result = [false, "Illegal IpAddress"];
					}
				} else if (
					!this.regxpIP.test(endpoint[prop]) &&
					endpoint[prop] !== ""
				) {
					result = [false, "Illegal IpAddress"];
				}
			} else if (prop === logicalKey) {
				if (required) {
					if (endpoint[prop] === "") {
						result = [false, "logical can not be empty"];
					} else if (!this.Util.isInteger(endpoint[prop])) {
						result = [false, "LogicalID can only be an integer"];
					}
				} else if (
					!this.Util.isInteger(endpoint[prop]) &&
					endpoint[prop] !== ""
				) {
					result = [false, "LogicalID can only be an integer"];
				}
			} else if (prop === nextKey) {
				if (required && endpoint[prop] === "") {
					result = [false, "Endpoint can not be empty"];
				}
			}
		}
		return result;
	}

	inputHolder(title: string): string {
		const titleArr = title.split(" ");
		if (titleArr.length > 1) {
			return titleArr.slice(0, 2).join("");
		} else {
			return title;
		}
	}

	labelStyle(required: boolean): object {
		let style;
		if (!required) {
			style = { "margin-left": "18px", "margin-right": "-18px" };
		} else {
			style = {};
		}
		return style;
	}

	handleOk(): void {
		// Verify that items of EndPoint is correct
		if (this.EndpointEnable) {
			let endCheckResult = [];
			if (this.title === "Tn") {
				const ANendCheckResult = this.endCheckBeforeSubmit(
					this.ANEndpointInputs,
					this.transferFormItems.find((item) => {
						return item.title === "AN Endpoint";
					}).required
				);
				const CNendCheckResult = this.endCheckBeforeSubmit(
					this.CNEndpointInputs,
					this.transferFormItems.find((item) => {
						return item.title === "CN Endpoint";
					}).required
				);
				if (ANendCheckResult[0] && CNendCheckResult[0]) {
					endCheckResult[0] = true;
				} else {
					if (ANendCheckResult[0] === false) {
						endCheckResult = ANendCheckResult;
					} else {
						endCheckResult = CNendCheckResult;
					}
				}
			}
			if (!endCheckResult[0]) {
				this.message.error(endCheckResult[1].toString());
				return;
			}
			// replace the params about endPoint
			for (let prop in this.formData) {
				if (
					this.title === "Tn" &&
					typeof this.ANEndpointInputs[prop] !== "undefined"
				) {
					this.formData[prop] = this.ANEndpointInputs[prop];
				} else if (
					this.title === "Tn" &&
					typeof this.CNEndpointInputs[prop] !== "undefined"
				) {
					this.formData[prop] = this.CNEndpointInputs[prop];
				}
			}
		}
		let params: object;
		if (this.title === "An") {
			const an_coverage_area_ta_list: string[] = [];
			this.areaList.forEach((item) => {
				let str: string = "";
				item.forEach((area) => {
					str += area.selected + ";";
				});
				an_coverage_area_ta_list.push(str.substring(0, str.length - 1));
			});
			params = { ...this.formData, an_coverage_area_ta_list };
		} else {
			params = { ...this.formData };
		}
		// Verify that each item exclude endpoint is not empty, include special handeling of area_list
		let checkParams: object = params;
		let requireKeyList: string[] = [];
		let targetFormItems: any[] = [];
		if (this.title === "An" || this.title === "Cn") {
			targetFormItems = this.coreFormItems;
		} else if ((this.title = "Tn")) {
			targetFormItems = this.transferFormItems;
		}
		for (let item of targetFormItems) {
			if (typeof item.required !== "undefined" && item.required) {
				if (
					typeof item.type !== "undefined" &&
					item.type !== "endpoint"
				) {
					requireKeyList.push(item.key);
				}
			}
		}
		checkParams = this.Util.pick(params, requireKeyList);
		if (
			this.Util.deepCheck(checkParams) &&
			this.areaCheckBeforeSubmit(params)
		) {
			this.paramsDataChange.emit(params);
			this.noPassParaChange.emit(this.notPassPara);
			this.handleCancel();
		} else {
			this.message.error("Please complete the form");
		}
	}
}