Updating licenses in 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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
24
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
27 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
28 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
29 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
30 import org.openecomp.appc.adapter.iaas.impl.VMURL;
31 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
32 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
33 import org.openecomp.appc.exceptions.APPCException;
34 import org.openecomp.appc.i18n.Msg;
35 import com.att.cdp.exceptions.ResourceNotFoundException;
36 import com.att.cdp.zones.Context;
37 import com.att.cdp.zones.model.ModelObject;
38 import com.att.cdp.zones.model.Server;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.att.eelf.i18n.EELFResourceManager;
42 import org.openecomp.sdnc.sli.SvcLogicContext;
43 import org.glassfish.grizzly.http.util.HttpStatus;
44
45 import java.util.Map;
46
47 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.START_SERVICE;
48 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
49
50 public class StartServer extends ProviderServerOperation {
51
52     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartServer.class);
53
54     /**
55      * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#startServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
56      */
57     @SuppressWarnings("nls")
58     public Server startServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59         Server server = null;
60         RequestContext rc = new RequestContext(ctx);
61         rc.isAlive();
62
63         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
64
65         try {
66             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
67                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
68
69             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
70
71             VMURL vm = VMURL.parseURL(vm_url);
72             if (validateVM(rc, appName, vm_url, vm)) return null;
73
74             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
75             String identStr = (ident == null) ? null : ident.toString();
76
77             Context context = null;
78             ctx.setAttribute("START_STATUS", "ERROR");
79             try {
80                 context = getContext(rc, vm_url, identStr);
81                 if (context != null) {
82                     rc.reset();
83                     server = lookupServer(rc, context, vm.getServerId());
84                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
85                     String msg;
86
87                         /*
88                          * We determine what to do based on the current state of the server
89                          */
90
91                         /*
92                          * Pending is a bit of a special case. If we find the server is in a
93                          * pending state, then the provider is in the process of changing state
94                          * of the server. So, lets try to wait a little bit and see if the state
95                          * settles down to one we can deal with. If not, then we have to fail
96                          * the request.
97                          */
98
99                     if (server.getStatus().equals(Server.Status.PENDING)) {
100                         waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
101                                 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
102                     }
103
104                     switch (server.getStatus()) {
105                         case DELETED:
106                             // Nothing to do, the server is gone
107                             msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
108                                     server.getTenantId(), "started");
109                             logger.error(msg);
110                             //                                  metricsLogger.error(msg);
111                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
112
113                         case RUNNING:
114                             // Nothing to do, the server is already running
115                             logger.info("Server was already running");
116                             break;
117
118                         case ERROR:
119                             // Server is in error state
120                             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
121                                     server.getTenantId(), "start");
122                             logger.error(msg);
123                             //                                  metricsLogger.error(msg);
124                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
125
126                         case READY:
127                             // Server is stopped attempt to start the server
128                             rc.reset();
129                             startServer(rc, server);
130                             break;
131
132                         case PAUSED:
133                             // if paused, un-pause it
134                             rc.reset();
135                             unpauseServer(rc, server);
136                             //                                  metricsLogger.info("Server status: PAUSED");
137                             break;
138
139                         case SUSPENDED:
140                             // Attempt to resume the suspended server
141                             rc.reset();
142                             resumeServer(rc, server);
143                             //                                  metricsLogger.info("Server status: SUSPENDED");
144                             break;
145
146                         default:
147                             // Hmmm, unknown status, should never occur
148                             msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
149                                     server.getTenantId(), server.getStatus().name());
150                             generateEvent(rc, false, msg);
151                             logger.error(msg);
152                             //                                  metricsLogger.error(msg);
153                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
154                     }
155                     context.close();
156                     doSuccess(rc);
157                     ctx.setAttribute("START_STATUS", "SUCCESS");
158                 }
159                 else
160                 {
161                     ctx.setAttribute("START_STATUS", "CONTEXT_NOT_FOUND");
162                 }
163             } catch (ResourceNotFoundException e) {
164                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
165                 logger.error(msg);
166                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
167             } catch (Throwable t) {
168                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
169                         START_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
170                 logger.error(msg, t);
171                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
172             }
173         } catch (RequestFailedException e) {
174             doFailure(rc, e.getStatus(), e.getMessage());
175         }
176
177         return server;
178     }
179
180     @Override
181     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
182
183         setMDC(Operation.START_SERVICE.toString(), "App-C IaaS Adapter:Start", ADAPTER_NAME);
184         logOperation(Msg.STARTING_SERVER, params, context);
185         return startServer(params, context);
186     }
187 }