1 import { Component, OnInit, Input, Output, EventEmitter, ElementRef } from '@angular/core';
2 import { TRANSFRER_FORM_ITEMS, CORE_FORM_ITEMS, ADDRESS , NexthopInfo_Options } from '@src/constants/constants';
3 import { NzMessageService } from "ng-zorro-antd";
4 import { stringify } from '@angular/core/src/util';
5 import { Util } from '../../../../../../shared/utils/utils';
6 import { SlicingTaskServices } from '@src/app/core/services/slicingTaskServices';
10 selector: 'app-subnet-params-model',
11 templateUrl: './subnet-params-model.component.html',
12 styleUrls: ['./subnet-params-model.component.less']
14 export class SubnetParamsModelComponent implements OnInit {
16 @Input() showModel: boolean;
17 @Input() detailData: any;
18 @Input() title: string;
19 @Output() cancel = new EventEmitter<boolean>();
20 @Output() paramsDataChange = new EventEmitter<any>();
21 @Output() noPassParaChange = new EventEmitter<any>();
23 transferFormItems: any[] = TRANSFRER_FORM_ITEMS;
24 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
26 coreFormItems : any = [];
28 // 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
29 NexthopInfoOptions = NexthopInfo_Options;
30 ANEndpointInputs: object = {};
31 CNEndpointInputs: object = {};
32 ANkeyList: string[] = [];
33 CNkeyList: string[] = [];
34 EndpointEnable: boolean = false; // Whether to enable the three parameters of Endpoint
35 keyList: string[] = []; // keys of endPoint
36 specialParaTN: string[] = ['Resource Sharing Level', 'Connection Links', 'AN Endpoint', 'CN Endpoint'];
37 // Parameters not passed to the back end
38 notPassPara: string[] = ['tn_connection_links'];
39 connectionLinkTable: any[] = [];
40 connectionTableHeader: string[] = [];
41 objectKeys = Object.keys;
42 // Comment: Above code
45 private message: NzMessageService,
47 private http: SlicingTaskServices
56 this.formData = JSON.parse(JSON.stringify(this.detailData));
57 if (this.title === 'An' || this.title === 'Cn') {
58 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
60 else if (this.title === 'Tn') {
61 this.getConnectionLinkTable();
62 this.ANkeyList = this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).options.map((val) => {return val.key})
63 this.CNkeyList = this.transferFormItems.find((item) => {return item.title === 'CN Endpoint'}).options.map((val) => {return val.key})
64 this.keyList = this.ANkeyList.concat(this.CNkeyList)
65 if (typeof this.formData !== 'undefined' && Object.keys(this.formData).length !== 0) {
66 this.EndpointEnable = this.keyList.every((item) => {return this.formData.hasOwnProperty(item)})
68 if(this.EndpointEnable){
69 this.ANEndpointInputs = this.Util.pick(this.formData, this.ANkeyList)
70 this.CNEndpointInputs = this.Util.pick(this.formData, this.CNkeyList)
72 this.transferFormItems.map((item,index)=>{
73 if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
74 this.transferFormItems.splice(index,1)
79 // If the endpoint related parameters from the back end are incomplete, delete the endpoint item
80 //-------> Comment: Above code
81 if (this.title === 'An') {
82 this.AreaFormatting();
88 this.connectionLinkTable.forEach((item) => {
89 if (item.hasOwnProperty('linkId') && typeof this.formData['tn_connection_links'] !== 'undefined' && this.formData['tn_connection_links'] !== '' && this.formData['tn_connection_links'] !== null && item['linkId'] === this.formData['tn_connection_links']) {
97 changeResourceShare () {
102 if (Object.prototype.toString.call(val) === '[object Object]') {
109 getConnectionLinkTable (): void{
110 this.http.getConnectionLinkTable(this.getConnetionFailed).then ((res) => {
111 this.connectionLinkTable = res.result_body.connection_links_list
112 this.addCheckStatus() // add init check status for connection link table
113 this.judgeTn() // init judge
114 this.getTableHeader()
118 getTableHeader (): void { // Find the common key of all data
119 let keyList :any[] = this.connectionLinkTable.map((item) => {
120 return Object.keys(item)
122 this.connectionTableHeader = this.Util.intersection(keyList).filter((item) => {
123 return item !== 'checked'
125 // Filter redundant items in table data
126 this.connectionLinkTable.forEach((item) => {
127 for (let key in item) {
128 if (key !== 'linkId' && key !== 'checked' && this.connectionTableHeader.indexOf(key) === -1) {
131 // Filter out the null values in each item
132 for (let i in item[key]) {
133 if (i === 'jitter' && (item[key][i] === '' || item[key][i] === 'null' || item[key][i] === null)) {
142 getConnetionFailed () {
143 console.log('failed')
147 if (this.formData['sliceProfile_TN_resourceSharingLevel'] === 'non-shared') {
148 this.connectionLinkTable.forEach ((item) => {
151 this.formData['tn_connection_links'] = null
152 this.notPassPara = ['tn_connection_links']
153 this.transferFormItems.forEach((item) => {
154 if (item.title === 'Connection Links') {
156 } else if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
161 } else if (this.formData['sliceProfile_TN_resourceSharingLevel'] === 'shared') {
162 this.transferFormItems.forEach((item) => {
163 if (item.title === 'Connection Links') {
165 } else if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
166 if (typeof this.formData['tn_connection_links'] !== 'undefined' && this.formData['tn_connection_links'] !== null && this.formData['tn_connection_links'] !== '') {
168 item.required = false
169 this.notPassPara = []
170 this.notPassPara = this.notPassPara.concat(this.ANkeyList, this.CNkeyList)
172 this.formData['tn_connection_links'] = ''
175 this.notPassPara = []
182 validateEndPoint (key: string, value: any, required: boolean): string {
184 if (this.Util.isEmpty(value)) {
185 return 'can not be empty';
188 if (key === 'ip_address') {
189 if (!this.regxpIP.test(value)) {
191 return 'xxx.xxx.xxx.xxx';
198 } else if (key === 'logical_link') {
199 if (!this.Util.isInteger(value)){
201 return 'integer only'
213 changeLinkCheck (id: string) : void{ // update the selection state
214 this.connectionLinkTable.forEach((item) => {
215 if (item['linkId'] === id) {
221 this.formData['tn_connection_links'] = id // get the selected id
226 let areaList = [...this.formData.an_coverage_area_ta_list];
227 this.areaList = areaList.map ( (item: any) => {
228 let arr = item.split(';');
229 item = arr.map( (ite, index) => {
233 } else if (index === 1){
241 obj.options = [{name: ite, id: ite}]
248 creatAreaList (): void {
266 this.areaList.push(arr)
269 deleteAreaList (index: number): void {
270 this.areaList.splice(index,1);
273 handleChange(area: any[], areaItem: any): void{
274 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
275 areaItem.options = ADDRESS;
276 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
277 ADDRESS.forEach( item => {
278 if(item.name === area[0].selected) {
279 areaItem.options = item.city;
282 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
283 ADDRESS.forEach( (item: any) => {
284 item.city.forEach(city => {
285 if (city.name === area[1].selected) {
286 areaItem.options = city.county;
293 handleChangeSelected(area: any[], areaItem: any) {
294 if (areaItem.key === 'province') {
295 area[1].selected = ''
296 area[1].options = [];
297 area[2].selected = '';
298 area[2].options = [];
299 } else if (areaItem.key === 'city') {
300 area[2].selected = '';
301 area[2].options = [];
305 handleCancel() : void{
306 this.showModel = false
307 this.cancel.emit(this.showModel)
310 // prompt text for each item of area_list
311 checkArea (area: any) : string{
312 if (area.every((item) => {return item.selected === ''})) {
315 if (area.some((item) => {return item.selected === ''})) {
321 // special handling for address
322 areaCheckBeforeSubmit (target: object) : Boolean{
323 for (const prop in target) {
324 if (target.hasOwnProperty(prop)) {
325 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
326 // if the vlaue is "shanghai;shanghai;", the input is incomplete
327 return target[prop].every((item) => {return this.Util.deepCheck(item.split(';'))});
334 endCheckBeforeSubmit (endpoint, required) : Array<any>{
335 // check params of Endpoint
336 let result: Array<any> = [true, ''];
338 endPointList = this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).options
342 for (let item of endPointList) {
343 if (item.title === 'ip_address') {
345 } else if (item.title === 'logical_link') {
346 logicalKey = item.key
347 } else if (item.title === 'nexthop_info') {
351 for (let prop in endpoint) {
352 if (prop === ipKey) {
354 if (endpoint[prop] === '') {
355 result = [false, 'Endpoint can not be empty']
356 } else if (!this.regxpIP.test(endpoint[prop])) {
357 result = [false, 'Illegal IpAddress']
359 } else if (!this.regxpIP.test(endpoint[prop]) && endpoint[prop] !== '') {
360 result = [false, 'Illegal IpAddress']
362 } else if (prop === logicalKey) {
364 if (endpoint[prop] === '') {
365 result = [false, 'logical can not be empty']
366 } else if (!this.Util.isInteger(endpoint[prop])) {
367 result = [false, 'LogicalID can only be an integer']
369 } else if (!this.Util.isInteger(endpoint[prop]) && endpoint[prop] !== '') {
370 result = [false, 'LogicalID can only be an integer']
372 } else if (prop === nextKey) {
373 if (required && endpoint[prop] === '') {
374 result = [false, 'Endpoint can not be empty']
381 inputHolder (title: string): string {
382 const titleArr = title.split(' ')
383 if (titleArr.length > 1) {
384 return titleArr.slice(0, 2).join('');
390 labelStyle (required: boolean) : object{
393 style = {'margin-left': '18px', 'margin-right': '-18px'};
401 // Verify that items of EndPoint is correct
402 if (this.EndpointEnable) {
403 let endCheckResult = []
404 if (this.title === 'Tn') {
405 const ANendCheckResult = this.endCheckBeforeSubmit(this.ANEndpointInputs, this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).required)
406 const CNendCheckResult = this.endCheckBeforeSubmit(this.CNEndpointInputs, this.transferFormItems.find((item) => {return item.title === 'CN Endpoint'}).required)
407 if (ANendCheckResult[0] && CNendCheckResult[0]) {
408 endCheckResult[0] = true
410 if (ANendCheckResult[0] === false) {
411 endCheckResult = ANendCheckResult
413 endCheckResult = CNendCheckResult
417 if (!endCheckResult[0]) {
418 this.message.error(endCheckResult[1].toString());
421 // replace the params about endPoint
422 for (let prop in this.formData) {
423 if (this.title === 'Tn' && typeof this.ANEndpointInputs[prop] !== 'undefined') {
424 this.formData[prop] = this.ANEndpointInputs[prop];
425 } else if (this.title === 'Tn' && typeof this.CNEndpointInputs[prop] !== 'undefined') {
426 this.formData[prop] = this.CNEndpointInputs[prop];
431 if (this.title === 'An') {
432 const an_coverage_area_ta_list: string[] = [];
433 this.areaList.forEach( item => {
434 let str: string = '';
435 item.forEach( area => {
436 str += area.selected + ';';
438 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
440 params = {...this.formData, an_coverage_area_ta_list};
442 params = {...this.formData};
444 // Verify that each item exclude endpoint is not empty, include special handeling of area_list
445 let checkParams : object= params
446 let requireKeyList :string[] = []
447 let targetFormItems : any[] = []
448 if (this.title === 'An' || this.title === 'Cn') {
449 targetFormItems = this.coreFormItems
450 } else if (this.title = 'Tn') {
451 targetFormItems = this.transferFormItems
453 for (let item of targetFormItems) {
454 if (typeof item.required !== 'undefined' && item.required) {
455 if (typeof item.type !== 'undefined' && item.type !== 'endpoint')
457 requireKeyList.push(item.key)
461 checkParams = this.Util.pick(params, requireKeyList)
462 if (this.Util.deepCheck(checkParams) && this.areaCheckBeforeSubmit(params)) {
463 this.paramsDataChange.emit(params);
464 this.noPassParaChange.emit(this.notPassPara)
467 this.message.error('Please complete the form');