X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fportal%2Fnonrtric%2Fcontrolpanel%2Fpolicyagentapi%2FPolicyAgentApiImpl.java;h=44f060e77b9adf67c1242e2ff5bf2704ca9c608e;hb=6b4c6ef963e37ebdff26337cd90684e3e2ff2a6c;hp=30dcdc5121210c64e2f8596c810ec5c594442d91;hpb=c5abb3fc87ac95b29b212326158c93f97e8574a0;p=portal%2Fnonrtric-controlpanel.git diff --git a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java index 30dcdc5..44f060e 100644 --- a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java +++ b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java @@ -32,7 +32,6 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Map; import org.immutables.gson.Gson; import org.immutables.value.Value; @@ -41,95 +40,72 @@ import org.oransc.portal.nonrtric.controlpanel.model.PolicyInfo; import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances; import org.oransc.portal.nonrtric.controlpanel.model.PolicyType; import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes; +import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient; +import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.client.RestTemplate; @Component("PolicyAgentApi") public class PolicyAgentApiImpl implements PolicyAgentApi { private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - RestTemplate restTemplate; + private final AsyncRestClient webClient; private static com.google.gson.Gson gson = new GsonBuilder() // .serializeNulls() // .create(); // - private final String urlPrefix; - @Autowired public PolicyAgentApiImpl( @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) { - this(urlPrefix, new RestTemplate()); + this(new AsyncRestClient(urlPrefix)); logger.debug("ctor prefix '{}'", urlPrefix); } - public PolicyAgentApiImpl(String urlPrefix, RestTemplate restTemplate) { - this.urlPrefix = urlPrefix; - this.restTemplate = restTemplate; - } - - private String baseUrl() { - return urlPrefix; - } - - @Value.Immutable - @Gson.TypeAdapters - interface PolicyTypeInfo { - - public String name(); - - public String schema(); + public PolicyAgentApiImpl(AsyncRestClient webClient) { + this.webClient = webClient; } @Override public ResponseEntity getAllPolicyTypes() { + final String TITLE = "title"; try { - String url = baseUrl() + "/policy_schemas"; - ResponseEntity rsp = this.restTemplate.getForEntity(url, String.class); + final String url = "/policy_schemas"; + ResponseEntity rsp = webClient.getForEntity(url).block(); if (!rsp.getStatusCode().is2xxSuccessful()) { return rsp; } PolicyTypes result = new PolicyTypes(); - JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray(); for (JsonElement schema : schemas) { JsonObject schemaObj = schema.getAsJsonObject(); - if (schemaObj.get("title") != null) { - String title = schemaObj.get("title").getAsString(); - String schemaAsStr = schemaObj.toString(); - PolicyType pt = new PolicyType(title, schemaAsStr); - result.add(pt); - } else { - logger.warn("Ignoring schema: {}", schemaObj); + String title = ""; + if (schemaObj.get(TITLE) != null) { + title = schemaObj.get(TITLE).getAsString(); } + PolicyType pt = new PolicyType(title, schemaObj.toString()); + result.add(pt); } return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode()); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @Override public ResponseEntity getPolicyInstancesForType(String type) { - String url = baseUrl() + "/policies?type={type}"; - Map uriVariables = Map.of("type", type); - ResponseEntity rsp = this.restTemplate.getForEntity(url, String.class, uriVariables); - if (!rsp.getStatusCode().is2xxSuccessful()) { - return rsp; - } - try { + String url = "/policies?type=" + type; + ResponseEntity rsp = webClient.getForEntity(url).block(); + if (!rsp.getStatusCode().is2xxSuccessful()) { + return rsp; + } + Type listType = new TypeToken>() {}.getType(); List rspParsed = gson.fromJson(rsp.getBody(), listType); PolicyInstances result = new PolicyInstances(); @@ -138,47 +114,48 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { } return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode()); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @Override public ResponseEntity getPolicyInstance(String id) { - String url = baseUrl() + "/policy?id={id}"; - Map uriVariables = Map.of("id", id); - - return this.restTemplate.getForEntity(url, Object.class, uriVariables); + try { + String url = "/policy?id=" + id; + ResponseEntity rsp = webClient.getForEntity(url).block(); + JsonObject obj = JsonParser.parseString(rsp.getBody()).getAsJsonObject(); + String str = obj.toString(); + return new ResponseEntity<>(str, rsp.getStatusCode()); + } catch (Exception e) { + ResponseEntity rsp = ErrorResponseHandler.handleException(e); + return new ResponseEntity<>(rsp.getBody(), rsp.getStatusCode()); + } } @Override public ResponseEntity putPolicy(String policyTypeIdString, String policyInstanceId, Object json, String ric) { - String url = baseUrl() + "/policy?type={type}&id={id}&ric={ric}&service={service}"; - Map uriVariables = Map.of( // - "type", policyTypeIdString, // - "id", policyInstanceId, // - "ric", ric, // - "service", "controlpanel"); + String url = + "/policy?type=" + policyTypeIdString + "&id=" + policyInstanceId + "&ric=" + ric + "&service=controlpanel"; try { - this.restTemplate.put(url, createJsonHttpEntity(json), uriVariables); + String jsonStr = json.toString(); + webClient.putForEntity(url, jsonStr).block(); return new ResponseEntity<>(HttpStatus.OK); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @Override public ResponseEntity deletePolicy(String policyInstanceId) { - String url = baseUrl() + "/policy?id={id}"; - Map uriVariables = Map.of("id", policyInstanceId); + String url = "/policy?id=" + policyInstanceId; try { - this.restTemplate.delete(url, uriVariables); + webClient.deleteForEntity(url).block(); return new ResponseEntity<>(HttpStatus.OK); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } - } @Value.Immutable @@ -193,38 +170,20 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { @Override public ResponseEntity getRicsSupportingType(String typeName) { - String url = baseUrl() + "/rics?policyType={typeName}"; - Map uriVariables = Map.of("typeName", typeName); - String rsp = this.restTemplate.getForObject(url, String.class, uriVariables); - try { + String url = "/rics?policyType=" + typeName; + ResponseEntity rsp = webClient.getForEntity(url).block(); + Type listType = new TypeToken>() {}.getType(); - List rspParsed = gson.fromJson(rsp, listType); + List rspParsed = gson.fromJson(rsp.getBody(), listType); Collection result = new ArrayList<>(rspParsed.size()); for (RicInfo ric : rspParsed) { result.add(ric.ricName()); } - return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK); + String json = gson.toJson(result); + return new ResponseEntity<>(json, HttpStatus.OK); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } - - private HttpEntity createJsonHttpEntity(Object content) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - return new HttpEntity<>(content, headers); - } - - private ResponseEntity handleException(Exception throwable) { - if (throwable instanceof HttpClientErrorException) { - HttpClientErrorException e = (HttpClientErrorException) throwable; - return new ResponseEntity<>(e.getMessage(), e.getStatusCode()); - } else if (throwable instanceof HttpServerErrorException) { - HttpServerErrorException e = (HttpServerErrorException) throwable; - return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode()); - } - return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } - }