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.portal.nonrtric.controlpanel.mock;
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.portal.nonrtric.controlpanel.model.PolicyInfo;
39 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances;
40 import org.oransc.portal.nonrtric.controlpanel.model.PolicyType;
41 import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes;
42 import org.oransc.portal.nonrtric.controlpanel.policyagentapi.PolicyAgentApi;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.boot.test.context.TestConfiguration;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.client.RestClientException;
52 * Creates a mock implementation of the policy controller client API.
55 public class PolicyControllerMockConfiguration {
57 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 private static com.google.gson.Gson gson = new GsonBuilder() //
64 public PolicyAgentApi policyAgentApi() {
65 MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
69 class MockPolicyAgentApi implements PolicyAgentApi {
70 private final Database database = new Database();
73 public ResponseEntity<Object> getPolicyInstance(String id) {
74 return new ResponseEntity<>(database.getInstance(id), HttpStatus.OK);
78 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
80 database.putInstance(policyTypeIdString, policyInstanceId, json, ric);
81 return new ResponseEntity<>(HttpStatus.OK);
85 public ResponseEntity<String> deletePolicy(String policyInstanceId) {
86 database.deleteInstance(policyInstanceId);
87 return new ResponseEntity<>(HttpStatus.OK);
91 public ResponseEntity<String> getAllPolicyTypes() {
92 PolicyTypes result = new PolicyTypes();
93 result.addAll(database.getTypes());
94 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
98 public ResponseEntity<String> getPolicyInstancesForType(String type) {
99 PolicyInstances result = new PolicyInstances();
100 List<PolicyInfo> inst = database.getInstances(Optional.of(type));
102 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
106 public ResponseEntity<String> getRicsSupportingType(String typeName) {
107 Vector<String> res = new Vector<>();
111 return new ResponseEntity<>(gson.toJson(res), HttpStatus.OK);
118 PolicyType policyType = new PolicyType("", "{}");
119 types.put("", policyType);
121 String schema = getStringFromFile("demo-policy-schema-1.json");
122 policyType = new PolicyType("type2", schema);
123 types.put("type2", policyType);
125 schema = getStringFromFile("demo-policy-schema-2.json");
126 policyType = new PolicyType("type3", schema);
127 types.put("type3", policyType);
129 schema = getStringFromFile("demo-policy-schema-3.json");
130 policyType = new PolicyType("type4", schema);
131 types.put("type4", policyType);
133 putInstance("", "123", "{\"data\":\"data\"}", "ric_1");
136 private String getStringFromFile(String path) {
138 InputStream inputStream =
139 MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
140 return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
141 } catch (Exception e) {
142 logger.error("Cannot read file :" + path, e);
147 String normalize(String str) {
148 return str.replace('\n', ' ');
151 private String getTimeStampUTC() {
152 return java.time.Instant.now().toString();
155 void putInstance(String typeId, String instanceId, Object instanceData, String ric) {
156 PolicyInfo i = PolicyInfo.builder() //
157 .policyData(instanceData) //
158 .lastModified(getTimeStampUTC()) //
159 .policyId(instanceId) //
161 .serviceId("service") //
162 .policyTypeId(typeId) //
164 instances.put(instanceId, i);
167 public void deleteInstance(String instanceId) {
168 instances.remove(instanceId);
171 Object getInstance(String id) throws RestClientException {
172 PolicyInfo i = instances.get(id);
174 throw new RestClientException("Type not found: " + id);
179 public Collection<PolicyType> getTypes() {
180 return types.values();
183 public List<PolicyInfo> getInstances(Optional<String> typeId) {
184 ArrayList<PolicyInfo> result = new ArrayList<>();
185 for (PolicyInfo i : instances.values()) {
186 if (typeId.isPresent()) {
187 if (i.policyTypeId.equals(typeId.get())) {
198 private Map<String, PolicyType> types = new HashMap<>();
199 private Map<String, PolicyInfo> instances = new HashMap<>();