2 * Copyright © 2019 Bell Canada
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.sdclistener.client;
19 import org.onap.ccsdk.cds.sdclistener.SdcListenerConfiguration;
20 import org.onap.ccsdk.cds.sdclistener.SdcListenerNotificationCallback;
21 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
22 import org.onap.ccsdk.cds.sdclistener.exceptions.SdcListenerException;
23 import org.onap.sdc.api.IDistributionClient;
24 import org.onap.sdc.api.results.IDistributionClientResult;
25 import org.onap.sdc.impl.DistributionClientFactory;
26 import org.onap.sdc.utils.DistributionActionResultEnum;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.context.event.ApplicationReadyEvent;
31 import org.springframework.context.annotation.ComponentScan;
32 import org.springframework.context.event.EventListener;
33 import org.springframework.stereotype.Component;
35 import java.util.Optional;
38 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
39 public class SdcListenerClient {
41 private static Logger LOG = LoggerFactory.getLogger(SdcListenerClient.class);
44 private SdcListenerConfiguration configuration;
47 private SdcListenerNotificationCallback notification;
50 private SdcListenerDto listenerDto;
52 private IDistributionClient distributionClient;
55 * This method initializes the SDC Distribution client.
57 @EventListener(ApplicationReadyEvent.class)
58 public void initSdcClient() throws SdcListenerException {
59 LOG.info("Initialize the SDC distribution client");
61 distributionClient = Optional.of(DistributionClientFactory.createDistributionClient())
62 .orElseThrow(() -> new SdcListenerException("Could not able to create SDC Distribution client"));
64 listenerDto.setManagedChannelForGrpc();
66 listenerDto.setDistributionClient(distributionClient);
68 IDistributionClientResult result = distributionClient.init(configuration, notification);
69 startSdcClientBasedOnTheResult(result);
72 private void startSdcClientBasedOnTheResult(IDistributionClientResult result) throws SdcListenerException {
73 if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
74 throw new SdcListenerException(
75 "SDC distribution client init failed with reason:" + result.getDistributionMessageResult());
78 LOG.info("Initialization of the SDC distribution client is complete");
81 result = this.distributionClient.start();
83 if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
84 throw new SdcListenerException("Startup of the SDC distribution client failed with reason: "
85 + result.getDistributionMessageResult());
89 private void closeSdcDistributionclient() throws SdcListenerException {
90 LOG.info("Closing SDC distribution client");
91 IDistributionClientResult status = this.distributionClient.stop();
92 if (status.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
93 throw new SdcListenerException(
94 "Failed to close the SDC distribution client due to : " + status.getDistributionMessageResult());