2 * ============LICENSE_START=======================================================
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
11 * 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=========================================================
21 package org.openecomp.mso.asdc.client;
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
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;
41 public class ASDCGlobalController {
43 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
44 private Map <String,ASDCController> controllers = new HashMap <String,ASDCController> ();
46 private MsoJsonProperties msoProp= null;
48 private void loadControllers () throws ASDCParametersException {
50 List<String> controllerNames = ASDCConfiguration.getAllDefinedControllers();
52 StringBuilder controllerListLog = new StringBuilder("List of controllers loaded:");
53 for (String controllerName : controllerNames) {
54 controllers.put(controllerName, new ASDCController(controllerName));
55 controllerListLog.append(controllerName);
56 controllerListLog.append(";");
58 LOGGER.debug(controllerListLog.toString());
61 private boolean sameControllersDefined() throws ASDCParametersException {
62 List<String> controllerNames = ASDCConfiguration.getAllDefinedControllers();
63 if (controllerNames.size() == controllers.size()) {
64 boolean areIdentical = true;
66 for (String name:controllerNames) {
67 if (!controllers.containsKey(name)) {
80 * Check that controllers list needs to be updated or not.
81 * @param return true if the list has been updated
83 private boolean updateControllersListIfNeeded () {
84 boolean updateNeeded=false;
87 MsoPropertiesFactory msoPropFactory = new MsoPropertiesFactory();
88 MsoJsonProperties newMsoProp;
90 newMsoProp = msoPropFactory.getMsoJsonProperties(ASDCConfiguration.MSO_PROP_ASDC);
92 if (msoPropFactory.propertiesHaveChanged(ASDCConfiguration.MSO_PROP_ASDC, msoProp) && !sameControllersDefined()) {
94 LOGGER.debug("List of ASDC controllers has been changed, trying to kill them");
97 // Wait that all controllers are down before restarting, next pass will kill them all
98 if (this.controllers.size() == 0) {
100 this.loadControllers();
104 } catch (ASDCParametersException e) {
105 LOGGER.warn (MessageEnum.ASDC_LOAD_ASDC_CLIENT_EXC,
107 "All ASDC Envs", "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "ASDCParametersException in updateControllersListIfNeeded",
109 } catch (MsoPropertiesException e) {
110 LOGGER.warn (MessageEnum.ASDC_LOAD_ASDC_CLIENT_EXC,
112 "All ASDC Envs", "ASDC", "", MsoLogger.ErrorCode.BusinessProcesssError, "MsoPropertiesException in updateControllersListIfNeeded",
121 * Checks for each controller if it is STOPPED and restart if it is the case.
123 public void checkInStoppedState () {
125 for (ASDCController controller : controllers.values()) {
126 if (ASDCControllerStatus.STOPPED.equals (controller.getControllerStatus ())) {
128 // Try to restart just in case of issues
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",
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",
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",
151 public void closeASDC () {
152 List<String> controllerToRemove = new LinkedList<String>();
154 for (ASDCController controller : controllers.values()) {
156 controller.closeASDC ();
157 controllerToRemove.add(controller.controllerName);
159 } catch (RuntimeException e) {
160 LOGGER.warn (MessageEnum.ASDC_SHUTDOWN_ASDC_CLIENT_EXC,
162 controller.getAddress (),
163 controller.getEnvironment (), "ASDC", "closeASDC", MsoLogger.ErrorCode.BusinessProcesssError, "RuntimeException in closeASDC",
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",
174 // Now remove the ones properly closed
175 for (String toRemove:controllerToRemove) {
176 controllers.remove(toRemove);
182 * Check whether the config has been changed
184 public boolean updateControllersConfigIfNeeded () {
185 boolean listUpdated=updateControllersListIfNeeded();
188 for (ASDCController controller : controllers.values()) {
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",
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",
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",
212 public Map<String, ASDCController> getControllers() {