4e10953efb30aa2dae07c9662e2e8a0d353b8451
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / client / ASDCGlobalController.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.client;
22
23
24
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.openecomp.mso.asdc.client.exceptions.ASDCControllerException;
32 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
33 import org.openecomp.mso.logger.MessageEnum;
34 import org.openecomp.mso.logger.MsoLogger;
35 import org.openecomp.mso.properties.MsoJsonProperties;
36 import org.openecomp.mso.properties.MsoPropertiesException;
37 import org.openecomp.mso.properties.MsoPropertiesFactory;
38
39
40
41 public class ASDCGlobalController {
42
43     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
44     private Map <String,ASDCController> controllers = new HashMap <String,ASDCController> ();
45         
46     private MsoJsonProperties msoProp= null;
47     
48     private void loadControllers () throws ASDCParametersException {
49         
50                 List<String> controllerNames = ASDCConfiguration.getAllDefinedControllers();
51
52                 StringBuffer controllerListLog = new StringBuffer("List of controllers loaded:");
53                 for (String controllerName : controllerNames) {
54                         controllers.put(controllerName, new ASDCController(controllerName));
55                         controllerListLog.append(controllerName);
56                         controllerListLog.append(";");
57                 }
58                 LOGGER.debug(controllerListLog.toString());
59     }
60     
61     private boolean sameControllersDefined() throws ASDCParametersException {
62         List<String> controllerNames = ASDCConfiguration.getAllDefinedControllers();
63         if (controllerNames.size() == controllers.size()) {
64                 boolean areIdentical = true;
65                 
66                 for (String name:controllerNames) {
67                         if (!controllers.containsKey(name)) {
68                                 areIdentical = false;
69                                 break;
70                         }
71                 }
72                 return areIdentical;
73                 
74         } else {
75                 return false;
76         }
77     }
78     
79     /**
80      * Check that controllers list needs to be updated or not.
81      * @param return true if the list has been updated
82      */
83     private boolean updateControllersListIfNeeded ()  {
84         boolean updateNeeded=false;
85         try {
86         
87                         MsoPropertiesFactory msoPropFactory = new MsoPropertiesFactory();
88                         MsoJsonProperties newMsoProp;
89         
90                         newMsoProp = msoPropFactory.getMsoJsonProperties(ASDCConfiguration.MSO_PROP_ASDC);
91         
92                         if (msoPropFactory.propertiesHaveChanged(ASDCConfiguration.MSO_PROP_ASDC, msoProp) && !sameControllersDefined()) {
93                                 updateNeeded = true;
94                                 LOGGER.debug("List of ASDC controllers has been changed, trying to kill them");
95                                 this.closeASDC();
96                                 
97                                 // Wait that all controllers are down before restarting, next pass will kill them all
98                                 if (this.controllers.size() == 0) {
99                                         msoProp = newMsoProp;
100                                         this.loadControllers();
101                                 }
102                         }                       
103                 
104         } catch (ASDCParametersException e) {
105                          LOGGER.warn (MessageEnum.ASDC_LOAD_ASDC_CLIENT_EXC,
106                      "All ASDC Hosts",
107                      "All ASDC Envs", "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCParametersException in updateControllersListIfNeeded",
108                      e);
109                 } catch (MsoPropertiesException e) {
110                          LOGGER.warn (MessageEnum.ASDC_LOAD_ASDC_CLIENT_EXC,
111                     "All ASDC Hosts",
112                     "All ASDC Envs", "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "MsoPropertiesException in updateControllersListIfNeeded",
113                     e);
114                 }
115         return updateNeeded;
116                 
117
118     }
119     
120     /**
121      * Checks for each controller if it is STOPPED and restart if it is the case.
122      */
123     public void checkInStoppedState () {
124
125         for (ASDCController controller : controllers.values()) {
126             if (ASDCControllerStatus.STOPPED.equals (controller.getControllerStatus ())) {
127
128                 // Try to restart just in case of issues
129                 try {
130                     controller.initASDC ();
131                 } catch (ASDCControllerException ec) {
132                     LOGGER.warn (MessageEnum.ASDC_INIT_ASDC_CLIENT_EXC,
133                                  controller.getAddress (),
134                                  controller.getEnvironment (), "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCControllerException in checkInStoppedState",
135                                  ec);
136                 } catch (ASDCParametersException ep) {
137                     LOGGER.warn (MessageEnum.ASDC_LOAD_ASDC_CLIENT_EXC,
138                                  controller.getAddress (),
139                                  controller.getEnvironment (), "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCParametersException in checkInStoppedState",
140                                  ep);
141                 } catch (RuntimeException | IOException e) {
142                     LOGGER.error (MessageEnum.ASDC_SINGLETON_CHECKT_EXC,
143                                   controller.getAddress (),
144                                   controller.getEnvironment (), "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "RuntimeException in checkInStoppedState",
145                                   e);
146                 }
147             }
148         }
149     }
150
151     public void closeASDC () {
152         List<String> controllerToRemove = new LinkedList<String>();
153         
154         for (ASDCController controller : controllers.values()) {
155             try {
156                 controller.closeASDC ();
157                 controllerToRemove.add(controller.controllerName);
158                 
159             } catch (RuntimeException e) {
160                 LOGGER.warn (MessageEnum.ASDC_SHUTDOWN_ASDC_CLIENT_EXC,
161                              "RuntimeException",
162                              controller.getAddress (),
163                              controller.getEnvironment (), "ASDC", "closeASDC", MsoLogger.ErrorCode.BusinessProcesssError, "RuntimeException in closeASDC",
164                              e);
165             } catch (ASDCControllerException e) {
166                 LOGGER.warn (MessageEnum.ASDC_SHUTDOWN_ASDC_CLIENT_EXC,
167                              "ASDCControllerException",
168                              controller.getAddress (),
169                              controller.getEnvironment (), "ASDC", "closeASDC", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCControllerException in closeASDC",
170                              e);
171             }
172         }
173         
174         // Now remove the ones properly closed
175         for (String toRemove:controllerToRemove) {
176                 controllers.remove(toRemove);
177         }
178         
179     }
180
181     /**
182      * Check whether the config has been changed
183      */
184     public boolean updateControllersConfigIfNeeded () {
185         boolean listUpdated=updateControllersListIfNeeded();
186         if (!listUpdated) {
187         
188                 for (ASDCController controller : controllers.values()) {
189                     try {
190                         controller.updateConfigIfNeeded ();
191                     } catch (ASDCControllerException ec) {
192                         LOGGER.warn (MessageEnum.ASDC_INIT_ASDC_CLIENT_EXC,
193                                      controller.getAddress (),
194                                      controller.getEnvironment (), "ASDC", "closeASDC", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCControllerException in updateControllersConfigIfNeeded",
195                                      ec);
196                     } catch (ASDCParametersException ep) {
197                         LOGGER.warn (MessageEnum.ASDC_LOAD_ASDC_CLIENT_EXC,
198                                      controller.getAddress (),
199                                      controller.getEnvironment (), "ASDC", "closeASDC", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCParametersException in updateControllersConfigIfNeeded",
200                                      ep);
201                     } catch (RuntimeException | IOException e) {
202                         LOGGER.error (MessageEnum.ASDC_SINGLETON_CHECKT_EXC,
203                                       controller.getAddress (),
204                                       controller.getEnvironment (), "ASDC", "closeASDC", MsoLogger.ErrorCode.BusinessProcesssError, "RuntimeException in updateControllersConfigIfNeeded",
205                                       e);
206                     }
207                 }
208         }
209         return listUpdated;
210     }
211
212         public Map<String, ASDCController> getControllers() {
213                 return controllers;
214         }
215     
216     
217 }