Add test of typed policy editor component
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-instance-dialog / no-type-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 { 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 } 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
33 @Component({
34   selector: 'nrcp-no-type-policy-instance-dialog',
35   templateUrl: './no-type-policy-instance-dialog.component.html',
36   styleUrls: ['./no-type-policy-instance-dialog.component.scss']
37 })
38 export class NoTypePolicyInstanceDialogComponent implements OnInit {
39   instanceForm: FormGroup;
40   @ViewChild(RicSelectorComponent)
41   private ricSelectorComponent: RicSelectorComponent;
42   @ViewChild(NoTypePolicyEditorComponent)
43   private policyEditorComponent: NoTypePolicyEditorComponent;
44   policyInstanceId: string; // null if not yet created
45   policyJson: string;
46   darkMode: boolean;
47   ric: string;
48   allRicIds: string[] = [];
49
50   constructor(
51     public dialogRef: MatDialogRef<NoTypePolicyInstanceDialogComponent>,
52     private policySvc: PolicyService,
53     private errorService: ErrorDialogService,
54     private notificationService: NotificationService,
55     @Inject(MAT_DIALOG_DATA) private data,
56     private ui: UiService) {
57     this.policyInstanceId = data.instanceId;
58     this.policyJson = data.instanceJson ? formatJsonString(data.instanceJson) : '{}';
59     this.ric = data.ric;
60   }
61
62   ngOnInit() {
63     this.ui.darkModeState.subscribe((isDark) => {
64       this.darkMode = isDark;
65     });
66     this.instanceForm = new FormGroup({});
67   }
68
69   onSubmit() {
70     if (this.policyInstanceId == null) {
71       this.policyInstanceId = uuid.v4();
72     }
73     const self: NoTypePolicyInstanceDialogComponent = this;
74     let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(this.policyEditorComponent.policyJsonTextArea.value);
75     this.policySvc.putPolicy(createPolicyInstance).subscribe(
76       {
77         next(_) {
78           self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted');
79           self.dialogRef.close();
80         },
81         error(error: HttpErrorResponse) {
82           self.errorService.displayError('Submit failed: ' + error.error);
83         },
84         complete() { }
85       });
86   }
87
88   private createPolicyInstance(policyJson: string): CreatePolicyInstance {
89     let createPolicyInstance = {} as CreatePolicyInstance;
90     createPolicyInstance.policy_data = JSON.parse(policyJson);
91     createPolicyInstance.policy_id = this.policyInstanceId;
92     createPolicyInstance.policytype_id = '';
93     createPolicyInstance.ric_id = this.ricSelectorComponent ? this.ricSelectorComponent.selectedRic : this.ric;
94     createPolicyInstance.service_id = 'controlpanel';
95     return createPolicyInstance;
96   }
97 }