db8f01fb63ac00dc76eead3d4ffce6e89b034153
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-type.datasource.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
21 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { Injectable } from '@angular/core';
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { of } from 'rxjs/observable/of';
25 import { Observable } from 'rxjs/Observable';
26
27 import { PolicyTypeSchema } from '../interfaces/policy.types';
28 import { PolicyService } from '../services/policy/policy.service';
29 import { NotificationService } from '../services/ui/notification.service';
30
31 @Injectable({
32     providedIn: 'root'
33 })
34
35 export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
36
37     policyTypes: PolicyTypeSchema[] = [];
38
39     private policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
40
41     public rowCount = 1; // hide footer during intial load
42
43     constructor(private policySvc: PolicyService,
44         private notificationService: NotificationService) {
45         super();
46     }
47
48     public getPolicyTypes() {
49         this.policyTypes = [] as PolicyTypeSchema[];
50         this.policySvc.getPolicyTypes().subscribe(data => {
51             if (data.policytype_ids.length != 0) {
52                 data.policytype_ids.forEach(policyId => {
53                     var policyTypeSchema = {} as PolicyTypeSchema
54                     if (policyId === "") {
55                         policyTypeSchema.name = '';
56                         policyTypeSchema.schemaObject = '{}';
57                         this.policyTypes.push(policyTypeSchema);
58                     }
59                     else {
60                         this.policySvc.getPolicyType(policyId).subscribe(policyType => {
61                             policyTypeSchema.schemaObject = policyType.policy_schema;
62                             policyTypeSchema.name = policyType.policy_schema.title;
63                             this.policyTypes.push(policyTypeSchema);
64                         })
65                     }
66                     this.policyTypeSubject.next(this.policyTypes);
67                 })
68             }
69         })
70     }
71
72     connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
73         return of(this.policyTypeSubject.getValue());
74     }
75
76     disconnect(collectionViewer: CollectionViewer): void {
77         this.policyTypeSubject.complete();
78     }
79 }