6354f91eb53da66a2ee73171073add0c3008937b
[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[] = ["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["tn_connection_links"] !== "undefined" &&
135                                 this.formData["tn_connection_links"] !== "" &&
136                                 this.formData["tn_connection_links"] !== null &&
137                                 item["linkId"] === this.formData["tn_connection_links"]
138                         ) {
139                                 item.checked = true;
140                         } else {
141                                 item.checked = false;
142                         }
143                 });
144         }
145
146         // changeResourceShare() {
147         //      this.judgeTn();
148         // }
149
150         isObject(val) {
151                 if (Object.prototype.toString.call(val) === "[object Object]") {
152                         return true;
153                 } else {
154                         return false;
155                 }
156         }
157
158         getConnectionLinkTable(pageNo, pageSize): void {
159                 const pageObj = {
160                         pageNo: pageNo,
161                         pageSize: pageSize,
162                 };
163                 this.http
164                         .getConnectionLinkTable(pageObj, this.getConnetionFailed)
165                         .then((res) => {
166                                 if (pageNo === 1) {
167                                         // the first page
168                                         this.connectionLinkTable =
169                                                 res.result_body.connection_links_list;
170                                         this.recordNum = res.result_body.record_number;
171                                         // Use default value to occupy
172                                         // let defaultItem: object = {};
173                                         // if (this.connectionLinkTable.length !== 0) {
174                                         //      defaultItem = JSON.parse(
175                                         //              JSON.stringify(this.connectionLinkTable[0])
176                                         //      );
177                                         //      for (let key in defaultItem) {
178                                         //              if (!this.isObject(defaultItem[key])) {
179                                         //                      defaultItem[key] = "";
180                                         //              }
181                                         //              // else {
182                                         //              //      for (let i in defaultItem[key]) {
183                                         //              //              defaultItem[key][i] = "";
184                                         //              //      }
185                                         //              // }
186                                         //      }
187                                         //      for (let i = pageSize; i < this.recordNum; i++) {
188                                         //              this.connectionLinkTable.push(defaultItem);
189                                         //      }
190                                         // }
191                                         this.hasPageNo = [1];
192                                 } else if (pageNo > 1) {
193                                         // delete the default page of the page and add the actual data of the page
194                                         // const startIndex = pageSize * (pageNo - 1);
195                                         // const endIndex = startIndex + pageSize - 1;
196                                         // this.connectionLinkTable.splice(startIndex, pageSize);
197                                         // const frontArr = this.connectionLinkTable.slice(
198                                         //      0,
199                                         //      startIndex
200                                         // );
201                                         // const backArr = this.connectionLinkTable.slice(startIndex);
202                                         // this.connectionLinkTable = frontArr.concat(
203                                         //      res.result_body.connection_links_list,
204                                         //      backArr
205                                         // );
206                                         this.connectionLinkTable =
207                                                 res.result_body.connection_links_list;
208                                         this.recordNum = res.result_body.record_number;
209                                 }
210                                 this.hasPageNo.push(pageNo);
211                                 this.addCheckStatus(); // add init check status for connection link table
212                                 this.judgeTn(); // init judge
213                                 this.getTableHeader();
214                         });
215         }
216
217         getTableHeader(): void {
218                 // Find the common key of all data
219                 let keyList: any[] = this.connectionLinkTable.map((item) => {
220                         return Object.keys(item);
221                 });
222                 this.connectionTableHeader = this.Util.intersection(keyList).filter(
223                         (item) => {
224                                 return item !== "checked";
225                         }
226                 );
227                 // Filter redundant items in table data
228                 this.connectionLinkTable.forEach((item) => {
229                         for (let key in item) {
230                                 if (
231                                         key !== "linkId" &&
232                                         key !== "checked" &&
233                                         this.connectionTableHeader.indexOf(key) === -1
234                                 ) {
235                                         delete item[key];
236                                 } else {
237                                         // Filter out the null values in each item
238                                         for (let i in item[key]) {
239                                                 if (
240                                                         i === "jitter" &&
241                                                         (item[key][i] === "" ||
242                                                                 item[key][i] === "null" ||
243                                                                 item[key][i] === null)
244                                                 ) {
245                                                         delete item[key][i];
246                                                 }
247                                         }
248                                 }
249                         }
250                 });
251         }
252
253         pageIndexChange(e) {
254                 // judge whether there is data of the page, if not, request it from the back end
255                 // if (this.hasPageNo.indexOf(e) === -1) {
256                 this.getConnectionLinkTable(e, this.pageSize);
257                 // }
258         }
259         getConnetionFailed() {
260                 console.log("failed");
261         }
262
263         judgeTn(): void {
264                 if (
265                         this.formData["sliceProfile_TN_resourceSharingLevel"] ===
266                         "non-shared"
267                 ) {
268                         //:todo clear??
269                         // this.connectionLinkTable.forEach((item) => {
270                         //      item.checked = false;
271                         // });
272                         // this.formData["tn_connection_links"] = null;
273                         this.notPassPara = ["tn_connection_links"];
274                         this.transferFormItems.forEach((item) => {
275                                 if (item.title === "Connection Links") {
276                                         item.disable = true;
277                                 } else if (
278                                         item.title === "AN Endpoint" ||
279                                         item.title === "CN Endpoint"
280                                 ) {
281                                         item.required = true;
282                                         item.disable = false;
283                                 }
284                         });
285                 } else if (
286                         this.formData["sliceProfile_TN_resourceSharingLevel"] === "shared"
287                 ) {
288                         this.transferFormItems.forEach((item) => {
289                                 if (item.title === "Connection Links") {
290                                         item.disable = false;
291                                 } else if (
292                                         item.title === "AN Endpoint" ||
293                                         item.title === "CN Endpoint"
294                                 ) {
295                                         if (
296                                                 typeof this.formData["tn_connection_links"] !==
297                                                         "undefined" &&
298                                                 this.formData["tn_connection_links"] !== null &&
299                                                 this.formData["tn_connection_links"] !== ""
300                                         ) {
301                                                 item.disable = true;
302                                                 item.required = false;
303                                                 this.notPassPara = [];
304                                                 this.notPassPara = this.notPassPara.concat(
305                                                         this.ANkeyList,
306                                                         this.CNkeyList
307                                                 );
308                                         } else {
309                                                 //:todo
310                                                 this.formData["tn_connection_links"] = "";
311                                                 item.disable = false;
312                                                 item.required = true;
313                                                 this.notPassPara = [];
314                                         }
315                                 }
316                         });
317                 }
318         }
319
320         validateEndPoint(key: string, value: any, required: boolean): string {
321                 if (required) {
322                         if (this.Util.isEmpty(value)) {
323                                 return "can not be empty";
324                         }
325                 }
326                 if (key === "ip_address") {
327                         if (!this.regxpIP.test(value)) {
328                                 if (value !== "") {
329                                         return "xxx.xxx.xxx.xxx";
330                                 } else {
331                                         return "";
332                                 }
333                         } else {
334                                 return "";
335                         }
336                 } else if (key === "logical_link") {
337                         if (!this.Util.isInteger(value)) {
338                                 if (value !== "") {
339                                         return "integer only";
340                                 } else {
341                                         return "";
342                                 }
343                         } else {
344                                 return "";
345                         }
346                 } else {
347                         return "";
348                 }
349         }
350
351         changeLinkCheck(id: string): void {
352                 // update the selection state
353                 this.connectionLinkTable.forEach((item) => {
354                         if (item["linkId"] === id) {
355                                 item.checked = true;
356                         } else {
357                                 item.checked = false;
358                         }
359                 });
360                 this.formData["tn_connection_links"] = id; //  get the selected id
361                 this.judgeTn();
362         }
363
364         AreaFormatting() {
365                 let areaList = [...this.formData.an_coverage_area_ta_list];
366                 this.areaList = areaList.map((item: any) => {
367                         let arr = item.split(";");
368                         item = arr.map((ite, index) => {
369                                 let key: string;
370                                 if (!index) {
371                                         key = "province";
372                                 } else if (index === 1) {
373                                         key = "city";
374                                 } else {
375                                         key = "district";
376                                 }
377                                 const obj: any = {};
378                                 obj.key = key;
379                                 obj.selected = ite;
380                                 obj.options = [{ name: ite, id: ite }];
381                                 return obj;
382                         });
383                         return item;
384                 });
385         }
386
387         handleCancel(): void {
388                 this.noPassParaChange.emit(this.notPassPara);
389                 this.showModel = false;
390                 this.cancel.emit(this.showModel);
391         }
392
393         // special handling for address
394         areaCheckBeforeSubmit(target: object): Boolean {
395                 for (const prop in target) {
396                         if (target.hasOwnProperty(prop)) {
397                                 if (
398                                         prop === "an_coverage_area_ta_list" ||
399                                         prop === "cn_coverage_area_ta_list"
400                                 ) {
401                                         // if the vlaue is "shanghai;shanghai;", the input is incomplete
402                                         return target[prop].every((item) => {
403                                                 return this.Util.deepCheck(item.split(";"));
404                                         });
405                                 }
406                         }
407                 }
408                 return true;
409         }
410
411         endCheckBeforeSubmit(endpoint, required): Array<any> {
412                 // check params of Endpoint
413                 let result: Array<any> = [true, ""];
414                 let endPointList;
415                 endPointList = this.transferFormItems.find((item) => {
416                         return item.title === "AN Endpoint";
417                 }).options;
418                 let ipKey = "";
419                 let logicalKey = "";
420                 let nextKey = "";
421                 for (let item of endPointList) {
422                         if (item.title === "ip_address") {
423                                 ipKey = item.key;
424                         } else if (item.title === "logical_link") {
425                                 logicalKey = item.key;
426                         } else if (item.title === "nexthop_info") {
427                                 nextKey = item.key;
428                         }
429                 }
430                 for (let prop in endpoint) {
431                         if (prop === ipKey) {
432                                 if (required) {
433                                         if (endpoint[prop] === "") {
434                                                 result = [false, "Endpoint can not be empty"];
435                                         } else if (!this.regxpIP.test(endpoint[prop])) {
436                                                 result = [false, "Illegal IpAddress"];
437                                         }
438                                 } else if (
439                                         !this.regxpIP.test(endpoint[prop]) &&
440                                         endpoint[prop] !== ""
441                                 ) {
442                                         result = [false, "Illegal IpAddress"];
443                                 }
444                         } else if (prop === logicalKey) {
445                                 if (required) {
446                                         if (endpoint[prop] === "") {
447                                                 result = [false, "logical can not be empty"];
448                                         } else if (!this.Util.isInteger(endpoint[prop])) {
449                                                 result = [false, "LogicalID can only be an integer"];
450                                         }
451                                 } else if (
452                                         !this.Util.isInteger(endpoint[prop]) &&
453                                         endpoint[prop] !== ""
454                                 ) {
455                                         result = [false, "LogicalID can only be an integer"];
456                                 }
457                         } else if (prop === nextKey) {
458                                 if (required && endpoint[prop] === "") {
459                                         result = [false, "Endpoint can not be empty"];
460                                 }
461                         }
462                 }
463                 return result;
464         }
465
466         inputHolder(title: string): string {
467                 const titleArr = title.split(" ");
468                 if (titleArr.length > 1) {
469                         return titleArr.slice(0, 2).join("");
470                 } else {
471                         return title;
472                 }
473         }
474
475         labelStyle(required: boolean): object {
476                 let style;
477                 if (!required) {
478                         style = { "margin-left": "18px", "margin-right": "-18px" };
479                 } else {
480                         style = {};
481                 }
482                 return style;
483         }
484
485         handleOk(): void {
486                 // Verify that items of EndPoint is correct
487                 if (this.EndpointEnable) {
488                         let endCheckResult = [];
489                         if (this.title === "Tn") {
490                                 const ANendCheckResult = this.endCheckBeforeSubmit(
491                                         this.ANEndpointInputs,
492                                         this.transferFormItems.find((item) => {
493                                                 return item.title === "AN Endpoint";
494                                         }).required
495                                 );
496                                 const CNendCheckResult = this.endCheckBeforeSubmit(
497                                         this.CNEndpointInputs,
498                                         this.transferFormItems.find((item) => {
499                                                 return item.title === "CN Endpoint";
500                                         }).required
501                                 );
502                                 if (ANendCheckResult[0] && CNendCheckResult[0]) {
503                                         endCheckResult[0] = true;
504                                 } else {
505                                         if (ANendCheckResult[0] === false) {
506                                                 endCheckResult = ANendCheckResult;
507                                         } else {
508                                                 endCheckResult = CNendCheckResult;
509                                         }
510                                 }
511                         }
512                         if (!endCheckResult[0]) {
513                                 this.message.error(endCheckResult[1].toString());
514                                 return;
515                         }
516                         // replace the params about endPoint
517                         for (let prop in this.formData) {
518                                 if (
519                                         this.title === "Tn" &&
520                                         typeof this.ANEndpointInputs[prop] !== "undefined"
521                                 ) {
522                                         this.formData[prop] = this.ANEndpointInputs[prop];
523                                 } else if (
524                                         this.title === "Tn" &&
525                                         typeof this.CNEndpointInputs[prop] !== "undefined"
526                                 ) {
527                                         this.formData[prop] = this.CNEndpointInputs[prop];
528                                 }
529                         }
530                 }
531                 let params: object;
532                 if (this.title === "An") {
533                         const an_coverage_area_ta_list: string[] = [];
534                         this.areaList.forEach((item) => {
535                                 let str: string = "";
536                                 item.forEach((area) => {
537                                         str += area.selected + ";";
538                                 });
539                                 an_coverage_area_ta_list.push(str.substring(0, str.length - 1));
540                         });
541                         params = { ...this.formData, an_coverage_area_ta_list };
542                 } else {
543                         params = { ...this.formData };
544                 }
545                 // Verify that each item exclude endpoint is not empty, include special handeling of area_list
546                 let checkParams: object = params;
547                 let requireKeyList: string[] = [];
548                 let targetFormItems: any[] = [];
549                 if (this.title === "An" || this.title === "Cn") {
550                         targetFormItems = this.coreFormItems;
551                 } else if ((this.title = "Tn")) {
552                         targetFormItems = this.transferFormItems;
553                 }
554                 for (let item of targetFormItems) {
555                         if (typeof item.required !== "undefined" && item.required) {
556                                 if (
557                                         typeof item.type !== "undefined" &&
558                                         item.type !== "endpoint"
559                                 ) {
560                                         requireKeyList.push(item.key);
561                                 }
562                         }
563                 }
564                 checkParams = this.Util.pick(params, requireKeyList);
565                 if (
566                         this.Util.deepCheck(checkParams) &&
567                         this.areaCheckBeforeSubmit(params)
568                 ) {
569                         this.paramsDataChange.emit(params);
570                         this.noPassParaChange.emit(this.notPassPara);
571                         this.handleCancel();
572                 } else {
573                         this.message.error("Please complete the form");
574                 }
575         }
576 }