From e887a0c6c415a00888e17d1bbfe4acf548a2cfa5 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 17 Apr 2020 15:09:21 -0400 Subject: Add PNF support to new CDS actor Made the following updates: - added new A&AI get-PNF Operation by refactoring AaiGetOperation, separating out Tenant and PNF operations - added PNF support to the CDS actor - added logging to the CDS Handler - added get-pnf to the A&AI simulator Issue-ID: POLICY-2505 Change-Id: Iff140e7c864f762790d8e2ecaba62c161c859e6e Signed-off-by: Jim Hahn --- .../actor/aai/AaiActorServiceProvider.java | 7 +- .../actor/aai/AaiCustomQueryOperation.java | 6 +- .../controlloop/actor/aai/AaiGetOperation.java | 78 +++------- .../controlloop/actor/aai/AaiGetPnfOperation.java | 92 +++++++++++ .../actor/aai/AaiGetTenantOperation.java | 90 +++++++++++ .../actor/aai/AaiActorServiceProviderTest.java | 3 +- .../actor/aai/AaiCustomQueryOperationTest.java | 16 +- .../controlloop/actor/aai/AaiGetOperationTest.java | 143 +++++------------ .../actor/aai/AaiGetPnfOperationTest.java | 169 ++++++++++++++++++++ .../actor/aai/AaiGetTenantOperationTest.java | 170 +++++++++++++++++++++ .../actor.aai/src/test/resources/service.yaml | 4 +- 11 files changed, 603 insertions(+), 175 deletions(-) create mode 100644 models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperation.java create mode 100644 models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperation.java create mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperationTest.java create mode 100644 models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperationTest.java (limited to 'models-interactions/model-actors/actor.aai') 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 index a65a000ed..0fe70b7ea 100644 --- 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 @@ -38,10 +38,7 @@ public class AaiActorServiceProvider extends HttpActor { super(NAME, HttpActorParams.class); addOperator(new HttpOperator(NAME, AaiCustomQueryOperation.NAME, AaiCustomQueryOperation::new)); - - // add all "get" operators - for (String operation : AaiGetOperation.OPERATIONS) { - addOperator(new HttpOperator(NAME, operation, AaiGetOperation::new)); - } + addOperator(new HttpOperator(NAME, AaiGetTenantOperation.NAME, AaiGetTenantOperation::new)); + addOperator(new HttpOperator(NAME, AaiGetPnfOperation.NAME, AaiGetPnfOperation::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 index b00928847..2cc2a69f4 100644 --- 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 @@ -84,10 +84,10 @@ public class AaiCustomQueryOperation extends HttpOperation { @Override protected CompletableFuture startPreprocessorAsync() { ControlLoopOperationParams tenantParams = - params.toBuilder().actor(AaiConstants.ACTOR_NAME).operation(AaiGetOperation.TENANT) + params.toBuilder().actor(AaiConstants.ACTOR_NAME).operation(AaiGetTenantOperation.NAME) .targetEntity(vserver).payload(null).retry(null).timeoutSec(null).build(); - return params.getContext().obtain(AaiGetOperation.getTenantKey(vserver), tenantParams); + return params.getContext().obtain(AaiGetTenantOperation.getKey(vserver), tenantParams); } @Override @@ -137,7 +137,7 @@ public class AaiCustomQueryOperation extends HttpOperation { * Constructs the custom query using the previously retrieved tenant data. */ private Map makeRequest() { - StandardCoderObject tenant = params.getContext().getProperty(AaiGetOperation.getTenantKey(vserver)); + StandardCoderObject tenant = params.getContext().getProperty(AaiGetTenantOperation.getKey(vserver)); String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK); if (resourceLink == null) { 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 index a20200807..07738b084 100644 --- 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 @@ -22,15 +22,10 @@ package org.onap.policy.controlloop.actor.aai; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import java.util.concurrent.CompletableFuture; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import org.onap.policy.aai.AaiConstants; -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.StandardCoderObject; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; @@ -49,17 +44,6 @@ public class AaiGetOperation extends HttpOperation { 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 OPERATIONS = Set.of(TENANT); - /** * Responses that are retrieved from A&AI are placed in the operation context under @@ -78,50 +62,22 @@ public class AaiGetOperation extends HttpOperation { this.propertyPrefix = 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 public void generateSubRequestId(int attempt) { setSubRequestId(String.valueOf(attempt)); } - @Override - protected CompletableFuture startOperationAsync(int attempt, OperationOutcome outcome) { - Map headers = makeHeaders(); - - headers.put("Accept", MediaType.APPLICATION_JSON); - - StringBuilder str = new StringBuilder(getClient().getBaseUrl()); - - String path = getPath(); - WebTarget web = getClient().getWebTarget().path(path); - str.append(path); - - web = addQuery(web, str, "?", "search-node-type", "vserver"); - web = addQuery(web, str, "&", "filter", "vserver-name:EQUALS:" + params.getTargetEntity()); - - Builder webldr = web.request(); - for (Entry header : headers.entrySet()) { - webldr.header(header.getKey(), header.getValue()); - } - - String url = str.toString(); - - logMessage(EventType.OUT, CommInfrastructure.REST, url, null); - - return handleResponse(outcome, url, callback -> webldr.async().get(callback)); - } - - private WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) { + /** + * Adds a query parameter to a web target. + * + * @param web target to which the parameter should be added + * @param str the separator and parameter are appended here, for logging purposes + * @param separator separator to be added to "str"; that's its only use + * @param name parameter name + * @param value parameter value + * @return "web" + */ + protected WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) { str.append(separator); str.append(name); str.append('='); @@ -130,6 +86,18 @@ public class AaiGetOperation extends HttpOperation { return web.queryParam(name, value); } + /** + * Adds headers to the web builder. + * + * @param webldr builder to which the headers should be added + * @param headers headers to be added + */ + protected void addHeaders(Builder webldr, Map headers) { + for (Entry header : headers.entrySet()) { + webldr.header(header.getKey(), header.getValue()); + } + } + @Override protected Map makeHeaders() { return AaiUtil.makeHeaders(params); diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperation.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperation.java new file mode 100644 index 000000000..fbf40967c --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperation.java @@ -0,0 +1,92 @@ +/*- + * ============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.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import org.onap.policy.aai.AaiConstants; +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.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; + +/** + * A&AI get-pnf operator. + */ +public class AaiGetPnfOperation extends AaiGetOperation { + private static final String URI_SEP = "/"; + + public static final String NAME = "Pnf"; + + // property prefixes + private static final String KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + NAME + "."; + + /** + * Constructs the object. + * + * @param params operation parameters + * @param config configuration for this operation + */ + public AaiGetPnfOperation(ControlLoopOperationParams params, HttpConfig config) { + super(params, config); + } + + /** + * Gets the "context key" for the PNF 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 getKey(String targetEntity) { + return (KEY_PREFIX + targetEntity); + } + + @Override + protected CompletableFuture startOperationAsync(int attempt, OperationOutcome outcome) { + Map headers = makeHeaders(); + + headers.put("Accept", MediaType.APPLICATION_JSON); + + StringBuilder str = new StringBuilder(getClient().getBaseUrl()); + + String path = getPath() + URI_SEP + URLEncoder.encode(params.getTargetEntity(), StandardCharsets.UTF_8); + WebTarget web = getClient().getWebTarget().path(path); + str.append(path); + + web = addQuery(web, str, "?", "depth", "0"); + + Builder webldr = web.request(); + addHeaders(webldr, headers); + + String url = str.toString(); + + logMessage(EventType.OUT, CommInfrastructure.REST, url, null); + + return handleResponse(outcome, url, callback -> webldr.async().get(callback)); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperation.java b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperation.java new file mode 100644 index 000000000..cbd179189 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/main/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperation.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.aai; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import org.onap.policy.aai.AaiConstants; +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.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; + +/** + * A&AI get-tenant operator. + */ +public class AaiGetTenantOperation extends AaiGetOperation { + + public static final String NAME = "Tenant"; + + // property prefixes + private static final String KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + NAME + "."; + + /** + * Constructs the object. + * + * @param params operation parameters + * @param config configuration for this operation + */ + public AaiGetTenantOperation(ControlLoopOperationParams params, HttpConfig config) { + super(params, config); + } + + /** + * 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 getKey(String targetEntity) { + return (KEY_PREFIX + targetEntity); + } + + @Override + protected CompletableFuture startOperationAsync(int attempt, OperationOutcome outcome) { + Map headers = makeHeaders(); + + headers.put("Accept", MediaType.APPLICATION_JSON); + + StringBuilder str = new StringBuilder(getClient().getBaseUrl()); + + String path = getPath(); + WebTarget web = getClient().getWebTarget().path(path); + str.append(path); + + web = addQuery(web, str, "?", "search-node-type", "vserver"); + web = addQuery(web, str, "&", "filter", "vserver-name:EQUALS:" + params.getTargetEntity()); + + Builder webldr = web.request(); + addHeaders(webldr, headers); + + String url = str.toString(); + + logMessage(EventType.OUT, CommInfrastructure.REST, url, null); + + return handleResponse(outcome, url, callback -> webldr.async().get(callback)); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java index 12b6ca44c..97a8491b2 100644 --- a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiActorServiceProviderTest.java @@ -38,7 +38,8 @@ public class AaiActorServiceProviderTest extends BasicActor { // verify that it has the operators we expect List expected = new LinkedList<>(); expected.add(AaiCustomQueryOperation.NAME); - expected.addAll(AaiGetOperation.OPERATIONS); + expected.add(AaiGetTenantOperation.NAME); + expected.add(AaiGetPnfOperation.NAME); Collections.sort(expected); diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java index aa8165e34..243a0c696 100644 --- a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiCustomQueryOperationTest.java @@ -103,7 +103,7 @@ public class AaiCustomQueryOperationTest extends BasicAaiOperation future2) @@ -267,17 +267,17 @@ public class AaiCustomQueryOperationTest extends BasicAaiOperation { - private static final String INPUT_FIELD = "input"; - private static final String TEXT = "my-text"; + private static final String MY_NAME = "my-operation-name"; + private static final String PARAM_NAME = "my-param"; + private static final String PARAM_VALUE = "my-value"; + private static final String MY_URL = "my-url"; private AaiGetOperation oper; public AaiGetOperationTest() { - super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT); + super(AaiConstants.ACTOR_NAME, MY_NAME); } @BeforeClass @@ -73,45 +71,10 @@ public class AaiGetOperationTest extends BasicAaiOperation { oper = new AaiGetOperation(params, config); } - /** - * Tests "success" case with simulator. - */ @Test - public void testSuccess() throws Exception { - HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build(); - config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); - - params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build(); - oper = new AaiGetOperation(params, config); - - outcome = oper.start().get(); - assertEquals(PolicyResult.SUCCESS, outcome.getResult()); - } - - /** - * Tests "failure" case with simulator. - */ - @Test - public void testFailure() throws Exception { - HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build(); - config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); - - params = params.toBuilder().targetEntity("failedVserver").retry(0).timeoutSec(5).executor(blockingExecutor) - .build(); - oper = new AaiGetOperation(params, config); - - outcome = oper.start().get(); - assertEquals(PolicyResult.FAILURE, outcome.getResult()); - } - - @Test - public void testGetRetry() { - // use default if null retry - assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null)); - - // otherwise, use specified value - assertEquals(0, oper.getRetry(0)); - assertEquals(10, oper.getRetry(10)); + public void testConstructor() { + assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); + assertEquals(MY_NAME, oper.getName()); } @Test @@ -121,71 +84,47 @@ public class AaiGetOperationTest extends BasicAaiOperation { } @Test - @SuppressWarnings("unchecked") - public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception { - - // return a map in the reply - Map reply = Map.of(INPUT_FIELD, TEXT); - when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply)); + public void testAddQuery() { + WebTarget web = mock(WebTarget.class); + when(web.queryParam(any(), any())).thenReturn(web); - when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse)); + StringBuilder bldr = new StringBuilder(); - oper.generateSubRequestId(1); - outcome.setSubRequestId(oper.getSubRequestId()); - - CompletableFuture future2 = oper.startOperationAsync(1, outcome); - assertFalse(future2.isDone()); - - executor.runAll(100); - assertTrue(future2.isDone()); - - assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); - - // data should have been cached within the context - StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)); - assertNotNull(data); - assertEquals(TEXT, data.getString(INPUT_FIELD)); - - assertEquals("1", future2.get().getSubRequestId()); + assertSame(web, oper.addQuery(web, bldr, ",", PARAM_NAME, PARAM_VALUE)); + assertEquals(",my-param=my-value", bldr.toString()); } - /** - * Tests startOperationAsync() when there's a failure. - */ @Test - @SuppressWarnings("unchecked") - public void testStartOperationAsyncFailure() throws Exception { + public void testAddHeaders() { + Builder bldr = mock(Builder.class); + oper.addHeaders(bldr, Map.of("hdrA", "valA", "hdrB", "valB")); - when(rawResponse.getStatus()).thenReturn(500); - when(rawResponse.readEntity(String.class)).thenReturn(""); - - when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse)); - - CompletableFuture future2 = oper.startOperationAsync(1, outcome); - assertFalse(future2.isDone()); - - executor.runAll(100); - assertTrue(future2.isDone()); - - assertEquals(PolicyResult.FAILURE, future2.get().getResult()); - - // data should NOT have been cached within the context - assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY))); + verify(bldr, times(2)).header(any(), any()); + verify(bldr).header("hdrA", "valA"); + verify(bldr).header("hdrB", "valB"); } @Test - public void testMakeHeaders() { - verifyHeaders(oper.makeHeaders()); + public void testPostProcessResponse() throws Exception { + StandardCoderObject resp = new StandardCoderObject(); + CompletableFuture future2 = oper.postProcessResponse(outcome, MY_URL, null, resp); + + assertSame(outcome, future2.get()); + assertSame(resp, context.getProperty("AAI.my-operation-name.my-target")); } @Test - public void testAaiGetOperator() { - assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); - assertEquals(AaiGetOperation.TENANT, oper.getName()); + public void testGetRetry() { + // use default if null retry + assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null)); + + // otherwise, use specified value + assertEquals(0, oper.getRetry(0)); + assertEquals(10, oper.getRetry(10)); } @Test - public void testGetTenantKey() { - assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY)); + public void testMakeHeaders() { + verifyHeaders(oper.makeHeaders()); } } diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperationTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperationTest.java new file mode 100644 index 000000000..22702404b --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetPnfOperationTest.java @@ -0,0 +1,169 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.client.InvocationCallback; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; +import org.onap.policy.controlloop.policy.PolicyResult; + +public class AaiGetPnfOperationTest extends BasicAaiOperation { + private static final String INPUT_FIELD = "input"; + private static final String TEXT = "my-text"; + + private AaiGetPnfOperation oper; + + public AaiGetPnfOperationTest() { + super(AaiConstants.ACTOR_NAME, AaiGetPnfOperation.NAME); + } + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + initBeforeClass(); + } + + @AfterClass + public static void tearDownAfterClass() { + destroyAfterClass(); + } + + /** + * Sets up. + */ + @Before + public void setUp() throws Exception { + super.setUpBasic(); + oper = new AaiGetPnfOperation(params, config); + } + + @Test + public void testConstructor() { + assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); + assertEquals(AaiGetPnfOperation.NAME, oper.getName()); + } + + /** + * Tests "success" case with simulator. + */ + @Test + public void testSuccess() throws Exception { + HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build(); + config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); + + params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build(); + oper = new AaiGetPnfOperation(params, config); + + outcome = oper.start().get(); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + } + + /** + * Tests "failure" case with simulator. + */ + @Test + public void testFailure() throws Exception { + HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build(); + config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); + + params = params.toBuilder().targetEntity("getFail").retry(0).timeoutSec(5).executor(blockingExecutor).build(); + oper = new AaiGetPnfOperation(params, config); + + outcome = oper.start().get(); + assertEquals(PolicyResult.FAILURE, outcome.getResult()); + } + + @Test + @SuppressWarnings("unchecked") + public void testStartOperationAsync_testStartQueryAsync() throws Exception { + + // return a map in the reply + Map reply = Map.of(INPUT_FIELD, TEXT); + when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply)); + + when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse)); + + oper.generateSubRequestId(1); + outcome.setSubRequestId(oper.getSubRequestId()); + + CompletableFuture future2 = oper.startOperationAsync(1, outcome); + assertFalse(future2.isDone()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); + + // data should have been cached within the context + StandardCoderObject data = context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY)); + assertNotNull(data); + assertEquals(TEXT, data.getString(INPUT_FIELD)); + + assertEquals("1", future2.get().getSubRequestId()); + } + + /** + * Tests startOperationAsync() when there's a failure. + */ + @Test + @SuppressWarnings("unchecked") + public void testStartOperationAsyncFailure() throws Exception { + + when(rawResponse.getStatus()).thenReturn(500); + when(rawResponse.readEntity(String.class)).thenReturn(""); + + when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.startOperationAsync(1, outcome); + assertFalse(future2.isDone()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + assertEquals(PolicyResult.FAILURE, future2.get().getResult()); + + // data should NOT have been cached within the context + assertNull(context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY))); + } + + @Test + public void testGetKey() { + assertEquals("AAI.Pnf." + TARGET_ENTITY, AaiGetPnfOperation.getKey(TARGET_ENTITY)); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperationTest.java b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperationTest.java new file mode 100644 index 000000000..496d2ea68 --- /dev/null +++ b/models-interactions/model-actors/actor.aai/src/test/java/org/onap/policy/controlloop/actor/aai/AaiGetTenantOperationTest.java @@ -0,0 +1,170 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.client.InvocationCallback; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; +import org.onap.policy.controlloop.policy.PolicyResult; + +public class AaiGetTenantOperationTest extends BasicAaiOperation { + private static final String INPUT_FIELD = "input"; + private static final String TEXT = "my-text"; + + private AaiGetTenantOperation oper; + + public AaiGetTenantOperationTest() { + super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME); + } + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + initBeforeClass(); + } + + @AfterClass + public static void tearDownAfterClass() { + destroyAfterClass(); + } + + /** + * Sets up. + */ + @Before + public void setUp() throws Exception { + super.setUpBasic(); + oper = new AaiGetTenantOperation(params, config); + } + + @Test + public void testConstructor() { + assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName()); + assertEquals(AaiGetTenantOperation.NAME, oper.getName()); + } + + /** + * Tests "success" case with simulator. + */ + @Test + public void testSuccess() throws Exception { + HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build(); + config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); + + params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build(); + oper = new AaiGetTenantOperation(params, config); + + outcome = oper.start().get(); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + } + + /** + * Tests "failure" case with simulator. + */ + @Test + public void testFailure() throws Exception { + HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build(); + config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); + + params = params.toBuilder().targetEntity("failedVserver").retry(0).timeoutSec(5).executor(blockingExecutor) + .build(); + oper = new AaiGetTenantOperation(params, config); + + outcome = oper.start().get(); + assertEquals(PolicyResult.FAILURE, outcome.getResult()); + } + + @Test + @SuppressWarnings("unchecked") + public void testStartOperationAsync_testStartQueryAsync() throws Exception { + + // return a map in the reply + Map reply = Map.of(INPUT_FIELD, TEXT); + when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply)); + + when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse)); + + oper.generateSubRequestId(1); + outcome.setSubRequestId(oper.getSubRequestId()); + + CompletableFuture future2 = oper.startOperationAsync(1, outcome); + assertFalse(future2.isDone()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); + + // data should have been cached within the context + StandardCoderObject data = context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY)); + assertNotNull(data); + assertEquals(TEXT, data.getString(INPUT_FIELD)); + + assertEquals("1", future2.get().getSubRequestId()); + } + + /** + * Tests startOperationAsync() when there's a failure. + */ + @Test + @SuppressWarnings("unchecked") + public void testStartOperationAsyncFailure() throws Exception { + + when(rawResponse.getStatus()).thenReturn(500); + when(rawResponse.readEntity(String.class)).thenReturn(""); + + when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse)); + + CompletableFuture future2 = oper.startOperationAsync(1, outcome); + assertFalse(future2.isDone()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + assertEquals(PolicyResult.FAILURE, future2.get().getResult()); + + // data should NOT have been cached within the context + assertNull(context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY))); + } + + @Test + public void testGetKey() { + assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY)); + } +} diff --git a/models-interactions/model-actors/actor.aai/src/test/resources/service.yaml b/models-interactions/model-actors/actor.aai/src/test/resources/service.yaml index d0c11be7b..db186028a 100644 --- a/models-interactions/model-actors/actor.aai/src/test/resources/service.yaml +++ b/models-interactions/model-actors/actor.aai/src/test/resources/service.yaml @@ -30,4 +30,6 @@ actors: CustomQuery: path: cq Tenant: - path: tenant \ No newline at end of file + path: tenant + Pnf: + path: pnf -- cgit 1.2.3-korg