import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
+import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.oransc.policyagent.repository.Rics;
import org.oransc.policyagent.repository.Service;
import org.oransc.policyagent.repository.Services;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@Autowired
private Services services;
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static Gson gson = new GsonBuilder() //
.serializeNulls() //
.create(); //
@ApiResponse(code = 200, message = "Policy schemas", response = Object.class, responseContainer = "List"), //
@ApiResponse(code = 404, message = "RIC is not found", response = String.class)})
public ResponseEntity<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
- synchronized (this.policyTypes) {
- if (ricName == null) {
- Collection<PolicyType> types = this.policyTypes.getAll();
+ if (ricName == null) {
+ Collection<PolicyType> types = this.policyTypes.getAll();
+ return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
+ } else {
+ try {
+ Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
- } else {
- try {
- Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
- return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
- } catch (ServiceException e) {
- return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND);
- }
+ } catch (ServiceException e) {
+ return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND);
}
}
}
responseContainer = "List"),
@ApiResponse(code = 404, message = "RIC is not found", response = String.class)})
public ResponseEntity<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
- synchronized (this.policyTypes) {
- if (ricName == null) {
- Collection<PolicyType> types = this.policyTypes.getAll();
+ if (ricName == null) {
+ Collection<PolicyType> types = this.policyTypes.getAll();
+ return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK);
+ } else {
+ try {
+ Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK);
- } else {
- try {
- Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
- return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK);
- } catch (ServiceException e) {
- return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND);
- }
+ } catch (ServiceException e) {
+ return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND);
}
}
}
@ApiResponse(code = 404, message = "Policy is not found")} //
)
public ResponseEntity<String> getPolicy( //
- @RequestParam(name = "instance", required = true) String instance) {
+ @RequestParam(name = "id", required = true) String id) {
try {
- Policy p = policies.getPolicy(instance);
+ Policy p = policies.getPolicy(id);
return new ResponseEntity<>(p.json(), HttpStatus.OK);
} catch (ServiceException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
value = { //
@ApiResponse(code = 204, message = "Policy deleted", response = Object.class),
@ApiResponse(code = 404, message = "Policy is not found", response = String.class),
- @ApiResponse(code = 423, message = "RIC is locked", response = String.class)})
+ @ApiResponse(code = 423, message = "RIC is not operational", response = String.class)})
public Mono<ResponseEntity<Object>> deletePolicy( //
- @RequestParam(name = "instance", required = true) String id) {
- Policy policy;
+ @RequestParam(name = "id", required = true) String id) {
try {
- policy = policies.getPolicy(id);
+ Policy policy = policies.getPolicy(id);
keepServiceAlive(policy.ownerServiceName());
- if (policy.ric().getState() != Ric.RicState.IDLE) {
- return Mono.just(new ResponseEntity<>("Busy, synchronizing", HttpStatus.LOCKED));
- }
Ric ric = policy.ric();
- return ric.getLock().lock(LockType.SHARED) // //
- .flatMap(lock -> a1ClientFactory.createA1Client(policy.ric())) //
+ return ric.getLock().lock(LockType.SHARED) //
+ .flatMap(notUsed -> assertRicStateIdle(ric)) //
+ .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.ric())) //
.doOnNext(notUsed -> policies.remove(policy)) //
.flatMap(client -> client.deletePolicy(policy)) //
.doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
value = { //
@ApiResponse(code = 201, message = "Policy created", response = Object.class), //
@ApiResponse(code = 200, message = "Policy updated", response = Object.class), //
- @ApiResponse(code = 423, message = "RIC is locked", response = String.class), //
+ @ApiResponse(code = 423, message = "RIC is not operational", response = String.class), //
@ApiResponse(code = 404, message = "RIC or policy type is not found", response = String.class) //
})
public Mono<ResponseEntity<Object>> putPolicy( //
@RequestParam(name = "type", required = false, defaultValue = "") String typeName, //
- @RequestParam(name = "instance", required = true) String instanceId, //
+ @RequestParam(name = "id", required = true) String instanceId, //
@RequestParam(name = "ric", required = true) String ricName, //
@RequestParam(name = "service", required = true) String service, //
+ @RequestParam(name = "transient", required = false, defaultValue = "false") boolean isTransient, //
@RequestBody Object jsonBody) {
String jsonString = gson.toJson(jsonBody);
Ric ric = rics.get(ricName);
PolicyType type = policyTypes.get(typeName);
keepServiceAlive(service);
- 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();
-
- final boolean isCreate = this.policies.get(policy.id()) == null;
-
- return ric.getLock().lock(LockType.SHARED) //
- .flatMap(p -> validateModifiedPolicy(policy)) //
- .flatMap(notUsed -> a1ClientFactory.createA1Client(ric)) //
- .flatMap(client -> client.putPolicy(policy)) //
- .doOnNext(notUsed -> policies.put(policy)) //
- .doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
- .doOnError(t -> ric.getLock().unlockBlocking()) //
- .flatMap(notUsed -> Mono.just(new ResponseEntity<>(isCreate ? HttpStatus.CREATED : HttpStatus.OK))) //
- .onErrorResume(this::handleException);
+ if (ric == null || type == null) {
+ return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
-
- return ric == null || type == null ? Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND))
- : Mono.just(new ResponseEntity<>(HttpStatus.LOCKED)); // Synchronizing
+ Policy policy = ImmutablePolicy.builder() //
+ .id(instanceId) //
+ .json(jsonString) //
+ .type(type) //
+ .ric(ric) //
+ .ownerServiceName(service) //
+ .lastModified(getTimeStampUtc()) //
+ .isTransient(isTransient) //
+ .build();
+
+ final boolean isCreate = this.policies.get(policy.id()) == null;
+
+ return ric.getLock().lock(LockType.SHARED) //
+ .flatMap(notUsed -> assertRicStateIdle(ric)) //
+ .flatMap(notUsed -> checkSupportedType(ric, type)) //
+ .flatMap(notUsed -> validateModifiedPolicy(policy)) //
+ .flatMap(notUsed -> a1ClientFactory.createA1Client(ric)) //
+ .flatMap(client -> client.putPolicy(policy)) //
+ .doOnNext(notUsed -> policies.put(policy)) //
+ .doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
+ .doOnError(trowable -> ric.getLock().unlockBlocking()) //
+ .flatMap(notUsed -> Mono.just(new ResponseEntity<>(isCreate ? HttpStatus.CREATED : HttpStatus.OK))) //
+ .onErrorResume(this::handleException);
}
@SuppressWarnings({"unchecked"})
RejectionException e = new RejectionException("Policy cannot change RIC, policyId: " + current.id() + //
", RIC name: " + current.ric().name() + //
", new name: " + policy.ric().name(), HttpStatus.CONFLICT);
+ logger.debug("Request rejected, {}", e.getMessage());
+ return Mono.error(e);
+ }
+ return Mono.just("OK");
+ }
+
+ private Mono<Object> checkSupportedType(Ric ric, PolicyType type) {
+ if (!ric.isSupportingType(type.name())) {
+ logger.debug("Request rejected, type not supported, RIC: {}", ric);
+ RejectionException e = new RejectionException(
+ "Type: " + type.name() + " not supported by RIC: " + ric.name(), HttpStatus.NOT_FOUND);
return Mono.error(e);
}
return Mono.just("OK");
}
+ private Mono<Object> assertRicStateIdle(Ric ric) {
+ if (ric.getState() == Ric.RicState.AVAILABLE) {
+ return Mono.just("OK");
+ } else {
+ logger.debug("Request rejected RIC not IDLE, ric: {}", ric);
+ RejectionException e = new RejectionException(
+ "Ric is not operational, RIC name: " + ric.name() + ", state: " + ric.getState(), HttpStatus.LOCKED);
+ return Mono.error(e);
+ }
+ }
+
@GetMapping("/policies")
@ApiOperation(value = "Query policies")
@ApiResponses(
if ((ric != null && this.rics.get(ric) == null)) {
return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND);
}
- synchronized (policies) {
- String filteredPolicies = policiesToJson(filter(type, ric, service));
- return new ResponseEntity<>(filteredPolicies, HttpStatus.OK);
- }
+
+ String filteredPolicies = policiesToJson(filter(type, ric, service));
+ return new ResponseEntity<>(filteredPolicies, HttpStatus.OK);
}
@GetMapping("/policy_ids")
if ((ric != null && this.rics.get(ric) == null)) {
return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND);
}
- synchronized (policies) {
- String policyIdsJson = toPolicyIdsJson(filter(type, ric, service));
- return new ResponseEntity<>(policyIdsJson, HttpStatus.OK);
- }
+
+ String policyIdsJson = toPolicyIdsJson(filter(type, ric, service));
+ return new ResponseEntity<>(policyIdsJson, HttpStatus.OK);
}
@GetMapping("/policy_status")
@ApiResponse(code = 404, message = "Policy is not found", response = String.class)} //
)
public Mono<ResponseEntity<String>> getPolicyStatus( //
- @RequestParam(name = "instance", required = true) String instance) {
+ @RequestParam(name = "id", required = true) String id) {
try {
- Policy policy = policies.getPolicy(instance);
+ Policy policy = policies.getPolicy(id);
return a1ClientFactory.createA1Client(policy.ric()) //
.flatMap(client -> client.getPolicyStatus(policy)) //
}
private Collection<Policy> filter(String type, String ric, String service) {
- synchronized (policies) {
- if (type != null) {
- return filter(policies.getForType(type), null, ric, service);
- } else if (service != null) {
- return filter(policies.getForService(service), type, ric, null);
- } else if (ric != null) {
- return filter(policies.getForRic(ric), type, null, service);
- } else {
- return policies.getAll();
- }
+ if (type != null) {
+ return filter(policies.getForType(type), null, ric, service);
+ } else if (service != null) {
+ return filter(policies.getForService(service), type, ric, null);
+ } else if (ric != null) {
+ return filter(policies.getForRic(ric), type, null, service);
+ } else {
+ return policies.getAll();
}
}
policyInfo.service = p.ownerServiceName();
policyInfo.lastModified = p.lastModified();
if (!policyInfo.validate()) {
- throw new NullPointerException("BUG, all fields must be set");
+ logger.error("BUG, all fields must be set");
}
v.add(policyInfo);
}