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