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