Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / hibernate / HibernateBundleAgent.java
1 /**
2  * Copyright 2016 [ZTE] and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.aai.esr.hibernate;
18
19 import io.dropwizard.ConfiguredBundle;
20 import io.dropwizard.setup.Bootstrap;
21 import io.dropwizard.setup.Environment;
22
23 import org.onap.aai.esr.ExtsysAppConfiguration;
24 import org.onap.aai.esr.dao.DaoManager;
25 import org.onap.aai.esr.entity.db.BaseData;
26 import org.onap.aai.esr.entity.db.EmsData;
27 import org.onap.aai.esr.entity.db.SdncData;
28 import org.onap.aai.esr.entity.db.VimData;
29 import org.onap.aai.esr.entity.db.VnfmData;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class HibernateBundleAgent implements ConfiguredBundle<ExtsysAppConfiguration> {
34
35   private final HibernateBundleExt bundle = new HibernateBundleExt(EmsData.class, BaseData.class, VimData.class,
36       VnfmData.class, SdncData.class);
37   private static final Logger LOGGER = LoggerFactory.getLogger(HibernateBundleAgent.class);
38
39   @Override
40   public void run(final ExtsysAppConfiguration configuration, final Environment environment)
41       throws Exception {
42     Thread thread = new Thread(new Runnable() {
43       int retry = 0;
44       boolean flag = true;
45       public void run() {
46         while (retry < 1000) {
47           LOGGER.info("init hibernateBundle.retry:" + retry);
48           retry++;
49           try {
50             bundle.runExt(configuration, environment);
51           } catch (Exception e1) {
52             flag = false;
53             LOGGER.warn(
54                 "init hibernateBundle failed, sleep 15S and try again.errorMsg:" + e1.getMessage());
55             threadSleep(15000);
56           }
57           if (flag) {
58             LOGGER.info("init hibernateBundle success!");
59             initDao();
60             break;
61           }
62         }
63       }
64     });
65     thread.setName("init hibernateBundle");
66     thread.start();
67   }
68
69   private void initDao() {
70     DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory());
71   }
72
73   private void threadSleep(int second) {
74     LOGGER.info("start sleep ....");
75     try {
76       Thread.sleep(second);
77     } catch (InterruptedException error) {
78       LOGGER.error("thread sleep error.errorMsg:" + error.getMessage());
79     }
80     LOGGER.info("sleep end .");
81   }
82
83   @Override
84   public void initialize(Bootstrap<?> bootstrap) {
85     bundle.initializeExt(bootstrap);
86   }
87 //
88 //  public SessionFactory getSessionFactory() {
89 //    return bundle.getSessionFactory();
90 //  }
91 }