Reorganise policy control part of frontend
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-type / 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 { PolicyType, PolicyTypes, PolicyTypeSchema } from '@interfaces/policy.types';
28 import { PolicyService } from '@services/policy/policy.service';
29
30 @Injectable({
31     providedIn: 'root'
32 })
33
34 export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
35
36     policyTypes: PolicyTypeSchema[] = [];
37
38     policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
39
40     public rowCount = 1; // hide footer during intial load
41
42     constructor(public policySvc: PolicyService) {
43         super();
44     }
45
46     public getPolicyTypes() {
47         this.policyTypes = [] as PolicyTypeSchema[];
48         this.policySvc.getPolicyTypes()
49             .subscribe((policyType: PolicyTypes) => {
50                 this.rowCount = policyType.policytype_ids.length;
51                 if (policyType.policytype_ids.length != 0) {
52                     policyType.policytype_ids.forEach(policyTypeId => {
53                         var policyTypeSchema = {} as PolicyTypeSchema
54                         if (policyTypeId === "") {
55                             policyTypeSchema.id = '';
56                             policyTypeSchema.name = '';
57                             policyTypeSchema.schemaObject = '{}';
58                             this.policyTypes.push(policyTypeSchema);
59                         }
60                         else {
61                             this.policySvc.getPolicyType(policyTypeId)
62                                 .subscribe((policyType: PolicyType) => {
63                                     policyTypeSchema.id = policyTypeId;
64                                     policyTypeSchema.schemaObject = policyType.policy_schema;
65                                     policyTypeSchema.name = policyType.policy_schema.title;
66                                     this.policyTypes.push(policyTypeSchema);
67                                 })
68                         }
69                         this.policyTypeSubject.next(this.policyTypes);
70                     })
71                 }
72             })
73     }
74
75     public getPolicyType(policyTypeId: string): PolicyTypeSchema {
76         var policyTypeSchema = {} as PolicyTypeSchema;
77         this.policySvc.getPolicyType(policyTypeId)
78             .subscribe((policyType: PolicyType) => {
79                 policyTypeSchema.id = policyTypeId;
80                 policyTypeSchema.schemaObject = policyType.policy_schema;
81                 policyTypeSchema.name = policyType.policy_schema.title;
82             })
83             if (policyTypeId === "") {
84                 policyTypeSchema.id = '<No Type>';
85             }
86         return policyTypeSchema;
87     }
88
89     connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
90         return of(this.policyTypeSubject.getValue());
91     }
92
93     disconnect(collectionViewer: CollectionViewer): void {
94         this.policyTypeSubject.complete();
95     }
96 }