19a6f4e3c0b81bebb88f49c056c07508822af1aa
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / service / SdcConnectionJob.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  */
21 package org.onap.aai.modelloader.service;
22
23 import java.util.Date;
24 import java.util.Timer;
25 import java.util.TimerTask;
26
27 import org.onap.aai.modelloader.config.ModelLoaderConfig;
28 import org.onap.aai.modelloader.notification.EventCallback;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.openecomp.sdc.api.IDistributionClient;
32 import org.openecomp.sdc.api.results.IDistributionClientResult;
33 import org.openecomp.sdc.utils.DistributionActionResultEnum;
34
35 public class SdcConnectionJob extends TimerTask {
36   static Logger logger = LoggerFactory.getInstance().getLogger(SdcConnectionJob.class.getName());
37
38   private IDistributionClient client;
39   private ModelLoaderConfig config;
40   private EventCallback callback; 
41   private Timer timer;
42
43   public SdcConnectionJob(IDistributionClient client, 
44       ModelLoaderConfig config, 
45       EventCallback callback, 
46       Timer timer) {
47     this.client = client;
48     this.timer = timer;
49     this.callback = callback;
50     this.config = config;
51   }
52
53   @Override
54   public void run() {
55
56     IDistributionClientResult initResult = client.init(config, callback);
57
58     if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
59       String errorMsg = "Failed to initialize distribution client: "
60           + initResult.getDistributionMessageResult();
61       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
62       return;
63     }
64
65     IDistributionClientResult startResult = client.start();
66     if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
67       String errorMsg = "Failed to start distribution client: "
68           + startResult.getDistributionMessageResult();
69       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
70       return;
71     }
72
73     // Success.  Cancel the timer job
74     timer.cancel();
75     logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
76   }
77 }