From 421a0843285f262b5cfc907ebd804ecd06d3fbe3 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 19 Jan 2018 19:55:02 +0000 Subject: Fix Tech Debt/JUnit on VFC POJOs Making fields private, fixing field and methods to follow Java guidelines, and adding getter and setter methods. Unit test added for all classes in org.onap.policy.vfc Issue-ID: POLICY-455 Change-Id: I262337a816706cd5243849cd51e57689275545bf Signed-off-by: liamfallon --- .../org/onap/policy/vfc/VFCHealActionVmInfo.java | 21 +- .../onap/policy/vfc/VFCHealAdditionalParams.java | 20 +- .../java/org/onap/policy/vfc/VFCHealRequest.java | 30 ++- .../main/java/org/onap/policy/vfc/VFCManager.java | 246 +++++++++++---------- .../main/java/org/onap/policy/vfc/VFCRequest.java | 30 ++- .../main/java/org/onap/policy/vfc/VFCResponse.java | 30 ++- .../org/onap/policy/vfc/VFCResponseDescriptor.java | 54 ++++- .../org/onap/policy/vfc/util/Serialization.java | 4 +- 8 files changed, 303 insertions(+), 132 deletions(-) (limited to 'controlloop/common/model-impl/vfc/src/main/java') diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealActionVmInfo.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealActionVmInfo.java index 144b97133..dd1ccfdd5 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealActionVmInfo.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealActionVmInfo.java @@ -27,13 +27,28 @@ public class VFCHealActionVmInfo implements Serializable { private static final long serialVersionUID = 3208673205100673119L; @SerializedName("vmid") - public String vmid; + private String vmid; @SerializedName("vmname") - public String vmname; - + private String vmname; public VFCHealActionVmInfo() { + // Default constructor for VFCHealActionVmInfo + } + + public String getVmid() { + return vmid; + } + + public void setVmid(String vmid) { + this.vmid = vmid; + } + + public String getVmname() { + return vmname; } + public void setVmname(String vmname) { + this.vmname = vmname; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealAdditionalParams.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealAdditionalParams.java index a4647cb9f..4ffdb5b31 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealAdditionalParams.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealAdditionalParams.java @@ -27,12 +27,28 @@ public class VFCHealAdditionalParams implements Serializable { private static final long serialVersionUID = 2656694137285096191L; @SerializedName("action") - public String action; + private String action; @SerializedName("actionvminfo") - public VFCHealActionVmInfo actionInfo; + private VFCHealActionVmInfo actionInfo; public VFCHealAdditionalParams() { + // Default constructor for VFCHealAdditionalParams } + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + public VFCHealActionVmInfo getActionInfo() { + return actionInfo; + } + + public void setActionInfo(VFCHealActionVmInfo actionInfo) { + this.actionInfo = actionInfo; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealRequest.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealRequest.java index a19f72bce..787e341de 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealRequest.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCHealRequest.java @@ -27,15 +27,39 @@ public class VFCHealRequest implements Serializable { private static final long serialVersionUID = -7341931593089709247L; @SerializedName("vnfInstanceId") - public String vnfInstanceId; + private String vnfInstanceId; @SerializedName("cause") - public String cause; + private String cause; @SerializedName("additionalParams") - public VFCHealAdditionalParams additionalParams; + private VFCHealAdditionalParams additionalParams; public VFCHealRequest() { + // Default constructor for VFCHealRequest } + public String getVnfInstanceId() { + return vnfInstanceId; + } + + public void setVnfInstanceId(String vnfInstanceId) { + this.vnfInstanceId = vnfInstanceId; + } + + public String getCause() { + return cause; + } + + public void setCause(String cause) { + this.cause = cause; + } + + public VFCHealAdditionalParams getAdditionalParams() { + return additionalParams; + } + + public void setAdditionalParams(VFCHealAdditionalParams additionalParams) { + this.additionalParams = additionalParams; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCManager.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCManager.java index c951984bd..7146e43ca 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCManager.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCManager.java @@ -32,116 +32,138 @@ import org.slf4j.LoggerFactory; import com.google.gson.JsonSyntaxException; public final class VFCManager implements Runnable { - - private String vfcUrlBase; - private String username; - private String password; - private VFCRequest vfcRequest; - WorkingMemory workingMem; - private static final Logger logger = LoggerFactory.getLogger(VFCManager.class); - private static final Logger netLogger = LoggerFactory.getLogger(org.onap.policy.drools.event.comm.Topic.NETWORK_LOGGER); - - public VFCManager(WorkingMemory wm, VFCRequest request) { - workingMem = wm; - vfcRequest = request; - - /* - * TODO: What if these are null? - */ - String url = PolicyEngine.manager.getEnvironmentProperty("vfc.url"); - String username = PolicyEngine.manager.getEnvironmentProperty("vfc.username"); - String password = PolicyEngine.manager.getEnvironmentProperty("vfc.password"); - - setVFCParams(url, username, password); - - } - - public void setVFCParams(String baseUrl, String name, String pwd) { - vfcUrlBase = baseUrl + "/api/nslcm/v1"; - username = name; - password = pwd; - } - - @Override - public void run() { - - Map headers = new HashMap(); - Pair httpDetails; - - VFCResponse responseError = new VFCResponse(); - responseError.responseDescriptor = new VFCResponseDescriptor(); - responseError.responseDescriptor.status = "error"; - - headers.put("Accept", "application/json"); - String vfcUrl = vfcUrlBase + "/ns/" + vfcRequest.nsInstanceId + "/heal"; - try { - String vfcRequestJson = Serialization.gsonPretty.toJson(vfcRequest); - netLogger.info("[OUT|{}|{}|]{}{}", "VFC", vfcUrl, System.lineSeparator(), vfcRequestJson); - - httpDetails = new RESTManager().post(vfcUrl, username, password, headers, - "application/json", vfcRequestJson); - } catch (Exception e) { - logger.error(e.getMessage(), e); - workingMem.insert(responseError); - return; - } - - if (httpDetails == null) { - workingMem.insert(responseError); - return; - } - - if (httpDetails.a == 202) { - try { - VFCResponse response = Serialization.gsonPretty.fromJson(httpDetails.b, VFCResponse.class); - netLogger.info("[IN|{}|{}|]{}{}", "VFC", vfcUrl, System.lineSeparator(), httpDetails.b); - String body = Serialization.gsonPretty.toJson(response); - logger.debug("Response to VFC Heal post:"); - logger.debug(body); - - String jobId = response.jobId; - int attemptsLeft = 20; - - String urlGet = vfcUrlBase + "/jobs/" + jobId; - VFCResponse responseGet = null; - - while (attemptsLeft-- > 0) { - - netLogger.info("[OUT|{}|{}|]", "VFC", urlGet); - Pair httpDetailsGet = new RESTManager().get(urlGet, username, password, headers); - responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.b, VFCResponse.class); - netLogger.info("[IN|{}|{}|]{}{}", "VFC", urlGet, System.lineSeparator(), httpDetailsGet.b); - responseGet.requestId = vfcRequest.requestId.toString(); - body = Serialization.gsonPretty.toJson(responseGet); - logger.debug("Response to VFC Heal get:"); - logger.debug(body); - - if (httpDetailsGet.a == 200) { - if (responseGet.responseDescriptor.status.equalsIgnoreCase("finished") || - responseGet.responseDescriptor.status.equalsIgnoreCase("error")) { - logger.debug("VFC Heal Status {}", responseGet.responseDescriptor.status); - workingMem.insert(responseGet); - break; - } - } - Thread.sleep(20000); - } - if ((attemptsLeft <= 0) - && (responseGet != null) - && (responseGet.responseDescriptor != null) - && (responseGet.responseDescriptor.status != null) - && (!responseGet.responseDescriptor.status.isEmpty())) { - logger.debug("VFC timeout. Status: ({})", responseGet.responseDescriptor.status); - workingMem.insert(responseGet); - } - } catch (JsonSyntaxException e) { - logger.error("Failed to deserialize into VFCResponse {}", e.getLocalizedMessage(), e); - } catch (InterruptedException e) { - logger.error("Interrupted exception: {}", e.getLocalizedMessage(), e); - Thread.currentThread().interrupt(); - } - } else { - logger.warn("VFC Heal Restcall failed"); - } - } + private static final String SYSTEM_LS = System.lineSeparator(); + + private String vfcUrlBase; + private String username; + private String password; + private VFCRequest vfcRequest; + private WorkingMemory workingMem; + private static final Logger logger = LoggerFactory.getLogger(VFCManager.class); + private static final Logger netLogger = LoggerFactory.getLogger(org.onap.policy.drools.event.comm.Topic.NETWORK_LOGGER); + + // The REST manager used for processing REST calls for this VFC manager + private RESTManager restManager; + + public VFCManager(WorkingMemory wm, VFCRequest request) { + if (wm == null || request == null) { + throw new IllegalArgumentException("the parameters \"wm\" and \"request\" on the VFCManager constructor may not be null"); + } + workingMem = wm; + vfcRequest = request; + + restManager = new RESTManager(); + + setVFCParams(getPEManagerEnvProperty("vfc.url"), getPEManagerEnvProperty("vfc.username"), getPEManagerEnvProperty("vfc.password")); + } + + public void setVFCParams(String baseUrl, String name, String pwd) { + vfcUrlBase = baseUrl + "/api/nslcm/v1"; + username = name; + password = pwd; + } + + @Override + public void run() { + Map headers = new HashMap<>(); + Pair httpDetails; + + VFCResponse responseError = new VFCResponse(); + responseError.setResponseDescriptor(new VFCResponseDescriptor()); + responseError.getResponseDescriptor().setStatus("error"); + + headers.put("Accept", "application/json"); + String vfcUrl = vfcUrlBase + "/ns/" + vfcRequest.getNSInstanceId() + "/heal"; + try { + String vfcRequestJson = Serialization.gsonPretty.toJson(vfcRequest); + netLogger.info("[OUT|{}|{}|]{}{}", "VFC", vfcUrl, SYSTEM_LS, vfcRequestJson); + + httpDetails = restManager.post(vfcUrl, username, password, headers, "application/json", vfcRequestJson); + } catch (Exception e) { + logger.error(e.getMessage(), e); + workingMem.insert(responseError); + return; + } + + if (httpDetails == null) { + workingMem.insert(responseError); + return; + } + + if (httpDetails.a != 202) { + logger.warn("VFC Heal Restcall failed"); + return; + } + + try { + VFCResponse response = Serialization.gsonPretty.fromJson(httpDetails.b, VFCResponse.class); + netLogger.info("[IN|{}|{}|]{}{}", "VFC", vfcUrl, SYSTEM_LS, httpDetails.b); + String body = Serialization.gsonPretty.toJson(response); + logger.debug("Response to VFC Heal post:"); + logger.debug(body); + + String jobId = response.getJobId(); + int attemptsLeft = 20; + + String urlGet = vfcUrlBase + "/jobs/" + jobId; + VFCResponse responseGet = null; + + while (attemptsLeft-- > 0) { + netLogger.info("[OUT|{}|{}|]", "VFC", urlGet); + Pair httpDetailsGet = restManager.get(urlGet, username, password, headers); + responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.b, VFCResponse.class); + netLogger.info("[IN|{}|{}|]{}{}", "VFC", urlGet, SYSTEM_LS, httpDetailsGet.b); + responseGet.setRequestId(vfcRequest.getRequestId().toString()); + body = Serialization.gsonPretty.toJson(responseGet); + logger.debug("Response to VFC Heal get:"); + logger.debug(body); + + String responseStatus = responseGet.getResponseDescriptor().getStatus(); + if (httpDetailsGet.a == 200 && (responseStatus.equalsIgnoreCase("finished") || responseStatus.equalsIgnoreCase("error"))) { + logger.debug("VFC Heal Status {}", responseGet.getResponseDescriptor().getStatus()); + workingMem.insert(responseGet); + break; + } + Thread.sleep(20000); + } + if ((attemptsLeft <= 0) + && (responseGet != null) + && (responseGet.getResponseDescriptor() != null) + && (responseGet.getResponseDescriptor().getStatus() != null) + && (!responseGet.getResponseDescriptor().getStatus().isEmpty())) { + logger.debug("VFC timeout. Status: ({})", responseGet.getResponseDescriptor().getStatus()); + workingMem.insert(responseGet); + } + } catch (JsonSyntaxException e) { + logger.error("Failed to deserialize into VFCResponse {}", e.getLocalizedMessage(), e); + } catch (InterruptedException e) { + logger.error("Interrupted exception: {}", e.getLocalizedMessage(), e); + Thread.currentThread().interrupt(); + } catch (Exception e) { + logger.error("Unknown error deserializing into VFCResponse {}", e.getLocalizedMessage(), e); + } + } + + /** + * Protected setter for rest manager to allow mocked rest manager to be used for testing + * @param restManager the test REST manager + */ + protected void setRestManager(final RESTManager restManager) { + this.restManager = restManager; + } + + /** + * This method reads and validates environmental properties coming from the policy engine. Null properties cause + * an {@link IllegalArgumentException} runtime exception to be thrown + * @param string the name of the parameter to retrieve + * @return the property value + */ + + private String getPEManagerEnvProperty(String enginePropertyName) { + String enginePropertyValue = PolicyEngine.manager.getEnvironmentProperty(enginePropertyName); + if (enginePropertyValue == null) { + throw new IllegalArgumentException("The value of policy engine manager environment property \"" + enginePropertyName + "\" may not be null"); + } + return enginePropertyValue; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCRequest.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCRequest.java index 89c9b08ff..78802d641 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCRequest.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCRequest.java @@ -27,13 +27,37 @@ public class VFCRequest implements Serializable { private static final long serialVersionUID = 3736300970326332512L; // These fields are not serialized and not part of JSON - public transient String nsInstanceId; - public transient UUID requestId; + private transient String nsInstanceId; + private transient UUID requestId; @SerializedName("healVnfData") - public VFCHealRequest healRequest; + private VFCHealRequest healRequest; public VFCRequest() { + // Default constructor for VFCRequest } + public String getNSInstanceId() { + return nsInstanceId; + } + + public void setNSInstanceId(String nsInstanceId) { + this.nsInstanceId = nsInstanceId; + } + + public UUID getRequestId() { + return requestId; + } + + public void setRequestId(UUID requestId) { + this.requestId = requestId; + } + + public VFCHealRequest getHealRequest() { + return healRequest; + } + + public void setHealRequest(VFCHealRequest healRequest) { + this.healRequest = healRequest; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponse.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponse.java index 5d6efa0ce..38b73b306 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponse.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponse.java @@ -27,14 +27,38 @@ public class VFCResponse implements Serializable { private static final long serialVersionUID = 9151443891238218455L; @SerializedName("jobId") - public String jobId; + private String jobId; @SerializedName("responseDescriptor") - public VFCResponseDescriptor responseDescriptor; + private VFCResponseDescriptor responseDescriptor; - public transient String requestId; + private transient String requestId; public VFCResponse() { + // Default constructor for VFCResponse } + public String getJobId() { + return jobId; + } + + public void setJobId(String jobId) { + this.jobId = jobId; + } + + public VFCResponseDescriptor getResponseDescriptor() { + return responseDescriptor; + } + + public void setResponseDescriptor(VFCResponseDescriptor responseDescriptor) { + this.responseDescriptor = responseDescriptor; + } + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponseDescriptor.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponseDescriptor.java index 62c61a7f6..81eb3bc4d 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponseDescriptor.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/VFCResponseDescriptor.java @@ -27,28 +27,72 @@ public class VFCResponseDescriptor implements Serializable { private static final long serialVersionUID = 6827782899144150158L; @SerializedName("progress") - String progress; + private String progress; @SerializedName("status") - String status; + private String status; @SerializedName("statusDescription") - String statusDescription; + private String statusDescription; @SerializedName("errorCode") - String errorCode; + private String errorCode; @SerializedName("responseId") - String responseId; + private String responseId; @SerializedName("responseHistoryList") public List responseHistoryList; public VFCResponseDescriptor() { + // Default constructor for VFCResponseDescriptor } public String getStatus() { return status; } + public String getProgress() { + return progress; + } + + public void setProgress(String progress) { + this.progress = progress; + } + + public String getStatusDescription() { + return statusDescription; + } + + public void setStatusDescription(String statusDescription) { + this.statusDescription = statusDescription; + } + + public String getErrorCode() { + return errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public String getResponseId() { + return responseId; + } + + public void setResponseId(String responseId) { + this.responseId = responseId; + } + + public List getResponseHistoryList() { + return responseHistoryList; + } + + public void setResponseHistoryList(List responseHistoryList) { + this.responseHistoryList = responseHistoryList; + } + + public void setStatus(String status) { + this.status = status; + } } diff --git a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/util/Serialization.java b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/util/Serialization.java index 655085288..dc8662b84 100644 --- a/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/util/Serialization.java +++ b/controlloop/common/model-impl/vfc/src/main/java/org/onap/policy/vfc/util/Serialization.java @@ -22,8 +22,10 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; public final class Serialization { + private Serialization() { + } - final static public Gson gsonPretty = new GsonBuilder().disableHtmlEscaping() + public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping() .setPrettyPrinting() .create(); -- cgit 1.2.3-korg