Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actor.cds / src / main / java / org / onap / policy / controlloop / actor / cds / CdsActorServiceManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Bell Canada. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.controlloop.actor.cds;
20
21 import java.util.concurrent.CompletableFuture;
22 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
23 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
24 import org.onap.policy.cds.api.CdsProcessorListener;
25 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
26 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * CDS Actor service-manager implementation.
32  */
33 public class CdsActorServiceManager implements CdsProcessorListener {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(CdsActorServiceManager.class);
36
37     private final CompletableFuture<OperationOutcome> future;
38
39     private final OperationOutcome outcome;
40
41     /**
42      * Constructs the object.
43      *
44      * @param outcome the operation outcome to populate
45      * @param future the future to complete
46      */
47     public CdsActorServiceManager(OperationOutcome outcome, CompletableFuture<OperationOutcome> future) {
48         this.outcome = outcome;
49         this.future = future;
50     }
51
52     /**
53      * {@inheritDoc}.
54      */
55     @Override
56     public void onMessage(final ExecutionServiceOutput message) {
57         LOGGER.info("Received notification from CDS: {}", message);
58         EventType eventType = message.getStatus().getEventType();
59         switch (eventType) {
60             case EVENT_COMPONENT_PROCESSING:
61                 LOGGER.info("CDS is processing the message: {}", message);
62                 break;
63             case EVENT_COMPONENT_EXECUTED:
64                 outcome.setResult(OperationResult.SUCCESS);
65                 outcome.setResponse(message);
66                 future.complete(outcome);
67                 break;
68             default:
69                 outcome.setResult(OperationResult.FAILURE);
70                 outcome.setResponse(message);
71                 future.complete(outcome);
72                 break;
73         }
74     }
75
76     /**
77      * {@inheritDoc}.
78      */
79     @Override
80     public void onError(final Throwable throwable) {
81         future.completeExceptionally(throwable);
82     }
83 }