2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
27 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.DETACHVOLUME_SERVICE;
28 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
29 import com.att.cdp.exceptions.ZoneException;
30 import com.att.cdp.zones.ComputeService;
31 import com.att.cdp.zones.Context;
32 import com.att.cdp.zones.VolumeService;
33 import com.att.cdp.zones.model.ModelObject;
34 import com.att.cdp.zones.model.Server;
35 import com.att.cdp.zones.model.Volume;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
40 import java.util.Map.Entry;
41 import java.util.Iterator;
42 import org.glassfish.grizzly.http.util.HttpStatus;
43 import org.onap.appc.Constants;
44 import org.onap.appc.adapter.iaas.ProviderAdapter;
45 import org.onap.appc.adapter.iaas.impl.IdentityURL;
46 import org.onap.appc.adapter.iaas.impl.RequestContext;
47 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
48 import org.onap.appc.configuration.Configuration;
49 import org.onap.appc.configuration.ConfigurationFactory;
50 import org.onap.appc.adapter.iaas.impl.VMURL;
51 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
52 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
53 import org.onap.appc.exceptions.APPCException;
54 import com.att.cdp.exceptions.TimeoutException;
55 import com.att.cdp.openstack.util.ExceptionMapper;
56 import org.onap.appc.i18n.Msg;
57 import com.woorea.openstack.base.client.OpenStackBaseException;
58 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
59 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
61 public class DettachVolumeServer extends ProviderServerOperation {
62 private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
63 private static final Configuration config = ConfigurationFactory.getConfiguration();
66 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
67 throws APPCException {
68 setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
69 logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
70 return dettachVolume(params, context);
73 private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
75 RequestContext requestContext = new RequestContext(ctx);
76 requestContext.isAlive();
77 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
78 String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
79 String volumeId = params.get(ProviderAdapter.VOLUME_ID);
80 VMURL vm = VMURL.parseURL(vmUrl);
82 String tenantName = "Unknown";// to be used also in case of exception
84 if (validateVM(requestContext, appName, vmUrl, vm)) {
87 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
88 String identStr = (ident == null) ? null : ident.toString();
89 context = getContext(requestContext, vmUrl, identStr);
90 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
91 if (skipHypervisorCheck == null && ctx != null) {
92 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
94 if (context != null) {
95 tenantName = context.getTenantName();// this variable also is
98 requestContext.reset();
99 server = lookupServer(requestContext, context, vm.getServerId());
100 // Always perform Hypervisor check
101 // unless the skip is set to true
102 if (!"true".equalsIgnoreCase(skipHypervisorCheck)) {
103 // Check of the Hypervisor for the VM Server is UP and reachable
104 checkHypervisor(server);
106 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
107 if (volumeId == null || volumeId.isEmpty()) {
108 ctx.setAttribute("VOLUME_STATUS", "FAILURE");
109 doFailure(requestContext, HttpStatus.BAD_REQUEST_400, "Volumeid is mandatory");
111 Context contx = server.getContext();
112 ComputeService service = contx.getComputeService();
113 Volume volume = new Volume();
114 VolumeService vs = contx.getVolumeService();
115 Volume s = vs.getVolume(volumeId);
116 boolean flag = false;
117 if (validateDetach(service, vm.getServerId(), volumeId)) {
118 volume.setId(volumeId);
119 logger.info("Ready to Detach Volume from the server:");
120 service.detachVolume(server, volume);
123 String msg = "Volume with volume id " + volumeId + " cannot be detached as it does not exists";
124 logger.info("Volume doesnot exists:");
125 ctx.setAttribute("VOLUME_STATUS", "FAILURE");
126 doFailure(requestContext, HttpStatus.METHOD_NOT_ALLOWED_405, msg);
130 if (validateDetach(requestContext, service, vm.getServerId(), volumeId)) {
131 String msg = "Volume with volume id " + volumeId + " cannot be detached ";
132 ctx.setAttribute("VOLUME_STATUS", "FAILURE");
133 doFailure(requestContext, HttpStatus.CONFLICT_409, msg);
135 logger.info("status of detaching volume");
136 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
137 doSuccess(requestContext);
142 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
144 } catch (ZoneException e) {
145 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
147 doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
148 } catch (RequestFailedException e) {
149 logger.error("An error occurred when processing the request", e);
150 doFailure(requestContext, e.getStatus(), e.getMessage());
151 } catch (Exception e) {
152 String msg = EELFResourceManager.format(Msg.DETTACHINGVOLUME_SERVER, e, e.getClass().getSimpleName(),
153 DETACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
154 logger.error(msg, e);
156 ExceptionMapper.mapException((OpenStackBaseException) e);
157 } catch (ZoneException e1) {
158 logger.error(e1.getMessage());
161 doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
166 protected boolean validateDetach(ComputeService ser, String vm, String volumeId)
167 throws RequestFailedException, ZoneException {
168 boolean flag = false;
169 Map<String, String> map = ser.getAttachments(vm);
170 if (map != null && !(map.isEmpty())) {
171 Iterator<Entry<String, String>> it = map.entrySet().iterator();
172 while (it.hasNext()) {
173 Map.Entry volumes = (Map.Entry) it.next();
174 logger.info("volumes available in before detach");
175 logger.info("device" + volumes.getKey() + "volume" + volumes.getValue());
176 if (volumes.getValue().equals(volumeId)) {
181 logger.info("DettachVolume Flag" + flag);
185 protected boolean validateDetach(RequestContext rc, ComputeService ser, String vm, String volumeId)
186 throws RequestFailedException, ZoneException {
187 boolean flag = false;
189 config.setProperty(Constants.PROPERTY_RETRY_DELAY, "10");
190 config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "30");
191 while (rc.attempt()) {
192 Map<String, String> map = ser.getAttachments(vm);
193 if (map != null && !(map.isEmpty())) {
194 Iterator<Entry<String, String>> it = map.entrySet().iterator();
195 logger.info("volumes available after detach ");
196 while (it.hasNext()) {
197 Map.Entry volumes = (Map.Entry) it.next();
198 logger.info(" devices " + volumes.getKey() + " volumes" + volumes.getValue());
199 if (volumes.getValue().equals(volumeId)) {
200 logger.info("Device" + volumes.getKey() + "Volume" + volumes.getValue());
206 logger.info("Dettachvolume flag-->" + flag + "Attempts" + rc.getAttempts());
216 logger.info(rc.getAttempts() + "No.of attempts");
220 if ((rc.getAttempts() == 30) && (!flag)) {
221 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
222 Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
225 throw new TimeoutException(msg);
227 logger.info("DettachVolume Flag -->" + flag);