1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.clamp.controlloop.runtime.instantiation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.IOException;
import java.util.ArrayList;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.provider.PolicyModelsProviderFactory;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Class to perform unit test of {@link ControlLoopInstantiationProvider}}.
*
*/
class ControlLoopInstantiationProviderTest {
private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json";
private static final String CL_INSTANTIATION_UPDATE_JSON =
"src/test/resources/rest/controlloops/ControlLoopsUpdate.json";
private static final String CL_INSTANTIATION_CHANGE_STATE_JSON =
"src/test/resources/rest/controlloops/PassiveCommand.json";
private static final String CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON =
"src/test/resources/rest/controlloops/ControlLoopElementsNotFound.json";
private static final String CL_INSTANTIATION_CONTROLLOOP_DEFINITION_NOT_FOUND_JSON =
"src/test/resources/rest/controlloops/ControlLoopsNotFound.json";
private static final String TOSCA_TEMPLATE_YAML =
"src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
private static final String CONTROL_LOOP_NOT_FOUND = "Control Loop not found";
private static final String DELETE_BAD_REQUEST = "Control Loop State is still %s";
private static final String ORDERED_STATE_INVALID = "ordered state invalid or not specified on command";
private static final String CONTROLLOOP_ELEMENT_NAME_NOT_FOUND =
"\"ControlLoops\" INVALID, item has status INVALID\n"
+ " \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
+ " \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n"
+ " \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
+ " \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n";
private static final String CONTROLLOOP_DEFINITION_NOT_FOUND = "\"ControlLoops\" INVALID, item has status INVALID\n"
+ " \"entry org.onap.domain.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
+ " item \"ControlLoop\" value \"org.onap.domain.PMSHControlLoopDefinition\" INVALID,"
+ " Commissioned control loop definition not FOUND\n"
+ " \"entry org.onap.domain.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n"
+ " item \"ControlLoop\" value \"org.onap.domain.PMSHControlLoopDefinition\" INVALID,"
+ " Commissioned control loop definition not FOUND\n";
private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
private static SupervisionHandler supervisionHandler;
private static CommissioningProvider commissioningProvider;
private static ControlLoopProvider clProvider;
private static PolicyModelsProvider modelsProvider;
private static ParticipantProvider participantProvider;
@BeforeAll
public static void setUpBeforeClass() throws Exception {
serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_TEMPLATE_YAML),
ToscaServiceTemplate.class);
}
/**
* setup Db Provider Parameters.
*
* @throws PfModelException if an error occurs
*/
@BeforeAll
public static void setupDbProviderParameters() throws PfModelException {
ClRuntimeParameterGroup controlLoopParameters = CommonTestData.geParameterGroup("instantproviderdb");
modelsProvider =
CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters());
clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters());
participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters());
var participantStatisticsProvider =
new ParticipantStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters());
var clElementStatisticsProvider =
new ClElementStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters());
commissioningProvider = new CommissioningProvider(modelsProvider, clProvider, null, participantProvider);
var monitoringProvider =
new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
var participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters());
var controlLoopUpdatePublisher = Mockito.mock(ControlLoopUpdatePublisher.class);
var controlLoopStateChangePublisher = Mockito.mock(ControlLoopStateChangePublisher.class);
var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class);
var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class);
var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class);
supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider,
controlLoopUpdatePublisher, controlLoopStateChangePublisher,
participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher);
}
@BeforeEach
public void populateDb() throws Exception {
createEntryInDB();
}
@AfterAll
public static void closeDbProvider() throws PfModelException {
clProvider.close();
modelsProvider.close();
}
@AfterEach
public void cleanDatabase() throws Exception {
deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
}
@Test
void testInstantiationCrud() throws Exception {
ControlLoops controlLoopsCreate =
InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud");
ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
assertThat(controlLoopsDb.getControlLoopList()).isEmpty();
var instantiationProvider =
new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate);
InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate);
controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty();
assertThat(controlLoopsCreate).isEqualTo(controlLoopsDb);
for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) {
ControlLoops controlLoopsGet =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
assertThat(controlLoop).isEqualTo(controlLoopsGet.getControlLoopList().get(0));
}
ControlLoops controlLoopsUpdate =
InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Crud");
assertThat(controlLoopsUpdate).isNotEqualTo(controlLoopsDb);
instantiationResponse = instantiationProvider.updateControlLoops(controlLoopsUpdate);
InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsUpdate);
controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty();
assertThat(controlLoopsUpdate).isEqualTo(controlLoopsDb);
InstantiationCommand instantiationCommand =
InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Crud");
instantiationResponse = instantiationProvider.issueControlLoopCommand(instantiationCommand);
InstantiationUtils.assertInstantiationResponse(instantiationResponse, instantiationCommand);
for (ToscaConceptIdentifier toscaConceptIdentifier : instantiationCommand.getControlLoopIdentifierList()) {
ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(),
toscaConceptIdentifier.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
assertThat(instantiationCommand.getOrderedState())
.isEqualTo(controlLoopsGet.getControlLoopList().get(0).getOrderedState());
}
// in order to delete a controlLoop the state must be UNINITIALISED
controlLoopsCreate.getControlLoopList().forEach(cl -> cl.setState(ControlLoopState.UNINITIALISED));
instantiationProvider.updateControlLoops(controlLoopsCreate);
for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) {
instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
}
controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
assertThat(controlLoopsDb.getControlLoopList()).isEmpty();
}
private ControlLoops getControlLoopsFromDb(ControlLoops controlLoopsSource) throws Exception {
ControlLoops controlLoopsDb = new ControlLoops();
controlLoopsDb.setControlLoopList(new ArrayList<>());
var instantiationProvider = new ControlLoopInstantiationProvider(clProvider,
commissioningProvider, supervisionHandler);
for (ControlLoop controlLoop : controlLoopsSource.getControlLoopList()) {
ControlLoops controlLoopsFromDb =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
controlLoopsDb.getControlLoopList().addAll(controlLoopsFromDb.getControlLoopList());
}
return controlLoopsDb;
}
@Test
void testInstantiationDelete() throws Exception {
ControlLoops controlLoops =
InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete");
assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty();
ControlLoop controlLoop0 = controlLoops.getControlLoopList().get(0);
var instantiationProvider =
new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
assertThatThrownBy(
() -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion()))
.hasMessageMatching(CONTROL_LOOP_NOT_FOUND);
InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoops),
controlLoops);
for (ControlLoopState state : ControlLoopState.values()) {
if (!ControlLoopState.UNINITIALISED.equals(state)) {
assertThatDeleteThrownBy(controlLoops, state);
}
}
controlLoop0.setState(ControlLoopState.UNINITIALISED);
instantiationProvider.updateControlLoops(controlLoops);
for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
}
for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
ControlLoops controlLoopsGet =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
}
}
private void assertThatDeleteThrownBy(ControlLoops controlLoops, ControlLoopState state) throws Exception {
ControlLoop controlLoop = controlLoops.getControlLoopList().get(0);
controlLoop.setState(state);
var instantiationProvider =
new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
instantiationProvider.updateControlLoops(controlLoops);
assertThatThrownBy(
() -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()))
.hasMessageMatching(String.format(DELETE_BAD_REQUEST, state));
}
@Test
void testCreateControlLoops_NoDuplicates() throws Exception {
ControlLoops controlLoopsCreate =
InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "NoDuplicates");
ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate);
assertThat(controlLoopsDb.getControlLoopList()).isEmpty();
var instantiationProvider =
new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate);
InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate);
assertThatThrownBy(() -> instantiationProvider.createControlLoops(controlLoopsCreate)).hasMessageMatching(
controlLoopsCreate.getControlLoopList().get(0).getKey().asIdentifier() + " already defined");
for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) {
instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
}
}
@Test
void testCreateControlLoops_CommissionedClElementNotFound() throws Exception {
ControlLoops controlLoops = InstantiationUtils
.getControlLoopsFromResource(CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON, "ClElementNotFound");
var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
// to validate control Loop, it needs to define ToscaServiceTemplate
// InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider);
assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty();
assertThatThrownBy(() -> provider.createControlLoops(controlLoops))
.hasMessageMatching(CONTROLLOOP_ELEMENT_NAME_NOT_FOUND);
}
@Test
void testCreateControlLoops_CommissionedClNotFound() throws Exception {
ControlLoops controlLoops = InstantiationUtils
.getControlLoopsFromResource(CL_INSTANTIATION_CONTROLLOOP_DEFINITION_NOT_FOUND_JSON, "ClNotFound");
assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty();
var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
assertThatThrownBy(() -> provider.createControlLoops(controlLoops))
.hasMessageMatching(CONTROLLOOP_DEFINITION_NOT_FOUND);
}
@Test
void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException {
var instantiationProvider =
new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
assertThatThrownBy(() -> instantiationProvider.issueControlLoopCommand(new InstantiationCommand()))
.hasMessageMatching(ORDERED_STATE_INVALID);
}
@Test
void testInstantiationVersions() throws Exception {
// create controlLoops V1
ControlLoops controlLoopsV1 =
InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V1");
assertThat(getControlLoopsFromDb(controlLoopsV1).getControlLoopList()).isEmpty();
var instantiationProvider =
new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler);
InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV1),
controlLoopsV1);
// create controlLoops V2
ControlLoops controlLoopsV2 =
InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V2");
assertThat(getControlLoopsFromDb(controlLoopsV2).getControlLoopList()).isEmpty();
InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV2),
controlLoopsV2);
// GET controlLoops V2
for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
ControlLoops controlLoopsGet =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
assertThat(controlLoop).isEqualTo(controlLoopsGet.getControlLoopList().get(0));
}
// DELETE controlLoops V1
for (ControlLoop controlLoop : controlLoopsV1.getControlLoopList()) {
instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
}
// GET controlLoops V1 is not available
for (ControlLoop controlLoop : controlLoopsV1.getControlLoopList()) {
ControlLoops controlLoopsGet =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
}
// GET controlLoops V2 is still available
for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
ControlLoops controlLoopsGet =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
assertThat(controlLoop).isEqualTo(controlLoopsGet.getControlLoopList().get(0));
}
// DELETE controlLoops V2
for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion());
}
// GET controlLoops V2 is not available
for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) {
ControlLoops controlLoopsGet =
instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion());
assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
}
}
private synchronized void deleteEntryInDB(String name, String version) throws Exception {
if (!modelsProvider.getServiceTemplateList(null, null).isEmpty()) {
modelsProvider.deleteServiceTemplate(name, version);
}
}
private synchronized void createEntryInDB() throws Exception {
try {
deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion());
modelsProvider.createServiceTemplate(serviceTemplate);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|