First part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / OamRestartProcessor.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.processor;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.onap.appc.exceptions.APPCException;
29 import org.onap.appc.i18n.Msg;
30 import org.onap.appc.oam.AppcOam;
31 import org.onap.appc.oam.util.AsyncTaskHelper;
32 import org.onap.appc.oam.util.ConfigurationHelper;
33 import org.onap.appc.oam.util.OperationHelper;
34 import org.onap.appc.oam.util.StateHelper;
35 import org.onap.appc.requesthandler.LCMStateManager;
36 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
37
38 /**
39  * Processor to handle restart OAM API.
40  */
41 public class OamRestartProcessor extends BaseProcessor {
42     /**
43      * Action phases:
44      * <br> -ToStop: call bundles stop
45      * <br> -Stopped: check if all bundle state reached stopped
46      * <br> -ToStart: call bundles start
47      * <br> -Started: action is full completed
48      * <br> -Error: indication of error, such as timeout reached, bundler operation failure and etc.
49      */
50     private enum ActionPhases {
51         ToStop,
52         Stopped,
53         ToStart,
54         Started,
55         Error
56     }
57
58     /**
59      * Constructor
60      *
61      * @param eelfLogger          for logging
62      * @param configurationHelper for property reading
63      * @param stateHelper         for APP-C OAM state checking
64      * @param asyncTaskHelper     for scheduling async task
65      * @param operationHelper     for operational helper
66      */
67     public OamRestartProcessor(EELFLogger eelfLogger,
68                                ConfigurationHelper configurationHelper,
69                                StateHelper stateHelper,
70                                AsyncTaskHelper asyncTaskHelper,
71                                OperationHelper operationHelper) {
72         super(eelfLogger, configurationHelper, stateHelper, asyncTaskHelper, operationHelper);
73
74         rpc = AppcOam.RPC.restart;
75         auditMsg = Msg.OAM_OPERATION_RESTARTING;
76     }
77
78     @Override
79     protected void scheduleAsyncTask() {
80         runnable = new MyRunnable(this);
81         super.scheduleAsyncTask();
82     }
83
84     /**
85      * This runnable does the async handling for the restart REST API. And it will be scheduled to run one time.
86      *
87      * <p>This runnable will the following operations: <br>
88      *     - do APP-C OAM bundle stop and then start through BundlerHelper<br>
89      *     - and always enable LCM operation handling (which can be disabled through maintenance mode API).<br>
90      * <p>Once above operations are done, the runnale will <br>
91      *     - post message through operatonHelper <br>
92      *     - set APP-C OAM state to started <br>
93      *     - audit log the state <br>
94      */
95     class MyRunnable extends BaseActionRunnable {
96
97         ActionPhases currentPhase = ActionPhases.ToStop;
98         private LCMStateManager lcmStateManager;
99
100         MyRunnable(BaseProcessor parent) {
101             super(parent);
102
103             actionName = "OAM Restart";
104             auditMsg = Msg.OAM_OPERATION_RESTARTED;
105             finalState = AppcOamStates.Started;
106         }
107
108         /**
109          * Do restart action, include stop then start and always enable LCM operation.
110          * @return true if action is successful, false when aciton is failed or aborted
111          */
112         @Override
113         boolean doAction() {
114             logDebug(String.format("Executing %s task at phase (%s)",
115                 actionName, currentPhase == null ? "null" : currentPhase.name()));
116
117             boolean isBundleOperationCompleted = true;
118             try {
119                 switch (currentPhase) {
120                     case ToStop:
121                         isBundleOperationCompleted = bundleHelper.bundleOperations(
122                             AppcOam.RPC.stop, bundleNameToFuture, myParent.asyncTaskHelper, this);
123                         currentPhase = ActionPhases.Stopped;
124                         break;
125                     case Stopped:
126                         // check state
127                         AppcOamStates currentState = stateHelper.getBundlesState();
128                         if (currentState == AppcOamStates.Stopped) {
129                             currentPhase = ActionPhases.ToStart;
130                         } else {
131                             logDebug(String.format("%s task is waiting in stopped phase, current state is %s",
132                                 actionName, currentState));
133                         }
134                         break;
135                     case ToStart:
136                         isBundleOperationCompleted = bundleHelper.bundleOperations(
137                             AppcOam.RPC.start, bundleNameToFuture, myParent.asyncTaskHelper, this);
138                         currentPhase = ActionPhases.Started;
139                         break;
140                     case Error:
141                         // do nothing
142                         break;
143                     default:
144                         // Should not reach log it and return false;
145                         logger.error("%s task doAction reached %s phase. not supported. return false.",
146                             actionName, currentPhase.name());
147                         stateHelper.setState(AppcOamStates.Error);
148                         return false;
149                 }
150
151                 if (isTimeout("restart doAction")
152                     || hasBundleOperationFailure()) {
153                     currentPhase = ActionPhases.Error;
154                     return true;
155                 }
156                 if (isBundleOperationCompleted) {
157                     return true;
158                 }
159
160                 setAbortStatus();
161             } catch (APPCException e) {
162                 setErrorStatus(e);
163                 stateHelper.setState(AppcOamStates.Error);
164             }
165
166             return false;
167         }
168
169         /**
170          * With additional to get the LCMStateManager service
171          * @see BaseActionRunnable#checkState()
172          */
173         @Override
174         boolean checkState() {
175             switch (currentPhase) {
176                 case Started:
177                     try {
178                         lcmStateManager = operationHelper.getService(LCMStateManager.class);
179                         return super.checkState();
180                     } catch (APPCException e) {
181                         logDebug("LCMStateManager is not available.");
182                     }
183                     break;
184                 default:
185                     // in all the other ActionPhase, we want the run go back to doAction
186                     return true;
187             }
188             return false;
189         }
190
191         /**
192          * Final handling. The thread is cancelled.
193          * @param setState boolean to indicate if set OAM state or not
194          */
195         @Override
196         void postDoAction(boolean setState) {
197             AppcOamStates newState = null;
198             if (setState) {
199                 logDebug("Always enable LCM operation");
200                 lcmStateManager.enableLCMOperations();
201                 newState = finalState;
202             }
203             postAction(newState);
204             super.postDoAction(setState);
205         }
206
207     }
208 }