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