[AAI-3] Remove dependency on SDC
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / service / SdcConnectionJob.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Model Loader
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  * 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=========================================================
19  *
20  * ECOMP and OpenECOMP are trademarks
21  * and service marks of AT&T Intellectual Property.
22  */
23 package org.openecomp.modelloader.service;
24
25 import java.util.Date;
26 import java.util.Timer;
27 import java.util.TimerTask;
28
29 import org.openecomp.cl.api.Logger;
30 import org.openecomp.cl.eelf.LoggerFactory;
31 import org.openecomp.modelloader.config.ModelLoaderConfig;
32 import org.openecomp.modelloader.notification.EventCallback;
33 import org.openecomp.sdc.api.IDistributionClient;
34 import org.openecomp.sdc.api.results.IDistributionClientResult;
35 import org.openecomp.sdc.utils.DistributionActionResultEnum;
36
37 public class SdcConnectionJob extends TimerTask {
38   static Logger logger = LoggerFactory.getInstance().getLogger(SdcConnectionJob.class.getName());
39
40   private IDistributionClient client;
41   private ModelLoaderConfig config;
42   private EventCallback callback; 
43   private Timer timer;
44
45   public SdcConnectionJob(IDistributionClient client, 
46       ModelLoaderConfig config, 
47       EventCallback callback, 
48       Timer timer) {
49     this.client = client;
50     this.timer = timer;
51     this.callback = callback;
52     this.config = config;
53   }
54
55   @Override
56   public void run() {
57
58     IDistributionClientResult initResult = client.init(config, callback);
59
60     if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
61       String errorMsg = "Failed to initialize distribution client: "
62           + initResult.getDistributionMessageResult();
63       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
64       return;
65     }
66
67     IDistributionClientResult startResult = client.start();
68     if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
69       String errorMsg = "Failed to start distribution client: "
70           + startResult.getDistributionMessageResult();
71       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
72       return;
73     }
74
75     // Success.  Cancel the timer job
76     timer.cancel();
77     logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
78   }
79 }