diff options
author | Jim Hahn <jrh3@att.com> | 2020-02-12 13:01:57 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-02-12 14:29:21 -0500 |
commit | 3b4884e5688a71116b3935e3a4ad243ab6287c80 (patch) | |
tree | dfd320bef72050eabfb43e449cf644d4b3824b54 /models-interactions/model-actors/actor.aai/src/main | |
parent | 35867f2e63c26d47417bfefc9a0912f17c4a873a (diff) |
Add A&AI actor and some operators
Added A&AI Actor, as well as operators for the "custom query" and
the "tenant" query (on which the custom query depends).
Issue-ID: POLICY-1625
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: Ic9dadc07a6057cb1abb9698da86eb9431fc9ed3e
Diffstat (limited to 'models-interactions/model-actors/actor.aai/src/main')
5 files changed, 358 insertions, 0 deletions
diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProvider.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProvider.java new file mode 100644 index 000000000..df427c32c --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProvider.java @@ -0,0 +1,47 @@ +/*- + * ============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.aai; + +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; + +/** + * A&AI Actor. + */ +public class AaiActorServiceProvider extends HttpActor { + public static final String NAME = AaiConstants.ACTOR_NAME; + + /** + * Constructs the object. + */ + public AaiActorServiceProvider() { + super(NAME); + + addOperator(HttpOperator.makeOperator(NAME, AaiCustomQueryOperation.NAME, + AaiCustomQueryOperation::new)); + + // add all "get" operators + for (String operation : AaiGetOperation.OPERATIONS) { + addOperator(HttpOperator.makeOperator(NAME, operation, AaiGetOperation::new)); + } + } +} diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java new file mode 100644 index 000000000..5f432704c --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperation.java @@ -0,0 +1,127 @@ +/*- + * ============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.aai; + +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.aai.AaiConstants; +import org.onap.policy.aai.AaiCqResponse; +import org.onap.policy.common.utils.coder.StandardCoderObject; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A&AI Custom Query. Stores the {@link AaiCqResponse} in the context. In addition, if the + * context does not contain the "tenant" data for the vserver, then it will request that, + * as well. + */ +public class AaiCustomQueryOperation extends HttpOperation<String> { + private static final Logger logger = LoggerFactory.getLogger(AaiCustomQueryOperation.class); + + public static final String NAME = "CustomQuery"; + + public static final String RESOURCE_LINK = "resource-link"; + public static final String RESULT_DATA = "result-data"; + + private static final String PREFIX = "/aai/v16"; + + /** + * Constructs the object. + * + * @param params operation parameters + * @param operator operator that created this operation + */ + public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpOperator operator) { + super(params, operator, String.class); + } + + /** + * Queries the vserver, if necessary. + */ + @Override + protected CompletableFuture<OperationOutcome> startPreprocessorAsync() { + String vserver = params.getTargetEntity(); + + ControlLoopOperationParams tenantParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME) + .operation(AaiGetOperation.TENANT).payload(null).retry(null).timeoutSec(null).build(); + + return params.getContext().obtain(AaiGetOperation.getTenantKey(vserver), tenantParams); + } + + @Override + protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) { + + Map<String, String> request = makeRequest(); + + Entity<Map<String, String>> 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().put(callback, makePath(), entity, headers)); + // @formatter:on + } + + /** + * Constructs the custom query using the previously retrieved tenant data. + */ + private Map<String, String> makeRequest() { + String vserver = params.getTargetEntity(); + StandardCoderObject tenant = params.getContext().getProperty(AaiGetOperation.getTenantKey(vserver)); + + String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK); + if (resourceLink == null) { + throw new IllegalArgumentException("cannot perform custom query - no resource-link"); + } + + resourceLink = resourceLink.replace(PREFIX, ""); + + return Map.of("start", resourceLink, "query", "query/closed-loop"); + } + + @Override + protected Map<String, Object> makeHeaders() { + return AaiUtil.makeHeaders(params); + } + + /** + * Injects the response into the context. + */ + @Override + protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, String response) { + + logger.info("{}: caching response for {}", getFullName(), params.getRequestId()); + params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response)); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java new file mode 100644 index 000000000..3bc359af2 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetOperation.java @@ -0,0 +1,133 @@ +/*- + * ============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.aai; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.common.utils.coder.StandardCoderObject; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Superclass of A&AI operators that use "get" to perform their request and store their + * response within the context as a {@link StandardCoderObject}. The property name under + * which they are stored is ${actor}.${operation}.${targetEntity}. + */ +public class AaiGetOperation extends HttpOperation<StandardCoderObject> { + private static final Logger logger = LoggerFactory.getLogger(AaiGetOperation.class); + + public static final int DEFAULT_RETRY = 3; + + // operation names + public static final String TENANT = "Tenant"; + + // property prefixes + private static final String TENANT_KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + TENANT + "."; + + /** + * Operation names supported by this operator. + */ + public static final Set<String> OPERATIONS = Set.of(TENANT); + + + /** + * Responses that are retrieved from A&AI are placed in the operation context under + * the name "${propertyPrefix}.${targetEntity}". + */ + private final String propertyPrefix; + + /** + * Constructs the object. + * + * @param params operation parameters + * @param operator operator that created this operation + */ + public AaiGetOperation(ControlLoopOperationParams params, HttpOperator operator) { + super(params, operator, StandardCoderObject.class); + this.propertyPrefix = operator.getFullName() + "."; + } + + /** + * Gets the "context key" for the tenant query response associated with the given + * target entity. + * + * @param targetEntity target entity + * @return the "context key" for the response associated with the given target + */ + public static String getTenantKey(String targetEntity) { + return (TENANT_KEY_PREFIX + targetEntity); + } + + @Override + protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) { + + Map<String, Object> headers = makeHeaders(); + + headers.put("Accept", MediaType.APPLICATION_JSON); + String url = makeUrl(); + + logRestRequest(url, null); + + // @formatter:off + return handleResponse(outcome, url, + callback -> operator.getClient().get(callback, makePath(), headers)); + // @formatter:on + } + + @Override + protected Map<String, Object> makeHeaders() { + return AaiUtil.makeHeaders(params); + } + + @Override + public String makePath() { + return (operator.getPath() + "/" + params.getTargetEntity()); + } + + /** + * Injects the response into the context. + */ + @Override + protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, + StandardCoderObject response) { + String entity = params.getTargetEntity(); + + logger.info("{}: caching response of {} for {}", getFullName(), entity, params.getRequestId()); + + params.getContext().setProperty(propertyPrefix + entity, response); + } + + /** + * Provides a default retry value, if none specified. + */ + @Override + protected int getRetry(Integer retry) { + return (retry == null ? DEFAULT_RETRY : retry); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiUtil.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiUtil.java new file mode 100644 index 000000000..14edc3aa1 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiUtil.java @@ -0,0 +1,50 @@ +/*- + * ============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.aai; + +import java.util.HashMap; +import java.util.Map; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; + +/** + * Utilities used by A&AI classes. + */ +public class AaiUtil { + + private AaiUtil() { + // do nothing + } + + /** + * Makes standard request headers for A&AI requests. + * + * @param params operation parameters + * @return new request headers + */ + public static Map<String, Object> makeHeaders(ControlLoopOperationParams params) { + Map<String, Object> headers = new HashMap<>(); + + headers.put("X-FromAppId", "POLICY"); + headers.put("X-TransactionId", params.getRequestId().toString()); + + return headers; + } +} diff --git a/models-interactions/model-actors/actor.aai/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor b/models-interactions/model-actors/actor.aai/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor new file mode 100644 index 000000000..6a52e3f17 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor @@ -0,0 +1 @@ +org.onap.policy.controlloop.actor.aai.AaiActorServiceProvider |