Refactor babel-related code to not update parameter values
[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 org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.modelloader.notification.EventCallback;
29 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
30 import org.onap.aai.modelloader.service.SdcConnectionJob;
31 import org.onap.sdc.api.IDistributionClient;
32 import org.onap.sdc.api.results.IDistributionClientResult;
33 import org.onap.sdc.utils.DistributionActionResultEnum;
34 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
35 import org.springframework.boot.context.event.ApplicationReadyEvent;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.context.event.EventListener;
38
39 @Configuration
40 @ConditionalOnProperty(value = "ml.distribution.connection.enabled", havingValue = "true", matchIfMissing = true)
41 public class DistributionClientStartupConfig {
42
43     private static final Logger logger = LoggerFactory.getInstance().getLogger(DistributionClientStartupConfig.class);
44
45     private final IDistributionClient client;
46     private final ModelLoaderConfig config;
47     private final EventCallback eventCallback;
48
49     public DistributionClientStartupConfig(IDistributionClient client, ModelLoaderConfig config,
50             EventCallback eventCallback) {
51         this.client = client;
52         this.config = config;
53         this.eventCallback = eventCallback;
54     }
55
56     @EventListener(ApplicationReadyEvent.class)
57     protected void initSdcClient() {
58         // Initialize distribution client
59         logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
60         IDistributionClientResult initResult = null;
61         initResult = client.init(config, eventCallback);
62
63         if (initResult.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
64             // Start distribution client
65             logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
66             IDistributionClientResult startResult = client.start();
67             if (startResult.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
68                 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
69             } else {
70                 String errorMsg = "Failed to start distribution client: " + startResult.getDistributionMessageResult();
71                 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
72
73                 // Kick off a timer to retry the SDC connection
74                 Timer timer = new Timer();
75                 TimerTask task = new SdcConnectionJob(client, config, eventCallback, timer);
76                 timer.schedule(task, new Date(), 60000);
77             }
78         } else {
79             String errorMsg = "Failed to initialize distribution client: " + initResult.getDistributionMessageResult();
80             logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
81
82             // Kick off a timer to retry the SDC connection
83             Timer timer = new Timer();
84             TimerTask task = new SdcConnectionJob(client, config, eventCallback, timer);
85             timer.schedule(task, new Date(), 60000);
86         }
87     }
88 }