12b2f949f05ca552574fc9f1a317d67f53b86817
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance-dialog / policy-instance-dialog.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 import { Component, Inject, OnInit, ViewChild } from '@angular/core';
21 import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
22 import * as uuid from 'uuid';
23 import { CreatePolicyInstance, PolicyInstance, PolicyTypeSchema } from '../../interfaces/policy.types';
24 import { PolicyService } from '../../services/policy/policy.service';
25 import { ErrorDialogService } from '../../services/ui/error-dialog.service';
26 import { NotificationService } from './../../services/ui/notification.service';
27 import { UiService } from '../../services/ui/ui.service';
28 import { HttpErrorResponse } from '@angular/common/http';
29 import { FormGroup, FormControl, Validators } from '@angular/forms';
30 import { Ric, Rics } from '../../interfaces/ric';
31 import { TypedPolicyEditorComponent } from '../typed-policy-editor/typed-policy-editor.component';
32
33
34 @Component({
35     selector: 'nrcp-policy-instance-dialog',
36     templateUrl: './policy-instance-dialog.component.html',
37     styleUrls: ['./policy-instance-dialog.component.scss']
38 })
39 export class PolicyInstanceDialogComponent implements OnInit {
40     @ViewChild(TypedPolicyEditorComponent)
41     policyEditor: TypedPolicyEditorComponent;
42     instanceForm: FormGroup;
43
44
45     ric: string;
46     allRics: Ric[];
47     policyInstanceId: string; // null if not yet created
48     policyTypeName: string;
49     jsonSchemaObject: any = {};
50     darkMode: boolean;
51
52     private fetchRics() {
53         console.log('fetchRics ' + this.policyTypeName);
54         const self: PolicyInstanceDialogComponent = this;
55         this.dataService.getRics(this.policyTypeName).subscribe(
56             {
57                 next(value: Rics) {
58                     self.allRics = value.rics;
59                     console.log(value);
60                 }
61             });
62     }
63
64     constructor(
65         private dataService: PolicyService,
66         private errorService: ErrorDialogService,
67         private notificationService: NotificationService,
68         @Inject(MAT_DIALOG_DATA) public data,
69         private dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
70         private ui: UiService) {
71         this.policyInstanceId = data.instanceId;
72         this.policyTypeName = data.name;
73         this.jsonSchemaObject = data.createSchema;
74         this.ric = data.ric;
75     }
76
77     ngOnInit() {
78         this.ui.darkModeState.subscribe((isDark) => {
79             this.darkMode = isDark;
80         });
81         this.instanceForm = new FormGroup({
82             'ricSelector': new FormControl(this.ric, [
83                 Validators.required
84             ])
85         });
86         if (!this.policyInstanceId) {
87             this.fetchRics();
88         }
89     }
90
91     get ricSelector() { return this.instanceForm.get('ricSelector'); }
92
93     onSubmit() {
94         if (this.policyInstanceId == null) {
95             this.policyInstanceId = uuid.v4();
96         }
97         const policyJson: string = this.policyEditor.prettyLiveFormData;
98         const self: PolicyInstanceDialogComponent = this;
99         let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(policyJson);
100         this.dataService.putPolicy(createPolicyInstance).subscribe(
101             {
102                 next(_) {
103                     self.notificationService.success('Policy ' + self.policyTypeName + ':' + self.policyInstanceId +
104                         ' submitted');
105                     self.dialogRef.close();
106                 },
107                 error(error: HttpErrorResponse) {
108                     self.errorService.displayError('Submit failed: ' + error.error);
109                 },
110                 complete() { }
111             });
112     }
113
114     private createPolicyInstance(policyJson: string) {
115         let createPolicyInstance = {} as CreatePolicyInstance;
116         createPolicyInstance.policy_data = JSON.parse(policyJson);
117         createPolicyInstance.policy_id = this.policyInstanceId;
118         createPolicyInstance.policytype_id = this.policyTypeName;
119         createPolicyInstance.ric_id = (!this.ricSelector.value.ric_id) ? this.ric : this.ricSelector.value.ric_id;
120         createPolicyInstance.service_id = 'controlpanel';
121         return createPolicyInstance;
122     }
123
124     onClose() {
125         this.dialogRef.close();
126     }
127
128     get isJsonFormValid(): boolean {
129         return this.policyEditor ? this.policyEditor.formIsValid : false;
130     }
131 }
132
133 export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig {
134     const createSchema = policyTypeSchema.schemaObject;
135     const instanceId = instance ? instance.policy_id : null;
136     const instanceJson = instance ? instance.policy_data : null;
137     const name = policyTypeSchema.name;
138     const ric = instance ? instance.ric_id : null;
139     return {
140         maxWidth: '1200px',
141         maxHeight: '900px',
142         width: '900px',
143         role: 'dialog',
144         disableClose: false,
145         panelClass: darkMode ? 'dark-theme' : '',
146         data: {
147             createSchema,
148             instanceId,
149             instanceJson,
150             name,
151             ric
152         }
153     };
154 }
155