2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
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;
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;
53 public class VmStatuschecker extends ProviderServerOperation {
55 private static final EELFLogger logger = EELFManager.getInstance().getLogger(VmStatuschecker.class);
56 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
59 * to check the status of the VM
61 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext ctx)
62 throws UnknownProviderException, IllegalArgumentException {
64 RequestContext rc = new RequestContext(ctx);
67 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
70 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
71 ProviderAdapter.PROPERTY_PROVIDER_NAME);
73 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
75 VMURL vm = VMURL.parseURL(vm_url);
76 if (validateVM(rc, appName, vm_url, vm))
79 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
80 String identStr = (ident == null) ? null : ident.toString();
82 Context context = null;
83 String tenantName = "Unknown";//to be used also in case of exception
85 context = getContext(rc, vm_url, identStr);
86 if (context != null) {
87 tenantName = context.getTenantName();//this varaible also is used in case of exception
88 server = lookupServer(rc, context, vm.getServerId());
89 logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
92 switch (server.getStatus()) {
114 statusvm = "suspended";
118 statusvm = "pending";
122 statusvm = "default-unknown state-should never occur";
127 String statusofVM = statusvm;
129 SvcLogicContext svcLogic = rc.getSvcLogicContext();
130 svcLogic.setStatus(Outcome.SUCCESS.toString());
131 svcLogic.setAttribute("org.openecomp.statusofvm", statusofVM);
132 svcLogic.setAttribute(Constants.STATUS_OF_VM, statusofVM);
133 svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE,
134 Integer.toString(HttpStatus.OK_200.getStatusCode()));
136 } catch (ResourceNotFoundException e) {
137 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
139 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
140 } catch (Exception e1) {
141 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
142 e1.getClass().getSimpleName(), RESTART_SERVICE.toString(), vm_url,
144 logger.error(msg, e1);
145 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
147 } catch (RequestFailedException e) {
148 doFailure(rc, e.getStatus(), e.getMessage());
155 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
156 throws UnknownProviderException {
157 setMDC(Operation.VMSTATUSCHECK_SERVICE.toString(), "App-C IaaS Adapter:VmStatusCheck", ADAPTER_NAME);
158 logOperation(Msg.CHECKING_SERVER, params, context);
159 return vmStatuschecker(params, context);