56c3a119a6466cdf3e4bfbab55ab624a578169ef
[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) 2020 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 { FormGroup } from '@angular/forms';
22 import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
23 import { PolicyService } from '../../services/policy/policy.service';
24 import { NotificationService } from '../../services/ui/notification.service';
25 import { UiService } from '../../services/ui/ui.service';
26 import { HttpErrorResponse } from '@angular/common/http';
27 import { ErrorDialogService } from '../../services/ui/error-dialog.service';
28 import * as uuid from 'uuid';
29 import { CreatePolicyInstance, PolicyInstance, PolicyTypeSchema } from '../../interfaces/policy.types';
30 import { RicSelectorComponent } from '../ric-selector/ric-selector.component';
31 import { formatJsonString, NoTypePolicyEditorComponent } from '../no-type-policy-editor/no-type-policy-editor.component';
32 import { TypedPolicyEditorComponent } from '../typed-policy-editor/typed-policy-editor.component';
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   instanceForm: FormGroup;
41   @ViewChild(RicSelectorComponent)
42   ricSelector: RicSelectorComponent;
43   @ViewChild(NoTypePolicyEditorComponent)
44   noTypePolicyEditor: NoTypePolicyEditorComponent;
45   @ViewChild(TypedPolicyEditorComponent)
46   typedPolicyEditor: TypedPolicyEditorComponent;
47   policyInstanceId: string; // null if not yet created
48   policyJson: string;
49   policyTypeName: string;
50   jsonSchemaObject: any = {};
51   darkMode: boolean;
52   ric: string;
53   allRicIds: string[] = [];
54
55   constructor(
56     public dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
57     private policySvc: PolicyService,
58     private errorService: ErrorDialogService,
59     private notificationService: NotificationService,
60     @Inject(MAT_DIALOG_DATA) private data,
61     private ui: UiService) {
62     this.policyInstanceId = data.instanceId;
63     this.policyTypeName = data.name ? data.name : '< No Type >';
64     this.policyJson = data.instanceJson;
65     this.jsonSchemaObject = data.createSchema;
66     this.ric = data.ric;
67   }
68
69   ngOnInit() {
70     this.ui.darkModeState.subscribe((isDark) => {
71       this.darkMode = isDark;
72     });
73     this.instanceForm = new FormGroup({});
74     this.formatNoTypePolicyJson();
75   }
76
77   private formatNoTypePolicyJson() {
78     if (!this.typeHasSchema()) {
79       if (this.policyJson) {
80         this.policyJson = formatJsonString(this.policyJson);
81       } else {
82         this.policyJson = '{}';
83       }
84     }
85   }
86
87   onSubmit() {
88     if (this.policyInstanceId == null) {
89       this.policyInstanceId = uuid.v4();
90     }
91     const self: PolicyInstanceDialogComponent = this;
92     let policyData: string;
93     if (this.typeHasSchema()) {
94       policyData = this.typedPolicyEditor.prettyLiveFormData;
95     } else {
96       policyData = this.noTypePolicyEditor.policyJsonTextArea.value;
97     }
98     let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(policyData);
99     this.policySvc.putPolicy(createPolicyInstance).subscribe(
100       {
101         next(_) {
102           self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted');
103           self.dialogRef.close();
104         },
105         error(error: HttpErrorResponse) {
106           self.errorService.displayError('Submit failed: ' + error.error);
107         },
108         complete() { }
109       });
110   }
111
112   typeHasSchema(): boolean {
113     return this.jsonSchemaObject !== '{}';
114   }
115
116   isFormValid(): boolean {
117     let isValid: boolean = this.instanceForm.valid;
118     if (this.typeHasSchema()) {
119       isValid = isValid && this.typedPolicyEditor ? this.typedPolicyEditor.formIsValid : false;
120     }
121     return isValid;
122   }
123
124   private createPolicyInstance(policyJson: string): CreatePolicyInstance {
125     let createPolicyInstance = {} as CreatePolicyInstance;
126     createPolicyInstance.policy_data = JSON.parse(policyJson);
127     createPolicyInstance.policy_id = this.policyInstanceId;
128     createPolicyInstance.policytype_id = '';
129     createPolicyInstance.ric_id = this.ricSelector ? this.ricSelector.selectedRic : this.ric;
130     createPolicyInstance.service_id = 'controlpanel';
131     return createPolicyInstance;
132   }
133 }
134
135 export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig {
136   const createSchema = policyTypeSchema.schemaObject;
137   const instanceId = instance ? instance.policy_id : null;
138   const instanceJson = instance ? instance.policy_data : null;
139   const name = policyTypeSchema.name;
140   const ric = instance ? instance.ric_id : null;
141   return {
142       maxWidth: '1200px',
143       maxHeight: '900px',
144       width: '900px',
145       role: 'dialog',
146       disableClose: false,
147       panelClass: darkMode ? 'dark-theme' : '',
148       data: {
149           createSchema,
150           instanceId,
151           instanceJson,
152           name,
153           ric
154       }
155   };
156 }