973d0af368f5b7be9fd5eb388af46747aad05d89
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / OamMmodeProcessor.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.openecomp.appc.oam.processor;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.openecomp.appc.exceptions.APPCException;
29 import org.openecomp.appc.exceptions.InvalidInputException;
30 import org.openecomp.appc.exceptions.InvalidStateException;
31 import org.openecomp.appc.i18n.Msg;
32 import org.openecomp.appc.oam.AppcOam;
33 import org.openecomp.appc.oam.OAMCommandStatus;
34 import org.openecomp.appc.oam.util.AsyncTaskHelper;
35 import org.openecomp.appc.oam.util.ConfigurationHelper;
36 import org.openecomp.appc.oam.util.OperationHelper;
37 import org.openecomp.appc.oam.util.StateHelper;
38 import org.openecomp.appc.requesthandler.LCMStateManager;
39 import org.openecomp.appc.requesthandler.RequestHandler;
40 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
41
42 /**
43  * Processor to handle maintenance mode OAM API.
44  */
45 public class OamMmodeProcessor extends BaseProcessor {
46     /**
47      * Constructor
48      *
49      * @param eelfLogger          for logging
50      * @param configurationHelper for property reading
51      * @param stateHelper         for APP-C OAM state checking
52      * @param asyncTaskHelper     for scheduling async task
53      * @param operationHelper     for operational helper
54      */
55     public OamMmodeProcessor(EELFLogger eelfLogger,
56                              ConfigurationHelper configurationHelper,
57                              StateHelper stateHelper,
58                              AsyncTaskHelper asyncTaskHelper,
59                              OperationHelper operationHelper) {
60         super(eelfLogger, configurationHelper, stateHelper, asyncTaskHelper, operationHelper);
61
62         rpc = AppcOam.RPC.maintenance_mode;
63         auditMsg = Msg.OAM_OPERATION_ENTERING_MAINTENANCE_MODE;
64     }
65
66     @Override
67     protected void preProcess(final Object requestInput)
68             throws InvalidInputException, InvalidStateException, APPCException {
69         super.preProcess(requestInput);
70
71         //Close the gate so that no more new LCM request will be excepted.
72         LCMStateManager lcmStateManager = operationHelper.getService(LCMStateManager.class);
73         lcmStateManager.disableLCMOperations();
74     }
75
76     @Override
77     protected void scheduleAsyncTask() {
78         runnable = new MyRunnable(this);
79         super.scheduleAsyncTask();
80     }
81
82     /**
83      * This runnable does the async handling for the maintenance mode REST API, and will be scheduled to run
84      * until terminating condition reaches.
85      *
86      * <p>The runnable will conintue run if: <br>
87      *   - the runnable is not canceled outside <br>
88      *   - the in progress LCM request count is not zero<br>
89      * <p> When LCM request count reaches to zero, this runnable will: <br>
90      *     - post message through operatonHelper <br>
91      *     - set APP-C OAM state to maintenance mode <br>
92      *     - audit log the state <br>
93      *     - terminate this runnable itself <br>
94      */
95     class MyRunnable extends BaseActionRunnable {
96         private int inprogressRequestCount;
97
98         MyRunnable(BaseProcessor parent) {
99             super(parent);
100
101             actionName = "OAM Maintanence mode";
102             auditMsg = Msg.OAM_OPERATION_MAINTENANCE_MODE;
103             finalState = AppcOamStates.MaintenanceMode;
104         }
105
106         @Override
107         boolean doAction() {
108             // always return true, so that we can check the LCM request count
109             return true;
110         }
111
112         @Override
113         boolean checkState() {
114             logDebug(String.format("Executing %s task", actionName));
115
116             if (!myParent.isSameAsyncTask()) {
117                 // cancel myself if I am not the current backgroundOamTask
118                 myParent.cancelAsyncTask();
119                 logDebug(String.format("Finished %s task due to task removed", actionName));
120                 return true;
121             }
122
123             boolean hasError = false;
124             try {
125                 inprogressRequestCount = getInprogressLCMRequestCount();
126                 if (inprogressRequestCount > 0) {
127                     // if there are still LCM request in progress, keep waiting
128                     return false;
129                 }
130
131                 setStatus(OAMCommandStatus.SUCCESS);
132             } catch (Exception e) {
133                 setErrorStatus(e);
134                 hasError = true;
135             }
136
137             postAction(hasError ? AppcOamStates.Error : finalState);
138             return true;
139         }
140
141         /**
142          * Get in progress LCM request count through RequestHandler.
143          * @return thecount of in progress LCM request
144          * @throws APPCException if RequestHandler throws it.
145          */
146         private int getInprogressLCMRequestCount() throws APPCException {
147             RequestHandler requestHandler = operationHelper.getService(RequestHandler.class);
148
149             if (requestHandler == null) {
150                 return 0;
151             }
152
153             return requestHandler.getInprogressRequestCount();
154         }
155
156         @Override
157         void keepWaiting() {
158             logDebug("The application '%s' has '%s' outstanding LCM request to complete" +
159                             " before coming to a complete maintenance_mode.",
160                     configurationHelper.getAppcName(), inprogressRequestCount);
161         }
162     }
163 }