diff options
author | Jim Hahn <jrh3@att.com> | 2020-02-06 21:48:12 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-02-07 13:21:52 -0500 |
commit | e06578535f6afadac715c04ed03c74c05a075780 (patch) | |
tree | 1c9a0141daf15b93cb4f6452703d92cf8a6d751c /models-interactions/model-actors/actor.sdnc/src/main | |
parent | accad88260f99c1b5c5329285b73aa84349e623b (diff) |
Clean up and enhancement of Actor re-design
Added junits for the remaining code.
Enhancements to facilitate implementation of Operators:
- Added allOf(), anyOf() facilities
- Added AsyncResponseHandler for handling asynchronous I/O via the
HttpClient
- Added logRestRequest() and logRestResponse() for logging REST
requests and responses
- Added HttpActor and HttpOperator, which can be used as superclasses
- Added doTask()
- Lifted data from the event into ControlLoopEventContext
Updates per previous review comments:
- Changed logException() to runFunction().
- Removed the aaiCqResponse field.
- Lifted fields from Policy into ControlLoopOperationParams, eliminating
the need to include Policy in the class.
OperatorPartial depends on the string values in the ControlLoopOperation
being set to one of the string values of PolicyResult. Instead of
passing ControlLoopOperation around, the operators should pass around
an object that uses PolicyResult directly, rather than depending on
the string values being set correctly. Created OperationOutcome for
this purpose.
Stop pipeline when the controller completes.
Use whenComplete() where appropriate.
startOperationAsync() should not block. Modified it to launch the task
in the background via its own thread.
Extracted CallbackManager into its own file.
Replaced actor setOperators() with addOperator()
Renamed add() to wrap(), and modified it to remove the future when it
completes.
Fixed the signature on delayedRemove() and delayedComplete().
Replaced xxxAsync() calls with just xxx() calls, where appropriate to
avoid the extra overhead of submitting it to a work queue.
Renamed handleFailure() to handlePreprocessorFailure().
Updates per WIP review comments
Issue-ID: POLICY-1625
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: Id4c4c7ade979bdb76cc54266837609cc69a22c58
Diffstat (limited to 'models-interactions/model-actors/actor.sdnc/src/main')
4 files changed, 102 insertions, 103 deletions
diff --git a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/BandwidthOnDemandOperator.java b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/BandwidthOnDemandOperator.java index 0e721bf8c..2927bd85b 100644 --- a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/BandwidthOnDemandOperator.java +++ b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/BandwidthOnDemandOperator.java @@ -22,7 +22,7 @@ package org.onap.policy.controlloop.actor.sdnc; import java.util.UUID; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.sdnc.SdncHealRequest; import org.onap.policy.sdnc.SdncHealRequestHeaderInfo; import org.onap.policy.sdnc.SdncHealRequestInfo; @@ -37,6 +37,12 @@ import org.onap.policy.sdnc.SdncRequest; public class BandwidthOnDemandOperator extends SdncOperator { public static final String NAME = "BandwidthOnDemand"; + public static final String URI = "/GENERIC-RESOURCE-API:vf-module-topology-operation"; + + // fields in the enrichment data + public static final String SERVICE_ID_KEY = "service-instance.service-instance-id"; + public static final String VNF_ID = "vnfId"; + /** * Constructs the object. * @@ -47,19 +53,19 @@ public class BandwidthOnDemandOperator extends SdncOperator { } @Override - protected SdncRequest constructRequest(VirtualControlLoopEvent onset) { - String serviceInstance = onset.getAai().get("service-instance.service-instance-id"); + protected SdncRequest constructRequest(ControlLoopEventContext context) { + String serviceInstance = context.getEnrichment().get(SERVICE_ID_KEY); if (StringUtils.isBlank(serviceInstance)) { - throw new IllegalArgumentException("missing enrichment data, service-instance-id"); + throw new IllegalArgumentException("missing enrichment data, " + SERVICE_ID_KEY); } SdncHealVfModuleParameter bandwidth = new SdncHealVfModuleParameter(); bandwidth.setName("bandwidth"); - bandwidth.setValue(onset.getAai().get("bandwidth")); + bandwidth.setValue(context.getEnrichment().get("bandwidth")); SdncHealVfModuleParameter timeStamp = new SdncHealVfModuleParameter(); timeStamp.setName("bandwidth-change-time"); - timeStamp.setValue(onset.getAai().get("bandwidth-change-time")); + timeStamp.setValue(context.getEnrichment().get("bandwidth-change-time")); SdncHealVfModuleParametersInfo vfParametersInfo = new SdncHealVfModuleParametersInfo(); vfParametersInfo.addParameters(bandwidth); @@ -80,11 +86,11 @@ public class BandwidthOnDemandOperator extends SdncOperator { SdncRequest request = new SdncRequest(); request.setNsInstanceId(serviceInstance); - request.setRequestId(onset.getRequestId()); - request.setUrl("/GENERIC-RESOURCE-API:vf-module-topology-operation"); + request.setRequestId(context.getRequestId()); + request.setUrl(URI); SdncHealVnfInfo vnfInfo = new SdncHealVnfInfo(); - vnfInfo.setVnfId(onset.getAai().get("vnfId")); + vnfInfo.setVnfId(context.getEnrichment().get(VNF_ID)); SdncHealVfModuleInfo vfModuleInfo = new SdncHealVfModuleInfo(); vfModuleInfo.setVfModuleId(""); diff --git a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/RerouteOperator.java b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/RerouteOperator.java index 59af31f4f..da400f8eb 100644 --- a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/RerouteOperator.java +++ b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/RerouteOperator.java @@ -22,7 +22,7 @@ package org.onap.policy.controlloop.actor.sdnc; import java.util.UUID; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.sdnc.SdncHealNetworkInfo; import org.onap.policy.sdnc.SdncHealRequest; import org.onap.policy.sdnc.SdncHealRequestHeaderInfo; @@ -33,6 +33,12 @@ import org.onap.policy.sdnc.SdncRequest; public class RerouteOperator extends SdncOperator { public static final String NAME = "Reroute"; + public static final String URI = "/GENERIC-RESOURCE-API:network-topology-operation"; + + // fields in the enrichment data + public static final String SERVICE_ID_KEY = "service-instance.service-instance-id"; + public static final String NETWORK_ID_KEY = "network-information.network-id"; + /** * Constructs the object. * @@ -43,17 +49,17 @@ public class RerouteOperator extends SdncOperator { } @Override - protected SdncRequest constructRequest(VirtualControlLoopEvent onset) { - String serviceInstance = onset.getAai().get("service-instance.service-instance-id"); + protected SdncRequest constructRequest(ControlLoopEventContext context) { + String serviceInstance = context.getEnrichment().get(SERVICE_ID_KEY); if (StringUtils.isBlank(serviceInstance)) { - throw new IllegalArgumentException("missing enrichment data, service-instance-id"); + throw new IllegalArgumentException("missing enrichment data, " + SERVICE_ID_KEY); } SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo(); serviceInfo.setServiceInstanceId(serviceInstance); - String networkId = onset.getAai().get("network-information.network-id"); + String networkId = context.getEnrichment().get(NETWORK_ID_KEY); if (StringUtils.isBlank(networkId)) { - throw new IllegalArgumentException("missing enrichment data, network-id"); + throw new IllegalArgumentException("missing enrichment data, " + NETWORK_ID_KEY); } SdncHealNetworkInfo networkInfo = new SdncHealNetworkInfo(); networkInfo.setNetworkId(networkId); @@ -67,8 +73,8 @@ public class RerouteOperator extends SdncOperator { SdncRequest request = new SdncRequest(); request.setNsInstanceId(serviceInstance); - request.setRequestId(onset.getRequestId()); - request.setUrl("/GENERIC-RESOURCE-API:network-topology-operation"); + request.setRequestId(context.getRequestId()); + request.setUrl(URI); SdncHealRequest healRequest = new SdncHealRequest(); healRequest.setRequestHeaderInfo(headerInfo); diff --git a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncActorServiceProvider.java b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncActorServiceProvider.java index 13276f929..8dc8ba50d 100644 --- a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncActorServiceProvider.java +++ b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncActorServiceProvider.java @@ -26,14 +26,10 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.UUID; -import java.util.function.Function; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.actorserviceprovider.Util; -import org.onap.policy.controlloop.actorserviceprovider.impl.ActorImpl; -import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpActorParams; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.sdnc.SdncHealNetworkInfo; import org.onap.policy.sdnc.SdncHealRequest; @@ -49,7 +45,7 @@ import org.onap.policy.sdnc.SdncRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SdncActorServiceProvider extends ActorImpl { +public class SdncActorServiceProvider extends HttpActor { private static final Logger logger = LoggerFactory.getLogger(SdncActorServiceProvider.class); public static final String NAME = "SDNC"; @@ -78,23 +74,10 @@ public class SdncActorServiceProvider extends ActorImpl { * Constructs the object. */ public SdncActorServiceProvider() { - // @formatter:off - super(NAME, - new RerouteOperator(NAME), - new BandwidthOnDemandOperator(NAME)); + super(NAME); - // @formatter:on - } - - @Override - protected Function<String, Map<String, Object>> makeOperatorParameters(Map<String, Object> actorParameters) { - String actorName = getName(); - - // @formatter:off - return Util.translate(actorName, actorParameters, HttpActorParams.class) - .doValidation(actorName) - .makeOperationParameters(actorName); - // @formatter:on + addOperator(new RerouteOperator(NAME)); + addOperator(new BandwidthOnDemandOperator(NAME)); } diff --git a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperator.java b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperator.java index c2e4c8f8b..479ee908d 100644 --- a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperator.java +++ b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperator.java @@ -22,45 +22,31 @@ package org.onap.policy.controlloop.actor.sdnc; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import lombok.AccessLevel; -import lombok.Getter; -import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; -import org.onap.policy.common.endpoints.utils.NetLoggerUtil; -import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; -import org.onap.policy.common.parameters.ValidationResult; -import org.onap.policy.controlloop.ControlLoopOperation; -import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.controlloop.actorserviceprovider.AsyncResponseHandler; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.Util; -import org.onap.policy.controlloop.actorserviceprovider.impl.OperatorPartial; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; -import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; -import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.sdnc.SdncRequest; import org.onap.policy.sdnc.SdncResponse; -import org.onap.policy.sdnc.util.Serialization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Superclass for SDNC Operators. */ -public abstract class SdncOperator extends OperatorPartial { +public abstract class SdncOperator extends HttpOperator { private static final Logger logger = LoggerFactory.getLogger(SdncOperator.class); - @Getter(AccessLevel.PROTECTED) - private HttpClient client; - - /** - * URI path for this particular operation. - */ - private String path; - /** * Constructs the object. * @@ -71,74 +57,92 @@ public abstract class SdncOperator extends OperatorPartial { super(actorName, name); } - // TODO add a junit for this and for plug-in via ActorService @Override - protected void doConfigure(Map<String, Object> parameters) { - HttpParams params = Util.translate(getFullName(), parameters, HttpParams.class); - ValidationResult result = params.validate(getFullName()); - if (!result.isValid()) { - throw new ParameterValidationRuntimeException("invalid parameters", result); - } + protected CompletableFuture<OperationOutcome> startOperationAsync(ControlLoopOperationParams params, int attempt, + OperationOutcome outcome) { - client = HttpClientFactoryInstance.getClientFactory().get(params.getClientName()); - path = params.getPath(); - } - - @Override - protected ControlLoopOperation doOperation(ControlLoopOperationParams params, int attempt, - ControlLoopOperation operation) { - - SdncRequest request = constructRequest(params.getContext().getEvent()); - PolicyResult result = doRequest(request); - - return setOutcome(params, operation, result); + SdncRequest request = constructRequest(params.getContext()); + return postRequest(params, outcome, request); } /** * Constructs the request. * - * @param onset event for which the request should be constructed + * @param context associated event context * @return a new request */ - protected abstract SdncRequest constructRequest(VirtualControlLoopEvent onset); + protected abstract SdncRequest constructRequest(ControlLoopEventContext context); /** - * Posts the request and retrieves the response. + * Posts the request and and arranges to retrieve the response. * + * @param params operation parameters + * @param outcome updated with the response * @param sdncRequest request to be posted * @return the result of the request */ - private PolicyResult doRequest(SdncRequest sdncRequest) { + private CompletableFuture<OperationOutcome> postRequest(ControlLoopOperationParams params, OperationOutcome outcome, + SdncRequest sdncRequest) { Map<String, Object> headers = new HashMap<>(); headers.put("Accept", "application/json"); - String sdncUrl = client.getBaseUrl(); - - String sdncRequestJson = Serialization.gsonPretty.toJson(sdncRequest); + String sdncUrl = getClient().getBaseUrl(); - // TODO move this into a utility - NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, sdncUrl, sdncRequestJson); - logger.info("[OUT|{}|{}|]{}{}", CommInfrastructure.REST, sdncUrl, NetLoggerUtil.SYSTEM_LS, sdncRequestJson); + Util.logRestRequest(sdncUrl, sdncRequest); Entity<SdncRequest> entity = Entity.entity(sdncRequest, MediaType.APPLICATION_JSON); - // TODO modify this to use asynchronous client operations - Response rawResponse = client.post(path, entity, headers); - String strResponse = HttpClient.getBody(rawResponse, String.class); + ResponseHandler handler = new ResponseHandler(params, outcome, sdncUrl); + return handler.handle(getClient().post(handler, getPath(), entity, headers)); + } + + private class ResponseHandler extends AsyncResponseHandler<Response> { + private final String sdncUrl; - // TODO move this into a utility - NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, sdncUrl, strResponse); - logger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, NetLoggerUtil.SYSTEM_LS, strResponse); - logger.info("Response to Sdnc Heal post:"); - logger.info(strResponse); + public ResponseHandler(ControlLoopOperationParams params, OperationOutcome outcome, String sdncUrl) { + super(params, outcome); + this.sdncUrl = sdncUrl; + } - SdncResponse response = Serialization.gsonPretty.fromJson(strResponse, SdncResponse.class); + /** + * Handles the response. + */ + @Override + protected OperationOutcome doComplete(Response rawResponse) { + String strResponse = HttpClient.getBody(rawResponse, String.class); + + Util.logRestResponse(sdncUrl, strResponse); + + SdncResponse response; + try { + response = makeDecoder().decode(strResponse, SdncResponse.class); + } catch (CoderException e) { + logger.warn("Sdnc Heal cannot decode response with http error code {}", rawResponse.getStatus(), e); + return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.FAILURE_EXCEPTION); + } + + if (response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode())) { + return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.SUCCESS); + + } else { + logger.info("Sdnc Heal Restcall failed with http error code {}", rawResponse.getStatus()); + return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.FAILURE); + } + } - if (response.getResponseOutput() == null || !"200".equals(response.getResponseOutput().getResponseCode())) { - logger.info("Sdnc Heal Restcall failed with http error code {}", rawResponse.getStatus()); - return PolicyResult.FAILURE; + /** + * Handles exceptions. + */ + @Override + protected OperationOutcome doFailed(Throwable thrown) { + logger.info("Sdnc Heal Restcall threw an exception", thrown); + return SdncOperator.this.setOutcome(getParams(), getOutcome(), PolicyResult.FAILURE_EXCEPTION); } + } + + // these may be overridden by junit tests - return PolicyResult.SUCCESS; + protected StandardCoder makeDecoder() { + return new StandardCoder(); } } |