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