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.web.client.RestClientException;
48 * Creates a mock implementation of the policy controller client API.
51 public class PolicyControllerMockConfiguration {
53 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56 public PolicyAgentApi policyAgentApi() {
57 MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
61 class MockPolicyAgentApi implements PolicyAgentApi {
62 private final Database database = new Database();
65 public String getPolicyInstance(String id) throws RestClientException {
66 return database.getInstance(id);
70 public void putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
71 throws RestClientException {
72 database.putInstance(policyTypeIdString, policyInstanceId, json, ric);
76 public void deletePolicy(String policyInstanceId) throws RestClientException {
77 database.deleteInstance(policyInstanceId);
81 public PolicyTypes getAllPolicyTypes() throws RestClientException {
82 PolicyTypes result = new PolicyTypes();
83 result.addAll(database.getTypes());
88 public PolicyInstances getPolicyInstancesForType(String type) {
89 PolicyInstances result = new PolicyInstances();
90 List<PolicyInfo> inst = database.getInstances(Optional.of(type));
96 public Collection<String> getRicsSupportingType(String typeName) {
97 Vector<String> res = new Vector<>();
108 String schema = getStringFromFile("anr-policy-schema.json");
109 PolicyType policy = new PolicyType("ANR", schema);
110 types.put("ANR", policy);
112 schema = getStringFromFile("demo-policy-schema-1.json");
113 policy = new PolicyType("type2", schema);
114 types.put("type2", policy);
116 schema = getStringFromFile("demo-policy-schema-2.json");
117 policy = new PolicyType("type3", schema);
118 types.put("type3", policy);
120 schema = getStringFromFile("demo-policy-schema-3.json");
121 policy = new PolicyType("type4", schema);
122 types.put("type4", policy);
124 putInstance("ANR", "ANR-1", getStringFromFile("anr-policy-instance.json"), "ric_1");
125 } catch (Exception e) {
130 private String getStringFromFile(String path) {
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);
141 String normalize(String str) {
142 return str.replace('\n', ' ');
145 private String getTimeStampUTC() {
146 return java.time.Instant.now().toString();
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);
155 public void deleteInstance(String instanceId) {
156 instances.remove(instanceId);
159 String getInstance(String id) throws RestClientException {
160 PolicyInfo i = instances.get(id);
162 throw new RestClientException("Type not found: " + id);
167 public Collection<PolicyType> getTypes() {
168 return types.values();
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())) {
186 private Map<String, PolicyType> types = new HashMap<>();
187 private Map<String, PolicyInfo> instances = new HashMap<>();