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;
60 public class DettachVolumeServer extends ProviderServerOperation {
61 private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
62 private static final Configuration config = ConfigurationFactory.getConfiguration();
65 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
66 throws APPCException {
67 setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
68 logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
69 return dettachVolume(params, context);
72 private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
74 RequestContext requestContext = new RequestContext(ctx);
75 requestContext.isAlive();
76 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
77 String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
78 String volumeId = params.get(ProviderAdapter.VOLUME_ID);
79 VMURL vm = VMURL.parseURL(vmUrl);
81 String tenantName = "Unknown";// to be used also in case of exception
83 if (validateVM(requestContext, appName, vmUrl, vm)) {
86 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
87 String identStr = (ident == null) ? null : ident.toString();
88 context = getContext(requestContext, vmUrl, identStr);
89 if (context != null) {
90 tenantName = context.getTenantName();// this variable also is
93 requestContext.reset();
94 server = lookupServer(requestContext, context, vm.getServerId());
95 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
96 if (volumeId == null || volumeId.isEmpty()) {
97 ctx.setAttribute("VOLUME_STATUS", "FAILURE");
98 doFailure(requestContext, HttpStatus.BAD_REQUEST_400, "Volumeid is mandatory");
100 Context contx = server.getContext();
101 ComputeService service = contx.getComputeService();
102 Volume volume = new Volume();
103 VolumeService vs = contx.getVolumeService();
104 Volume s = vs.getVolume(volumeId);
105 boolean flag = false;
106 if (validateDetach(service, vm.getServerId(), volumeId)) {
107 volume.setId(volumeId);
108 logger.info("Ready to Detach Volume from the server:");
109 service.detachVolume(server, volume);
112 String msg = "Volume with volume id " + volumeId + " cannot be detached as it does not exists";
113 logger.info("Volume doesnot exists:");
114 ctx.setAttribute("VOLUME_STATUS", "FAILURE");
115 doFailure(requestContext, HttpStatus.METHOD_NOT_ALLOWED_405, msg);
119 if (validateDetach(requestContext, service, vm.getServerId(), volumeId)) {
120 String msg = "Volume with volume id " + volumeId + " cannot be detached ";
121 ctx.setAttribute("VOLUME_STATUS", "FAILURE");
122 doFailure(requestContext, HttpStatus.CONFLICT_409, msg);
124 logger.info("status of detaching volume");
125 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
126 doSuccess(requestContext);
131 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
133 } catch (ZoneException e) {
134 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
136 doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
137 } catch (RequestFailedException e) {
138 logger.error("An error occurred when processing the request", e);
139 doFailure(requestContext, e.getStatus(), e.getMessage());
140 } catch (Exception e) {
141 String msg = EELFResourceManager.format(Msg.DETTACHINGVOLUME_SERVER, e, e.getClass().getSimpleName(),
142 DETACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
143 logger.error(msg, e);
145 ExceptionMapper.mapException((OpenStackBaseException) e);
146 } catch (ZoneException e1) {
147 logger.error(e1.getMessage());
150 doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
155 protected boolean validateDetach(ComputeService ser, String vm, String volumeId)
156 throws RequestFailedException, ZoneException {
157 boolean flag = false;
158 Map<String, String> map = ser.getAttachments(vm);
159 if (map != null && !(map.isEmpty())) {
160 Iterator<Entry<String, String>> it = map.entrySet().iterator();
161 while (it.hasNext()) {
162 Map.Entry volumes = (Map.Entry) it.next();
163 logger.info("volumes available in before detach");
164 logger.info("device" + volumes.getKey() + "volume" + volumes.getValue());
165 if (volumes.getValue().equals(volumeId)) {
170 logger.info("DettachVolume Flag" + flag);
174 protected boolean validateDetach(RequestContext rc, ComputeService ser, String vm, String volumeId)
175 throws RequestFailedException, ZoneException {
176 boolean flag = false;
178 config.setProperty(Constants.PROPERTY_RETRY_DELAY, "10");
179 config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "30");
180 while (rc.attempt()) {
181 Map<String, String> map = ser.getAttachments(vm);
182 if (map != null && !(map.isEmpty())) {
183 Iterator<Entry<String, String>> it = map.entrySet().iterator();
184 logger.info("volumes available after detach ");
185 while (it.hasNext()) {
186 Map.Entry volumes = (Map.Entry) it.next();
187 logger.info(" devices " + volumes.getKey() + " volumes" + volumes.getValue());
188 if (volumes.getValue().equals(volumeId)) {
189 logger.info("Device" + volumes.getKey() + "Volume" + volumes.getValue());
195 logger.info("Dettachvolume flag-->" + flag + "Attempts" + rc.getAttempts());
205 logger.info(rc.getAttempts() + "No.of attempts");
209 if ((rc.getAttempts() == 30) && (!flag)) {
210 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
211 Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
214 throw new TimeoutException(msg);
216 logger.info("DettachVolume Flag -->" + flag);