d8ac7d64911c63432cc5413fb7d420168058b620
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / ASDCControllerSingleton.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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
21 package org.openecomp.mso.asdc;
22
23
24 import javax.annotation.PreDestroy;
25 import javax.ejb.ConcurrencyManagement;
26 import javax.ejb.ConcurrencyManagementType;
27 import javax.ejb.Lock;
28 import javax.ejb.LockType;
29 import javax.ejb.Schedule;
30 import javax.ejb.Singleton;
31 import javax.ejb.Startup;
32
33 import org.openecomp.mso.asdc.client.ASDCGlobalController;
34 import org.openecomp.mso.logger.MsoLogger;
35
36 @Singleton(name = "ASDCController")
37 @Lock(LockType.READ)
38 @Startup
39 @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
40 public class ASDCControllerSingleton {
41
42     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
43     private static boolean working = false;
44
45     private ASDCGlobalController globalController = new ASDCGlobalController ();
46
47     /**
48      * Main Constructor of the ASDC Singleton
49      */
50     public ASDCControllerSingleton () {
51     }
52
53     @Schedule(second ="30", minute = "*", hour = "*", persistent = false)
54     public void periodicControllerTask () {
55         if (isWorking ()) {
56             LOGGER.debug ("Another thread is already trying to init ASDC, cancel this periodic call");
57             return;
58         }
59         try {
60             setWorking (true);
61             
62             globalController.updateControllersConfigIfNeeded();
63             globalController.checkInStoppedState();
64
65         } finally {
66             setWorking (false);
67         }
68     }
69
70     @PreDestroy
71     private void terminate () {
72         globalController.closeASDC ();
73     }
74
75     private static synchronized boolean isWorking () {
76         return ASDCControllerSingleton.working;
77     }
78
79     private static synchronized void setWorking (boolean working) {
80         ASDCControllerSingleton.working = working;
81     }
82 }