Updates of the NBI
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / MockPolicyAgent.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;
22
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URL;
29 import java.nio.file.Files;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.Vector;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.oransc.policyagent.clients.A1Client;
37 import org.oransc.policyagent.configuration.ApplicationConfig;
38 import org.oransc.policyagent.repository.ImmutablePolicyType;
39 import org.oransc.policyagent.repository.Policies;
40 import org.oransc.policyagent.repository.PolicyType;
41 import org.oransc.policyagent.repository.PolicyTypes;
42 import org.oransc.policyagent.repository.Rics;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
46 import org.springframework.boot.test.context.TestConfiguration;
47 import org.springframework.boot.web.server.LocalServerPort;
48 import org.springframework.context.annotation.Bean;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 import reactor.core.publisher.Flux;
52 import reactor.core.publisher.Mono;
53
54 @RunWith(SpringRunner.class)
55 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
56 public class MockPolicyAgent {
57
58     @Autowired
59     private Rics rics;
60
61     @Autowired
62     private Policies policies;
63
64     @Autowired
65     private PolicyTypes policyTypes;
66
67     static class MockApplicationConfig extends ApplicationConfig {
68
69         @Override
70         public void initialize() {
71             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
72             loadConfigurationFromFile(url.getFile());
73         }
74     }
75
76     private static class RicPolicyDatabase {
77         private Map<String, Map<String, String>> policies = new HashMap<>();
78
79         public void putPolicy(String nearRtRicUrl, String policyId, String policyString) {
80             getPolicies(nearRtRicUrl).put(policyId, policyString);
81         }
82
83         public Iterable<String> getPolicyIdentities(String nearRtRicUrl) {
84             return getPolicies(nearRtRicUrl).keySet();
85         }
86
87         public void deletePolicy(String nearRtRicUrl, String policyId) {
88             getPolicies(nearRtRicUrl).remove(policyId);
89         }
90
91         private Map<String, String> getPolicies(String nearRtRicUrl) {
92             if (!policies.containsKey(nearRtRicUrl)) {
93                 policies.put(nearRtRicUrl, new HashMap<>());
94             }
95             return policies.get(nearRtRicUrl);
96         }
97     }
98
99     static class A1ClientMock implements A1Client {
100
101         private final RicPolicyDatabase policies = new RicPolicyDatabase();
102         private final PolicyTypes policyTypes = new PolicyTypes();
103
104         A1ClientMock() {
105             loadTypes(this.policyTypes);
106         }
107
108         @Override
109         public Flux<String> getPolicyTypeIdentities(String nearRtRicUrl) {
110             Vector<String> result = new Vector<>();
111             for (PolicyType p : this.policyTypes.getAll()) {
112                 result.add(p.name());
113             }
114             return Flux.fromIterable(result);
115         }
116
117         @Override
118         public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
119             Iterable<String> result = policies.getPolicyIdentities(nearRtRicUrl);
120             return Flux.fromIterable(result);
121         }
122
123         @Override
124         public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
125             try {
126                 return Mono.just(this.policyTypes.getType(policyTypeId).schema());
127             } catch (Exception e) {
128                 return Mono.error(e);
129             }
130         }
131
132         @Override
133         public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
134             policies.putPolicy(nearRtRicUrl, policyId, policyString);
135             return Mono.just("OK");
136         }
137
138         @Override
139         public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
140             policies.deletePolicy(nearRtRicUrl, policyId);
141             return Mono.just("OK");
142         }
143
144         private static File[] getResourceFolderFiles(String folder) {
145             ClassLoader loader = Thread.currentThread().getContextClassLoader();
146             URL url = loader.getResource(folder);
147             String path = url.getPath();
148             return new File(path).listFiles();
149         }
150
151         private static String readFile(File file) throws IOException {
152             return new String(Files.readAllBytes(file.toPath()));
153         }
154
155         private void loadTypes(PolicyTypes policyTypes) {
156             File[] files = getResourceFolderFiles("policy_types/");
157             for (File file : files) {
158                 try {
159                     String schema = readFile(file);
160                     String typeName = title(schema);
161                     PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build();
162                     policyTypes.put(type);
163                 } catch (Exception e) {
164                     System.out.println("Could not load json schema " + e);
165                 }
166             }
167         }
168     }
169
170     /**
171      * overrides the BeanFactory
172      */
173     @TestConfiguration
174     static class TestBeanFactory {
175
176         private final Rics rics = new Rics();
177         private final Policies policies = new Policies();
178         private final PolicyTypes policyTypes = new PolicyTypes();
179
180         @Bean
181         public ApplicationConfig getApplicationConfig() {
182             return new MockApplicationConfig();
183         }
184
185         @Bean
186         A1Client getA1Client() {
187             return new A1ClientMock();
188         }
189
190         @Bean
191         public Policies getPolicies() {
192             return this.policies;
193         }
194
195         @Bean
196         public PolicyTypes getPolicyTypes() {
197             return this.policyTypes;
198         }
199
200         @Bean
201         public Rics getRics() {
202             return this.rics;
203         }
204     }
205
206     @LocalServerPort
207     private int port;
208
209     private void keepServerAlive() {
210         System.out.println("Keeping server alive!");
211         try {
212             synchronized (this) {
213                 this.wait();
214             }
215         } catch (Exception ex) {
216             System.out.println("Unexpected: " + ex.toString());
217         }
218     }
219
220     private static String title(String jsonSchema) {
221         JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
222         String title = parsedSchema.get("title").getAsString();
223         return title;
224     }
225
226     @Test
227     public void runMock() throws Exception {
228         keepServerAlive();
229     }
230
231 }