2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2019 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.ATTACHVOLUME_SERVICE;
 
  28 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
 
  29 import java.util.Iterator;
 
  30 import java.util.List;
 
  32 import java.util.Map.Entry;
 
  33 import org.glassfish.grizzly.http.util.HttpStatus;
 
  34 import org.onap.appc.Constants;
 
  35 import org.onap.appc.adapter.iaas.ProviderAdapter;
 
  36 import org.onap.appc.adapter.iaas.impl.IdentityURL;
 
  37 import org.onap.appc.adapter.iaas.impl.RequestContext;
 
  38 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
 
  39 import org.onap.appc.adapter.iaas.impl.VMURL;
 
  40 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
 
  41 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
 
  42 import org.onap.appc.configuration.Configuration;
 
  43 import org.onap.appc.configuration.ConfigurationFactory;
 
  44 import org.onap.appc.exceptions.APPCException;
 
  45 import org.onap.appc.i18n.Msg;
 
  46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  47 import com.att.cdp.exceptions.TimeoutException;
 
  48 import com.att.cdp.exceptions.ZoneException;
 
  49 import com.att.cdp.openstack.util.ExceptionMapper;
 
  50 import com.att.cdp.zones.ComputeService;
 
  51 import com.att.cdp.zones.Context;
 
  52 import com.att.cdp.zones.VolumeService;
 
  53 import com.att.cdp.zones.model.ModelObject;
 
  54 import com.att.cdp.zones.model.Server;
 
  55 import com.att.cdp.zones.model.Volume;
 
  56 import com.att.eelf.configuration.EELFLogger;
 
  57 import com.att.eelf.configuration.EELFManager;
 
  58 import com.att.eelf.i18n.EELFResourceManager;
 
  59 import com.woorea.openstack.base.client.OpenStackBaseException;
 
  60 import org.apache.commons.lang.StringUtils;
 
  61 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
 
  63 public class AttachVolumeServer extends ProviderServerOperation {
 
  65     private final EELFLogger logger = EELFManager.getInstance().getLogger(AttachVolumeServer.class);
 
  66     private static final Configuration config = ConfigurationFactory.getConfiguration();
 
  68     private Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
 
  70         RequestContext requestContext = new RequestContext(ctx);
 
  71         requestContext.isAlive();
 
  72         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
 
  73         String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
 
  74         String volumeId = params.get(ProviderAdapter.VOLUME_ID);
 
  75         String device = params.get(ProviderAdapter.DEVICE);
 
  76         if (StringUtils.isBlank(device)) {
 
  77             logger.info("Setting device to null");
 
  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                 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
 
 101                 Context contx = server.getContext();
 
 102                 if (!"true".equalsIgnoreCase(skipHypervisorCheck)) {
 
 103                     // Check of the Hypervisor for the VM Server is UP and reachable
 
 104                     checkHypervisor(server);
 
 106                 ComputeService service = contx.getComputeService();
 
 107                 if ((volumeId == null || volumeId.isEmpty()) || (device == null || device.isEmpty())) {
 
 108                     ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 109                     doFailure(requestContext, HttpStatus.BAD_REQUEST_400, "Both Device or Volumeid are mandatory");
 
 111                 VolumeService volumeService = contx.getVolumeService();
 
 112                 logger.info("collecting volume status for volume -id:" + volumeId);
 
 113                 List<Volume> volumes = volumeService.getVolumes();
 
 114                 Volume volume = new Volume();
 
 115                 boolean isAttached = false;
 
 116                 if (validateAttach(service, vm.getServerId(), volumeId, device)) {
 
 117                     String msg = "Volume with volume id " + volumeId + " cannot be attached as it already exists";
 
 118                     logger.info("Already volumes exists:");
 
 119                     ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 120                     doFailure(requestContext, HttpStatus.METHOD_NOT_ALLOWED_405, msg);
 
 123                     volume.setId(volumeId);
 
 124                     logger.info("Ready to Attach Volume to the server:");
 
 125                     service.attachVolume(server, volume, device);
 
 129                     if (validateAttach(requestContext, service, vm.getServerId(), volumeId, device)) {
 
 130                         ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
 
 131                         doSuccess(requestContext);
 
 133                         String msg = "Volume with " + volumeId + " unable  to attach";
 
 134                         logger.info("Volume with " + volumeId + " unable to attach");
 
 135                         ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 136                         doFailure(requestContext, HttpStatus.CONFLICT_409, msg);
 
 141                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
 
 143         } catch (ZoneException e) {
 
 144             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
 
 146             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 147             doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
 
 148         } catch (RequestFailedException e) {
 
 149             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 150             doFailure(requestContext, e.getStatus(), e.getMessage());
 
 151         } catch (Exception ex) {
 
 152             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
 
 153                     ATTACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
 
 154             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
 
 156                 ExceptionMapper.mapException((OpenStackBaseException) ex);
 
 157             } catch (ZoneException e1) {
 
 158                 logger.error(e1.getMessage());
 
 160             doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
 
 166     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
 
 167             throws APPCException {
 
 168         setMDC(Operation.ATTACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:attachVolume", ADAPTER_NAME);
 
 169         logOperation(Msg.ATTACHINGVOLUME_SERVER, params, context);
 
 170         return attachVolume(params, context);
 
 173     protected boolean validateAttach(ComputeService ser, String vm, String volumeId, String device)
 
 174             throws RequestFailedException, ZoneException {
 
 175         boolean isValid = false;
 
 176         Map<String, String> map = ser.getAttachments(vm);
 
 177         Iterator<Entry<String, String>> it = map.entrySet().iterator();
 
 178         while (it.hasNext()) {
 
 179             Map.Entry volumes = (Map.Entry) it.next();
 
 180             if (map != null && !(map.isEmpty())) {
 
 181                 logger.info("volumes available before attach");
 
 182                 logger.info("device" + volumes.getKey() + " Values " + volumes.getValue());
 
 183                 if (StringUtils.isBlank(device)) {
 
 184                     if (volumes.getValue().equals(volumeId)) {
 
 185                         logger.info("Device " + volumes.getKey() + " Volumes " + volumes.getValue());
 
 188                 } else if (volumes.getKey().equals(device) && (volumes.getValue().equals(volumeId))) {
 
 189                     logger.info("Device " + volumes.getKey() + " Volumes " + volumes.getValue());
 
 194         logger.info("AttachVolumeFlag " + isValid);
 
 198     protected boolean validateAttach(RequestContext rc, ComputeService ser, String vm, String volumeId, String device)
 
 199             throws RequestFailedException, ZoneException {
 
 200         boolean isValid = false;
 
 202         config.setProperty(Constants.PROPERTY_RETRY_DELAY, "10");
 
 203         config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "30");
 
 204         while (rc.attempt()) {
 
 205             Map<String, String> map = ser.getAttachments(vm);
 
 206             if (map != null && !(map.isEmpty())) {
 
 207                 Iterator<Entry<String, String>> it = map.entrySet().iterator();
 
 208                 logger.info("volumes available after attach ");
 
 209                 while (it.hasNext()) {
 
 210                     Map.Entry volumes = (Map.Entry) it.next();
 
 211                     logger.info(" devices " + volumes.getKey() + " volumes " + volumes.getValue());
 
 212                     if (StringUtils.isBlank(device)) {
 
 213                         if (volumes.getValue().equals(volumeId)) {
 
 214                             logger.info("Device " + volumes.getKey() + "Volumes " + volumes.getValue());
 
 218                     } else if (volumes.getKey().equals(device) && (volumes.getValue().equals(volumeId))) {
 
 219                         logger.info("Device " + volumes.getKey() + " Volume " + volumes.getValue());
 
 225                     logger.info("AttachVolume " + rc.getAttempts() + " No.of attempts ");
 
 232         if ((rc.getAttempts() == 30) && (!isValid)) {
 
 233             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
 
 234                     Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
 
 236             throw new TimeoutException(msg);
 
 238         logger.info("AttachVolume Flag -->" + isValid);