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