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