import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
+import org.onap.policy.cds.CdsResponse;
import org.onap.policy.cds.api.CdsProcessorListener;
import org.onap.policy.cds.client.CdsProcessorGrpcClient;
import org.onap.policy.cds.properties.CdsServerProperties;
.isNullOrEmpty(cbaActionName);
}
- class CdsActorServiceManager implements CdsProcessorListener {
+ public class CdsActorServiceManager implements CdsProcessorListener {
- private final AtomicReference<String> cdsResponse = new AtomicReference<>();
+ private final AtomicReference<String> cdsStatus = new AtomicReference<>();
/**
* {@inheritDoc}.
EventType eventType = message.getStatus().getEventType();
switch (eventType) {
case EVENT_COMPONENT_FAILURE:
- cdsResponse.compareAndSet(null, CdsActorConstants.FAILED);
+ cdsStatus.compareAndSet(null, CdsActorConstants.FAILED);
break;
case EVENT_COMPONENT_PROCESSING:
- cdsResponse.compareAndSet(null, CdsActorConstants.PROCESSING);
+ cdsStatus.compareAndSet(null, CdsActorConstants.PROCESSING);
break;
case EVENT_COMPONENT_EXECUTED:
- cdsResponse.compareAndSet(null, CdsActorConstants.SUCCESS);
+ cdsStatus.compareAndSet(null, CdsActorConstants.SUCCESS);
break;
default:
- cdsResponse.compareAndSet(null, CdsActorConstants.FAILED);
+ cdsStatus.compareAndSet(null, CdsActorConstants.FAILED);
break;
}
}
@Override
public void onError(final Throwable throwable) {
Status status = Status.fromThrowable(throwable);
- cdsResponse.compareAndSet(null, CdsActorConstants.ERROR);
+ cdsStatus.compareAndSet(null, CdsActorConstants.ERROR);
LOGGER.error("Failed processing blueprint {} {}", status, throwable);
}
* @param cdsClient CDS grpc client object.
* @param cdsProps CDS properties.
* @param executionServiceInput a valid CDS grpc request object.
- * @return Status of the CDS request, null if timeout happens or onError is invoked for any reason.
+ * @return the cds response.
*/
- public String sendRequestToCds(CdsProcessorGrpcClient cdsClient, CdsServerProperties cdsProps,
- ExecutionServiceInput executionServiceInput) {
+ public CdsResponse sendRequestToCds(CdsProcessorGrpcClient cdsClient, CdsServerProperties cdsProps,
+ ExecutionServiceInput executionServiceInput) {
try {
LOGGER.trace("Start CdsActorServiceProvider.executeCdsBlueprintProcessor {}.", executionServiceInput);
// TO-DO: Handle requests asynchronously once the callback support is added to actors.
CountDownLatch countDownLatch = cdsClient.sendRequest(executionServiceInput);
boolean status = countDownLatch.await(cdsProps.getTimeout(), TimeUnit.SECONDS);
if (!status) {
- cdsResponse.compareAndSet(null, CdsActorConstants.TIMED_OUT);
+ cdsStatus.compareAndSet(null, CdsActorConstants.TIMED_OUT);
}
- LOGGER.info("CDS response {}", getCdsResponse());
+ LOGGER.info("CDS status response {}", getCdsStatus());
} catch (InterruptedException ex) {
LOGGER.error("Caught exception in executeCdsBlueprintProcessor in CdsActorServiceProvider: ", ex);
- cdsResponse.compareAndSet(null, CdsActorConstants.INTERRUPTED);
+ cdsStatus.compareAndSet(null, CdsActorConstants.INTERRUPTED);
Thread.currentThread().interrupt();
}
- LOGGER.info("Status of the CDS gRPC request is: {}", getCdsResponse());
- return getCdsResponse();
+ LOGGER.info("Status of the CDS gRPC request is: {}", getCdsStatus());
+
+ CdsResponse response = new CdsResponse();
+ response.setRequestId(
+ executionServiceInput != null && executionServiceInput.getCommonHeader() != null
+ ? executionServiceInput.getCommonHeader().getRequestId() : null);
+ response.setStatus(this.getCdsStatus());
+ return response;
}
- String getCdsResponse() {
- return cdsResponse.get();
+ String getCdsStatus() {
+ return cdsStatus.get();
}
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
+import org.onap.policy.cds.CdsResponse;
import org.onap.policy.cds.client.CdsProcessorGrpcClient;
import org.onap.policy.cds.properties.CdsServerProperties;
import org.onap.policy.controlloop.ControlLoopOperation;
when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(countDownLatch);
CdsActorServiceProvider.CdsActorServiceManager cdsActorSvcMgr = cdsActor.new CdsActorServiceManager();
- String response = cdsActorSvcMgr
+ CdsResponse response = cdsActorSvcMgr
.sendRequestToCds(cdsClient, cdsProps, ExecutionServiceInput.newBuilder().build());
assertTrue(Thread.interrupted());
- assertEquals(response, CdsActorConstants.INTERRUPTED);
+ assertNotNull(response);
+ assertEquals(CdsActorConstants.INTERRUPTED, response.getStatus());
}
@Test
public void testSendRequestToCdsLatchTimedOut() {
CdsActorServiceProvider.CdsActorServiceManager cdsActorSvcMgr = cdsActor.new CdsActorServiceManager();
- String response = cdsActorSvcMgr
+ CdsResponse response = cdsActorSvcMgr
.sendRequestToCds(cdsClient, cdsProps, ExecutionServiceInput.newBuilder().build());
- assertEquals(response, CdsActorConstants.TIMED_OUT);
+ assertNotNull(response);
+ assertEquals(CdsActorConstants.TIMED_OUT, response.getStatus());
}
@Test
// #1: Failure test
cdsActorSvcMgr.onMessage(message);
- assertEquals(cdsActorSvcMgr.getCdsResponse(), CdsActorConstants.FAILED);
+ assertEquals(CdsActorConstants.FAILED, cdsActorSvcMgr.getCdsStatus());
// #2: Success test
cdsActorSvcMgr = sendRequestToCds();
message = ExecutionServiceOutput.newBuilder()
.setStatus(Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_EXECUTED).build()).build();
cdsActorSvcMgr.onMessage(message);
- assertEquals(cdsActorSvcMgr.getCdsResponse(), CdsActorConstants.SUCCESS);
+ assertEquals(CdsActorConstants.SUCCESS, cdsActorSvcMgr.getCdsStatus());
// #3: Processing test
cdsActorSvcMgr = sendRequestToCds();
message = ExecutionServiceOutput.newBuilder()
.setStatus(Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_PROCESSING).build()).build();
cdsActorSvcMgr.onMessage(message);
- assertEquals(cdsActorSvcMgr.getCdsResponse(), CdsActorConstants.PROCESSING);
+ assertEquals(CdsActorConstants.PROCESSING, cdsActorSvcMgr.getCdsStatus());
}
private CdsActorServiceManager sendRequestToCds() {
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Bell Canada.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.cds;
+
+import java.io.Serializable;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * Class representing a CDS response.
+ */
+@Getter
+@Setter
+@ToString
+public class CdsResponse implements Serializable {
+
+ private String requestId;
+ private String status;
+
+ /**
+ * Default empty constructor.
+ */
+ public CdsResponse() {
+ super();
+ }
+
+}