Applying license changes to all files
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / openecomp / appc / adapter / iaas / provider / operation / impl / StartServer.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.adapter.iaas.provider.operation.impl;
26
27 import org.openecomp.appc.Constants;
28 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
29 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
30 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
31 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
32 import org.openecomp.appc.adapter.iaas.impl.VMURL;
33 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
34 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
35 import org.openecomp.appc.exceptions.APPCException;
36 import org.openecomp.appc.i18n.Msg;
37 import com.att.cdp.exceptions.ResourceNotFoundException;
38 import com.att.cdp.zones.Context;
39 import com.att.cdp.zones.model.ModelObject;
40 import com.att.cdp.zones.model.Server;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.eelf.i18n.EELFResourceManager;
44 import org.openecomp.sdnc.sli.SvcLogicContext;
45 import org.glassfish.grizzly.http.util.HttpStatus;
46
47 import java.util.Map;
48
49 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.START_SERVICE;
50 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
51
52 public class StartServer extends ProviderServerOperation {
53
54     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartServer.class);
55
56     /**
57      * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#startServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
58      */
59     @SuppressWarnings("nls")
60     public Server startServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
61         Server server = null;
62         RequestContext rc = new RequestContext(ctx);
63         rc.isAlive();
64
65         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
66
67         try {
68             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
69                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
70
71             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
72
73             VMURL vm = VMURL.parseURL(vm_url);
74             if (validateVM(rc, appName, vm_url, vm)) return null;
75
76             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
77             String identStr = (ident == null) ? null : ident.toString();
78
79             Context context = null;
80             ctx.setAttribute("START_STATUS", "ERROR");
81             try {
82                 context = getContext(rc, vm_url, identStr);
83                 if (context != null) {
84                     rc.reset();
85                     server = lookupServer(rc, context, vm.getServerId());
86                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
87                     String msg;
88
89                         /*
90                          * We determine what to do based on the current state of the server
91                          */
92
93                         /*
94                          * Pending is a bit of a special case. If we find the server is in a
95                          * pending state, then the provider is in the process of changing state
96                          * of the server. So, lets try to wait a little bit and see if the state
97                          * settles down to one we can deal with. If not, then we have to fail
98                          * the request.
99                          */
100
101                     if (server.getStatus().equals(Server.Status.PENDING)) {
102                         waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
103                                 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
104                     }
105
106                     switch (server.getStatus()) {
107                         case DELETED:
108                             // Nothing to do, the server is gone
109                             msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
110                                     server.getTenantId(), "started");
111                             logger.error(msg);
112                             //                                  metricsLogger.error(msg);
113                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
114
115                         case RUNNING:
116                             // Nothing to do, the server is already running
117                             logger.info("Server was already running");
118                             break;
119
120                         case ERROR:
121                             // Server is in error state
122                             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
123                                     server.getTenantId(), "start");
124                             logger.error(msg);
125                             //                                  metricsLogger.error(msg);
126                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
127
128                         case READY:
129                             // Server is stopped attempt to start the server
130                             rc.reset();
131                             startServer(rc, server);
132                             break;
133
134                         case PAUSED:
135                             // if paused, un-pause it
136                             rc.reset();
137                             unpauseServer(rc, server);
138                             //                                  metricsLogger.info("Server status: PAUSED");
139                             break;
140
141                         case SUSPENDED:
142                             // Attempt to resume the suspended server
143                             rc.reset();
144                             resumeServer(rc, server);
145                             //                                  metricsLogger.info("Server status: SUSPENDED");
146                             break;
147
148                         default:
149                             // Hmmm, unknown status, should never occur
150                             msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
151                                     server.getTenantId(), server.getStatus().name());
152                             generateEvent(rc, false, msg);
153                             logger.error(msg);
154                             //                                  metricsLogger.error(msg);
155                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
156                     }
157                     context.close();
158                     doSuccess(rc);
159                     ctx.setAttribute("START_STATUS", "SUCCESS");
160                 }
161                 else
162                 {
163                     ctx.setAttribute("START_STATUS", "CONTEXT_NOT_FOUND");
164                 }
165             } catch (ResourceNotFoundException e) {
166                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
167                 logger.error(msg);
168                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
169             } catch (Throwable t) {
170                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
171                         START_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
172                 logger.error(msg, t);
173                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
174             }
175         } catch (RequestFailedException e) {
176             doFailure(rc, e.getStatus(), e.getMessage());
177         }
178
179         return server;
180     }
181
182     @Override
183     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
184
185         setMDC(Operation.START_SERVICE.toString(), "App-C IaaS Adapter:Start", ADAPTER_NAME);
186         logOperation(Msg.STARTING_SERVER, params, context);
187         return startServer(params, context);
188     }
189 }