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