aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actor.so/src/main
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2020-02-18 12:25:37 -0500
committerJim Hahn <jrh3@att.com>2020-02-19 12:45:30 -0500
commitcbbc4a71ce71f22439b083107c65c22cc63bc9cc (patch)
tree5123d1b22ddd887d693b5007060cb3bdc7f0bad6 /models-interactions/model-actors/actor.so/src/main
parent4ccc26577b51545b4b4db6823c6d926bc0ffc5a4 (diff)
Add SO actor
Issue-ID: POLICY-2371 Signed-off-by: Jim Hahn <jrh3@att.com> Change-Id: I3faf0276e8039dc43a976d18ff8e704fdbec3d49
Diffstat (limited to 'models-interactions/model-actors/actor.so/src/main')
-rw-r--r--models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorParams.java56
-rw-r--r--models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java5
-rw-r--r--models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java340
-rw-r--r--models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperator.java90
-rw-r--r--models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoParams.java55
-rw-r--r--models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java186
6 files changed, 732 insertions, 0 deletions
diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorParams.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorParams.java
new file mode 100644
index 000000000..3e82fe1a6
--- /dev/null
+++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorParams.java
@@ -0,0 +1,56 @@
+/*-
+ * ============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.so;
+
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+import org.onap.policy.common.parameters.annotations.Min;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpActorParams;
+
+@Getter
+@Setter
+@EqualsAndHashCode(callSuper = true)
+public class SoActorParams extends HttpActorParams {
+
+ /*
+ * Optional, default values that are used if missing from the operation-specific
+ * parameters.
+ */
+
+ /**
+ * Path to use for the "get" request.
+ */
+ private String pathGet = "/orchestrationRequests/v5/";
+
+ /**
+ * Maximum number of "get" requests permitted, after the initial request, to retrieve
+ * the response.
+ */
+ @Min(0)
+ private int maxGets = 20;
+
+ /**
+ * Time, in seconds, to wait between issuing "get" requests.
+ */
+ @Min(1)
+ private int waitSecGet = 20;
+}
diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java
index 51d14a2c0..c7c6b00e9 100644
--- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java
+++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java
@@ -92,8 +92,13 @@ public class SoActorServiceProvider extends ActorImpl {
// **HERE**
+ /**
+ * Constructs the object.
+ */
public SoActorServiceProvider() {
super(NAME);
+
+ addOperator(SoOperator.makeSoOperator(NAME, VfModuleCreate.NAME, VfModuleCreate::new));
}
// TODO old code: remove lines down to **HERE**
diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java
new file mode 100644
index 000000000..510a737a6
--- /dev/null
+++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java
@@ -0,0 +1,340 @@
+/*-
+ * ============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.so;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+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.onap.aai.domain.yang.CloudRegion;
+import org.onap.aai.domain.yang.GenericVnf;
+import org.onap.aai.domain.yang.ServiceInstance;
+import org.onap.aai.domain.yang.Tenant;
+import org.onap.policy.aai.AaiCqResponse;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.policy.PolicyResult;
+import org.onap.policy.controlloop.policy.Target;
+import org.onap.policy.so.SoModelInfo;
+import org.onap.policy.so.SoRequest;
+import org.onap.policy.so.SoRequestInfo;
+import org.onap.policy.so.SoRequestParameters;
+import org.onap.policy.so.SoRequestStatus;
+import org.onap.policy.so.SoResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Superclass for SDNC Operators. Note: subclasses should invoke {@link #resetGetCount()}
+ * each time they issue an HTTP request.
+ */
+public abstract class SoOperation extends HttpOperation<SoResponse> {
+ private static final Logger logger = LoggerFactory.getLogger(SoOperation.class);
+ private static final Coder coder = new StandardCoder();
+
+ public static final String FAILED = "FAILED";
+ public static final String COMPLETE = "COMPLETE";
+ public static final int SO_RESPONSE_CODE = 999;
+
+ // fields within the policy payload
+ public static final String REQ_PARAM_NM = "requestParameters";
+ public static final String CONFIG_PARAM_NM = "configurationParameters";
+
+ @Getter
+ private final SoOperator operator;
+
+ /**
+ * Number of "get" requests issued so far, on the current operation attempt.
+ */
+ @Getter
+ private int getCount;
+
+
+ /**
+ * Constructs the object.
+ *
+ * @param params operation parameters
+ * @param operator operator that created this operation
+ */
+ public SoOperation(ControlLoopOperationParams params, SoOperator operator) {
+ super(params, operator, SoResponse.class);
+ this.operator = operator;
+ }
+
+ /**
+ * 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, SoResponse response) {
+
+ // see if the request has "completed", whether or not it was successful
+ if (rawResponse.getStatus() == 200) {
+ String requestState = getRequestState(response);
+ if (COMPLETE.equalsIgnoreCase(requestState)) {
+ return CompletableFuture
+ .completedFuture(setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response));
+ }
+
+ if (FAILED.equalsIgnoreCase(requestState)) {
+ return CompletableFuture
+ .completedFuture(setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
+ }
+ }
+
+ // still incomplete
+
+ // need a request ID with which to query
+ if (response.getRequestReferences() == null || response.getRequestReferences().getRequestId() == null) {
+ throw new IllegalArgumentException("missing request ID in response");
+ }
+
+ // see if the limit for the number of "gets" has been reached
+ if (getCount++ >= operator.getMaxGets()) {
+ logger.warn("{}: execeeded 'get' limit {} for {}", getFullName(), operator.getMaxGets(),
+ params.getRequestId());
+ setOutcome(outcome, PolicyResult.FAILURE_TIMEOUT);
+ outcome.setMessage(SO_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, SoResponse response) {
+ String path = operator.getPathGet() + response.getRequestReferences().getRequestId();
+ String url = operator.getClient().getBaseUrl() + path;
+
+ logger.debug("{}: 'get' count {} for {}", getFullName(), getCount, params.getRequestId());
+
+ logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
+
+ // TODO should this use "path" or the full "url"?
+ return handleResponse(outcome, url, callback -> operator.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(SoResponse response) {
+ SoRequest request = response.getRequest();
+ if (request == null) {
+ return null;
+ }
+
+ SoRequestStatus status = request.getRequestStatus();
+ if (status == null) {
+ return null;
+ }
+
+ return status.getRequestState();
+ }
+
+ /**
+ * Treats everything as a success, so we always go into
+ * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
+ */
+ @Override
+ protected boolean isSuccess(Response rawResponse, SoResponse response) {
+ return true;
+ }
+
+ /**
+ * Prepends the message with the http status code.
+ */
+ @Override
+ public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
+ SoResponse response) {
+
+ // set default result and message
+ setOutcome(outcome, result);
+
+ outcome.setMessage(rawResponse.getStatus() + " " + outcome.getMessage());
+ return outcome;
+ }
+
+ protected SoModelInfo prepareSoModelInfo() {
+ Target target = params.getTarget();
+ if (target == null) {
+ throw new IllegalArgumentException("missing Target");
+ }
+
+ if (target.getModelCustomizationId() == null || target.getModelInvariantId() == null
+ || target.getModelName() == null || target.getModelVersion() == null
+ || target.getModelVersionId() == null) {
+ throw new IllegalArgumentException("missing VF Module model");
+ }
+
+ SoModelInfo soModelInfo = new SoModelInfo();
+ soModelInfo.setModelCustomizationId(target.getModelCustomizationId());
+ soModelInfo.setModelInvariantId(target.getModelInvariantId());
+ soModelInfo.setModelName(target.getModelName());
+ soModelInfo.setModelVersion(target.getModelVersion());
+ soModelInfo.setModelVersionId(target.getModelVersionId());
+ soModelInfo.setModelType("vfModule");
+ return soModelInfo;
+ }
+
+ /**
+ * Construct requestInfo for the SO requestDetails.
+ *
+ * @return SO request information
+ */
+ protected SoRequestInfo constructRequestInfo() {
+ SoRequestInfo soRequestInfo = new SoRequestInfo();
+ soRequestInfo.setSource("POLICY");
+ soRequestInfo.setSuppressRollback(false);
+ soRequestInfo.setRequestorId("policy");
+ return soRequestInfo;
+ }
+
+ /**
+ * Builds the request parameters from the policy payload.
+ */
+ protected SoRequestParameters buildRequestParameters() {
+ if (params.getPayload() == null) {
+ return null;
+ }
+
+ String json = params.getPayload().get(REQ_PARAM_NM);
+ if (json == null) {
+ return null;
+ }
+
+ try {
+ return coder.decode(json, SoRequestParameters.class);
+ } catch (CoderException e) {
+ throw new IllegalArgumentException("invalid payload value: " + REQ_PARAM_NM);
+ }
+ }
+
+ /**
+ * Builds the configuration parameters from the policy payload.
+ */
+ protected List<Map<String, String>> buildConfigurationParameters() {
+ if (params.getPayload() == null) {
+ return null;
+ }
+
+ String json = params.getPayload().get(CONFIG_PARAM_NM);
+ if (json == null) {
+ return null;
+ }
+
+ try {
+ @SuppressWarnings("unchecked")
+ List<Map<String, String>> result = coder.decode(json, ArrayList.class);
+ return result;
+ } catch (CoderException | RuntimeException e) {
+ throw new IllegalArgumentException("invalid payload value: " + CONFIG_PARAM_NM);
+ }
+ }
+
+ /*
+ * These methods extract data from the Custom Query and throw an
+ * IllegalArgumentException if the desired data item is not found.
+ */
+
+ protected GenericVnf getVnfItem(AaiCqResponse aaiCqResponse, SoModelInfo soModelInfo) {
+ GenericVnf vnf = aaiCqResponse.getGenericVnfByVfModuleModelInvariantId(soModelInfo.getModelInvariantId());
+ if (vnf == null) {
+ throw new IllegalArgumentException("missing generic VNF");
+ }
+
+ return vnf;
+ }
+
+ protected ServiceInstance getServiceInstance(AaiCqResponse aaiCqResponse) {
+ ServiceInstance vnfService = aaiCqResponse.getServiceInstance();
+ if (vnfService == null) {
+ throw new IllegalArgumentException("missing VNF Service Item");
+ }
+
+ return vnfService;
+ }
+
+ protected Tenant getDefaultTenant(AaiCqResponse aaiCqResponse) {
+ Tenant tenant = aaiCqResponse.getDefaultTenant();
+ if (tenant == null) {
+ throw new IllegalArgumentException("missing Tenant Item");
+ }
+
+ return tenant;
+ }
+
+ protected CloudRegion getDefaultCloudRegion(AaiCqResponse aaiCqResponse) {
+ CloudRegion cloudRegion = aaiCqResponse.getDefaultCloudRegion();
+ if (cloudRegion == null) {
+ throw new IllegalArgumentException("missing Cloud Region");
+ }
+
+ return cloudRegion;
+ }
+
+ // 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(operator.getWaitSecGet(), TimeUnit.SECONDS);
+ }
+}
diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperator.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperator.java
new file mode 100644
index 000000000..011201f23
--- /dev/null
+++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperator.java
@@ -0,0 +1,90 @@
+/*-
+ * ============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.so;
+
+import java.util.Map;
+import java.util.function.BiFunction;
+import lombok.Getter;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.controlloop.actorserviceprovider.Operation;
+import org.onap.policy.controlloop.actorserviceprovider.Util;
+import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
+
+@Getter
+public abstract class SoOperator extends HttpOperator {
+
+ /**
+ * 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;
+
+
+ public SoOperator(String actorName, String name) {
+ super(actorName, name);
+ }
+
+ @Override
+ protected void doConfigure(Map<String, Object> parameters) {
+ SoParams params = Util.translate(getFullName(), parameters, SoParams.class);
+ ValidationResult result = params.validate(getFullName());
+ if (!result.isValid()) {
+ throw new ParameterValidationRuntimeException("invalid parameters", result);
+ }
+
+ this.pathGet = params.getPathGet() + (params.getPathGet().endsWith("/") ? "" : "/");
+ this.maxGets = params.getMaxGets();
+ this.waitSecGet = params.getWaitSecGet();
+
+ super.doConfigure(params);
+ }
+
+ /**
+ * Makes an operator that will construct operations.
+ *
+ * @param actorName actor name
+ * @param operation operation name
+ * @param operationMaker function to make an operation
+ * @return a new operator
+ */
+ public static SoOperator makeSoOperator(String actorName, String operation,
+ BiFunction<ControlLoopOperationParams, SoOperator, SoOperation> operationMaker) {
+
+ return new SoOperator(actorName, operation) {
+ @Override
+ public Operation buildOperation(ControlLoopOperationParams params) {
+ return operationMaker.apply(params, this);
+ }
+ };
+ }
+}
diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoParams.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoParams.java
new file mode 100644
index 000000000..53f6e932f
--- /dev/null
+++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoParams.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.so;
+
+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 SoParams 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;
+}
diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java
new file mode 100644
index 000000000..f356dcefc
--- /dev/null
+++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java
@@ -0,0 +1,186 @@
+/*-
+ * ============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.so;
+
+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.aai.domain.yang.CloudRegion;
+import org.onap.aai.domain.yang.GenericVnf;
+import org.onap.aai.domain.yang.ServiceInstance;
+import org.onap.aai.domain.yang.Tenant;
+import org.onap.policy.aai.AaiConstants;
+import org.onap.policy.aai.AaiCqResponse;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
+import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.so.SoCloudConfiguration;
+import org.onap.policy.so.SoModelInfo;
+import org.onap.policy.so.SoOperationType;
+import org.onap.policy.so.SoRelatedInstance;
+import org.onap.policy.so.SoRelatedInstanceListElement;
+import org.onap.policy.so.SoRequest;
+import org.onap.policy.so.SoRequestDetails;
+import org.onap.policy.so.SoRequestParameters;
+
+public class VfModuleCreate extends SoOperation {
+ public static final String NAME = "VF Module Create";
+
+ public VfModuleCreate(ControlLoopOperationParams params, SoOperator operator) {
+ super(params, operator);
+ }
+
+ /**
+ * Ensures that A&AI customer query has been performed, and then runs the guard query.
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+ ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
+ .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
+
+ // run Custom Query and Guard, in parallel
+ return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync);
+ }
+
+ @Override
+ protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
+
+ // starting a whole new attempt - reset the count
+ resetGetCount();
+
+ Pair<String, SoRequest> pair = makeRequest();
+ String path = pair.getLeft();
+ SoRequest request = pair.getRight();
+
+ Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
+ String url = getOperator().getClient().getBaseUrl() + path;
+
+ logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
+
+ // TODO should this use "path" or the full "url"?
+
+ return handleResponse(outcome, url, callback -> getOperator().getClient().post(callback, path, entity, null));
+ }
+
+ /**
+ * Makes a request.
+ *
+ * @return a pair containing the request URL and the new request
+ */
+ protected Pair<String, SoRequest> makeRequest() {
+ final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
+ final SoModelInfo soModelInfo = prepareSoModelInfo();
+ final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
+ final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
+ final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
+ final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
+
+ SoRequest request = new SoRequest();
+ request.setOperationType(SoOperationType.SCALE_OUT);
+
+ //
+ //
+ // Do NOT send SO the requestId, they do not support this field
+ //
+ request.setRequestDetails(new SoRequestDetails());
+ request.getRequestDetails().setRequestParameters(new SoRequestParameters());
+ request.getRequestDetails().getRequestParameters().setUserParams(null);
+
+ // cloudConfiguration
+ request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
+
+ // modelInfo
+ request.getRequestDetails().setModelInfo(soModelInfo);
+
+ // requestInfo
+ request.getRequestDetails().setRequestInfo(constructRequestInfo());
+ request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
+
+ // relatedInstanceList
+ SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
+ SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
+ relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
+ relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
+
+ // Service Item
+ relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
+ relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
+ relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
+ relatedInstanceListElement1.getRelatedInstance().getModelInfo()
+ .setModelInvariantId(vnfServiceItem.getModelInvariantId());
+ relatedInstanceListElement1.getRelatedInstance().getModelInfo()
+ .setModelVersionId(vnfServiceItem.getModelVersionId());
+ relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
+ aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
+ relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
+ aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
+
+ // VNF Item
+ relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
+ relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
+ relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
+ relatedInstanceListElement2.getRelatedInstance().getModelInfo()
+ .setModelInvariantId(vnfItem.getModelInvariantId());
+ relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
+
+ relatedInstanceListElement2.getRelatedInstance().getModelInfo()
+ .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
+ relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
+ aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
+
+ relatedInstanceListElement2.getRelatedInstance().getModelInfo()
+ .setModelCustomizationId(vnfItem.getModelCustomizationId());
+
+ // Insert the Service Item and VNF Item
+ request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
+ request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
+
+ // Request Parameters
+ request.getRequestDetails().setRequestParameters(buildRequestParameters());
+
+ // Configuration Parameters
+ request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters());
+
+ // compute the path
+ String path = "/serviceInstantiation/v7/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/"
+ + vnfItem.getVnfId() + "/vfModules/scaleOut";
+
+ return Pair.of(path, request);
+ }
+
+ /**
+ * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
+ * query.
+ *
+ * @param tenantItem tenant item from A&AI named-query response
+ * @return SO cloud configuration
+ */
+ private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
+ SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
+ cloudConfiguration.setTenantId(tenantItem.getTenantId());
+ cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
+ return cloudConfiguration;
+ }
+}