2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.controlloop.actor.cds;
22 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.HashMap;
34 import java.util.UUID;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.TimeoutException;
40 import java.util.concurrent.atomic.AtomicBoolean;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.aai.domain.yang.ServiceInstance;
47 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
48 import org.onap.policy.aai.AaiCqResponse;
49 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
50 import org.onap.policy.cds.properties.CdsServerProperties;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.common.utils.coder.StandardCoderObject;
55 import org.onap.policy.common.utils.time.PseudoExecutor;
56 import org.onap.policy.controlloop.VirtualControlLoopEvent;
57 import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
58 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
59 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
60 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
61 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
62 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
63 import org.onap.policy.controlloop.policy.PolicyResult;
64 import org.onap.policy.controlloop.policy.Target;
65 import org.onap.policy.controlloop.policy.TargetType;
67 public class GrpcOperationTest {
68 private static final String TARGET_ENTITY = "entity";
69 private static final String MY_VNF = "my-vnf";
70 private static final String MY_SVC_ID = "my-service-instance-id";
71 private static final String RESOURCE_ID = "my-resource-id";
72 private static final String CDS_BLUEPRINT_NAME = "vfw-cds";
73 private static final String CDS_BLUEPRINT_VERSION = "1.0.0";
74 private static final UUID REQUEST_ID = UUID.randomUUID();
75 private static final Coder coder = new StandardCoder();
78 private CdsProcessorGrpcClient cdsClient;
79 private CdsServerProperties cdsProps;
80 private VirtualControlLoopEvent onset;
81 private PseudoExecutor executor;
82 private Target target;
83 private GrpcOperation operation;
89 public void setUp() throws Exception {
90 MockitoAnnotations.initMocks(this);
92 // Setup the CDS properties
93 cdsProps = new CdsServerProperties();
94 cdsProps.setHost("10.10.10.10");
95 cdsProps.setPort(2000);
96 cdsProps.setUsername("testUser");
97 cdsProps.setPassword("testPassword");
98 cdsProps.setTimeout(1);
101 when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
104 onset = new VirtualControlLoopEvent();
105 onset.setRequestId(REQUEST_ID);
108 executor = new PseudoExecutor();
110 target = new Target();
111 target.setType(TargetType.VM);
112 target.setResourceID(RESOURCE_ID);
116 public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
118 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
119 ControlLoopEventContext context = mock(ControlLoopEventContext.class);
120 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
121 when(context.getEvent()).thenReturn(onset);
123 AtomicBoolean guardStarted = new AtomicBoolean();
125 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
126 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
127 .targetEntity(TARGET_ENTITY).target(target).build();
128 GrpcConfig config = new GrpcConfig(executor, cdsProps);
130 operation = new GrpcOperation(params, config) {
132 protected CompletableFuture<OperationOutcome> startGuardAsync() {
133 guardStarted.set(true);
138 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
139 assertNotNull(future3);
140 assertTrue(guardStarted.get());
141 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
143 future2.complete(params.makeOutcome());
144 assertTrue(executor.runAll(100));
145 assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
146 assertTrue(future3.isDone());
150 * Tests startPreprocessorAsync() when the target type is PNF.
153 public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
155 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
156 ControlLoopEventContext context = mock(ControlLoopEventContext.class);
157 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
158 when(context.getEvent()).thenReturn(onset);
160 AtomicBoolean guardStarted = new AtomicBoolean();
162 target.setType(TargetType.PNF);
164 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
165 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
166 .targetEntity(TARGET_ENTITY).target(target).build();
167 GrpcConfig config = new GrpcConfig(executor, cdsProps);
169 operation = new GrpcOperation(params, config) {
171 protected CompletableFuture<OperationOutcome> startGuardAsync() {
172 guardStarted.set(true);
177 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
178 assertNotNull(future3);
179 assertTrue(guardStarted.get());
180 verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
182 future2.complete(params.makeOutcome());
183 assertTrue(executor.runAll(100));
184 assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
185 assertTrue(future3.isDone());
189 public void testStartOperationAsync() throws Exception {
191 ControlLoopEventContext context = new ControlLoopEventContext(onset);
194 verifyOperation(context);
198 * Tests startOperationAsync() when the target type is PNF.
201 public void testStartOperationAsyncPnf() throws Exception {
203 target.setType(TargetType.PNF);
205 ControlLoopEventContext context = new ControlLoopEventContext(onset);
206 loadPnfData(context);
208 verifyOperation(context);
212 public void testStartOperationAsyncWithAdditionalParams() throws Exception {
214 Map<String, String> additionalParams = new HashMap<>();
215 additionalParams.put("test", "additionalParams");
216 onset.setAdditionalEventParams(additionalParams);
217 ControlLoopEventContext context = new ControlLoopEventContext(onset);
219 verifyOperation(context);
223 public void testStartOperationAsyncError() throws Exception {
225 ControlLoopEventContext context = new ControlLoopEventContext(onset);
226 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
227 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
228 .targetEntity(TARGET_ENTITY).target(target).build();
230 GrpcConfig config = new GrpcConfig(executor, cdsProps);
231 operation = new GrpcOperation(params, config);
232 assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
235 private void verifyOperation(ControlLoopEventContext context) {
237 Map<String, Object> payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME,
238 CdsActorConstants.KEY_CBA_VERSION, CDS_BLUEPRINT_VERSION, "data",
239 "{\"mapInfo\":{\"key\":\"val\"},\"arrayInfo\":[\"one\",\"two\"],\"paramInfo\":\"val\"}");
241 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
242 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
243 .targetEntity(TARGET_ENTITY).target(target).payload(payloadMap).build();
245 GrpcConfig config = new GrpcConfig(executor, cdsProps);
246 operation = new GrpcOperation(params, config);
247 assertEquals(1000, operation.getTimeoutMs(null));
248 assertEquals(1000, operation.getTimeoutMs(0));
249 assertEquals(2000, operation.getTimeoutMs(2));
250 operation.generateSubRequestId(1);
251 CompletableFuture<OperationOutcome> future3 = operation.startOperationAsync(1, params.makeOutcome());
252 assertNotNull(future3);
255 private void loadPnfData(ControlLoopEventContext context) throws CoderException {
256 String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
257 StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
259 context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
262 private void loadCqData(ControlLoopEventContext context) {
263 GenericVnf genvnf = new GenericVnf();
264 genvnf.setVnfId(MY_VNF);
266 ServiceInstance serviceInstance = new ServiceInstance();
267 serviceInstance.setServiceInstanceId(MY_SVC_ID);
269 AaiCqResponse cq = mock(AaiCqResponse.class);
270 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
271 when(cq.getServiceInstance()).thenReturn(serviceInstance);
273 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);