aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/main/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducer.java
blob: 410c83c4088283da8019bd269075565c97a89bce (plain)
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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 *  Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
 * ================================================================================
 * 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.apex.plugins.event.carrier.grpc;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
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.ExecutionServiceInput.Builder;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
import org.onap.policy.apex.service.engine.event.PeeredReference;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
import org.onap.policy.cds.api.CdsProcessorListener;
import org.onap.policy.cds.client.CdsProcessorGrpcClient;
import org.onap.policy.cds.properties.CdsServerProperties;
import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Concrete implementation of an Apex gRPC plugin that manages to send a GRPC request.
 *
 * @author Ajith Sreekumar(ajith.sreekumar@est.tech)
 *
 */
public class ApexGrpcProducer extends ApexPluginsEventProducer implements CdsProcessorListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApexGrpcProducer.class);

    private CdsServerProperties props;
    // The gRPC client
    private CdsProcessorGrpcClient client;

    private AtomicReference<ExecutionServiceOutput> cdsResponse = new AtomicReference<>();

    /**
     * {@inheritDoc}.
     */
    @Override
    public void init(final String producerName, final EventHandlerParameters producerParameters)
        throws ApexEventException {
        this.name = producerName;

        // Check and get the gRPC Properties
        if (!(producerParameters.getCarrierTechnologyParameters() instanceof GrpcCarrierTechnologyParameters)) {
            final String errorMessage =
                "Specified producer properties are not applicable to gRPC producer (" + this.name + ")";
            throw new ApexEventException(errorMessage);
        }
        GrpcCarrierTechnologyParameters grpcProducerProperties =
            (GrpcCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
        grpcProducerProperties.validateGrpcParameters(true);
        client = makeGrpcClient(grpcProducerProperties);
    }

    private CdsProcessorGrpcClient makeGrpcClient(GrpcCarrierTechnologyParameters grpcProducerProperties) {
        props = new CdsServerProperties();
        props.setHost(grpcProducerProperties.getHost());
        props.setPort(grpcProducerProperties.getPort());
        props.setUsername(grpcProducerProperties.getUsername());
        props.setPassword(grpcProducerProperties.getPassword());
        props.setTimeout(grpcProducerProperties.getTimeout());

        return new CdsProcessorGrpcClient(this, props);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
        final Object event) {

        ExecutionServiceInput executionServiceInput;
        Builder builder = ExecutionServiceInput.newBuilder();
        try {
            JsonFormat.parser().ignoringUnknownFields().merge((String) event, builder);
            executionServiceInput = builder.build();
        } catch (InvalidProtocolBufferException e) {
            throw new ApexEventRuntimeException(
                "Incoming Event cannot be converted to ExecutionServiceInput type for gRPC request." + e.getMessage());
        }
        try {
            CountDownLatch countDownLatch = client.sendRequest(executionServiceInput);
            if (!countDownLatch.await(props.getTimeout(), TimeUnit.SECONDS)) {
                cdsResponse.set(ExecutionServiceOutput.newBuilder().setStatus(Status.newBuilder()
                    .setErrorMessage(CdsActorConstants.TIMED_OUT).setEventType(EventType.EVENT_COMPONENT_FAILURE))
                    .build());
                LOGGER.error("gRPC Request timed out.");
            }
        } catch (InterruptedException e) {
            LOGGER.error("gRPC request failed. {}", e.getMessage());
            cdsResponse.set(ExecutionServiceOutput.newBuilder().setStatus(Status.newBuilder()
                .setErrorMessage(CdsActorConstants.INTERRUPTED).setEventType(EventType.EVENT_COMPONENT_FAILURE))
                .build());
            Thread.currentThread().interrupt();
        }

        if (!EventType.EVENT_COMPONENT_EXECUTED.equals(cdsResponse.get().getStatus().getEventType())) {
            LOGGER.error("Sending event \"{}\" by {} to CDS failed.", eventName, this.name);
        }

        consumeEvent(executionId, cdsResponse.get());
    }

    private void consumeEvent(long executionId, ExecutionServiceOutput event) {
        // Find the peered consumer for this producer
        final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
        if (peeredRequestorReference == null) {
            return;
        }
        // Find the gRPC Response Consumer that will take in the response to APEX Engine
        final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
        if (!(consumer instanceof ApexGrpcConsumer)) {
            final String errorMessage = "Recieve of gRPC response by APEX failed,"
                + "The consumer is not an instance of ApexGrpcConsumer\n. The received gRPC response:" + event;
            throw new ApexEventRuntimeException(errorMessage);
        }

        // Use the consumer to consume this response event in APEX
        final ApexGrpcConsumer grpcConsumer = (ApexGrpcConsumer) consumer;
        try {
            grpcConsumer.getEventReceiver().receiveEvent(executionId, new Properties(),
                JsonFormat.printer().print(event));
        } catch (ApexEventException | InvalidProtocolBufferException e) {
            throw new ApexEventRuntimeException("Consuming gRPC response failed.", e);
        }
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void stop() {
        client.close();
    }

    @Override
    public void onMessage(ExecutionServiceOutput message) {
        cdsResponse.set(message);
    }

    @Override
    public void onError(Throwable throwable) {
        String errorMsg = throwable.getLocalizedMessage();
        cdsResponse.set(ExecutionServiceOutput.newBuilder()
            .setStatus(Status.newBuilder().setErrorMessage(errorMsg).setEventType(EventType.EVENT_COMPONENT_FAILURE))
            .build());
        LOGGER.error("Failed processing blueprint {}", errorMsg, throwable);
    }
}