Merge "New version of NearRT-RIC simulator"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / utils / MockA1Client.java
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 package org.oransc.policyagent.utils;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.oransc.policyagent.clients.A1Client;
29 import org.oransc.policyagent.repository.Policies;
30 import org.oransc.policyagent.repository.Policy;
31 import org.oransc.policyagent.repository.PolicyType;
32 import org.oransc.policyagent.repository.PolicyTypes;
33 import reactor.core.publisher.Mono;
34
35 public class MockA1Client implements A1Client {
36     private final Map<String, Policies> policies = new HashMap<>();
37     private final PolicyTypes policyTypes;
38
39     public MockA1Client(PolicyTypes policyTypes) {
40         this.policyTypes = policyTypes;
41     }
42
43     @Override
44     public Mono<Collection<String>> getPolicyTypeIdentities(String nearRtRicUrl) {
45         synchronized (this.policyTypes) {
46             Vector<String> result = new Vector<>();
47             for (PolicyType p : this.policyTypes.getAll()) {
48                 result.add(p.name());
49             }
50             return Mono.just(result);
51         }
52     }
53
54     @Override
55     public Mono<Collection<String>> getPolicyIdentities(String nearRtRicUrl) {
56         synchronized (this.policies) {
57             Vector<String> result = new Vector<>();
58             for (Policy policy : getPolicies(nearRtRicUrl).getAll()) {
59                 if (policy.ric().getConfig().baseUrl().equals(nearRtRicUrl)) {
60                     result.add(policy.id());
61                 }
62             }
63
64             return Mono.just(result);
65         }
66     }
67
68     @Override
69     public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
70         try {
71             return Mono.just(this.policyTypes.getType(policyTypeId).schema());
72         } catch (Exception e) {
73             return Mono.error(e);
74         }
75     }
76
77     @Override
78     public Mono<String> putPolicy(Policy p) {
79         getPolicies(p.ric().getConfig().baseUrl()).put(p);
80         return Mono.just("OK");
81     }
82
83     @Override
84     public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
85         getPolicies(nearRtRicUrl).removeId(policyId);
86         return Mono.just("OK");
87     }
88
89     private Policies getPolicies(String url) {
90         if (!policies.containsKey(url)) {
91             policies.put(url, new Policies());
92         }
93         return policies.get(url);
94     }
95
96 }