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