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) {
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 final String volumeStatus = "VOLUME_STATUS";
83 final String statusFail = "FAILURE";
84 final String statusSuccess = "SUCCESS";
85 final String statusNotFound = "CONTEXT_NOT_FOUND";
86 String tenantName = "Unknown";// to be used also in case of exception
88 if (validateVM(requestContext, appName, vmUrl, vm)) {
91 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
92 String identStr = (ident == null) ? null : ident.toString();
93 context = getContext(requestContext, vmUrl, identStr);
94 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
95 if (skipHypervisorCheck == null && ctx != null) {
96 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
98 if (context != null) {
99 tenantName = context.getTenantName();// this variable also is
102 requestContext.reset();
103 server = lookupServer(requestContext, context, vm.getServerId());
104 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
105 Context contx = server.getContext();
106 if (!"true".equalsIgnoreCase(skipHypervisorCheck)) {
107 // Check of the Hypervisor for the VM Server is UP and reachable
108 checkHypervisor(server);
110 ComputeService service = contx.getComputeService();
111 if ((volumeId == null || volumeId.isEmpty()) || (device == null || device.isEmpty())) {
112 ctx.setAttribute(volumeStatus, statusFail);
113 doFailure(requestContext, HttpStatus.BAD_REQUEST_400, "Both Device or Volumeid are mandatory");
115 VolumeService volumeService = contx.getVolumeService();
116 logger.info("collecting volume status for volume -id:" + volumeId);
117 //List<Volume> volumes = volumeService.getVolumes();
118 Volume volume = new Volume();
119 boolean isAttached = false;
120 if (validateAttach(service, vm.getServerId(), volumeId, device)) {
121 String msg = "Volume with volume id " + volumeId + " cannot be attached as it already exists";
122 logger.info("Already volumes exists:");
123 ctx.setAttribute(volumeStatus, statusFail);
124 doFailure(requestContext, HttpStatus.METHOD_NOT_ALLOWED_405, msg);
127 volume.setId(volumeId);
128 logger.info("Ready to Attach Volume to the server:");
129 service.attachVolume(server, volume, device);
133 if (validateAttach(requestContext, service, vm.getServerId(), volumeId, device)) {
134 ctx.setAttribute(volumeStatus, statusSuccess);
135 doSuccess(requestContext);
137 String msg = "Volume with " + volumeId + " unable to attach";
138 logger.info("Volume with " + volumeId + " unable to attach");
139 ctx.setAttribute(volumeStatus, statusFail);
140 doFailure(requestContext, HttpStatus.CONFLICT_409, msg);
145 ctx.setAttribute(volumeStatus, statusNotFound);
147 } catch (ZoneException e) {
148 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
150 ctx.setAttribute(volumeStatus, statusFail);
151 doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
152 } catch (RequestFailedException e) {
153 ctx.setAttribute(volumeStatus, statusFail);
154 doFailure(requestContext, e.getStatus(), e.getMessage());
155 } catch (Exception ex) {
156 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
157 ATTACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
158 ctx.setAttribute(volumeStatus, statusFail);
160 ExceptionMapper.mapException((OpenStackBaseException) ex);
161 } catch (ZoneException e1) {
162 logger.error(e1.getMessage());
164 doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
170 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
171 throws APPCException {
172 setMDC(Operation.ATTACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:attachVolume", ADAPTER_NAME);
173 logOperation(Msg.ATTACHINGVOLUME_SERVER, params, context);
174 return attachVolume(params, context);
177 protected boolean validateAttach(ComputeService ser, String vm, String volumeId, String device)
178 throws ZoneException {
179 boolean isValid = false;
180 Map<String, String> map = ser.getAttachments(vm);
181 Iterator<Entry<String, String>> it = map.entrySet().iterator();
182 while (it.hasNext()) {
183 Map.Entry<String, String> volumes = it.next();
184 logger.info("volumes available before attach");
185 logger.info("device" + volumes.getKey() + " Values " + volumes.getValue());
186 if (StringUtils.isBlank(device)) {
187 if (volumes.getValue().equals(volumeId)) {
188 logger.info("Device " + volumes.getKey() + " Volumes " + volumes.getValue());
191 } else if (volumes.getKey().equals(device) && (volumes.getValue().equals(volumeId))) {
192 logger.info("Device " + volumes.getKey() + " Volumes " + volumes.getValue());
196 logger.info("AttachVolumeFlag " + isValid);
200 protected boolean validateAttach(RequestContext rc, ComputeService ser, String vm, String volumeId, String device)
201 throws ZoneException {
202 boolean isValid = false;
204 config.setProperty(Constants.PROPERTY_RETRY_DELAY, "10");
205 config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "30");
206 while (rc.attempt()) {
207 Map<String, String> map = ser.getAttachments(vm);
208 if (map != null && !(map.isEmpty())) {
209 Iterator<Entry<String, String>> it = map.entrySet().iterator();
210 logger.info("volumes available after attach ");
211 while (it.hasNext()) {
212 Map.Entry<String, String> volumes = it.next();
213 logger.info(" devices " + volumes.getKey() + " volumes " + volumes.getValue());
214 if (StringUtils.isBlank(device) && (volumes.getValue().equals(volumeId))) {
215 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);