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