update angular to latest ver 8
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / ac-xapp / ac-xapp.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 import { HttpErrorResponse } from '@angular/common/http';
22 import { Component, OnDestroy, OnInit } from '@angular/core';
23 import { FormControl, FormGroup, Validators } from '@angular/forms';
24 import { Subscription } from 'rxjs';
25 import { ACAdmissionIntervalControl } from '../interfaces/ac-xapp.types';
26 import { RicInstance } from '../interfaces/dashboard.types';
27 import { ACXappService } from '../services/ac-xapp/ac-xapp.service';
28 import { InstanceSelectorService } from '../services/instance-selector/instance-selector.service';
29 import { ErrorDialogService } from '../services/ui/error-dialog.service';
30 import { NotificationService } from '../services/ui/notification.service';
31
32 @Component({
33   selector: 'rd-ac-xapp',
34   templateUrl: './ac-xapp.component.html',
35   styleUrls: ['./ac-xapp.component.scss']
36 })
37 export class AcXappComponent implements OnInit, OnDestroy {
38
39   //declare acForm as Public variable. Private variables should not be used in template HTML
40   acForm: FormGroup;
41   private instanceChange: Subscription;
42   private instanceKey: string;
43
44   constructor(
45     private acXappService: ACXappService,
46     private errorDialogService: ErrorDialogService,
47     private notificationService: NotificationService,
48     public instanceSelectorService: InstanceSelectorService, ) { }
49
50   ngOnInit() {
51     this.acForm = new FormGroup({
52       // Names must match the ACAdmissionIntervalControl interface
53       enforce: new FormControl(true, [Validators.required]),
54       class: new FormControl('', [Validators.required, Validators.min(1), Validators.max(256)]),
55       window_length: new FormControl('', [Validators.required, Validators.min(15), Validators.max(300)]),
56       blocking_rate: new FormControl('', [Validators.required, Validators.min(0), Validators.max(100)]),
57       trigger_threshold: new FormControl('', [Validators.required, Validators.min(1)])
58     });
59
60     this.instanceChange = this.instanceSelectorService.getSelectedInstance().subscribe((instance: RicInstance) => {
61       if (instance.key) {
62         // TODO: show pending action indicator
63         this.instanceKey = instance.key;
64         this.acXappService.getPolicy(instance.key).subscribe((res: ACAdmissionIntervalControl) => {
65           this.acForm.controls['class'].setValue(res.class);
66           this.acForm.controls['enforce'].setValue(res.enforce);
67           this.acForm.controls['window_length'].setValue(res.window_length);
68           this.acForm.controls['blocking_rate'].setValue(res.blocking_rate);
69           this.acForm.controls['trigger_threshold'].setValue(res.trigger_threshold);
70           // TODO: clear pending action indicator
71         },
72           (error: HttpErrorResponse) => {
73             // TODO: clear pending action indicator
74             this.errorDialogService.displayError(error.message);
75           });
76       }
77     });
78   }
79
80   ngOnDestroy() {
81     this.instanceChange.unsubscribe();
82   }
83
84   updateAc = (acFormValue: ACAdmissionIntervalControl) => {
85     if (this.acForm.valid) {
86       // convert strings to numbers using the plus operator
87       const acFormValueConverted = {
88         class: +acFormValue.class,
89         enforce: acFormValue.enforce,
90         window_length: +acFormValue.window_length,
91         blocking_rate: +acFormValue.blocking_rate,
92         trigger_threshold: +acFormValue.trigger_threshold
93       };
94       this.acXappService.putPolicy(this.instanceKey, acFormValueConverted).subscribe(
95         response => {
96           if (response.status === 200) {
97             this.notificationService.success('AC update policy succeeded!');
98           }
99         },
100         (error => {
101           this.errorDialogService.displayError('AC update policy failed: ' + error.message);
102         })
103       );
104     }
105   }
106
107   hasError(controlName: string, errorName: string) {
108     if (this.acForm.controls[controlName].hasError(errorName)) {
109       return true;
110     }
111     return false;
112   }
113
114   validateControl(controlName: string) {
115     if (this.acForm.controls[controlName].invalid && this.acForm.controls[controlName].touched) {
116       return true;
117     }
118     return false;
119   }
120
121 }