d8d88d319c59c9ee792880606e7fe326ed8fd7a4
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / OamStopProcessor.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.i18n.Msg;
30 import org.openecomp.appc.oam.AppcOam;
31 import org.openecomp.appc.oam.util.AsyncTaskHelper;
32 import org.openecomp.appc.oam.util.ConfigurationHelper;
33 import org.openecomp.appc.oam.util.OperationHelper;
34 import org.openecomp.appc.oam.util.StateHelper;
35 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
36
37 /**
38  * Processor to handle stop OAM API.
39  */
40 public class OamStopProcessor extends BaseProcessor {
41     /**
42      * Constructor
43      *
44      * @param eelfLogger          for logging
45      * @param configurationHelper for property reading
46      * @param stateHelper         for APP-C OAM state checking
47      * @param asyncTaskHelper     for scheduling async task
48      * @param operationHelper     for operational helper
49      */
50     public OamStopProcessor(EELFLogger eelfLogger,
51                             ConfigurationHelper configurationHelper,
52                             StateHelper stateHelper,
53                             AsyncTaskHelper asyncTaskHelper,
54                             OperationHelper operationHelper) {
55         super(eelfLogger, configurationHelper, stateHelper, asyncTaskHelper, operationHelper);
56
57         rpc = AppcOam.RPC.stop;
58         auditMsg = Msg.OAM_OPERATION_STOPPING;
59     }
60
61
62     @Override
63     protected void scheduleAsyncTask() {
64         runnable = new MyRunnable(this);
65         super.scheduleAsyncTask();
66     }
67
68     /**
69      * This runnable does the async handling for the stop REST API. And it will be scheduled to run one time.
70      *
71      * <p>This runnable will the following operations: <br>
72      *     - do APP-C OAM bundle stop and then refresh through BundlerHelper<br>
73      * <p>Once above operations are done, the runnale will <br>
74      *     - post message through operatonHelper <br>
75      *     - set APP-C OAM state to started <br>
76      *     - audit log the state <br>
77      */
78     class MyRunnable  extends BaseActionRunnable {
79
80         MyRunnable(BaseProcessor parent) {
81             super(parent);
82             actionName = "OAM Stop";
83             auditMsg = Msg.OAM_OPERATION_STOPPED;
84             finalState = AppcOamStates.Stopped;
85         }
86
87         /**
88          * Do stop action, include stop bundle .
89          * @return true if action is successful, false when aciton is failed.
90          */
91         @Override
92         boolean doAction() {
93             logDebug(String.format("Executing %s task", actionName));
94
95             try {
96                 boolean isBundleOperationCompleted = bundleHelper.bundleOperations(
97                     rpc, bundleNameToFuture, myParent.asyncTaskHelper, this);
98                 if (isBundleOperationCompleted) {
99                     return true;
100                 }
101
102                 setAbortStatus();
103             } catch (APPCException e) {
104                 setErrorStatus(e);
105                 stateHelper.setState(AppcOamStates.Error);
106             }
107             return false;
108         }
109
110         /**
111          * Final handling
112          * @param setState boolean to indicate if set OAM state or not
113          */
114         @Override
115         void postDoAction(boolean setState) {
116             postAction(setState ? finalState : null);
117             super.postDoAction(setState);
118         }
119     }
120 }