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