Merge "Modelloader: base image update"
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / config / DistributionClientStartupConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2024 Deutsche Telekom AG Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.modelloader.config;
21
22 import java.util.Date;
23 import java.util.Timer;
24 import java.util.TimerTask;
25
26 import javax.annotation.PreDestroy;
27
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.modelloader.notification.EventCallback;
31 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
32 import org.onap.aai.modelloader.service.SdcConnectionJob;
33 import org.onap.sdc.api.IDistributionClient;
34 import org.onap.sdc.api.results.IDistributionClientResult;
35 import org.onap.sdc.utils.DistributionActionResultEnum;
36 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
37 import org.springframework.boot.context.event.ApplicationReadyEvent;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.event.EventListener;
40
41 import lombok.RequiredArgsConstructor;
42
43 @Configuration
44 @RequiredArgsConstructor
45 @ConditionalOnProperty(value = "ml.distribution.connection.enabled", havingValue = "true", matchIfMissing = true)
46 public class DistributionClientStartupConfig {
47
48     private static final Logger logger = LoggerFactory.getInstance().getLogger(DistributionClientStartupConfig.class);
49
50     private final IDistributionClient distributionClient;
51     private final ModelLoaderConfig config;
52     private final EventCallback eventCallback;
53
54     @EventListener(ApplicationReadyEvent.class)
55     protected void initSdcClient() {
56         // Initialize distribution client
57         logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
58         IDistributionClientResult initResult = null;
59         initResult = distributionClient.init(config, eventCallback);
60
61         if (initResult.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
62             // Start distribution client
63             logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
64             IDistributionClientResult startResult = distributionClient.start();
65             if (startResult.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
66                 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
67             } else {
68                 String errorMsg = "Failed to start distribution client: " + startResult.getDistributionMessageResult();
69                 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
70
71                 // Kick off a timer to retry the SDC connection
72                 Timer timer = new Timer();
73                 TimerTask task = new SdcConnectionJob(distributionClient, config, eventCallback, timer);
74                 timer.schedule(task, new Date(), 60000);
75             }
76         } else {
77             String errorMsg = "Failed to initialize distribution client: " + initResult.getDistributionMessageResult();
78             logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
79
80             // Kick off a timer to retry the SDC connection
81             Timer timer = new Timer();
82             TimerTask task = new SdcConnectionJob(distributionClient, config, eventCallback, timer);
83             timer.schedule(task, new Date(), 60000);
84         }
85     }
86
87     @PreDestroy
88     public void destroy() {
89         logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
90         if (distributionClient != null) {
91             distributionClient.stop();
92         }
93     }
94 }