First part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / util / StateHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.oam.util;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.AppcState;
29 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
30 import org.osgi.framework.Bundle;
31
32 import java.util.Map;
33
34 /*
35  * Utility class provides general state helps
36  */
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;
43
44     /**
45      * Constructor
46      *
47      * @param eelfLogger of the logger
48      */
49     public StateHelper(EELFLogger eelfLogger, ConfigurationHelper cHelper) {
50         logger = eelfLogger;
51         configurationHelper = cHelper;
52         appcOamCurrentState = AppcOamStates.Unknown;
53     }
54
55     /**
56      * Set the passed in state to the class <b>appOamCurrentState</b>.
57      *
58      * @param appcOamStates of the new state
59      */
60     public void setState(AppcOamStates appcOamStates) {
61         appcOamCurrentState = appcOamStates;
62     }
63
64     /**
65      * Get the state
66      * @return the class <b>appOamCurrentState</b>
67      */
68     public AppcOamStates getState() {
69         return appcOamCurrentState;
70     }
71
72     /**
73      * Validate if the passed in state is the same as the class <b>appOamCurrentState</b>.
74      *
75      * @param appcOamStates of the to be compared state
76      * @return true if they are the same, otherwise false
77      */
78     boolean isSameState(AppcOamStates appcOamStates) {
79         return appcOamCurrentState == appcOamStates;
80     }
81
82     /**
83      * Get APP-C OAM current state
84      *
85      * <p>When appcOamCurrentState is null or unknown, reset it with APPC LCM bundle state.
86      *
87      * @return AppcOamStates of the current APP-C OAM state
88      */
89     public AppcOamStates getCurrentOamState() {
90         if (appcOamCurrentState == null || appcOamCurrentState.equals(AppcOamStates.Unknown)) {
91             appcOamCurrentState = getBundlesState();
92         }
93         return appcOamCurrentState;
94     }
95
96     /**
97      * Use getCurrentOamState to get current OAM AppcOamStates and then convert to AppcState of Yang.
98      *
99      * @return AppcState of current OAM state
100      */
101     public AppcState getCurrentOamYangState() {
102         try {
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()));
107         }
108         return AppcState.Unknown;
109     }
110
111     /**
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>
121      *   -  ACTIVE      (32) <br>
122      *
123      * @return AppcOamStates
124      */
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;
130         }
131
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;
139             }
140         }
141         return AppcOamStates.getOamStateFromBundleState(currentState);
142     }
143
144 }