Merge "SDC Listner Docker touchup"
[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 package org.onap.ccsdk.cds.sdclistener.client;
17
18 import java.util.Optional;
19 import org.onap.ccsdk.cds.sdclistener.SdcListenerConfiguration;
20 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
21 import org.onap.ccsdk.cds.sdclistener.SdcListenerNotificationCallback;
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 @Component
36 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
37 public class SdcListenerClient {
38
39     private static Logger LOG = LoggerFactory.getLogger(SdcListenerClient.class);
40
41     @Autowired
42     private SdcListenerConfiguration configuration;
43
44     @Autowired
45     private SdcListenerNotificationCallback notification;
46
47     @Autowired
48     private SdcListenerDto listenerDto;
49
50     private IDistributionClient distributionClient;
51
52     /**
53      * This method initializes the SDC Distribution client.
54      */
55     @EventListener(ApplicationReadyEvent.class)
56     public void initSdcClient() throws SdcListenerException {
57         LOG.info("Initialize the SDC distribution client");
58
59         distributionClient = Optional.of(DistributionClientFactory.createDistributionClient())
60             .orElseThrow(() -> new SdcListenerException("Could not able to create SDC Distribution client"));
61
62         listenerDto.setManagedChannelForGrpc();
63
64         listenerDto.setDistributionClient(distributionClient);
65
66         IDistributionClientResult result = distributionClient.init(configuration, notification);
67         startSdcClientBasedOnTheResult(result);
68     }
69
70     private void startSdcClientBasedOnTheResult(IDistributionClientResult result) throws SdcListenerException {
71         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
72             throw new SdcListenerException(
73                 "SDC distribution client init failed with reason:" + result.getDistributionMessageResult());
74         }
75
76         LOG.info("Initialization of the SDC distribution client is complete");
77
78         // Start the client.
79         result = this.distributionClient.start();
80
81         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
82             throw new SdcListenerException(
83                 "Startup of the SDC distribution client failed with reason: " + result.getDistributionMessageResult());
84         }
85     }
86
87     private void closeSdcDistributionclient() throws SdcListenerException {
88         LOG.info("Closing SDC distribution client");
89         IDistributionClientResult status = this.distributionClient.stop();
90         if (status.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
91             throw new SdcListenerException(
92                 "Failed to close the SDC distribution client due to : " + status.getDistributionMessageResult());
93         }
94     }
95 }