Fix for test coverage in oam.utils package
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / onap / appc / oam / util / StateHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.oam.util;
27
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;
32
33 import java.util.Map;
34
35 /*
36  * Utility class provides general state helps
37  */
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;
44
45     /**
46      * Constructor
47      *
48      * @param eelfLogger of the logger
49      */
50     public StateHelper(EELFLogger eelfLogger, ConfigurationHelper cHelper) {
51         logger = eelfLogger;
52         configurationHelper = cHelper;
53         appcOamCurrentState = AppcOamStates.Unknown;
54     }
55
56     /**
57      * Set the passed in state to the class <b>appOamCurrentState</b>.
58      *
59      * @param appcOamStates of the new state
60      */
61     public void setState(AppcOamStates appcOamStates) {
62         appcOamCurrentState = appcOamStates;
63     }
64
65     /**
66      * Get the state
67      * @return the class <b>appOamCurrentState</b>
68      */
69     public AppcOamStates getState() {
70         return appcOamCurrentState;
71     }
72
73     /**
74      * Validate if the passed in state is the same as the class <b>appOamCurrentState</b>.
75      *
76      * @param appcOamStates of the to be compared state
77      * @return true if they are the same, otherwise false
78      */
79     boolean isSameState(AppcOamStates appcOamStates) {
80         return appcOamCurrentState == appcOamStates;
81     }
82
83     /**
84      * Get APP-C OAM current state
85      *
86      * <p>When appcOamCurrentState is null or unknown, reset it with APPC LCM bundle state.
87      *
88      * @return AppcOamStates of the current APP-C OAM state
89      */
90     public AppcOamStates getCurrentOamState() {
91         if (appcOamCurrentState == null || appcOamCurrentState.equals(AppcOamStates.Unknown)) {
92             appcOamCurrentState = getBundlesState();
93         }
94         return appcOamCurrentState;
95     }
96
97     /**
98      * Use getCurrentOamState to get current OAM AppcOamStates and then convert to AppcState of Yang.
99      *
100      * @return AppcState of current OAM state
101      */
102     public AppcState getCurrentOamYangState() {
103         try {
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()));
108         }
109         return AppcState.Unknown;
110     }
111
112     /**
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>
122      *   -  ACTIVE      (32) <br>
123      *
124      * @return AppcOamStates
125      */
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;
131         }
132
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;
140             }
141         }
142         return AppcOamStates.getOamStateFromBundleState(currentState);
143     }
144
145     protected BundleHelper getBundleHelper(EELFLogger logger, ConfigurationHelper configurationHelper) {
146         return new BundleHelper(logger, configurationHelper, this);
147     }
148 }