import java.util.Collection;
import java.util.Vector;
+import org.oransc.policyagent.clients.A1ClientFactory;
import org.oransc.policyagent.configuration.ApplicationConfig;
import org.oransc.policyagent.exceptions.ServiceException;
import org.oransc.policyagent.repository.ImmutablePolicy;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Mono;
@RestController
@Api(value = "Policy Management API")
private final Rics rics;
private final PolicyTypes policyTypes;
private final Policies policies;
+ private final A1ClientFactory a1ClientFactory;
+
private static Gson gson = new GsonBuilder() //
.serializeNulls() //
.create(); //
@Autowired
- PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics) {
+ PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics,
+ A1ClientFactory a1ClientFactory) {
this.policyTypes = types;
this.policies = policies;
this.rics = rics;
+ this.a1ClientFactory = a1ClientFactory;
}
@GetMapping("/policy_schemas")
@ApiOperation(value = "Returns policy type schema definitions")
- @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")})
+ @ApiResponses(
+ value = {
+ @ApiResponse(code = 200, message = "Policy schemas", response = String.class, responseContainer = "List")})
public ResponseEntity<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
- if (ricName == null) {
- Collection<PolicyType> types = this.policyTypes.getAll();
- return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
- } else {
- try {
- Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
+ synchronized (this.policyTypes) {
+ if (ricName == null) {
+ Collection<PolicyType> types = this.policyTypes.getAll();
return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
- } catch (ServiceException e) {
- return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
+ } else {
+ try {
+ Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
+ return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
+ } catch (ServiceException e) {
+ return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
+ }
}
}
}
@GetMapping("/policy_schema")
@ApiOperation(value = "Returns one policy type schema definition")
- @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Type found")})
+ @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy schema", response = Object.class)})
public ResponseEntity<String> getPolicySchema(@RequestParam(name = "id", required = true) String id) {
try {
PolicyType type = policyTypes.getType(id);
@GetMapping("/policy_types")
@ApiOperation(value = "Returns policy types")
- @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")})
+ @ApiResponses(
+ value = {@ApiResponse(
+ code = 200,
+ message = "Policy type names",
+ response = String.class,
+ responseContainer = "List")})
public ResponseEntity<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
- if (ricName == null) {
- Collection<PolicyType> types = this.policyTypes.getAll();
- return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
- } else {
- try {
- Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
+ synchronized (this.policyTypes) {
+ if (ricName == null) {
+ Collection<PolicyType> types = this.policyTypes.getAll();
return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
- } catch (ServiceException e) {
- return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
+ } else {
+ try {
+ Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
+ return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
+ } catch (ServiceException e) {
+ return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
+ }
}
}
}
@GetMapping("/policy")
- @ApiOperation(value = "Returns the policy")
+ @ApiOperation(value = "Returns a policy configuration") //
@ApiResponses(
- value = {@ApiResponse(code = 200, message = "Policy found"),
- @ApiResponse(code = 204, message = "Policy is not found")})
+ value = { //
+ @ApiResponse(code = 200, message = "Policy found", response = Object.class), //
+ @ApiResponse(code = 204, message = "Policy is not found")} //
+ )
+
public ResponseEntity<String> getPolicy( //
@RequestParam(name = "instance", required = true) String instance) {
try {
}
@DeleteMapping("/policy")
- @ApiOperation(value = "Deletes the policy")
- @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted")})
- public ResponseEntity<Void> deletePolicy( //
- @RequestParam(name = "instance", required = true) String instance) {
+ @ApiOperation(value = "Deletes the policy", response = Object.class)
+ @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted", response = Object.class)})
+ public Mono<ResponseEntity<Void>> deletePolicy( //
+ @RequestParam(name = "instance", required = true) String id) {
+ Policy policy = policies.get(id);
+ if (policy != null && policy.ric().getState() == Ric.RicState.IDLE) {
+ policies.remove(policy);
+ return a1ClientFactory.createA1Client(policy.ric()) //
+ .flatMap(client -> client.deletePolicy(policy)) //
+ .flatMap(notUsed -> {
+ return Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT));
+ });
+ } else {
+ return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
+ }
+ }
- policies.removeId(instance);
- return new ResponseEntity<>(HttpStatus.NO_CONTENT);
+ @PutMapping(path = "/policy")
+ @ApiOperation(value = "Put a policy", response = String.class)
+ @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy created or updated")})
+ public Mono<ResponseEntity<String>> putPolicy( //
+ @RequestParam(name = "type", required = true) String typeName, //
+ @RequestParam(name = "instance", required = true) String instanceId, //
+ @RequestParam(name = "ric", required = true) String ricName, //
+ @RequestParam(name = "service", required = true) String service, //
+ @RequestBody Object jsonBody) {
+
+ String jsonString = gson.toJson(jsonBody);
+
+ Ric ric = rics.get(ricName);
+ PolicyType type = policyTypes.get(typeName);
+ if (ric != null && type != null && ric.getState() == Ric.RicState.IDLE) {
+ Policy policy = ImmutablePolicy.builder() //
+ .id(instanceId) //
+ .json(jsonString) //
+ .type(type) //
+ .ric(ric) //
+ .ownerServiceName(service) //
+ .lastModified(getTimeStampUTC()) //
+ .build();
+ return a1ClientFactory.createA1Client(ric) //
+ .flatMap(client -> client.putPolicy(policy)) //
+ .doOnNext(notUsed -> policies.put(policy)) //
+ .flatMap(notUsed -> {
+ return Mono.just(new ResponseEntity<>(HttpStatus.OK));
+ });
+ }
+ return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping("/policies")
@ApiOperation(value = "Returns the policies")
- @ApiResponses(value = {@ApiResponse(code = 200, message = "Policies found")})
+ @ApiResponses(
+ value = {
+ @ApiResponse(code = 200, message = "Policies", response = PolicyInfo.class, responseContainer = "List")})
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;
+ synchronized (policies) {
+ 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();
- }
+ 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>(policiesToJson(result), HttpStatus.OK);
+ return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
+ }
}
private boolean include(String filter, String value) {
private String policiesToJson(Collection<Policy> policies) {
Vector<PolicyInfo> v = new Vector<>(policies.size());
for (Policy p : policies) {
- PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
- .json(p.json()) //
- .id(p.id()) //
- .ric(p.ric().name()) //
- .type(p.type().name()) //
- .service(p.ownerServiceName()) //
- .lastModified(p.lastModified()) //
- .build();
+ PolicyInfo policyInfo = new PolicyInfo();
+ policyInfo.id = p.id();
+ policyInfo.json = p.json();
+ policyInfo.ric = p.ric().name();
+ policyInfo.type = p.type().name();
+ policyInfo.service = p.ownerServiceName();
+ policyInfo.lastModified = p.lastModified();
+ if (!policyInfo.validate()) {
+ throw new RuntimeException("BUG, all fields must be set");
+ }
v.add(policyInfo);
}
return gson.toJson(v);
return java.time.Instant.now().toString();
}
- @PutMapping(path = "/policy")
- @ApiOperation(value = "Create the policy")
- @ApiResponses(value = {@ApiResponse(code = 201, message = "Policy created")})
- public ResponseEntity<String> putPolicy( //
- @RequestParam(name = "type", required = true) String type, //
- @RequestParam(name = "instance", required = true) String instanceId, //
- @RequestParam(name = "ric", required = true) String ric, //
- @RequestParam(name = "service", required = true) String service, //
- @RequestBody String jsonBody) {
-
- try {
- // services.getService(service).ping();
- Ric ricObj = rics.getRic(ric);
- Policy policy = ImmutablePolicy.builder() //
- .id(instanceId) //
- .json(jsonBody) //
- .type(policyTypes.getType(type)) //
- .ric(ricObj) //
- .ownerServiceName(service) //
- .lastModified(getTimeStampUTC()) //
- .build();
- policies.put(policy);
- return new ResponseEntity<String>(HttpStatus.CREATED);
- } catch (ServiceException e) {
- return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
- }
- }
-
}