Add PNF support to new CDS actor
[policy/models.git] / models-interactions / model-actors / actor.cds / src / test / java / org / onap / policy / controlloop / actor / cds / GrpcOperationTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.controlloop.actor.cds;
21
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;
31
32 import java.util.HashMap;
33 import java.util.Map;
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;
66
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();
76
77     @Mock
78     private CdsProcessorGrpcClient cdsClient;
79     private CdsServerProperties cdsProps;
80     private VirtualControlLoopEvent onset;
81     private PseudoExecutor executor;
82     private Target target;
83     private GrpcOperation operation;
84
85     /**
86      * Sets up the fields.
87      */
88     @Before
89     public void setUp() throws Exception {
90         MockitoAnnotations.initMocks(this);
91
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);
99
100         // Setup cdsClient
101         when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
102
103         // Setup onset event
104         onset = new VirtualControlLoopEvent();
105         onset.setRequestId(REQUEST_ID);
106
107         // Setup executor
108         executor = new PseudoExecutor();
109
110         target = new Target();
111         target.setType(TargetType.VM);
112         target.setResourceID(RESOURCE_ID);
113     }
114
115     @Test
116     public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
117
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);
122
123         AtomicBoolean guardStarted = new AtomicBoolean();
124
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);
129
130         operation = new GrpcOperation(params, config) {
131             @Override
132             protected CompletableFuture<OperationOutcome> startGuardAsync() {
133                 guardStarted.set(true);
134                 return future2;
135             }
136         };
137
138         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
139         assertNotNull(future3);
140         assertTrue(guardStarted.get());
141         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
142
143         future2.complete(params.makeOutcome());
144         assertTrue(executor.runAll(100));
145         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
146         assertTrue(future3.isDone());
147     }
148
149     /**
150      * Tests startPreprocessorAsync() when the target type is PNF.
151      */
152     @Test
153     public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
154
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);
159
160         AtomicBoolean guardStarted = new AtomicBoolean();
161
162         target.setType(TargetType.PNF);
163
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);
168
169         operation = new GrpcOperation(params, config) {
170             @Override
171             protected CompletableFuture<OperationOutcome> startGuardAsync() {
172                 guardStarted.set(true);
173                 return future2;
174             }
175         };
176
177         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
178         assertNotNull(future3);
179         assertTrue(guardStarted.get());
180         verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
181
182         future2.complete(params.makeOutcome());
183         assertTrue(executor.runAll(100));
184         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
185         assertTrue(future3.isDone());
186     }
187
188     @Test
189     public void testStartOperationAsync() throws Exception {
190
191         ControlLoopEventContext context = new ControlLoopEventContext(onset);
192         loadCqData(context);
193
194         verifyOperation(context);
195     }
196
197     /**
198      * Tests startOperationAsync() when the target type is PNF.
199      */
200     @Test
201     public void testStartOperationAsyncPnf() throws Exception {
202
203         target.setType(TargetType.PNF);
204
205         ControlLoopEventContext context = new ControlLoopEventContext(onset);
206         loadPnfData(context);
207
208         verifyOperation(context);
209     }
210
211     @Test
212     public void testStartOperationAsyncWithAdditionalParams() throws Exception {
213
214         Map<String, String> additionalParams = new HashMap<>();
215         additionalParams.put("test", "additionalParams");
216         onset.setAdditionalEventParams(additionalParams);
217         ControlLoopEventContext context = new ControlLoopEventContext(onset);
218         loadCqData(context);
219         verifyOperation(context);
220     }
221
222     @Test
223     public void testStartOperationAsyncError() throws Exception {
224
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();
229
230         GrpcConfig config = new GrpcConfig(executor, cdsProps);
231         operation = new GrpcOperation(params, config);
232         assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
233     }
234
235     private void verifyOperation(ControlLoopEventContext context) {
236
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\"}");
240
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();
244
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);
253     }
254
255     private void loadPnfData(ControlLoopEventContext context) throws CoderException {
256         String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
257         StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
258
259         context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
260     }
261
262     private void loadCqData(ControlLoopEventContext context) {
263         GenericVnf genvnf = new GenericVnf();
264         genvnf.setVnfId(MY_VNF);
265
266         ServiceInstance serviceInstance = new ServiceInstance();
267         serviceInstance.setServiceInstanceId(MY_SVC_ID);
268
269         AaiCqResponse cq = mock(AaiCqResponse.class);
270         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
271         when(cq.getServiceInstance()).thenReturn(serviceInstance);
272
273         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
274     }
275 }