diff options
Diffstat (limited to 'models-interactions/model-actors/actor.vfc/src/main')
6 files changed, 491 insertions, 1 deletions
diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java new file mode 100644 index 000000000..2c5cf8eda --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/Restart.java @@ -0,0 +1,64 @@ +/*- + * ============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.vfc; + +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.vfc.VfcRequest; + +public class Restart extends VfcOperation { + public static final String NAME = "VF Module Create"; + + public Restart(ControlLoopOperationParams params, HttpConfig config) { + super(params, config); + } + + @Override + protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) { + + // starting a whole new attempt - reset the count + resetGetCount(); + + Pair<String, VfcRequest> pair = makeRequest(); + Entity<VfcRequest> entity = Entity.entity(pair.getRight(), MediaType.APPLICATION_JSON); + String path = getPath() + pair.getLeft(); + String url = getClient().getBaseUrl() + path; + + return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null)); + } + + /** + * Makes a request. + * + * @return a pair containing the request URL and the new request + */ + protected Pair<String, VfcRequest> makeRequest() { + + VfcRequest request = super.constructVfcRequest(); + String requestUrl = "/ns/" + request.getNsInstanceId() + "/heal"; + return Pair.of(requestUrl, request); + } +} diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java index 8d560ef3c..b7a41ddf0 100644 --- a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcActorServiceProvider.java @@ -37,6 +37,8 @@ import org.onap.policy.vfc.VfcRequest; public class VfcActorServiceProvider extends ActorImpl { private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id"; + // TODO old code: remove lines down to **HERE** + // Strings for VFC Actor private static final String VFC_ACTOR = "VFC"; @@ -50,10 +52,19 @@ public class VfcActorServiceProvider extends ActorImpl { private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build(); + // **HERE** + + /** + * Constructor. + */ public VfcActorServiceProvider() { super(VFC_ACTOR); + + addOperator(new VfcOperator(VFC_ACTOR, Restart.NAME, Restart::new)); } + // TODO old code: remove lines down to **HERE** + @Override public String actor() { return VFC_ACTOR; @@ -90,7 +101,7 @@ public class VfcActorServiceProvider extends ActorImpl { VfcRequest request = new VfcRequest(); String serviceInstance = onset.getAai().get("service-instance.service-instance-id"); if (serviceInstance == null || "".equals(serviceInstance)) { - // get service isntance from AaiCqResponse + // get service instance from AaiCqResponse if (aaiCqResponse == null) { return null; } @@ -120,4 +131,7 @@ public class VfcActorServiceProvider extends ActorImpl { } return request; } + + // **HERE** + } diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java new file mode 100644 index 000000000..4b5088a3a --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcConfig.java @@ -0,0 +1,63 @@ +/*- + * ============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.vfc; + +import java.util.concurrent.Executor; +import lombok.Getter; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; + +@Getter +public class VfcConfig extends HttpConfig { + + /** + * Path to use for the "get" request. A trailing "/" is added, if it is missing. + */ + private String pathGet; + + /** + * Maximum number of "get" requests permitted, after the initial request, to retrieve + * the response. + */ + private int maxGets; + + /** + * Time, in seconds, to wait between issuing "get" requests. + */ + private int waitSecGet; + + /** + * Constructor. + * + * @param blockingExecutor executor to be used for tasks that may perform blocking I/O + * @param params operator parameters + * @param clientFactory factory from which to obtain the {@link HttpClient} + */ + public VfcConfig(Executor blockingExecutor, VfcParams params, HttpClientFactory clientFactory) { + super(blockingExecutor, params, clientFactory); + + this.pathGet = params.getPathGet() + (params.getPathGet().endsWith("/") ? "" : "/"); + this.maxGets = params.getMaxGets(); + this.waitSecGet = params.getWaitSecGet(); + } + +} diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java new file mode 100644 index 000000000..827109d10 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperation.java @@ -0,0 +1,245 @@ +/*- + * ============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.vfc; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import javax.ws.rs.core.Response; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.vfc.VfcHealActionVmInfo; +import org.onap.policy.vfc.VfcHealAdditionalParams; +import org.onap.policy.vfc.VfcHealRequest; +import org.onap.policy.vfc.VfcRequest; +import org.onap.policy.vfc.VfcResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class VfcOperation extends HttpOperation<VfcResponse> { + private static final Logger logger = LoggerFactory.getLogger(VfcOperation.class); + + public static final String FAILED = "FAILED"; + public static final String COMPLETE = "COMPLETE"; + public static final int VFC_RESPONSE_CODE = 999; + public static final String GENERIC_VNF_ID = "generic-vnf.vnf-id"; + + public static final String REQ_PARAM_NM = "requestParameters"; + public static final String CONFIG_PARAM_NM = "configurationParameters"; + + private final VfcConfig config; + + /** + * Number of "get" requests issued so far, on the current operation attempt. + */ + @Getter + private int getCount; + + /** + * Constructs the object. + * + * @param params operation parameters + * @param config configuration for this operation + */ + public VfcOperation(ControlLoopOperationParams params, HttpConfig config) { + super(params, config, VfcResponse.class); + this.config = (VfcConfig) config; + } + + /** + * Subclasses should invoke this before issuing their first HTTP request. + */ + protected void resetGetCount() { + getCount = 0; + } + + /** + * Starts the GUARD. + */ + @Override + protected CompletableFuture<OperationOutcome> startPreprocessorAsync() { + return startGuardAsync(); + } + + /** + * If the response does not indicate that the request has been completed, then sleep a + * bit and issue a "get". + */ + @Override + protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url, + Response rawResponse, VfcResponse response) { + // Determine if the request has "completed" and determine if it was successful + if (rawResponse.getStatus() == 200) { + String requestState = getRequestState(response); + if ("finished".equalsIgnoreCase(requestState)) { + return CompletableFuture + .completedFuture(setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response)); + } + + if ("error".equalsIgnoreCase(requestState)) { + return CompletableFuture + .completedFuture(setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response)); + } + } + + // still incomplete + + // need a request ID with which to query + if (response == null || response.getJobId() == null) { + throw new IllegalArgumentException("missing job ID in response"); + } + + // see if the limit for the number of "gets" has been reached + if (getCount++ >= getMaxGets()) { + logger.warn("{}: execeeded 'get' limit {} for {}", getFullName(), getMaxGets(), params.getRequestId()); + setOutcome(outcome, PolicyResult.FAILURE_TIMEOUT); + outcome.setMessage(VFC_RESPONSE_CODE + " " + outcome.getMessage()); + return CompletableFuture.completedFuture(outcome); + } + + // sleep and then perform a "get" operation + Function<Void, CompletableFuture<OperationOutcome>> doGet = unused -> issueGet(outcome, response); + return sleep(getWaitMsGet(), TimeUnit.MILLISECONDS).thenComposeAsync(doGet); + } + + /** + * Issues a "get" request to see if the original request is complete yet. + * + * @param outcome outcome to be populated with the response + * @param response previous response + * @return a future that can be used to cancel the "get" request or await its response + */ + private CompletableFuture<OperationOutcome> issueGet(OperationOutcome outcome, VfcResponse response) { + String path = getPathGet() + response.getJobId(); + String url = getClient().getBaseUrl() + path; + + logger.debug("{}: 'get' count {} for {}", getFullName(), getCount, params.getRequestId()); + + logMessage(EventType.OUT, CommInfrastructure.REST, url, null); + + return handleResponse(outcome, url, callback -> getClient().get(callback, path, null)); + } + + /** + * Gets the request state of a response. + * + * @param response response from which to get the state + * @return the request state of the response, or {@code null} if it does not exist + */ + protected String getRequestState(VfcResponse response) { + if (response == null || response.getResponseDescriptor() == null + || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) { + return null; + } + return response.getResponseDescriptor().getStatus(); + } + + /** + * Treats everything as a success, so we always go into + * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}. + */ + @Override + protected boolean isSuccess(Response rawResponse, VfcResponse response) { + return true; + } + + /** + * Prepends the message with the http status code. + */ + @Override + public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse, + VfcResponse response) { + + // set default result and message + setOutcome(outcome, result); + + outcome.setMessage(rawResponse.getStatus() + " " + outcome.getMessage()); + return outcome; + } + + /** + * Construct VfcRequestObject from the ControlLoopOperationParams. + * + * @return request + */ + protected VfcRequest constructVfcRequest() { + ControlLoopEventContext context = params.getContext(); + String serviceInstance = context.getEnrichment().get("service-instance.service-instance-id"); + String vmId = context.getEnrichment().get("vserver.vserver-id"); + String vmName = context.getEnrichment().get("vserver.vserver-name"); + + if (StringUtils.isBlank(serviceInstance) || StringUtils.isBlank(vmId) || StringUtils.isBlank(vmName)) { + throw new IllegalArgumentException( + "Cannot extract enrichment data for service instance, server id, or server name."); + } + + VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo(); + vmActionInfo.setVmid(vmId); + vmActionInfo.setVmname(vmName); + + VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams(); + additionalParams.setAction(getName()); + additionalParams.setActionInfo(vmActionInfo); + + VfcHealRequest healRequest = new VfcHealRequest(); + healRequest.setVnfInstanceId(params.getContext().getEvent().getAai().get(GENERIC_VNF_ID)); + healRequest.setCause(getName()); + healRequest.setAdditionalParams(additionalParams); + + VfcRequest request = new VfcRequest(); + request.setHealRequest(healRequest); + request.setNsInstanceId(serviceInstance); + request.setRequestId(context.getEvent().getRequestId()); + + return request; + } + + // These may be overridden by jUnit tests + + /** + * Gets the wait time, in milliseconds, between "get" requests. + * + * @return the wait time, in milliseconds, between "get" requests + */ + public long getWaitMsGet() { + return TimeUnit.MILLISECONDS.convert(getWaitSecGet(), TimeUnit.SECONDS); + } + + public int getMaxGets() { + return config.getMaxGets(); + } + + public String getPathGet() { + return config.getPathGet(); + } + + public int getWaitSecGet() { + return config.getWaitSecGet(); + } +} diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java new file mode 100644 index 000000000..4e70d6550 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcOperator.java @@ -0,0 +1,49 @@ +/*- + * ============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.vfc; + +import java.util.Map; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; + +public class VfcOperator extends HttpOperator { + + public VfcOperator(String actorName, String name, + @SuppressWarnings("rawtypes") OperationMaker<HttpConfig, HttpOperation> operationMaker) { + super(actorName, name, operationMaker); + } + + @Override + protected HttpConfig makeConfiguration(Map<String, Object> parameters) { + VfcParams params = Util.translate(getFullName(), parameters, VfcParams.class); + ValidationResult result = params.validate(getFullName()); + if (!result.isValid()) { + throw new ParameterValidationRuntimeException("invalid parameters", result); + } + + return new VfcConfig(getBlockingExecutor(), params, getClientFactory()); + } +} diff --git a/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java new file mode 100644 index 000000000..31c2a9441 --- /dev/null +++ b/models-interactions/model-actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VfcParams.java @@ -0,0 +1,55 @@ +/*- + * ============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.vfc; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.SuperBuilder; +import org.onap.policy.common.parameters.annotations.Min; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; + +@NotNull +@NotBlank +@Data +@EqualsAndHashCode(callSuper = true) +@SuperBuilder(toBuilder = true) +public class VfcParams extends HttpParams { + + /** + * Path to use for the "get" request. + */ + private String pathGet; + + /** + * Maximum number of "get" requests permitted, after the initial request, to retrieve + * the response. + */ + @Min(0) + private int maxGets; + + /** + * Time, in seconds, to wait between issuing "get" requests. + */ + @Min(1) + private int waitSecGet; +} |