2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
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;
31 import java.util.Optional;
32 import java.util.Vector;
33 import java.util.stream.Collectors;
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;
50 * Creates a mock implementation of the policy controller client API.
53 public class PolicyControllerMockConfiguration {
55 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58 public PolicyAgentApi policyAgentApi() {
59 MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
63 class MockPolicyAgentApi implements PolicyAgentApi {
64 private final Database database = new Database();
67 public ResponseEntity<String> getPolicyInstance(String id) throws RestClientException {
68 return new ResponseEntity<>(database.getInstance(id), HttpStatus.OK);
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);
79 public void deletePolicy(String policyInstanceId) throws RestClientException {
80 database.deleteInstance(policyInstanceId);
84 public ResponseEntity<PolicyTypes> getAllPolicyTypes() throws RestClientException {
85 PolicyTypes result = new PolicyTypes();
86 result.addAll(database.getTypes());
87 return new ResponseEntity<>(result, HttpStatus.OK);
91 public ResponseEntity<PolicyInstances> getPolicyInstancesForType(String type) {
92 PolicyInstances result = new PolicyInstances();
93 List<PolicyInfo> inst = database.getInstances(Optional.of(type));
95 return new ResponseEntity<>(result, HttpStatus.OK);
99 public ResponseEntity<Collection<String>> getRicsSupportingType(String typeName) {
100 Vector<String> res = new Vector<>();
104 return new ResponseEntity<>(res, HttpStatus.OK);
111 String schema = getStringFromFile("anr-policy-schema.json");
112 PolicyType policy = new PolicyType("ANR", schema);
113 types.put("ANR", policy);
115 schema = getStringFromFile("demo-policy-schema-1.json");
116 policy = new PolicyType("type2", schema);
117 types.put("type2", policy);
119 schema = getStringFromFile("demo-policy-schema-2.json");
120 policy = new PolicyType("type3", schema);
121 types.put("type3", policy);
123 schema = getStringFromFile("demo-policy-schema-3.json");
124 policy = new PolicyType("type4", schema);
125 types.put("type4", policy);
127 putInstance("ANR", "ANR-1", getStringFromFile("anr-policy-instance.json"), "ric_1");
128 } catch (Exception e) {
133 private String getStringFromFile(String path) {
135 InputStream inputStream =
136 MethodHandles.lookup().lookupClass().getClassLoader().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);
144 String normalize(String str) {
145 return str.replace('\n', ' ');
148 private String getTimeStampUTC() {
149 return java.time.Instant.now().toString();
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);
158 public void deleteInstance(String instanceId) {
159 instances.remove(instanceId);
162 String getInstance(String id) throws RestClientException {
163 PolicyInfo i = instances.get(id);
165 throw new RestClientException("Type not found: " + id);
170 public Collection<PolicyType> getTypes() {
171 return types.values();
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())) {
189 private Map<String, PolicyType> types = new HashMap<>();
190 private Map<String, PolicyInfo> instances = new HashMap<>();