0a7fc850863ea6d9437ed2f1470d2932118012ce
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.tasks;
22
23 import static ch.qos.logback.classic.Level.WARN;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoInteractions;
33 import static org.mockito.Mockito.verifyNoMoreInteractions;
34 import static org.mockito.Mockito.when;
35
36 import ch.qos.logback.classic.spi.ILoggingEvent;
37 import ch.qos.logback.core.read.ListAppender;
38
39 import java.time.Duration;
40 import java.time.Instant;
41 import java.util.Arrays;
42 import java.util.Collections;
43
44 import org.junit.jupiter.api.BeforeEach;
45 import org.junit.jupiter.api.Test;
46 import org.junit.jupiter.api.extension.ExtendWith;
47 import org.mockito.Mock;
48 import org.mockito.junit.jupiter.MockitoExtension;
49 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
52 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
53 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
64 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
65
66 import reactor.core.publisher.Flux;
67 import reactor.core.publisher.Mono;
68
69 @ExtendWith(MockitoExtension.class)
70 class RicSynchronizationTaskTest {
71     private static final String POLICY_TYPE_1_NAME = "type1";
72     private static final PolicyType POLICY_TYPE_1 = ImmutablePolicyType.builder() //
73             .id(POLICY_TYPE_1_NAME) //
74             .schema("") //
75             .build();
76
77     private static final String RIC_1_NAME = "ric1";
78     private static final Ric RIC_1 = new Ric(ImmutableRicConfig.builder() //
79             .ricId(RIC_1_NAME) //
80             .baseUrl("baseUrl1") //
81             .managedElementIds(Collections.emptyList()) //
82             .controllerName("controllerName") //
83             .build());
84
85     private static Policy createPolicy(String policyId, boolean isTransient) {
86         return ImmutablePolicy.builder() //
87                 .id(policyId) //
88                 .json("") //
89                 .ownerServiceId("service") //
90                 .ric(RIC_1) //
91                 .type(POLICY_TYPE_1) //
92                 .lastModified(Instant.now()) //
93                 .isTransient(isTransient) //
94                 .build();
95     }
96
97     private static final Policy POLICY_1 = createPolicy("policyId1", false);
98
99     private static final String SERVICE_1_NAME = "service1";
100     private static final String SERVICE_1_CALLBACK_URL = "callbackUrl";
101     private static final Service SERVICE_1 = new Service(SERVICE_1_NAME, Duration.ofSeconds(1), SERVICE_1_CALLBACK_URL);
102
103     @Mock
104     private A1Client a1ClientMock;
105
106     @Mock
107     private A1ClientFactory a1ClientFactoryMock;
108
109     private PolicyTypes policyTypes;
110     private Policies policies;
111     private Services services;
112
113     @BeforeEach
114     void init() {
115         policyTypes = new PolicyTypes();
116         policies = new Policies();
117         services = new Services();
118         RIC_1.setState(RicState.UNAVAILABLE);
119         RIC_1.clearSupportedPolicyTypes();
120     }
121
122     private RicSynchronizationTask createTask() {
123         ApplicationConfig config = new ApplicationConfig();
124         return new RicSynchronizationTask(a1ClientFactoryMock, policyTypes, policies, services, config);
125     };
126
127     @Test
128     void ricAlreadySynchronizing_thenNoSynchronization() {
129         RIC_1.setState(RicState.SYNCHRONIZING);
130         RIC_1.addSupportedPolicyType(POLICY_TYPE_1);
131
132         policyTypes.put(POLICY_TYPE_1);
133         policies.put(POLICY_1);
134
135         RicSynchronizationTask synchronizerUnderTest = createTask();
136
137         synchronizerUnderTest.run(RIC_1);
138
139         verifyNoInteractions(a1ClientMock);
140
141         assertThat(policyTypes.size()).isEqualTo(1);
142         assertThat(policies.size()).isEqualTo(1);
143         assertThat(RIC_1.getState()).isEqualTo(RicState.SYNCHRONIZING);
144         assertThat(RIC_1.getSupportedPolicyTypeNames()).hasSize(1);
145     }
146
147     @Test
148     void ricIdlePolicyTypeInRepo_thenSynchronizationWithReuseOfTypeFromRepoAndCorrectServiceNotified() {
149         RIC_1.setState(RicState.AVAILABLE);
150
151         policyTypes.put(POLICY_TYPE_1);
152
153         services.put(SERVICE_1);
154         Service serviceWithoutCallbackUrlShouldNotBeNotified = new Service("service2", Duration.ofSeconds(1), "");
155         services.put(serviceWithoutCallbackUrlShouldNotBeNotified);
156
157         setUpCreationOfA1Client();
158         simulateRicWithOnePolicyType();
159
160         RicSynchronizationTask synchronizerUnderTest = spy(createTask());
161
162         AsyncRestClient restClientMock = setUpCreationOfAsyncRestClient(synchronizerUnderTest);
163         when(restClientMock.put(anyString(), anyString())).thenReturn(Mono.just("Ok"));
164
165         synchronizerUnderTest.run(RIC_1);
166
167         verify(a1ClientMock, times(1)).getPolicyTypeIdentities();
168         verifyNoMoreInteractions(a1ClientMock);
169
170         verify(synchronizerUnderTest).run(RIC_1);
171         verify(synchronizerUnderTest).createNotificationClient(SERVICE_1_CALLBACK_URL);
172         verifyNoMoreInteractions(synchronizerUnderTest);
173
174         verify(restClientMock).put("", "Synchronization completed for:" + RIC_1_NAME);
175         verifyNoMoreInteractions(restClientMock);
176
177         assertThat(policyTypes.size()).isEqualTo(1);
178         assertThat(policies.size()).isZero();
179         assertThat(RIC_1.getState()).isEqualTo(RicState.AVAILABLE);
180     }
181
182     @Test
183     void ricIdlePolicyTypeNotInRepo_thenSynchronizationWithTypeFromRic() throws Exception {
184         RIC_1.setState(RicState.AVAILABLE);
185
186         setUpCreationOfA1Client();
187         simulateRicWithOnePolicyType();
188         String typeSchema = "schema";
189         when(a1ClientMock.getPolicyTypeSchema(POLICY_TYPE_1_NAME)).thenReturn(Mono.just(typeSchema));
190
191         RicSynchronizationTask synchronizerUnderTest = createTask();
192
193         synchronizerUnderTest.run(RIC_1);
194
195         verify(a1ClientMock).getPolicyTypeIdentities();
196         verifyNoMoreInteractions(a1ClientMock);
197
198         assertThat(policyTypes.size()).isEqualTo(1);
199         assertThat(policyTypes.getType(POLICY_TYPE_1_NAME).schema()).isEqualTo(typeSchema);
200         assertThat(policies.size()).isZero();
201         assertThat(RIC_1.getState()).isEqualTo(RicState.AVAILABLE);
202     }
203
204     @Test
205     void ricIdleAndHavePolicies_thenSynchronizationWithRecreationOfPolicies() {
206         RIC_1.setState(RicState.AVAILABLE);
207
208         Policy transientPolicy = createPolicy("transientPolicyId", true);
209
210         policies.put(transientPolicy);
211         policies.put(POLICY_1);
212
213         setUpCreationOfA1Client();
214         simulateRicWithNoPolicyTypes();
215
216         when(a1ClientMock.deleteAllPolicies()).thenReturn(Flux.just("OK"));
217         when(a1ClientMock.putPolicy(any(Policy.class))).thenReturn(Mono.just("OK"));
218
219         RicSynchronizationTask synchronizerUnderTest = createTask();
220
221         synchronizerUnderTest.run(RIC_1);
222
223         verify(a1ClientMock).deleteAllPolicies();
224         verify(a1ClientMock).putPolicy(POLICY_1);
225         verifyNoMoreInteractions(a1ClientMock);
226
227         assertThat(policyTypes.size()).isZero();
228         assertThat(policies.size()).isEqualTo(1); // The transient policy shall be deleted
229         assertThat(RIC_1.getState()).isEqualTo(RicState.AVAILABLE);
230     }
231
232     @Test
233     void ricIdleAndErrorDeletingPoliciesFirstTime_thenSynchronizationWithDeletionOfPolicies() {
234         RIC_1.setState(RicState.AVAILABLE);
235
236         policies.put(POLICY_1);
237
238         setUpCreationOfA1Client();
239         simulateRicWithNoPolicyTypes();
240
241         when(a1ClientMock.deleteAllPolicies()) //
242                 .thenReturn(Flux.error(new Exception("Exception"))) //
243                 .thenReturn(Flux.just("OK"));
244
245         RicSynchronizationTask synchronizerUnderTest = createTask();
246
247         synchronizerUnderTest.run(RIC_1);
248
249         verify(a1ClientMock, times(2)).deleteAllPolicies();
250         verifyNoMoreInteractions(a1ClientMock);
251
252         assertThat(policyTypes.size()).isZero();
253         assertThat(policies.size()).isZero();
254         assertThat(RIC_1.getState()).isEqualTo(RicState.AVAILABLE);
255     }
256
257     @Test
258     void ricIdleAndErrorDeletingPoliciesAllTheTime_thenSynchronizationWithFailedRecovery() {
259         RIC_1.setState(RicState.AVAILABLE);
260
261         policies.put(POLICY_1);
262
263         setUpCreationOfA1Client();
264         simulateRicWithNoPolicyTypes();
265
266         String originalErrorMessage = "Exception";
267         when(a1ClientMock.deleteAllPolicies()).thenReturn(Flux.error(new Exception(originalErrorMessage)));
268
269         RicSynchronizationTask synchronizerUnderTest = createTask();
270
271         final ListAppender<ILoggingEvent> logAppender =
272                 LoggingUtils.getLogListAppender(RicSynchronizationTask.class, WARN);
273
274         synchronizerUnderTest.run(RIC_1);
275
276         verifyCorrectLogMessage(0, logAppender,
277                 "Synchronization failure for ric: " + RIC_1_NAME + ", reason: " + originalErrorMessage);
278
279         verify(a1ClientMock, times(2)).deleteAllPolicies();
280         verifyNoMoreInteractions(a1ClientMock);
281
282         assertThat(policyTypes.size()).isZero();
283         assertThat(policies.size()).isZero();
284         assertThat(RIC_1.getState()).isEqualTo(RicState.UNAVAILABLE);
285     }
286
287     @Test
288     void ricIdlePolicyTypeInRepo_thenSynchronizationWithErrorOnServiceNotificationErrorLogged() {
289         RIC_1.setState(RicState.AVAILABLE);
290
291         policyTypes.put(POLICY_TYPE_1);
292
293         services.put(SERVICE_1);
294
295         setUpCreationOfA1Client();
296         simulateRicWithOnePolicyType();
297
298         final ListAppender<ILoggingEvent> logAppender =
299                 LoggingUtils.getLogListAppender(RicSynchronizationTask.class, WARN);
300
301         RicSynchronizationTask synchronizerUnderTest = spy(createTask());
302
303         AsyncRestClient restClientMock = setUpCreationOfAsyncRestClient(synchronizerUnderTest);
304         String originalErrorMessage = "Exception";
305         when(restClientMock.put(anyString(), anyString())).thenReturn(Mono.error(new Exception(originalErrorMessage)));
306
307         synchronizerUnderTest.run(RIC_1);
308
309         ILoggingEvent loggingEvent = logAppender.list.get(0);
310         assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
311         verifyCorrectLogMessage(0, logAppender,
312                 "Service notification failed for service: " + SERVICE_1_NAME + ". Cause: " + originalErrorMessage);
313     }
314
315     private void setUpCreationOfA1Client() {
316         when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
317         doReturn(Flux.empty()).when(a1ClientMock).deleteAllPolicies();
318     }
319
320     private AsyncRestClient setUpCreationOfAsyncRestClient(RicSynchronizationTask synchronizerUnderTest) {
321         AsyncRestClient restClientMock = mock(AsyncRestClient.class);
322         doReturn(restClientMock).when(synchronizerUnderTest).createNotificationClient(anyString());
323         return restClientMock;
324     }
325
326     private void simulateRicWithOnePolicyType() {
327         when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(Mono.just(Arrays.asList(POLICY_TYPE_1_NAME)));
328     }
329
330     private void simulateRicWithNoPolicyTypes() {
331         when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(Mono.just(Collections.emptyList()));
332     }
333
334     private void verifyCorrectLogMessage(int messageIndex, ListAppender<ILoggingEvent> logAppender,
335             String expectedMessage) {
336         ILoggingEvent loggingEvent = logAppender.list.get(messageIndex);
337         assertThat(loggingEvent.getFormattedMessage()).isEqualTo(expectedMessage);
338     }
339 }