2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.oam.util;
27 import com.att.eelf.configuration.EELFLogger;
28 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.AppcState;
29 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
30 import org.osgi.framework.Bundle;
35 * Utility class provides general state helps
37 public class StateHelper {
38 /** logger inherited from AppcOam */
39 private final EELFLogger logger;
40 private ConfigurationHelper configurationHelper;
41 /** APP-C OAM current state in AppcOamStates value */
42 private volatile AppcOamStates appcOamCurrentState;
47 * @param eelfLogger of the logger
49 public StateHelper(EELFLogger eelfLogger, ConfigurationHelper cHelper) {
51 configurationHelper = cHelper;
52 appcOamCurrentState = AppcOamStates.Unknown;
56 * Set the passed in state to the class <b>appOamCurrentState</b>.
58 * @param appcOamStates of the new state
60 public void setState(AppcOamStates appcOamStates) {
61 appcOamCurrentState = appcOamStates;
66 * @return the class <b>appOamCurrentState</b>
68 public AppcOamStates getState() {
69 return appcOamCurrentState;
73 * Validate if the passed in state is the same as the class <b>appOamCurrentState</b>.
75 * @param appcOamStates of the to be compared state
76 * @return true if they are the same, otherwise false
78 boolean isSameState(AppcOamStates appcOamStates) {
79 return appcOamCurrentState == appcOamStates;
83 * Get APP-C OAM current state
85 * <p>When appcOamCurrentState is null or unknown, reset it with APPC LCM bundle state.
87 * @return AppcOamStates of the current APP-C OAM state
89 public AppcOamStates getCurrentOamState() {
90 if (appcOamCurrentState == null || appcOamCurrentState.equals(AppcOamStates.Unknown)) {
91 appcOamCurrentState = getBundlesState();
93 return appcOamCurrentState;
97 * Use getCurrentOamState to get current OAM AppcOamStates and then convert to AppcState of Yang.
99 * @return AppcState of current OAM state
101 public AppcState getCurrentOamYangState() {
103 AppcOamStates appcOamStates = getCurrentOamState();
104 return AppcState.valueOf(appcOamStates.name());
105 } catch (Exception ex) {
106 logger.error(String.format("Unable to determine the current APP-C OAM state due to %s.", ex.getMessage()));
108 return AppcState.Unknown;
112 * Get APPC state from the state of the set of APPC LCM bundles.
113 * <p>The state of each bundle will be checked and the lowest state will be uses as the returning AppcOamStates.
114 * <p>The bundle state order are defined in OSGI bundle (@see org.osgi.framework.Bundle) class
115 * as the int value assigned to each state as the following: <br>
116 * - UNINSTALLED (1) <br>
117 * - INSTALLED (2) <br>
118 * - RESOLVED (4) <br>
119 * - STARTING (8) <br>
120 * - STOPPING (16) <br>
123 * @return AppcOamStates
125 public AppcOamStates getBundlesState() {
126 BundleHelper bundleHelper = new BundleHelper(logger, configurationHelper, this);
127 Map<String, Bundle> lcmBundleMap = bundleHelper.getAppcLcmBundles();
128 if (lcmBundleMap == null || lcmBundleMap.isEmpty()) {
129 return AppcOamStates.Unknown;
132 // As we are picking up the lowest bundle state as general APP-C state, we will start with ACTIVE
133 int currentState = Bundle.ACTIVE;
134 for (Bundle bundle : lcmBundleMap.values()) {
135 int bundleState = bundle.getState();
136 logger.trace(String.format("getBundlesState: [%s] has state (%d)", bundle.getSymbolicName(), bundleState));
137 if (bundleState < currentState) {
138 currentState = bundleState;
141 return AppcOamStates.getOamStateFromBundleState(currentState);