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.event.EventListener;
37 import org.springframework.stereotype.Component;
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 = client.init(config, eventCallback);
62 if (initResult.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
63 // Start distribution client
64 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
65 IDistributionClientResult startResult = client.start();
66 if (startResult.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
67 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
69 String errorMsg = "Failed to start distribution client: " + startResult.getDistributionMessageResult();
70 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
72 // Kick off a timer to retry the SDC connection
73 Timer timer = new Timer();
74 TimerTask task = new SdcConnectionJob(client, config, eventCallback, timer);
75 timer.schedule(task, new Date(), 60000);
78 String errorMsg = "Failed to initialize distribution client: " + initResult.getDistributionMessageResult();
79 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
81 // Kick off a timer to retry the SDC connection
82 Timer timer = new Timer();
83 TimerTask task = new SdcConnectionJob(client, config, eventCallback, timer);
84 timer.schedule(task, new Date(), 60000);