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 / VmStatuschecker.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.common.enums.Outcome;
35 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderOperation;
36 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
37 import org.openecomp.appc.configuration.Configuration;
38 import org.openecomp.appc.configuration.ConfigurationFactory;
39 import org.openecomp.appc.exceptions.UnknownProviderException;
40 import org.openecomp.appc.i18n.Msg;
41 import com.att.cdp.exceptions.ResourceNotFoundException;
42 import com.att.cdp.zones.Context;
43 import com.att.cdp.zones.model.ModelObject;
44 import com.att.cdp.zones.model.Server;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import com.att.eelf.i18n.EELFResourceManager;
48 import org.openecomp.sdnc.sli.SvcLogicContext;
49 import org.glassfish.grizzly.http.util.HttpStatus;
50 import org.slf4j.MDC;
51
52 import java.util.Map;
53
54 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
55 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
56 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
57
58
59 public class VmStatuschecker extends ProviderServerOperation {
60
61     private static final EELFLogger logger = EELFManager.getInstance().getLogger(VmStatuschecker.class);
62     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
63
64     /* *********************************************************************************/
65         /* DEVEN PANCHAL: This method is used to check the status of the VM               */
66     /**********************************************************************************/
67     public Server vmStatuschecker(Map<String, String> params, SvcLogicContext ctx) throws UnknownProviderException, IllegalArgumentException {
68         Server server = null;
69         RequestContext rc = new RequestContext(ctx);
70         rc.isAlive();
71
72         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
73
74         try {
75             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
76                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
77
78             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
79
80             VMURL vm = VMURL.parseURL(vm_url);
81             if (validateVM(rc, appName, vm_url, vm)) return null;
82
83             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
84             String identStr = (ident == null) ? null : ident.toString();
85
86             Context context = null;
87             try {
88                 context = getContext(rc, vm_url, identStr);
89                 if (context != null) {
90                     server = lookupServer(rc, context, vm.getServerId());
91                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
92
93                     String statusvm;
94                     switch (server.getStatus()) {
95                         case DELETED:
96                             statusvm = "deleted";
97                             break;
98
99                         case RUNNING:
100                             statusvm = "running";
101                             break;
102
103                         case ERROR:
104                             statusvm = "error";
105                             break;
106
107                         case READY:
108                             statusvm = "ready";
109                             break;
110
111                         case PAUSED:
112                             statusvm = "paused";
113                             break;
114
115                         case SUSPENDED:
116                             statusvm = "suspended";
117                             break;
118
119                         case PENDING:
120                             statusvm = "pending";
121                             break;
122
123                         default:
124                             statusvm = "default-unknown state-should never occur";
125                             break;
126                     }
127
128
129                     String statusofVM = statusvm;
130                     context.close();
131                     SvcLogicContext svcLogic = rc.getSvcLogicContext();
132                     svcLogic.setStatus(Outcome.SUCCESS.toString());
133                     svcLogic.setAttribute("org.openecomp.statusofvm", statusofVM);
134                     svcLogic.setAttribute(Constants.STATUS_OF_VM, statusofVM);
135                     svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, Integer.toString(HttpStatus.OK_200.getStatusCode()));
136                 }
137             } catch (ResourceNotFoundException e) {
138                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
139                 logger.error(msg);
140                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
141             } catch (Throwable t) {
142                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
143                         RESTART_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
144                 logger.error(msg, t);
145                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
146             }
147         } catch (RequestFailedException e) {
148             doFailure(rc, e.getStatus(), e.getMessage());
149         }
150
151         return server;
152     }
153
154    /* *********************************************************************************/
155
156     @Override
157     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws UnknownProviderException {
158
159         setMDC(Operation.VMSTATUSCHECK_SERVICE.toString(), "App-C IaaS Adapter:VmStatusCheck", ADAPTER_NAME);
160         logOperation(Msg.CHECKING_SERVER, params, context);
161         return vmStatuschecker(params, context);
162     }
163 }