Added callback to R-APPS invoked after RIC recovery
[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         Vector<String> result = new Vector<>();
46         for (PolicyType p : this.policyTypes.getAll()) {
47             result.add(p.name());
48         }
49         return Mono.just(result);
50     }
51
52     @Override
53     public Mono<Collection<String>> getPolicyIdentities(String nearRtRicUrl) {
54         Vector<String> result = new Vector<>();
55         for (Policy policy : getPolicies(nearRtRicUrl).getAll()) {
56             if (policy.ric().getConfig().baseUrl().equals(nearRtRicUrl)) {
57                 result.add(policy.id());
58             }
59         }
60
61         return Mono.just(result);
62     }
63
64     @Override
65     public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
66         try {
67             return Mono.just(this.policyTypes.getType(policyTypeId).schema());
68         } catch (Exception e) {
69             return Mono.error(e);
70         }
71     }
72
73     @Override
74     public Mono<String> putPolicy(Policy p) {
75         getPolicies(p.ric().getConfig().baseUrl()).put(p);
76         return Mono.just("OK");
77     }
78
79     @Override
80     public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
81         getPolicies(nearRtRicUrl).removeId(policyId);
82         return Mono.just("OK");
83     }
84
85     private Policies getPolicies(String url) {
86         if (!policies.containsKey(url)) {
87             policies.put(url, new Policies());
88         }
89         return policies.get(url);
90     }
91
92 }