2  * ============LICENSE_START=======================================================
 
   3  * Copyright (C) 2019-2021 Bell Canada.
 
   4  * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  17  * ============LICENSE_END=========================================================
 
  20 package org.onap.policy.cds.client;
 
  22 import io.grpc.ManagedChannel;
 
  23 import io.grpc.stub.StreamObserver;
 
  24 import java.util.concurrent.CountDownLatch;
 
  25 import lombok.AllArgsConstructor;
 
  26 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BlueprintProcessingServiceGrpc;
 
  27 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BlueprintProcessingServiceGrpc.BlueprintProcessingServiceStub;
 
  28 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
 
  29 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
 
  30 import org.onap.policy.cds.api.CdsProcessorListener;
 
  31 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 
  32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
 
  33 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
 
  34 import org.slf4j.Logger;
 
  35 import org.slf4j.LoggerFactory;
 
  38 public class CdsProcessorHandler {
 
  39     private static final Logger LOGGER = LoggerFactory.getLogger(CdsProcessorHandler.class);
 
  40     private static final String LOG_MSG = "[{}|{}|{}|]{}{}";
 
  42     private CdsProcessorListener listener;
 
  45     CountDownLatch process(ExecutionServiceInput request, ManagedChannel channel) {
 
  46         final var header = request.getActionIdentifiers();
 
  47         LOGGER.info("Processing blueprint({}:{}) for action({})", header.getBlueprintVersion(),
 
  48             header.getBlueprintName(), header.getBlueprintVersion());
 
  50         final var finishLatch = new CountDownLatch(1);
 
  51         final BlueprintProcessingServiceStub asyncStub = BlueprintProcessingServiceGrpc.newStub(channel);
 
  52         final StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<>() {
 
  54             public void onNext(ExecutionServiceOutput output) {
 
  55                 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, output.toString());
 
  56                 listener.onMessage(output);
 
  60             public void onError(Throwable throwable) {
 
  61                 LOGGER.info(LOG_MSG, EventType.IN, CommInfrastructure.REST, url, NetLoggerUtil.SYSTEM_LS,
 
  63                 listener.onError(throwable);
 
  64                 finishLatch.countDown();
 
  68             public void onCompleted() {
 
  69                 LOGGER.info("Completed blueprint({}:{}) for action({})", header.getBlueprintVersion(),
 
  70                     header.getBlueprintName(), header.getBlueprintVersion());
 
  71                 finishLatch.countDown();
 
  75         final StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
 
  77             NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, request.toString());
 
  79             // Send the message to CDS backend for processing
 
  80             requestObserver.onNext(request);
 
  81             // Mark the end of requests
 
  82             requestObserver.onCompleted();
 
  83         } catch (RuntimeException e) {
 
  84             requestObserver.onError(e);