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