83d16a62996398f6b9289214668d82c8afccbd2d
[usecase-ui.git] /
1 import {
2         Component,
3         OnInit,
4         Input,
5         Output,
6         EventEmitter,
7         ElementRef,
8 } from "@angular/core";
9 import { TRANSFRER_FORM_ITEMS, CORE_FORM_ITEMS } from "./constants";
10 import { NzMessageService } from "ng-zorro-antd";
11 import { stringify } from "@angular/core/src/util";
12 import { Util } from "../../../../../../shared/utils/utils";
13 import { SlicingTaskServices } from "@src/app/core/services/slicingTaskServices";
14
15 @Component({
16         selector: "app-subnet-params-model",
17         templateUrl: "./subnet-params-model.component.html",
18         styleUrls: ["./subnet-params-model.component.less"],
19 })
20 export class SubnetParamsModelComponent implements OnInit {
21         @Input() showModel: boolean;
22         @Input() detailData: any;
23         @Input() title: string;
24         @Output() cancel = new EventEmitter<boolean>();
25         @Output() paramsDataChange = new EventEmitter<any>();
26         @Output() noPassParaChange = new EventEmitter<any>();
27
28         transferFormItems: any[] = TRANSFRER_FORM_ITEMS;
29         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
30         formData: any;
31         coreFormItems: any = [];
32         areaList: any[] = [];
33         areaLevel: number = 3;
34         ANEndpointInputs: object = {};
35         CNEndpointInputs: object = {};
36         ANkeyList: string[] = [];
37         CNkeyList: string[] = [];
38         EndpointEnable: boolean = false; // Whether to enable the three parameters of Endpoint
39         keyList: string[] = []; // keys of endPoint
40         specialParaTN: string[] = [
41                 "Resource Sharing Level",
42                 "Connection Links",
43                 "AN Endpoint",
44                 "CN Endpoint",
45         ];
46         // Parameters not passed to the back end
47         notPassPara: string[] = ["sliceProfile_TN_connection_links"];
48         connectionLinkTable: any[] = [];
49         connectionTableHeader: string[] = [];
50         pageSize: number = 2;
51         recordNum: number = 0;
52         hasPageNo: number[] = [];
53         objectKeys = Object.keys;
54         //  Comment: Above code
55
56         constructor(
57                 private message: NzMessageService,
58                 private Util: Util,
59                 private http: SlicingTaskServices
60         ) {}
61
62         ngOnInit() {}
63
64         ngOnChanges() {
65                 // It is excuted every time you open it
66                 if (this.title) {
67                         this.formData = JSON.parse(JSON.stringify(this.detailData));
68                         if (this.title === "An" || this.title === "Cn") {
69                                 this.coreFormItems =
70                                         this.title === "An"
71                                                 ? CORE_FORM_ITEMS.An
72                                                 : this.title === "Cn"
73                                                 ? CORE_FORM_ITEMS.Cn
74                                                 : [];
75                         } else if (this.title === "Tn") {
76                                 const pageNo = 1; // start from page 1
77                                 const pageSize = this.pageSize;
78                                 this.getConnectionLinkTable(pageNo, pageSize);
79                                 this.ANkeyList = this.transferFormItems
80                                         .find((item) => {
81                                                 return item.title === "AN Endpoint";
82                                         })
83                                         .options.map((val) => {
84                                                 return val.key;
85                                         });
86                                 this.CNkeyList = this.transferFormItems
87                                         .find((item) => {
88                                                 return item.title === "CN Endpoint";
89                                         })
90                                         .options.map((val) => {
91                                                 return val.key;
92                                         });
93                                 this.keyList = this.ANkeyList.concat(this.CNkeyList);
94                                 if (
95                                         typeof this.formData !== "undefined" &&
96                                         Object.keys(this.formData).length !== 0
97                                 ) {
98                                         this.EndpointEnable = this.keyList.every((item) => {
99                                                 return this.formData.hasOwnProperty(item);
100                                         });
101                                 }
102                                 if (this.EndpointEnable) {
103                                         this.ANEndpointInputs = this.Util.pick(
104                                                 this.formData,
105                                                 this.ANkeyList
106                                         );
107                                         this.CNEndpointInputs = this.Util.pick(
108                                                 this.formData,
109                                                 this.CNkeyList
110                                         );
111                                 } else {
112                                         this.transferFormItems.map((item, index) => {
113                                                 if (
114                                                         item.title === "AN Endpoint" ||
115                                                         item.title === "CN Endpoint"
116                                                 ) {
117                                                         this.transferFormItems.splice(index, 1);
118                                                 }
119                                         });
120                                 }
121                         }
122                         // If the endpoint related parameters from the back end are incomplete, delete the endpoint item
123                         //-------> Comment: Above code
124                         if (this.title === "An") {
125                                 this.AreaFormatting();
126                         }
127                 }
128         }
129
130         addCheckStatus() {
131                 this.connectionLinkTable.forEach((item) => {
132                         if (
133                                 item.hasOwnProperty("linkId") &&
134                                 typeof this.formData["sliceProfile_TN_connection_links"] !==
135                                         "undefined" &&
136                                 this.formData["sliceProfile_TN_connection_links"] !== "" &&
137                                 this.formData["sliceProfile_TN_connection_links"] !== null &&
138                                 item["linkId"] ===
139                                         this.formData["sliceProfile_TN_connection_links"]
140                         ) {
141                                 item.checked = true;
142                         } else {
143                                 item.checked = false;
144                         }
145                 });
146         }
147
148         // changeResourceShare() {
149         //      this.judgeTn();
150         // }
151
152         isObject(val) {
153                 if (Object.prototype.toString.call(val) === "[object Object]") {
154                         return true;
155                 } else {
156                         return false;
157                 }
158         }
159
160         getConnectionLinkTable(pageNo, pageSize): void {
161                 const pageObj = {
162                         pageNo: pageNo,
163                         pageSize: pageSize,
164                 };
165                 this.http
166                         .getConnectionLinkTable(pageObj, this.getConnetionFailed)
167                         .then((res) => {
168                                 if (pageNo === 1) {
169                                         // the first page
170                                         this.connectionLinkTable =
171                                                 res.result_body.connection_links_list;
172                                         this.recordNum = res.result_body.record_number;
173                                         // Use default value to occupy
174                                         // let defaultItem: object = {};
175                                         // if (this.connectionLinkTable.length !== 0) {
176                                         //      defaultItem = JSON.parse(
177                                         //              JSON.stringify(this.connectionLinkTable[0])
178                                         //      );
179                                         //      for (let key in defaultItem) {
180                                         //              if (!this.isObject(defaultItem[key])) {
181                                         //                      defaultItem[key] = "";
182                                         //              }
183                                         //              // else {
184                                         //              //      for (let i in defaultItem[key]) {
185                                         //              //              defaultItem[key][i] = "";
186                                         //              //      }
187                                         //              // }
188                                         //      }
189                                         //      for (let i = pageSize; i < this.recordNum; i++) {
190                                         //              this.connectionLinkTable.push(defaultItem);
191                                         //      }
192                                         // }
193                                         this.hasPageNo = [1];
194                                 } else if (pageNo > 1) {
195                                         // delete the default page of the page and add the actual data of the page
196                                         // const startIndex = pageSize * (pageNo - 1);
197                                         // const endIndex = startIndex + pageSize - 1;
198                                         // this.connectionLinkTable.splice(startIndex, pageSize);
199                                         // const frontArr = this.connectionLinkTable.slice(
200                                         //      0,
201                                         //      startIndex
202                                         // );
203                                         // const backArr = this.connectionLinkTable.slice(startIndex);
204                                         // this.connectionLinkTable = frontArr.concat(
205                                         //      res.result_body.connection_links_list,
206                                         //      backArr
207                                         // );
208                                         this.connectionLinkTable =
209                                                 res.result_body.connection_links_list;
210                                         this.recordNum = res.result_body.record_number;
211                                 }
212                                 this.hasPageNo.push(pageNo);
213                                 this.addCheckStatus(); // add init check status for connection link table
214                                 this.judgeTn(); // init judge
215                                 this.getTableHeader();
216                         });
217         }
218
219         getTableHeader(): void {
220                 // Find the common key of all data
221                 let keyList: any[] = this.connectionLinkTable.map((item) => {
222                         return Object.keys(item);
223                 });
224                 this.connectionTableHeader = this.Util.intersection(keyList).filter(
225                         (item) => {
226                                 return item !== "checked";
227                         }
228                 );
229                 // Filter redundant items in table data
230                 this.connectionLinkTable.forEach((item) => {
231                         for (let key in item) {
232                                 if (
233                                         key !== "linkId" &&
234                                         key !== "checked" &&
235                                         this.connectionTableHeader.indexOf(key) === -1
236                                 ) {
237                                         delete item[key];
238                                 } else {
239                                         // Filter out the null values in each item
240                                         for (let i in item[key]) {
241                                                 if (
242                                                         i === "jitter" &&
243                                                         (item[key][i] === "" ||
244                                                                 item[key][i] === "null" ||
245                                                                 item[key][i] === null)
246                                                 ) {
247                                                         delete item[key][i];
248                                                 }
249                                         }
250                                 }
251                         }
252                 });
253         }
254
255         pageIndexChange(e) {
256                 // judge whether there is data of the page, if not, request it from the back end
257                 // if (this.hasPageNo.indexOf(e) === -1) {
258                 this.getConnectionLinkTable(e, this.pageSize);
259                 // }
260         }
261         getConnetionFailed() {
262                 console.log("failed");
263         }
264
265         judgeTn(): void {
266                 if (
267                         this.formData["sliceProfile_TN_resourceSharingLevel"] ===
268                         "non-shared"
269                 ) {
270                         //:todo clear??
271                         // this.connectionLinkTable.forEach((item) => {
272                         //      item.checked = false;
273                         // });
274                         // this.formData["sliceProfile_TN_connection_links"] = null;
275                         this.notPassPara = ["sliceProfile_TN_connection_links"];
276                         this.transferFormItems.forEach((item) => {
277                                 if (item.title === "Connection Links") {
278                                         item.disable = true;
279                                 } else if (
280                                         item.title === "AN Endpoint" ||
281                                         item.title === "CN Endpoint"
282                                 ) {
283                                         item.required = true;
284                                         item.disable = false;
285                                 }
286                         });
287                 } else if (
288                         this.formData["sliceProfile_TN_resourceSharingLevel"] === "shared"
289                 ) {
290                         this.transferFormItems.forEach((item) => {
291                                 if (item.title === "Connection Links") {
292                                         item.disable = false;
293                                 } else if (
294                                         item.title === "AN Endpoint" ||
295                                         item.title === "CN Endpoint"
296                                 ) {
297                                         if (
298                                                 typeof this.formData[
299                                                         "sliceProfile_TN_connection_links"
300                                                 ] !== "undefined" &&
301                                                 this.formData["sliceProfile_TN_connection_links"] !==
302                                                         null &&
303                                                 this.formData["sliceProfile_TN_connection_links"] !== ""
304                                         ) {
305                                                 item.disable = true;
306                                                 item.required = false;
307                                                 this.notPassPara = [];
308                                                 this.notPassPara = this.notPassPara.concat(
309                                                         this.ANkeyList,
310                                                         this.CNkeyList
311                                                 );
312                                         } else {
313                                                 //:todo
314                                                 this.formData["sliceProfile_TN_connection_links"] = "";
315                                                 item.disable = false;
316                                                 item.required = true;
317                                                 this.notPassPara = [];
318                                         }
319                                 }
320                         });
321                 }
322         }
323
324         validateEndPoint(key: string, value: any, required: boolean): string {
325                 if (required) {
326                         if (this.Util.isEmpty(value)) {
327                                 return "can not be empty";
328                         }
329                 }
330                 if (key === "ip_address") {
331                         if (!this.regxpIP.test(value)) {
332                                 if (value !== "") {
333                                         return "xxx.xxx.xxx.xxx";
334                                 } else {
335                                         return "";
336                                 }
337                         } else {
338                                 return "";
339                         }
340                 } else if (key === "logical_link") {
341                         if (!this.Util.isInteger(value)) {
342                                 if (value !== "") {
343                                         return "integer only";
344                                 } else {
345                                         return "";
346                                 }
347                         } else {
348                                 return "";
349                         }
350                 } else {
351                         return "";
352                 }
353         }
354
355         changeLinkCheck(id: string): void {
356                 // update the selection state
357                 this.connectionLinkTable.forEach((item) => {
358                         if (item["linkId"] === id) {
359                                 item.checked = true;
360                         } else {
361                                 item.checked = false;
362                         }
363                 });
364                 this.formData["sliceProfile_TN_connection_links"] = id; //  get the selected id
365                 this.judgeTn();
366         }
367
368         AreaFormatting() {
369                 let areaList = [...this.formData.an_coverage_area_ta_list];
370                 this.areaList = areaList.map((item: any) => {
371                         let arr = item.split(";");
372                         item = arr.map((ite, index) => {
373                                 let key: string;
374                                 if (!index) {
375                                         key = "province";
376                                 } else if (index === 1) {
377                                         key = "city";
378                                 } else {
379                                         key = "district";
380                                 }
381                                 const obj: any = {};
382                                 obj.key = key;
383                                 obj.selected = ite;
384                                 obj.options = [{ name: ite, id: ite }];
385                                 return obj;
386                         });
387                         return item;
388                 });
389         }
390
391         handleCancel(): void {
392                 this.noPassParaChange.emit(this.notPassPara);
393                 this.showModel = false;
394                 this.cancel.emit(this.showModel);
395         }
396
397         // special handling for address
398         areaCheckBeforeSubmit(target: object): Boolean {
399                 for (const prop in target) {
400                         if (target.hasOwnProperty(prop)) {
401                                 if (
402                                         prop === "an_coverage_area_ta_list" ||
403                                         prop === "cn_coverage_area_ta_list"
404                                 ) {
405                                         // if the vlaue is "shanghai;shanghai;", the input is incomplete
406                                         return target[prop].every((item) => {
407                                                 return this.Util.deepCheck(item.split(";"));
408                                         });
409                                 }
410                         }
411                 }
412                 return true;
413         }
414
415         endCheckBeforeSubmit(endpoint, required): Array<any> {
416                 // check params of Endpoint
417                 let result: Array<any> = [true, ""];
418                 let endPointList;
419                 endPointList = this.transferFormItems.find((item) => {
420                         return item.title === "AN Endpoint";
421                 }).options;
422                 let ipKey = "";
423                 let logicalKey = "";
424                 let nextKey = "";
425                 for (let item of endPointList) {
426                         if (item.title === "ip_address") {
427                                 ipKey = item.key;
428                         } else if (item.title === "logical_link") {
429                                 logicalKey = item.key;
430                         } else if (item.title === "nexthop_info") {
431                                 nextKey = item.key;
432                         }
433                 }
434                 for (let prop in endpoint) {
435                         if (prop === ipKey) {
436                                 if (required) {
437                                         if (endpoint[prop] === "") {
438                                                 result = [false, "Endpoint can not be empty"];
439                                         } else if (!this.regxpIP.test(endpoint[prop])) {
440                                                 result = [false, "Illegal IpAddress"];
441                                         }
442                                 } else if (
443                                         !this.regxpIP.test(endpoint[prop]) &&
444                                         endpoint[prop] !== ""
445                                 ) {
446                                         result = [false, "Illegal IpAddress"];
447                                 }
448                         } else if (prop === logicalKey) {
449                                 if (required) {
450                                         if (endpoint[prop] === "") {
451                                                 result = [false, "logical can not be empty"];
452                                         } else if (!this.Util.isInteger(endpoint[prop])) {
453                                                 result = [false, "LogicalID can only be an integer"];
454                                         }
455                                 } else if (
456                                         !this.Util.isInteger(endpoint[prop]) &&
457                                         endpoint[prop] !== ""
458                                 ) {
459                                         result = [false, "LogicalID can only be an integer"];
460                                 }
461                         } else if (prop === nextKey) {
462                                 if (required && endpoint[prop] === "") {
463                                         result = [false, "Endpoint can not be empty"];
464                                 }
465                         }
466                 }
467                 return result;
468         }
469
470         inputHolder(title: string): string {
471                 const titleArr = title.split(" ");
472                 if (titleArr.length > 1) {
473                         return titleArr.slice(0, 2).join("");
474                 } else {
475                         return title;
476                 }
477         }
478
479         labelStyle(required: boolean): object {
480                 let style;
481                 if (!required) {
482                         style = { "margin-left": "18px", "margin-right": "-18px" };
483                 } else {
484                         style = {};
485                 }
486                 return style;
487         }
488
489         handleOk(): void {
490                 // Verify that items of EndPoint is correct
491                 if (this.EndpointEnable) {
492                         let endCheckResult = [];
493                         if (this.title === "Tn") {
494                                 const ANendCheckResult = this.endCheckBeforeSubmit(
495                                         this.ANEndpointInputs,
496                                         this.transferFormItems.find((item) => {
497                                                 return item.title === "AN Endpoint";
498                                         }).required
499                                 );
500                                 const CNendCheckResult = this.endCheckBeforeSubmit(
501                                         this.CNEndpointInputs,
502                                         this.transferFormItems.find((item) => {
503                                                 return item.title === "CN Endpoint";
504                                         }).required
505                                 );
506                                 if (ANendCheckResult[0] && CNendCheckResult[0]) {
507                                         endCheckResult[0] = true;
508                                 } else {
509                                         if (ANendCheckResult[0] === false) {
510                                                 endCheckResult = ANendCheckResult;
511                                         } else {
512                                                 endCheckResult = CNendCheckResult;
513                                         }
514                                 }
515                         }
516                         if (!endCheckResult[0]) {
517                                 this.message.error(endCheckResult[1].toString());
518                                 return;
519                         }
520                         // replace the params about endPoint
521                         for (let prop in this.formData) {
522                                 if (
523                                         this.title === "Tn" &&
524                                         typeof this.ANEndpointInputs[prop] !== "undefined"
525                                 ) {
526                                         this.formData[prop] = this.ANEndpointInputs[prop];
527                                 } else if (
528                                         this.title === "Tn" &&
529                                         typeof this.CNEndpointInputs[prop] !== "undefined"
530                                 ) {
531                                         this.formData[prop] = this.CNEndpointInputs[prop];
532                                 }
533                         }
534                 }
535                 let params: object;
536                 if (this.title === "An") {
537                         const an_coverage_area_ta_list: string[] = [];
538                         this.areaList.forEach((item) => {
539                                 let str: string = "";
540                                 item.forEach((area) => {
541                                         str += area.selected + ";";
542                                 });
543                                 an_coverage_area_ta_list.push(str.substring(0, str.length - 1));
544                         });
545                         params = { ...this.formData, an_coverage_area_ta_list };
546                 } else {
547                         params = { ...this.formData };
548                 }
549                 // Verify that each item exclude endpoint is not empty, include special handeling of area_list
550                 let checkParams: object = params;
551                 let requireKeyList: string[] = [];
552                 let targetFormItems: any[] = [];
553                 if (this.title === "An" || this.title === "Cn") {
554                         targetFormItems = this.coreFormItems;
555                 } else if ((this.title = "Tn")) {
556                         targetFormItems = this.transferFormItems;
557                 }
558                 for (let item of targetFormItems) {
559                         if (typeof item.required !== "undefined" && item.required) {
560                                 if (
561                                         typeof item.type !== "undefined" &&
562                                         item.type !== "endpoint"
563                                 ) {
564                                         requireKeyList.push(item.key);
565                                 }
566                         }
567                 }
568                 checkParams = this.Util.pick(params, requireKeyList);
569                 if (
570                         this.Util.deepCheck(checkParams) &&
571                         this.areaCheckBeforeSubmit(params)
572                 ) {
573                         this.paramsDataChange.emit(params);
574                         this.noPassParaChange.emit(this.notPassPara);
575                         this.handleCancel();
576                 } else {
577                         this.message.error("Please complete the form");
578                 }
579         }
580 }