package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification;
import com.google.gson.*;
-import com.nokia.cbam.lcm.v32.ApiException;
import com.nokia.cbam.lcm.v32.model.*;
-import org.joda.time.DateTime;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.concurrent.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
import org.threeten.bp.OffsetDateTime;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.NoSuchElementException;
-import java.util.Optional;
-import java.util.concurrent.*;
+import static java.util.Optional.empty;
import static com.nokia.cbam.lcm.v32.model.OperationType.*;
import static junit.framework.TestCase.*;
public class TestLifecycleChangeNotificationManager extends TestBase {
- public static final String OPERATION_EXECUTION_ID = "myOperationExecutionId";
-
@InjectMocks
private LifecycleChangeNotificationManager lifecycleChangeNotificationManager;
private VnfLifecycleChangeNotification recievedLcn = new VnfLifecycleChangeNotification();
recievedLcn.setLifecycleOperationOccurrenceId("instantiationOperationExecutionId");
healOperation.setStartTime(OffsetDateTime.now().plusDays(1));
recievedLcn.setVnfInstanceId(VNF_ID);
- when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions);
+ when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(operationExecutions));
prepOperation(instantiationOperation);
prepOperation(scaleOperation);
prepOperation(healOperation);
VimInfo vimInfo = new VimInfo();
vimInfo.setId(VIM_ID);
instantiateVnfRequest.getVims().add(vimInfo);
- when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new Gson().toJsonTree(instantiateVnfRequest));
- when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(vnfs);
+ when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(new Gson().toJsonTree(instantiateVnfRequest)));
+ when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfs));
vnfs.add(vnf);
vnf.setId(VNF_ID);
VnfProperty prop = new VnfProperty();
prop.setValue(VNFM_ID);
vnf.setExtensions(new ArrayList<>());
vnf.getExtensions().add(prop);
- when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf);
+ when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnf));
}
- private void prepOperation(OperationExecution operationExecution) throws ApiException {
- addEmptyModifiedConnectionPoints(operationExecution);
+ private void prepOperation(OperationExecution operationExecution) {
JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}");
operationExecution.setOperationParams(root);
switch (operationExecution.getOperationType()) {
case TERMINATE:
root.getAsJsonObject().addProperty("terminationType", "GRACEFULL");
}
- when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationExecution);
+ when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(operationExecution));
operationExecutions.add(operationExecution);
}
*/
@Test
public void testInstantiationSufficesTheLastInstantiation() {
- DateTime baseTime = DateTime.now();
+ OffsetDateTime baseTime = OffsetDateTime.now();
List<OperationExecution> operations = new ArrayList<>();
OperationExecution operation = buildOperation(OffsetDateTime.now(), INSTANTIATE);
*/
@Test
public void testNoInstantiation() {
- DateTime baseTime = DateTime.now();
+ OffsetDateTime baseTime = OffsetDateTime.now();
List<OperationExecution> operations = new ArrayList<>();
OperationExecution operation = buildOperation(OffsetDateTime.now(), TERMINATE);
*/
@Test
public void testUnableToListVnfs() throws Exception {
- ApiException expectedException = new ApiException();
+ RuntimeException expectedException = new RuntimeException();
when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
//when
try {
*/
@Test
public void testUnableToQueryVnf() throws Exception {
- ApiException expectedException = new ApiException();
+ RuntimeException expectedException = new RuntimeException();
when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
//when
try {
}
/**
- * if unable to send LCN to VF-C the error is propagated
+ * if unable to query all operation executions from CBAM the error is propagated
*/
@Test
- public void testUnableToQueryCurrentOperation() throws Exception {
+ public void testUnableToQueryCurrentOperations() throws Exception {
recievedLcn.setOperation(OperationType.TERMINATE);
recievedLcn.setStatus(OperationStatus.FINISHED);
- ApiException expectedException = new ApiException();
+ RuntimeException expectedException = new RuntimeException();
when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
//when
try {
} catch (Exception e) {
//verify
assertEquals(expectedException, e.getCause());
- verify(logger).error("Unable to retrieve the current VNF myVnfId", e.getCause());
+ verify(logger).error("Unable to retrieve the operation executions for the VNF myVnfId", e.getCause());
+ }
+ }
+
+ /**
+ * if unable to query the given operation execution from CBAM the error is propagated
+ */
+ @Test
+ public void testUnableToQueryCurrentOperation() throws Exception {
+ recievedLcn.setOperation(OperationType.TERMINATE);
+ recievedLcn.setStatus(OperationStatus.FINISHED);
+ RuntimeException expectedException = new RuntimeException();
+ when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(recievedLcn.getLifecycleOperationOccurrenceId(), NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
+ //when
+ try {
+ lifecycleChangeNotificationManager.handleLcn(recievedLcn);
+ fail();
+ } catch (Exception e) {
+ //verify
+ assertEquals(expectedException, e.getCause());
+ verify(logger).error("Unable to retrieve the operation execution with instantiationOperationExecutionId identifier", e.getCause());
}
}
secondTerminationOperationExecution.setOperationParams(buildTerminationParams());
nonProcessedEvent.setLifecycleOperationOccurrenceId(secondTerminationOperationExecution.getId());
lifecycleChangeNotificationManager.handleLcn(nonProcessedEvent);
+ addEmptyModifiedConnectionPoints(terminationOperation);
//add second termination
recievedLcn.setOperation(OperationType.TERMINATE);
recievedLcn.setStatus(OperationStatus.FINISHED);
lifecycleChangeNotificationManager.handleLcn(recievedLcn);
//verify
assertTrue(waitExitedWithSuccess.get());
+ assertEquals(empty(), affectedConnectionPoints.getValue());
}
/**
verify(logger).warn("The operation failed and the affected connection points were not reported");
}
+
+ /**
+ * affected connection points are passed to the actual notification processor
+ */
+ @Test
+ public void testAffectedConnectionPointProcessing() throws Exception {
+ //given
+ recievedLcn.setOperation(OperationType.INSTANTIATE);
+ recievedLcn.setStatus(OperationStatus.FINISHED);
+ recievedLcn.setLifecycleOperationOccurrenceId(instantiationOperation.getId());
+ instantiationOperation.setStatus(OperationStatus.FAILED);
+ addEmptyModifiedConnectionPoints(instantiationOperation);
+ OperationResult operationResult = new OperationResult();
+ ReportedAffectedConnectionPoints affectedCp = new ReportedAffectedConnectionPoints();
+ ReportedAffectedCp cp = new ReportedAffectedCp();
+ cp.setCpId("cpId");
+ affectedCp.getPost().add(cp);
+ operationResult.operationResult = affectedCp;
+ instantiationOperation.setAdditionalData(new Gson().toJsonTree(operationResult));
+
+ //when
+ lifecycleChangeNotificationManager.handleLcn(recievedLcn);
+ //verify
+ assertTrue(affectedConnectionPoints.getValue().isPresent());
+ ReportedAffectedConnectionPoints actualCps = new Gson().fromJson(new Gson().toJsonTree(affectedConnectionPoints.getValue().get()), ReportedAffectedConnectionPoints.class);
+ assertEquals(1, actualCps.getPost().size());
+ }
+
/**
* Failures in affected connection point processing are tolerated for failed operation
* (because the POST script was not able to run)
recievedLcn.setStatus(OperationStatus.FAILED);
recievedLcn.setLifecycleOperationOccurrenceId(instantiationOperation.getId());
instantiationOperation.setStatus(OperationStatus.FAILED);
+ addEmptyModifiedConnectionPoints(instantiationOperation);
JsonObject additionalData = (JsonObject) instantiationOperation.getAdditionalData();
additionalData.remove("operationResult");
//when
request.setType(ScaleDirection.OUT);
scaleOperation.setOperationParams(request);
scaleOperation.setStatus(OperationStatus.FAILED);
+ addEmptyModifiedConnectionPoints(scaleOperation);
((JsonObject) scaleOperation.getAdditionalData()).get("operationResult").getAsJsonObject().remove("cbam_post");
scaleOperation.setOperationType(OperationType.SCALE);
+
//when
lifecycleChangeNotificationManager.handleLcn(recievedLcn);
assertFalse(affectedConnectionPoints.getValue().isPresent());
request.setType(ScaleDirection.OUT);
scaleOperation.setOperationParams(request);
scaleOperation.setStatus(OperationStatus.FAILED);
+ addEmptyModifiedConnectionPoints(scaleOperation);
JsonObject operationResult = ((JsonObject) scaleOperation.getAdditionalData()).get("operationResult").getAsJsonObject();
operationResult.remove("cbam_post");
operationResult.addProperty("cbam_post", "");