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