2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
 
  23 package org.onap.appc.adapter.iaas.provider.operation.impl;
 
  25 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.DETACHVOLUME_SERVICE;
 
  26 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
 
  27 import com.att.cdp.exceptions.ZoneException;
 
  28 import com.att.cdp.zones.ComputeService;
 
  29 import com.att.cdp.zones.Context;
 
  30 import com.att.cdp.zones.VolumeService;
 
  31 import com.att.cdp.zones.model.ModelObject;
 
  32 import com.att.cdp.zones.model.Server;
 
  33 import com.att.cdp.zones.model.Volume;
 
  34 import com.att.eelf.configuration.EELFLogger;
 
  35 import com.att.eelf.configuration.EELFManager;
 
  36 import com.att.eelf.i18n.EELFResourceManager;
 
  37 import java.util.List;
 
  39 import java.util.Map.Entry;
 
  40 import java.util.Iterator;
 
  41 import org.glassfish.grizzly.http.util.HttpStatus;
 
  42 import org.onap.appc.Constants;
 
  43 import org.onap.appc.adapter.iaas.ProviderAdapter;
 
  44 import org.onap.appc.adapter.iaas.impl.IdentityURL;
 
  45 import org.onap.appc.adapter.iaas.impl.RequestContext;
 
  46 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
 
  47 import org.onap.appc.configuration.Configuration;
 
  48 import org.onap.appc.configuration.ConfigurationFactory;
 
  49 import org.onap.appc.adapter.iaas.impl.VMURL;
 
  50 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
 
  51 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
 
  52 import org.onap.appc.exceptions.APPCException;
 
  53 import com.att.cdp.exceptions.TimeoutException;
 
  54 import com.att.cdp.openstack.util.ExceptionMapper;
 
  55 import org.onap.appc.i18n.Msg;
 
  56 import com.woorea.openstack.base.client.OpenStackBaseException;
 
  57 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  59 public class DettachVolumeServer extends ProviderServerOperation {
 
  60     private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
 
  61     private static final Configuration config = ConfigurationFactory.getConfiguration();
 
  64     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
 
  65             throws APPCException {
 
  66         setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
 
  67         logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
 
  68         return dettachVolume(params, context);
 
  71     private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
 
  73         RequestContext requestContext = new RequestContext(ctx);
 
  74         requestContext.isAlive();
 
  75         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
 
  76         String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
 
  77         String volumeId = params.get(ProviderAdapter.VOLUME_ID);
 
  78         VMURL vm = VMURL.parseURL(vmUrl);
 
  80         String tenantName = "Unknown";// to be used also in case of exception
 
  82             if (validateVM(requestContext, appName, vmUrl, vm)) {
 
  85             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
 
  86             String identStr = (ident == null) ? null : ident.toString();
 
  87             context = getContext(requestContext, vmUrl, identStr);
 
  88             if (context != null) {
 
  89                 tenantName = context.getTenantName();// this variable also is
 
  92                 requestContext.reset();
 
  93                 server = lookupServer(requestContext, context, vm.getServerId());
 
  94                 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
 
  95                 Context contx = server.getContext();
 
  96                 ComputeService service = contx.getComputeService();
 
  97                 Volume volume = new Volume();
 
  98                 VolumeService vs = contx.getVolumeService();
 
  99                 Volume s = vs.getVolume(volumeId);
 
 100                 boolean flag = false;
 
 101                 if (validateDetach(service, vm.getServerId(), volumeId)) {
 
 102                     volume.setId(volumeId);
 
 103                     logger.info("Ready to Detach Volume from the server:");
 
 104                     service.detachVolume(server, volume);
 
 107                     String msg = "Volume with volume id " + volumeId + " cannot be detached as it does not exists";
 
 108                     logger.info("Volume doesnot exists:");
 
 109                     ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 110                     doFailure(requestContext, HttpStatus.METHOD_NOT_ALLOWED_405, msg);
 
 114                     if (validateDetach(requestContext, service, vm.getServerId(), volumeId)) {
 
 115                         String msg = "Volume with volume id " + volumeId + " cannot be detached ";
 
 116                         ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 117                         doFailure(requestContext, HttpStatus.CONFLICT_409, msg);
 
 119                         logger.info("status of detaching volume");
 
 120                         ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
 
 121                         doSuccess(requestContext);
 
 126                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
 
 128         } catch (ZoneException e) {
 
 129             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
 
 131             doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
 
 132         } catch (RequestFailedException e) {
 
 133             logger.error("An error occurred when processing the request", e);
 
 134             doFailure(requestContext, e.getStatus(), e.getMessage());
 
 135         } catch (Exception e) {
 
 136             String msg = EELFResourceManager.format(Msg.DETTACHINGVOLUME_SERVER, e, e.getClass().getSimpleName(),
 
 137                     DETACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
 
 138             logger.error(msg, e);
 
 140                 ExceptionMapper.mapException((OpenStackBaseException) e);
 
 141             } catch (ZoneException e1) {
 
 142                 logger.error(e1.getMessage());
 
 145             doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
 
 150     protected boolean validateDetach(ComputeService ser, String vm, String volumeId)
 
 151             throws RequestFailedException, ZoneException {
 
 152         boolean flag = false;
 
 153         Map<String, String> map = ser.getAttachments(vm);
 
 154         if (map != null && !(map.isEmpty())) {
 
 155             Iterator<Entry<String, String>> it = map.entrySet().iterator();
 
 156             while (it.hasNext()) {
 
 157                 Map.Entry volumes = (Map.Entry) it.next();
 
 158                 logger.info("volumes available in before detach");
 
 159                 logger.info("device" + volumes.getKey() + "volume" + volumes.getValue());
 
 160                 if (volumes.getValue().equals(volumeId)) {
 
 165         logger.info("DettachVolume  Flag" + flag);
 
 169     protected boolean validateDetach(RequestContext rc, ComputeService ser, String vm, String volumeId)
 
 170             throws RequestFailedException, ZoneException {
 
 171         boolean flag = false;
 
 173         config.setProperty(Constants.PROPERTY_RETRY_DELAY, "10");
 
 174         config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "30");
 
 175         while (rc.attempt()) {
 
 176             Map<String, String> map = ser.getAttachments(vm);
 
 177             if (map != null && !(map.isEmpty())) {
 
 178             Iterator<Entry<String, String>> it = map.entrySet().iterator();
 
 179             logger.info("volumes available after  detach ");
 
 180             while (it.hasNext()) {
 
 181                 Map.Entry volumes = (Map.Entry) it.next();
 
 182                 logger.info(" devices " + volumes.getKey() + " volumes" + volumes.getValue());
 
 183                 if (volumes.getValue().equals(volumeId)) {
 
 184                     logger.info("Device" + volumes.getKey() + "Volume" + volumes.getValue());
 
 190                 logger.info("Dettachvolume flag-->" + flag+"Attempts"+rc.getAttempts());
 
 202                 logger.info( rc.getAttempts() + "No.of attempts");
 
 206         if ((rc.getAttempts() == 30) && (!flag)) {
 
 207             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
 
 208                     Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
 
 211             throw new TimeoutException(msg);
 
 213         logger.info("DettachVolume Flag -->" + flag);