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