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