diff options
author | Jim Hahn <jrh3@att.com> | 2020-02-08 08:32:59 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-02-11 17:31:35 -0500 |
commit | 35867f2e63c26d47417bfefc9a0912f17c4a873a (patch) | |
tree | 77791874b4337725a0c109a2d1195ee0c4e5a3a6 /models-interactions/model-actors/actor.sdnc/src/main | |
parent | fd79f7920d454c35d6a8c02d430d9beba434dcc2 (diff) |
Add more code to facilitate actor implementation
Added obtain() to Context
Added startGuardAsync(), in anticipation of adding guards.
Moved logRestXxx() from Util to HttpOperation.
Added actor.test to facilitate testing of actors.
Changed timeoutSec from long to int in various places.
Made a couple of methods public to support junit testing.
Most of the methods required Params to be passed, which indicated a
design issue. Split Operator and Operation so that the Params could
be kept in a field and thus need not be passed to every method.
Basically, renamed OperatorPartial.java to OperationPartial.java and
created a new OperatorPartial.java. Of course, this makes it look to
gerrit like it's all new code, when in fact, most of it is unchanged,
other than removing the Params argument to the method calls. That
accounts for about half of the "lines changed" count.
Issue-ID: POLICY-1625
Change-Id: I9e98c9dadcbed145bf84deb06c9db1c864a3c24a
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-interactions/model-actors/actor.sdnc/src/main')
-rw-r--r-- | models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/BandwidthOnDemandOperation.java (renamed from models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/BandwidthOnDemandOperator.java) | 15 | ||||
-rw-r--r-- | models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/RerouteOperation.java (renamed from models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/RerouteOperator.java) | 15 | ||||
-rw-r--r-- | models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncActorServiceProvider.java | 8 | ||||
-rw-r--r-- | models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperation.java | 85 | ||||
-rw-r--r-- | models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperator.java | 148 |
5 files changed, 111 insertions, 160 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/BandwidthOnDemandOperation.java index 2927bd85b..26cdfada3 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/BandwidthOnDemandOperation.java @@ -23,6 +23,8 @@ package org.onap.policy.controlloop.actor.sdnc; import java.util.UUID; import org.apache.commons.lang3.StringUtils; 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.sdnc.SdncHealRequest; import org.onap.policy.sdnc.SdncHealRequestHeaderInfo; import org.onap.policy.sdnc.SdncHealRequestInfo; @@ -34,7 +36,7 @@ import org.onap.policy.sdnc.SdncHealVfModuleRequestInput; import org.onap.policy.sdnc.SdncHealVnfInfo; import org.onap.policy.sdnc.SdncRequest; -public class BandwidthOnDemandOperator extends SdncOperator { +public class BandwidthOnDemandOperation extends SdncOperation { public static final String NAME = "BandwidthOnDemand"; public static final String URI = "/GENERIC-RESOURCE-API:vf-module-topology-operation"; @@ -46,14 +48,17 @@ public class BandwidthOnDemandOperator extends SdncOperator { /** * Constructs the object. * - * @param actorName name of the actor with which this operator is associated + * @param params operation parameters + * @param operator operator that created this operation */ - public BandwidthOnDemandOperator(String actorName) { - super(actorName, NAME); + public BandwidthOnDemandOperation(ControlLoopOperationParams params, HttpOperator operator) { + super(params, operator); } @Override - protected SdncRequest constructRequest(ControlLoopEventContext context) { + protected SdncRequest makeRequest(int attempt) { + ControlLoopEventContext context = params.getContext(); + String serviceInstance = context.getEnrichment().get(SERVICE_ID_KEY); if (StringUtils.isBlank(serviceInstance)) { throw new IllegalArgumentException("missing enrichment data, " + SERVICE_ID_KEY); 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/RerouteOperation.java index da400f8eb..f255f3e84 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/RerouteOperation.java @@ -23,6 +23,8 @@ package org.onap.policy.controlloop.actor.sdnc; import java.util.UUID; import org.apache.commons.lang3.StringUtils; 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.sdnc.SdncHealNetworkInfo; import org.onap.policy.sdnc.SdncHealRequest; import org.onap.policy.sdnc.SdncHealRequestHeaderInfo; @@ -30,7 +32,7 @@ import org.onap.policy.sdnc.SdncHealRequestInfo; import org.onap.policy.sdnc.SdncHealServiceInfo; import org.onap.policy.sdnc.SdncRequest; -public class RerouteOperator extends SdncOperator { +public class RerouteOperation extends SdncOperation { public static final String NAME = "Reroute"; public static final String URI = "/GENERIC-RESOURCE-API:network-topology-operation"; @@ -42,14 +44,17 @@ public class RerouteOperator extends SdncOperator { /** * Constructs the object. * - * @param actorName name of the actor with which this operator is associated + * @param params operation parameters + * @param operator operator that created this operation */ - public RerouteOperator(String actorName) { - super(actorName, NAME); + public RerouteOperation(ControlLoopOperationParams params, HttpOperator operator) { + super(params, operator); } @Override - protected SdncRequest constructRequest(ControlLoopEventContext context) { + protected SdncRequest makeRequest(int attempt) { + ControlLoopEventContext context = params.getContext(); + String serviceInstance = context.getEnrichment().get(SERVICE_ID_KEY); if (StringUtils.isBlank(serviceInstance)) { throw new IllegalArgumentException("missing enrichment data, " + SERVICE_ID_KEY); 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 8dc8ba50d..99a4fdadd 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 @@ -30,6 +30,7 @@ import java.util.UUID; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.sdnc.SdncHealNetworkInfo; import org.onap.policy.sdnc.SdncHealRequest; @@ -76,8 +77,11 @@ public class SdncActorServiceProvider extends HttpActor { public SdncActorServiceProvider() { super(NAME); - addOperator(new RerouteOperator(NAME)); - addOperator(new BandwidthOnDemandOperator(NAME)); + addOperator(HttpOperator.makeOperator(NAME, RerouteOperation.NAME, + RerouteOperation::new)); + + addOperator(HttpOperator.makeOperator(NAME, BandwidthOnDemandOperation.NAME, + BandwidthOnDemandOperation::new)); } diff --git a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperation.java b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperation.java new file mode 100644 index 000000000..9d42c49d9 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperation.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actor.sdnc; + +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 org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.sdnc.SdncRequest; +import org.onap.policy.sdnc.SdncResponse; + +/** + * Superclass for SDNC Operators. + */ +public abstract class SdncOperation extends HttpOperation<SdncResponse> { + + /** + * Constructs the object. + * + * @param params operation parameters + * @param operator operator that created this operation + */ + public SdncOperation(ControlLoopOperationParams params, HttpOperator operator) { + super(params, operator, SdncResponse.class); + } + + @Override + protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) { + + SdncRequest request = makeRequest(attempt); + + Entity<SdncRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON); + + Map<String, Object> headers = makeHeaders(); + + headers.put("Accept", MediaType.APPLICATION_JSON); + String url = makeUrl(); + + logRestRequest(url, request); + + // @formatter:off + return handleResponse(outcome, url, + callback -> operator.getClient().post(callback, makePath(), entity, headers)); + // @formatter:on + } + + /** + * Makes the request. + * + * @param attempt current attempt, starting with "1" + * @return a new request to be posted + */ + protected abstract SdncRequest makeRequest(int attempt); + + /** + * Checks that the response has an "output" and that the output indicates success. + */ + @Override + protected boolean isSuccess(Response rawResponse, SdncResponse response) { + return response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode()); + } +} 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 deleted file mode 100644 index 479ee908d..000000000 --- a/models-interactions/model-actors/actor.sdnc/src/main/java/org/onap/policy/controlloop/actor/sdnc/SdncOperator.java +++ /dev/null @@ -1,148 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -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 org.onap.policy.common.endpoints.http.client.HttpClient; -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.controlloop.ControlLoopEventContext; -import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; -import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; -import org.onap.policy.controlloop.policy.PolicyResult; -import org.onap.policy.sdnc.SdncRequest; -import org.onap.policy.sdnc.SdncResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Superclass for SDNC Operators. - */ -public abstract class SdncOperator extends HttpOperator { - private static final Logger logger = LoggerFactory.getLogger(SdncOperator.class); - - /** - * Constructs the object. - * - * @param actorName name of the actor with which this operator is associated - * @param name operation name - */ - public SdncOperator(String actorName, String name) { - super(actorName, name); - } - - @Override - protected CompletableFuture<OperationOutcome> startOperationAsync(ControlLoopOperationParams params, int attempt, - OperationOutcome outcome) { - - SdncRequest request = constructRequest(params.getContext()); - return postRequest(params, outcome, request); - } - - /** - * Constructs the request. - * - * @param context associated event context - * @return a new request - */ - protected abstract SdncRequest constructRequest(ControlLoopEventContext context); - - /** - * 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 CompletableFuture<OperationOutcome> postRequest(ControlLoopOperationParams params, OperationOutcome outcome, - SdncRequest sdncRequest) { - Map<String, Object> headers = new HashMap<>(); - - headers.put("Accept", "application/json"); - String sdncUrl = getClient().getBaseUrl(); - - Util.logRestRequest(sdncUrl, sdncRequest); - - Entity<SdncRequest> entity = Entity.entity(sdncRequest, MediaType.APPLICATION_JSON); - - 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; - - public ResponseHandler(ControlLoopOperationParams params, OperationOutcome outcome, String sdncUrl) { - super(params, outcome); - this.sdncUrl = sdncUrl; - } - - /** - * 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); - } - } - - /** - * 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 - - protected StandardCoder makeDecoder() { - return new StandardCoder(); - } -} |