2d8c1fe04554337a95d5923a148edcbe01f81e92
[ccsdk/cds.git] / ms / sdclistener / application / src / main / java / org / onap / ccsdk / cds / sdclistener / client / SdcListenerClient.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.sdclistener.client;
18
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;
34
35 import java.util.Optional;
36
37 @Component
38 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
39 public class SdcListenerClient {
40
41     private static Logger LOG = LoggerFactory.getLogger(SdcListenerClient.class);
42
43     @Autowired
44     private SdcListenerConfiguration configuration;
45
46     @Autowired
47     private SdcListenerNotificationCallback notification;
48
49     @Autowired
50     private SdcListenerDto listenerDto;
51
52     private IDistributionClient distributionClient;
53
54     /**
55      * This method initializes the SDC Distribution client.
56      */
57     @EventListener(ApplicationReadyEvent.class)
58     public void initSdcClient() throws SdcListenerException {
59         LOG.info("Initialize the SDC distribution client");
60
61         distributionClient = Optional.of(DistributionClientFactory.createDistributionClient())
62                 .orElseThrow(() -> new SdcListenerException("Could not able to create SDC Distribution client"));
63
64         listenerDto.setManagedChannelForGrpc();
65
66         listenerDto.setDistributionClient(distributionClient);
67
68         IDistributionClientResult result = distributionClient.init(configuration, notification);
69         startSdcClientBasedOnTheResult(result);
70     }
71
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());
76         }
77
78         LOG.info("Initialization of the SDC distribution client is complete");
79
80         // Start the client.
81         result = this.distributionClient.start();
82
83         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
84             throw new SdcListenerException("Startup of the SDC distribution client failed with reason: "
85                     + result.getDistributionMessageResult());
86         }
87     }
88
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());
95         }
96     }
97
98 }