2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
6 * Modifications Copyright (C) 2020 Nordix Foundation
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ========================LICENSE_END===================================
21 package org.oransc.ric.portal.dashboard.config;
23 import com.google.gson.GsonBuilder;
25 import java.io.BufferedReader;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Optional;
35 import java.util.Vector;
36 import java.util.stream.Collectors;
38 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
39 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
40 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
41 import org.oransc.ric.portal.dashboard.model.PolicyType;
42 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
43 import org.oransc.ric.portal.dashboard.policyagentapi.PolicyAgentApi;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.boot.test.context.TestConfiguration;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.web.client.RestClientException;
53 * Creates a mock implementation of the policy controller client API.
56 public class PolicyControllerMockConfiguration {
58 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 private static com.google.gson.Gson gson = new GsonBuilder() //
65 public PolicyAgentApi policyAgentApi() {
66 MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
70 class MockPolicyAgentApi implements PolicyAgentApi {
71 private final Database database = new Database();
74 public ResponseEntity<Object> getPolicyInstance(String id) {
75 return new ResponseEntity<>(database.getInstance(id), HttpStatus.OK);
79 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
81 database.putInstance(policyTypeIdString, policyInstanceId, json, ric);
82 return new ResponseEntity<>("Policy was put successfully", HttpStatus.OK);
86 public ResponseEntity<String> deletePolicy(String policyInstanceId) {
87 database.deleteInstance(policyInstanceId);
88 return new ResponseEntity<>("Policy was deleted successfully", HttpStatus.NO_CONTENT);
92 public ResponseEntity<String> getAllPolicyTypes() {
93 PolicyTypes result = new PolicyTypes();
94 result.addAll(database.getTypes());
95 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
99 public ResponseEntity<String> getPolicyInstancesForType(String type) {
100 PolicyInstances result = new PolicyInstances();
101 List<PolicyInfo> inst = database.getInstances(Optional.of(type));
103 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
107 public ResponseEntity<String> getRicsSupportingType(String typeName) {
108 Vector<String> res = new Vector<>();
112 return new ResponseEntity<>(gson.toJson(res), HttpStatus.OK);
119 String schema = getStringFromFile("demo-policy-schema-1.json");
120 PolicyType policyType = new PolicyType("type2", schema);
121 types.put("type2", policyType);
123 schema = getStringFromFile("demo-policy-schema-2.json");
124 policyType = new PolicyType("type3", schema);
125 types.put("type3", policyType);
127 schema = getStringFromFile("demo-policy-schema-3.json");
128 policyType = new PolicyType("type4", schema);
129 types.put("type4", policyType);
132 private String getStringFromFile(String path) {
134 InputStream inputStream =
135 MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
136 return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
137 } catch (Exception e) {
138 logger.error("Cannot read file :" + path, e);
143 String normalize(String str) {
144 return str.replace('\n', ' ');
147 private String getTimeStampUTC() {
148 return java.time.Instant.now().toString();
151 void putInstance(String typeId, String instanceId, Object instanceData, String ric) {
152 PolicyInfo i = ImmutablePolicyInfo.builder().json(instanceData).lastModified(getTimeStampUTC())
153 .id(instanceId).ric(ric).service("service").type(typeId).build();
154 instances.put(instanceId, i);
157 public void deleteInstance(String instanceId) {
158 instances.remove(instanceId);
161 Object getInstance(String id) throws RestClientException {
162 PolicyInfo i = instances.get(id);
164 throw new RestClientException("Type not found: " + id);
169 public Collection<PolicyType> getTypes() {
170 return types.values();
173 public List<PolicyInfo> getInstances(Optional<String> typeId) {
174 ArrayList<PolicyInfo> result = new ArrayList<>();
175 for (PolicyInfo i : instances.values()) {
176 if (typeId.isPresent()) {
177 if (i.type().equals(typeId.get())) {
188 private Map<String, PolicyType> types = new HashMap<>();
189 private Map<String, PolicyInfo> instances = new HashMap<>();