Integrating A1 client with service supervision
[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         class Database {
96
97                 Database() {
98                         String schema = getStringFromFile("anr-policy-schema.json");
99                         PolicyType policy = new PolicyType("ANR", schema);
100                         types.put("ANR", policy);
101
102                         schema = getStringFromFile("demo-policy-schema-1.json");
103                         policy = new PolicyType("type2", schema);
104                         types.put("type2", policy);
105
106                         schema = getStringFromFile("demo-policy-schema-2.json");
107                         policy = new PolicyType("type3", schema);
108                         types.put("type3", policy);
109
110                         schema = getStringFromFile("demo-policy-schema-3.json");
111                         policy = new PolicyType("type4", schema);
112                         types.put("type4", policy);
113                         try {
114                                 putInstance("ANR", "ANR-1", getStringFromFile("anr-policy-instance.json"));
115                         } catch (Exception e) {
116                                 // Nothing
117                         }
118                 }
119
120                 private String getStringFromFile(String path) {
121                         try {
122                                 InputStream inputStream = MethodHandles.lookup().lookupClass().getClassLoader()
123                                                 .getResourceAsStream(path);
124                                 return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
125                         } catch (Exception e) {
126                                 logger.error("Cannot read file :" + path, e);
127                                 return "";
128                         }
129                 }
130
131                 String normalize(String str) {
132                         return str.replace('\n', ' ');
133                 }
134
135                 private String getTimeStampUTC() {
136                         return java.time.Instant.now().toString();
137                 }
138
139                 void putInstance(String typeId, String instanceId, String instanceData) {
140                         PolicyInfo i = ImmutablePolicyInfo.builder().json(instanceData).lastModified(getTimeStampUTC())
141                                         .id(instanceId).ric("ricXX").service("service").type(typeId).build();
142                         instances.put(instanceId, i);
143                 }
144
145                 public void deleteInstance(String instanceId) {
146                         instances.remove(instanceId);
147                 }
148
149                 String getInstance(String id) throws RestClientException {
150                         PolicyInfo i = instances.get(id);
151                         if (i == null) {
152                                 throw new RestClientException("Type not found: " + id);
153                         }
154                         return i.json();
155                 }
156
157                 public Collection<PolicyType> getTypes() {
158                         return types.values();
159                 }
160
161                 public List<PolicyInfo> getInstances(Optional<String> typeId) {
162                         ArrayList<PolicyInfo> result = new ArrayList<>();
163                         for (PolicyInfo i : instances.values()) {
164                                 if (typeId.isPresent()) {
165                                         if (i.type().equals(typeId.get())) {
166                                                 result.add(i);
167                                         }
168
169                                 } else {
170                                         result.add(i);
171                                 }
172                         }
173                         return result;
174                 }
175
176                 private Map<String, PolicyType> types = new HashMap<>();
177                 private Map<String, PolicyInfo> instances = new HashMap<>();
178
179         }
180
181 }