2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.modelloader.config;
22 import java.util.Date;
23 import java.util.Timer;
24 import java.util.TimerTask;
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;
40 @ConditionalOnProperty(value = "ml.distribution.connection.enabled", havingValue = "true", matchIfMissing = true)
41 public class DistributionClientStartupConfig {
43 private static final Logger logger = LoggerFactory.getInstance().getLogger(DistributionClientStartupConfig.class);
45 private final IDistributionClient client;
46 private final ModelLoaderConfig config;
47 private final EventCallback eventCallback;
49 public DistributionClientStartupConfig(IDistributionClient client, ModelLoaderConfig config,
50 EventCallback eventCallback) {
53 this.eventCallback = eventCallback;
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);
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");
70 String errorMsg = "Failed to start distribution client: " + startResult.getDistributionMessageResult();
71 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
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);
79 String errorMsg = "Failed to initialize distribution client: " + initResult.getDistributionMessageResult();
80 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
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);