First part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / OamStartProcessor.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 start OAM API.
40  */
41 public class OamStartProcessor extends BaseProcessor {
42
43     /**
44      * Constructor
45      *
46      * @param eelfLogger          for logging
47      * @param configurationHelper for property reading
48      * @param stateHelper         for APP-C OAM state checking
49      * @param asyncTaskHelper     for scheduling async task
50      * @param operationHelper     for operational helper
51      */
52     public OamStartProcessor(EELFLogger eelfLogger,
53                              ConfigurationHelper configurationHelper,
54                              StateHelper stateHelper,
55                              AsyncTaskHelper asyncTaskHelper,
56                              OperationHelper operationHelper) {
57         super(eelfLogger, configurationHelper, stateHelper, asyncTaskHelper, operationHelper);
58
59         rpc = AppcOam.RPC.start;
60         auditMsg = Msg.OAM_OPERATION_STARTING;
61     }
62
63     @Override
64     protected void scheduleAsyncTask() {
65         runnable = new MyRunnable(this);
66         super.scheduleAsyncTask();
67     }
68
69     /**
70      * This runnable does the async handling for the start REST API. And it will be scheduled to run one time.
71      *
72      * <p>This runnable will the following operations: <br>
73      *     - do APP-C OAM bundle start through BundlerHelper<br>
74      *     - and always enable LCM operation handling (which can be disabled through maintenance mode API).<br>
75      * <p>Once above operations are done, the runnale will <br>
76      *     - post message through operatonHelper <br>
77      *     - set APP-C OAM state to started <br>
78      *     - audit log the state <br>
79      */
80     class MyRunnable extends BaseActionRunnable {
81
82         private LCMStateManager lcmStateManager;
83
84         MyRunnable(BaseProcessor parent) {
85             super(parent);
86             actionName = "OAM Start";
87             auditMsg = Msg.OAM_OPERATION_STARTED;
88             finalState = AppcOamStates.Started;
89         }
90
91         /**
92          * Do start action, include start bundle if needed and always enable LCM operation.
93          * @return true if action is successful, false when aciton is failed or aborted
94          */
95         @Override
96         boolean doAction() {
97             logDebug(String.format("Executing %s task", actionName));
98
99             boolean isBundleOperationCompleted = true;
100             try {
101                 if (stateHelper.getState() != AppcOamStates.Started) {
102                     logDebug("Start - APPC OAM state is not started, start the bundles");
103                     isBundleOperationCompleted = bundleHelper.bundleOperations(
104                         rpc, bundleNameToFuture, myParent.asyncTaskHelper, this);
105                 }
106
107                 if (isBundleOperationCompleted) {
108                     return true;
109                 }
110
111                 setAbortStatus();
112             } catch (APPCException e) {
113                 setErrorStatus(e);
114                 stateHelper.setState(AppcOamStates.Error);
115             }
116
117             return false;
118         }
119
120         /**
121          * With additional to get the LCMStateManager service
122          * @see BaseActionRunnable#checkState()
123          */
124         @Override
125         boolean checkState() {
126             try {
127                 lcmStateManager = operationHelper.getService(LCMStateManager.class);
128                 return super.checkState();
129             } catch (APPCException e) {
130                 logDebug("LCMStateManager is not available.");
131                 return false;
132             }
133         }
134
135         /**
136          * Final handling
137          * @param setState boolean to indicate if set OAM state or not
138          */
139         @Override
140         void postDoAction(boolean setState) {
141             AppcOamStates newState = null;
142             if (setState) {
143                 logDebug("Always enable LCM operation");
144                 lcmStateManager.enableLCMOperations();
145                 newState = finalState;
146             }
147             postAction(newState);
148             super.postDoAction(setState);
149         }
150     }
151 }