2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2018 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.oam.util;
28 import com.att.eelf.configuration.EELFLogger;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.AppcState;
30 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
31 import org.osgi.framework.Bundle;
36 * Utility class provides general state helps
38 public class StateHelper {
39 /** logger inherited from AppcOam */
40 private final EELFLogger logger;
41 private ConfigurationHelper configurationHelper;
42 /** APP-C OAM current state in AppcOamStates value */
43 private volatile AppcOamStates appcOamCurrentState;
48 * @param eelfLogger of the logger
50 public StateHelper(EELFLogger eelfLogger, ConfigurationHelper cHelper) {
52 configurationHelper = cHelper;
53 appcOamCurrentState = AppcOamStates.Unknown;
57 * Set the passed in state to the class <b>appOamCurrentState</b>.
59 * @param appcOamStates of the new state
61 public void setState(AppcOamStates appcOamStates) {
62 appcOamCurrentState = appcOamStates;
67 * @return the class <b>appOamCurrentState</b>
69 public AppcOamStates getState() {
70 return appcOamCurrentState;
74 * Validate if the passed in state is the same as the class <b>appOamCurrentState</b>.
76 * @param appcOamStates of the to be compared state
77 * @return true if they are the same, otherwise false
79 boolean isSameState(AppcOamStates appcOamStates) {
80 return appcOamCurrentState == appcOamStates;
84 * Get APP-C OAM current state
86 * <p>When appcOamCurrentState is null or unknown, reset it with APPC LCM bundle state.
88 * @return AppcOamStates of the current APP-C OAM state
90 public AppcOamStates getCurrentOamState() {
91 if (appcOamCurrentState == null || appcOamCurrentState.equals(AppcOamStates.Unknown)) {
92 appcOamCurrentState = getBundlesState();
94 return appcOamCurrentState;
98 * Use getCurrentOamState to get current OAM AppcOamStates and then convert to AppcState of Yang.
100 * @return AppcState of current OAM state
102 public AppcState getCurrentOamYangState() {
104 AppcOamStates appcOamStates = getCurrentOamState();
105 return AppcState.valueOf(appcOamStates.name());
106 } catch (Exception ex) {
107 logger.error(String.format("Unable to determine the current APP-C OAM state due to %s.", ex.getMessage()));
109 return AppcState.Unknown;
113 * Get APPC state from the state of the set of APPC LCM bundles.
114 * <p>The state of each bundle will be checked and the lowest state will be uses as the returning AppcOamStates.
115 * <p>The bundle state order are defined in OSGI bundle (@see org.osgi.framework.Bundle) class
116 * as the int value assigned to each state as the following: <br>
117 * - UNINSTALLED (1) <br>
118 * - INSTALLED (2) <br>
119 * - RESOLVED (4) <br>
120 * - STARTING (8) <br>
121 * - STOPPING (16) <br>
124 * @return AppcOamStates
126 public AppcOamStates getBundlesState() {
127 BundleHelper bundleHelper = getBundleHelper(logger, configurationHelper);
128 Map<String, Bundle> lcmBundleMap = bundleHelper.getAppcLcmBundles();
129 if (lcmBundleMap == null || lcmBundleMap.isEmpty()) {
130 return AppcOamStates.Unknown;
133 // As we are picking up the lowest bundle state as general APP-C state, we will start with ACTIVE
134 int currentState = Bundle.ACTIVE;
135 for (Bundle bundle : lcmBundleMap.values()) {
136 int bundleState = bundle.getState();
137 logger.trace(String.format("getBundlesState: [%s] has state (%d)", bundle.getSymbolicName(), bundleState));
138 if (bundleState < currentState) {
139 currentState = bundleState;
142 return AppcOamStates.getOamStateFromBundleState(currentState);
145 protected BundleHelper getBundleHelper(EELFLogger logger, ConfigurationHelper configurationHelper) {
146 return new BundleHelper(logger, configurationHelper, this);