*/
package org.oransc.policyagent.controllers;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import java.util.Collection;
+import java.util.Vector;
+
import org.oransc.policyagent.configuration.ApplicationConfig;
import org.oransc.policyagent.exceptions.ServiceException;
import org.oransc.policyagent.repository.ImmutablePolicy;
private final ApplicationConfig appConfig;
private final PolicyTypes types;
private final Policies policies;
+ private static Gson gson = new GsonBuilder() //
+ .serializeNulls() //
+ .create(); //
@Autowired
PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) {
this.policies = policies;
}
- // http://localhost:8080/policy?type=type3&instance=xxx
@GetMapping("/policy")
public ResponseEntity<String> getPolicy( //
- @RequestParam(name = "type", required = false, defaultValue = "type1") String typeName, //
- @RequestParam(name = "instance", required = false, defaultValue = "new") String instanceId) {
+ @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) {
+ try {
+ Policy p = policies.get(instance);
+ return new ResponseEntity<String>(p.json(), HttpStatus.OK);
- return new ResponseEntity<String>("policy" + typeName + instanceId, HttpStatus.OK);
+ } catch (ServiceException e) {
+ return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
+ }
+ }
+
+ @GetMapping("/policies")
+ public ResponseEntity<String> getPolicies( //
+ @RequestParam(name = "type", required = false) String type, //
+ @RequestParam(name = "ric", required = false) String ric, //
+ @RequestParam(name = "service", required = false) String service) //
+ {
+ Collection<Policy> result = null;
+
+ if (type != null) {
+ result = policies.getForType(type);
+ result = filter(result, null, ric, service);
+ } else if (service != null) {
+ result = policies.getForService(service);
+ result = filter(result, type, ric, null);
+ } else if (ric != null) {
+ result = policies.getForRic(ric);
+ result = filter(result, type, null, service);
+ } else {
+ result = policies.getAll();
+ }
+
+ return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
+ }
+
+ private boolean include(String filter, String value) {
+ return filter == null || value.equals(filter);
+ }
+
+ private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
+ if (type == null && ric == null && service == null) {
+ return collection;
+ }
+ Vector<Policy> filtered = new Vector<>();
+ for (Policy p : collection) {
+ if (include(type, p.type().name()) && include(ric, p.ric().name())
+ && include(service, p.ownerServiceName())) {
+ filtered.add(p);
+ }
+ }
+ return filtered;
+ }
+
+ private String toJson(Collection<Policy> policies) {
+ Vector<PolicyInfo> v = new Vector<>(policies.size());
+ for (Policy p : policies) {
+ PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
+ .json(p.json()) //
+ .name(p.id()) //
+ .ric(p.ric().name()) //
+ .type(p.type().name()).build();
+ v.add(policyInfo);
+ }
+ return gson.toJson(v);
}
@PutMapping(path = "/policy")
@RequestParam(name = "service", required = true) String service, //
@RequestBody String jsonBody) {
- System.out.println("*********************** " + jsonBody);
-
try {
Policy policy = ImmutablePolicy.builder() //
.id(instanceId) //
.ric(appConfig.getRic(ric)) //
.ownerServiceName(service) //
.build();
- policies.put(instanceId, policy);
+ policies.put(policy);
return new ResponseEntity<String>(HttpStatus.OK);
} catch (ServiceException e) {
- System.out.println("*********************** " + e.getMessage());
-
return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
}
-
- }
-
- @PutMapping(path = "/policyX")
- public ResponseEntity<String> putPolicyX( //
- // @RequestParam(name = "type", required = false) String type, //
- // @RequestParam(name = "instance", required = true) String instance, //
- // @RequestParam(name = "ric", required = true) String ric, //
- @RequestBody String jsonBody) {
- System.out.println("*********************** " + jsonBody);
- // System.out.println("policy" + type + instance);
- return new ResponseEntity<String>(HttpStatus.OK);
}
}