Merge "Support of selecting RIC in dashboard"
[nonrtric.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / PolicyControllerMockConfiguration.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 package org.oransc.ric.portal.dashboard.config;
21
22 import java.io.BufferedReader;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.lang.invoke.MethodHandles;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Vector;
33 import java.util.stream.Collectors;
34
35 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
36 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
37 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
38 import org.oransc.ric.portal.dashboard.model.PolicyType;
39 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
40 import org.oransc.ric.portal.dashboard.policyagentapi.PolicyAgentApi;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.boot.test.context.TestConfiguration;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.web.client.RestClientException;
46
47 /**
48  * Creates a mock implementation of the policy controller client API.
49  */
50 @TestConfiguration
51 public class PolicyControllerMockConfiguration {
52
53         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55         @Bean
56         public PolicyAgentApi policyAgentApi() {
57                 MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
58                 return apiClient;
59         }
60
61         class MockPolicyAgentApi implements PolicyAgentApi {
62                 private final Database database = new Database();
63
64                 @Override
65                 public String getPolicyInstance(String id) throws RestClientException {
66                         return database.getInstance(id);
67                 }
68
69                 @Override
70                 public void putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
71                                 throws RestClientException {
72                         database.putInstance(policyTypeIdString, policyInstanceId, json, ric);
73                 }
74
75                 @Override
76                 public void deletePolicy(String policyInstanceId) throws RestClientException {
77                         database.deleteInstance(policyInstanceId);
78                 }
79
80                 @Override
81                 public PolicyTypes getAllPolicyTypes() throws RestClientException {
82                         PolicyTypes result = new PolicyTypes();
83                         result.addAll(database.getTypes());
84                         return result;
85                 }
86
87                 @Override
88                 public PolicyInstances getPolicyInstancesForType(String type) {
89                         PolicyInstances result = new PolicyInstances();
90                         List<PolicyInfo> inst = database.getInstances(Optional.of(type));
91                         result.addAll(inst);
92                         return result;
93                 }
94
95                 @Override
96                 public Collection<String> getRicsSupportingType(String typeName) {
97                         Vector<String> res = new Vector<>();
98                         res.add("ric_1");
99                         res.add("ric_2");
100                         res.add("ric_3");
101                         return res;
102                 }
103         }
104
105         class Database {
106
107                 Database() {
108                         String schema = getStringFromFile("anr-policy-schema.json");
109                         PolicyType policy = new PolicyType("ANR", schema);
110                         types.put("ANR", policy);
111
112                         schema = getStringFromFile("demo-policy-schema-1.json");
113                         policy = new PolicyType("type2", schema);
114                         types.put("type2", policy);
115
116                         schema = getStringFromFile("demo-policy-schema-2.json");
117                         policy = new PolicyType("type3", schema);
118                         types.put("type3", policy);
119
120                         schema = getStringFromFile("demo-policy-schema-3.json");
121                         policy = new PolicyType("type4", schema);
122                         types.put("type4", policy);
123                         try {
124                                 putInstance("ANR", "ANR-1", getStringFromFile("anr-policy-instance.json"), "ric_1");
125                         } catch (Exception e) {
126                                 // Nothing
127                         }
128                 }
129
130                 private String getStringFromFile(String path) {
131                         try {
132                                 InputStream inputStream = MethodHandles.lookup().lookupClass().getClassLoader()
133                                                 .getResourceAsStream(path);
134                                 return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
135                         } catch (Exception e) {
136                                 logger.error("Cannot read file :" + path, e);
137                                 return "";
138                         }
139                 }
140
141                 String normalize(String str) {
142                         return str.replace('\n', ' ');
143                 }
144
145                 private String getTimeStampUTC() {
146                         return java.time.Instant.now().toString();
147                 }
148
149                 void putInstance(String typeId, String instanceId, String instanceData, String ric) {
150                         PolicyInfo i = ImmutablePolicyInfo.builder().json(instanceData).lastModified(getTimeStampUTC())
151                                         .id(instanceId).ric(ric).service("service").type(typeId).build();
152                         instances.put(instanceId, i);
153                 }
154
155                 public void deleteInstance(String instanceId) {
156                         instances.remove(instanceId);
157                 }
158
159                 String getInstance(String id) throws RestClientException {
160                         PolicyInfo i = instances.get(id);
161                         if (i == null) {
162                                 throw new RestClientException("Type not found: " + id);
163                         }
164                         return i.json();
165                 }
166
167                 public Collection<PolicyType> getTypes() {
168                         return types.values();
169                 }
170
171                 public List<PolicyInfo> getInstances(Optional<String> typeId) {
172                         ArrayList<PolicyInfo> result = new ArrayList<>();
173                         for (PolicyInfo i : instances.values()) {
174                                 if (typeId.isPresent()) {
175                                         if (i.type().equals(typeId.get())) {
176                                                 result.add(i);
177                                         }
178
179                                 } else {
180                                         result.add(i);
181                                 }
182                         }
183                         return result;
184                 }
185
186                 private Map<String, PolicyType> types = new HashMap<>();
187                 private Map<String, PolicyInfo> instances = new HashMap<>();
188
189         }
190
191 }