2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
24 import org.openecomp.appc.Constants;
25 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
26 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
27 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
28 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
29 import org.openecomp.appc.adapter.iaas.impl.VMURL;
30 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
31 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Outcome;
32 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderOperation;
33 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
34 import org.openecomp.appc.configuration.Configuration;
35 import org.openecomp.appc.configuration.ConfigurationFactory;
36 import org.openecomp.appc.exceptions.UnknownProviderException;
37 import org.openecomp.appc.i18n.Msg;
38 import com.att.cdp.exceptions.ResourceNotFoundException;
39 import com.att.cdp.zones.Context;
40 import com.att.cdp.zones.model.ModelObject;
41 import com.att.cdp.zones.model.Server;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44 import com.att.eelf.i18n.EELFResourceManager;
45 import org.openecomp.sdnc.sli.SvcLogicContext;
46 import org.glassfish.grizzly.http.util.HttpStatus;
51 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
52 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
53 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
56 public class VmStatuschecker extends ProviderServerOperation {
58 private static final EELFLogger logger = EELFManager.getInstance().getLogger(VmStatuschecker.class);
59 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
61 /* *********************************************************************************/
62 /* DEVEN PANCHAL: This method is used to check the status of the VM */
63 /**********************************************************************************/
64 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext ctx) throws UnknownProviderException, IllegalArgumentException {
66 RequestContext rc = new RequestContext(ctx);
69 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
72 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
73 ProviderAdapter.PROPERTY_PROVIDER_NAME);
75 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
77 VMURL vm = VMURL.parseURL(vm_url);
78 if (validateVM(rc, appName, vm_url, vm)) return null;
80 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
81 String identStr = (ident == null) ? null : ident.toString();
83 Context context = null;
85 context = getContext(rc, vm_url, identStr);
86 if (context != null) {
87 server = lookupServer(rc, context, vm.getServerId());
88 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
91 switch (server.getStatus()) {
113 statusvm = "suspended";
117 statusvm = "pending";
121 statusvm = "default-unknown state-should never occur";
126 String statusofVM = statusvm;
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, Integer.toString(HttpStatus.OK_200.getStatusCode()));
134 } catch (ResourceNotFoundException e) {
135 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
137 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
138 } catch (Throwable t) {
139 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
140 RESTART_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
141 logger.error(msg, t);
142 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
144 } catch (RequestFailedException e) {
145 doFailure(rc, e.getStatus(), e.getMessage());
151 /* *********************************************************************************/
154 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) 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);