diff options
Diffstat (limited to 'models-interactions/model-impl')
5 files changed, 109 insertions, 96 deletions
diff --git a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java index ca0c1867f..7024231e3 100644 --- a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java +++ b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java @@ -62,12 +62,17 @@ public class AaiCqResponse { properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); // Define JAXB context try { - jaxbContext = - JAXBContextFactory - .createContext( - new Class[] {Vserver.class, GenericVnf.class, VfModule.class, - CloudRegion.class, ServiceInstance.class, Tenant.class, ModelVer.class}, - properties); + // @formatter:off + jaxbContext = JAXBContextFactory.createContext(new Class[] { + Vserver.class, + GenericVnf.class, + VfModule.class, + CloudRegion.class, + ServiceInstance.class, + Tenant.class, + ModelVer.class + }, properties); + // @formatter:on unmarshaller = jaxbContext.createUnmarshaller(); } catch (JAXBException e) { LOGGER.error("Could not initialize JAXBContext", e); @@ -81,8 +86,7 @@ public class AaiCqResponse { /** * Constructor creates a custom query response from a valid json string. * - * @param jsonString - * A&AI Custom Query response JSON string + * @param jsonString A&AI Custom Query response JSON string */ public AaiCqResponse(String jsonString) { @@ -98,8 +102,8 @@ public class AaiCqResponse { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject("vserver").toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString())); // Getting the vserver pojo again from the json Vserver vserver = this.getAaiObject(json, Vserver.class); @@ -110,8 +114,8 @@ public class AaiCqResponse { if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString())); // Getting the generic vnf pojo again from the json GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class); @@ -124,8 +128,8 @@ public class AaiCqResponse { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject("service-instance").toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString())); // Getting the employee pojo again from the json ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class); @@ -137,8 +141,8 @@ public class AaiCqResponse { if (resultsArray.getJSONObject(i).has(VF_MODULE)) { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString())); // Getting the vf module pojo again from the json VfModule vfModule = this.getAaiObject(json, VfModule.class); @@ -150,8 +154,8 @@ public class AaiCqResponse { if (resultsArray.getJSONObject(i).has("cloud-region")) { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString())); // Getting the cloud region pojo again from the json CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class); @@ -163,8 +167,8 @@ public class AaiCqResponse { if (resultsArray.getJSONObject(i).has("tenant")) { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject("tenant").toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString())); // Getting the tenant pojo again from the json Tenant tenant = this.getAaiObject(json, Tenant.class); @@ -176,8 +180,8 @@ public class AaiCqResponse { if (resultsArray.getJSONObject(i).has("model-ver")) { // Create the StreamSource by creating StringReader using the // JSON input - StreamSource json = new StreamSource(new StringReader( - resultsArray.getJSONObject(i).getJSONObject("model-ver").toString())); + StreamSource json = new StreamSource( + new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString())); // Getting the ModelVer pojo again from the json ModelVer modelVer = this.getAaiObject(json, ModelVer.class); @@ -209,8 +213,7 @@ public class AaiCqResponse { /** * Get list of A&AI objects in the custom query. * - * @param classOfResponse - * Class of the type of A&AI objects to be returned + * @param classOfResponse Class of the type of A&AI objects to be returned * @return List A&AI objects matching the class */ @SuppressWarnings("unchecked") @@ -292,8 +295,7 @@ public class AaiCqResponse { /** * Returns a generic Vnf matching vnf name. * - * @param vnfName - * Name of the vnf to match + * @param vnfName Name of the vnf to match * @return generic Vnf */ public GenericVnf getGenericVnfByVnfName(String vnfName) { @@ -318,8 +320,7 @@ public class AaiCqResponse { /** * Returns a generic Vnf matching model invariant ID. * - * @param modelInvariantId - * Name of the vnf to match + * @param modelInvariantId Name of the vnf to match * @return generic Vnf */ public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) { @@ -344,8 +345,7 @@ public class AaiCqResponse { /** * Returns a generic Vnf of a given VF Module ID. * - * @param vfModuleModelInvariantId - * of the vf module for which vnf is to be returned + * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned * @return generic Vnf */ public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) { @@ -355,7 +355,7 @@ public class AaiCqResponse { // Iterate through all the vfModules of that generic Vnf for (VfModule vfMod : genVnf.getVfModules().getVfModule()) { if (vfMod.getModelInvariantId() != null - && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) { + && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) { return genVnf; } } @@ -502,8 +502,7 @@ public class AaiCqResponse { VfModule vfModule = null; for (VfModule vfMod : this.getAllVfModules()) { - if (vfMod.getModelInvariantId() != null - && vfModelInvariantId.equals(vfMod.getModelInvariantId())) { + if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) { vfModule = vfMod; } @@ -560,28 +559,25 @@ public class AaiCqResponse { } /** - * Get the count of vfModules matching customizationId, InvariantId and - * VersionId. + * Get the count of vfModules matching customizationId, InvariantId and VersionId. * - * @param custId - * ModelCustomizationId - * @param invId - * ModelInvariantId - * @param verId - * ModelVersionId + * @param custId ModelCustomizationId + * @param invId ModelInvariantId + * @param verId ModelVersionId * @return Returns the count of vf modules */ public int getVfModuleCount(String custId, String invId, String verId) { List<VfModule> vfModuleList = this.getAllVfModules(); int count = 0; for (VfModule vfModule : vfModuleList) { - if (vfModule.getModelCustomizationId() != null && vfModule.getModelInvariantId() != null - && vfModule.getModelVersionId() != null) { - if (vfModule.getModelCustomizationId().equals(custId) - && vfModule.getModelInvariantId().equals(invId) + if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null + || vfModule.getModelVersionId() == null) { + continue; + } + + if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId) && vfModule.getModelVersionId().equals(verId)) { - count = count + 1; - } + count = count + 1; } } return count; diff --git a/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/CdsResponse.java b/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/CdsResponse.java index fca1aa2e9..499d1a7c9 100644 --- a/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/CdsResponse.java +++ b/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/CdsResponse.java @@ -31,6 +31,7 @@ import lombok.ToString; @Setter @ToString public class CdsResponse implements Serializable { + private static final long serialVersionUID = 2590429952699969650L; private String requestId; private String status; diff --git a/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/api/CdsProcessorListener.java b/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/api/CdsProcessorListener.java index c07c559c4..fb4c6ed79 100644 --- a/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/api/CdsProcessorListener.java +++ b/models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/api/CdsProcessorListener.java @@ -26,7 +26,9 @@ import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOu * the received messages appropriately, it needs to implement {@link CdsProcessorListener}. * </p> * - * <p>Here is a sample implementation of a listener: + * <p> + * Here is a sample implementation of a listener: + * * <pre> * new CdsProcessorListener { * @@ -49,11 +51,10 @@ public interface CdsProcessorListener { /** * Implements the workflow upon receiving the message from the server side. * - * <p>Note that the CDS client-server communication is configured to use a streaming approach, which means when - * client - * sends an event, the server can reply with multiple sub-responses until full completion of the processing. Hence, - * it is up to the implementation of this method to process the received message using {@link - * ExecutionServiceOutput#getStatus()#getEventType()}</p> + * <p>Note that the CDS client-server communication is configured to use a streaming approach, which means when a + * client sends an event, the server can reply with multiple sub-responses until full completion of the processing. + * Hence, it is up to the implementation of this method to process the received message using the + * getStatus().getEventType() method of {@link ExecutionServiceOutput} * * @param message ExecutionServiceOutput received by the CDS grpc server */ diff --git a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java index 2ee47a9fc..643c629b3 100644 --- a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java +++ b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java @@ -41,9 +41,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RestManager { - private static final Logger logger = LoggerFactory.getLogger(RestManager.class); + // Constants for string literals + private static final String CONTENT_TYPE = "Content-Type"; + public class Pair<A, B> { public final A first; public final B second; @@ -69,7 +71,7 @@ public class RestManager { Map<String, String> headers, String contentType, String body) { HttpPut put = new HttpPut(url); addHeaders(put, username, password, headers); - put.addHeader("Content-Type", contentType); + put.addHeader(CONTENT_TYPE, contentType); try { StringEntity input = new StringEntity(body); input.setContentType(contentType); @@ -96,7 +98,7 @@ public class RestManager { Map<String, String> headers, String contentType, String body) { HttpPost post = new HttpPost(url); addHeaders(post, username, password, headers); - post.addHeader("Content-Type", contentType); + post.addHeader(CONTENT_TYPE, contentType); try { StringEntity input = new StringEntity(body); input.setContentType(contentType); @@ -141,7 +143,7 @@ public class RestManager { HttpDeleteWithBody delete = new HttpDeleteWithBody(url); addHeaders(delete, username, password, headers); if (body != null && !body.isEmpty()) { - delete.addHeader("Content-Type", contentType); + delete.addHeader(CONTENT_TYPE, contentType); try { StringEntity input = new StringEntity(body); input.setContentType(contentType); diff --git a/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java b/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java index 406e35d33..14382f0e9 100644 --- a/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java +++ b/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java @@ -125,45 +125,7 @@ public final class VfcManager implements Runnable { } try { - VfcResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, VfcResponse.class); - NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetails.second); - 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) { - NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", "VFC", urlGet); - Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers); - responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.second, VfcResponse.class); - NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetailsGet.second); - 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.first == 200 - && ("finished".equalsIgnoreCase(responseStatus) || "error".equalsIgnoreCase(responseStatus))) { - logger.debug("VFC Heal Status {}", responseGet.getResponseDescriptor().getStatus()); - this.callback.onResponse(responseGet); - break; - } - Thread.sleep(20000); - } - boolean isTimeout = (attemptsLeft <= 0) && (responseGet != null) - && (responseGet.getResponseDescriptor() != null); - isTimeout = isTimeout && (responseGet.getResponseDescriptor().getStatus() != null) - && (!responseGet.getResponseDescriptor().getStatus().isEmpty()); - if (isTimeout) { - logger.debug("VFC timeout. Status: ({})", responseGet.getResponseDescriptor().getStatus()); - this.callback.onResponse(responseGet); - } + handleVfcResponse(headers, httpDetails, vfcUrl); } catch (JsonSyntaxException e) { logger.error("Failed to deserialize into VfcResponse {}", e.getLocalizedMessage(), e); } catch (InterruptedException e) { @@ -175,6 +137,57 @@ public final class VfcManager implements Runnable { } /** + * Handle a VFC response message. + * + * @param headers the headers in the response + * @param httpDetails the HTTP details in the response + * @param vfcUrl the response URL + * @throws InterruptedException on errors in the response + */ + private void handleVfcResponse(Map<String, String> headers, Pair<Integer, String> httpDetails, String vfcUrl) + throws InterruptedException { + VfcResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, VfcResponse.class); + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetails.second); + 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) { + NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", "VFC", urlGet); + Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers); + responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.second, VfcResponse.class); + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetailsGet.second); + 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.first == 200 + && ("finished".equalsIgnoreCase(responseStatus) || "error".equalsIgnoreCase(responseStatus))) { + logger.debug("VFC Heal Status {}", responseGet.getResponseDescriptor().getStatus()); + this.callback.onResponse(responseGet); + return; + } + Thread.sleep(20000); + } + boolean isTimeout = (attemptsLeft <= 0) && (responseGet != null) + && (responseGet.getResponseDescriptor() != null); + isTimeout = isTimeout && (responseGet.getResponseDescriptor().getStatus() != null) + && (!responseGet.getResponseDescriptor().getStatus().isEmpty()); + if (isTimeout) { + logger.debug("VFC timeout. Status: ({})", responseGet.getResponseDescriptor().getStatus()); + this.callback.onResponse(responseGet); + } + } + + /** * Protected setter for rest manager to allow mocked rest manager to be used for testing. * * @param restManager the test REST manager |