2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
26 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.ATTACHVOLUME_SERVICE;
27 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
28 import java.util.List;
30 import org.glassfish.grizzly.http.util.HttpStatus;
31 import org.onap.appc.Constants;
32 import org.onap.appc.adapter.iaas.ProviderAdapter;
33 import org.onap.appc.adapter.iaas.impl.IdentityURL;
34 import org.onap.appc.adapter.iaas.impl.RequestContext;
35 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
36 import org.onap.appc.adapter.iaas.impl.VMURL;
37 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
38 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
39 import org.onap.appc.exceptions.APPCException;
40 import org.onap.appc.i18n.Msg;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import com.att.cdp.exceptions.ZoneException;
43 import com.att.cdp.zones.ComputeService;
44 import com.att.cdp.zones.Context;
45 import com.att.cdp.zones.VolumeService;
46 import com.att.cdp.zones.model.ModelObject;
47 import com.att.cdp.zones.model.Server;
48 import com.att.cdp.zones.model.Volume;
49 import com.att.eelf.configuration.EELFLogger;
50 import com.att.eelf.configuration.EELFManager;
51 import com.att.eelf.i18n.EELFResourceManager;
53 public class DettachVolumeServer extends ProviderServerOperation {
54 private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
57 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
58 throws APPCException {
59 setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
60 logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
61 return dettachVolume(params, context);
64 private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
66 RequestContext rc = new RequestContext(ctx);
68 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
69 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
70 String volumeid = params.get(ProviderAdapter.VOLUME_ID);
71 VMURL vm = VMURL.parseURL(vm_url);
72 Context context = null;
73 String tenantName = "Unknown";// to be used also in case of exception
75 if (validateVM(rc, appName, vm_url, vm))
77 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
78 String identStr = (ident == null) ? null : ident.toString();
79 String vol_id = (volumeid == null) ? null : volumeid.toString();
80 context = getContext(rc, vm_url, identStr);
81 if (context != null) {
82 tenantName = context.getTenantName();// this varaible also is used in case of exception
84 server = lookupServer(rc, context, vm.getServerId());
85 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
86 Context contx = server.getContext();
87 ComputeService service = contx.getComputeService();
88 VolumeService vs = contx.getVolumeService();
89 logger.info("collecting volume status for volume -id:" + vol_id);
90 List<Volume> volList = vs.getVolumes();
91 logger.info("Size of volume list :" + volList.size());
92 if (volList != null && !volList.isEmpty()) {
93 for (Volume v : volList) {
94 logger.info("list of volumesif exists" + v.getId());
95 if (v.getId().equals(vol_id)) {
97 logger.info("Ready to Detach Volume from the server:" + Volume.Status.DETACHING);
98 service.detachVolume(server, v);
99 logger.info("Volume status after performing detach:" + v.getStatus());
100 if (validateDetach(vs, vol_id)) {
104 String msg = "Volume with volume id " + vol_id + " cannot be detached as it doesnot exists";
105 doFailure(rc, HttpStatus.NOT_IMPLEMENTED_501, msg);
111 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
113 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
115 } catch (ZoneException e) {
116 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
118 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
119 } catch (RequestFailedException e) {
120 doFailure(rc, e.getStatus(), e.getMessage());
121 } catch (Exception ex) {
122 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
123 ATTACHVOLUME_SERVICE.toString(), vm_url, tenantName);
124 logger.error(msg, ex);
125 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
130 protected boolean validateDetach(VolumeService vs, String volId) throws RequestFailedException, ZoneException {
131 boolean flag = false;
132 List<Volume> volList = vs.getVolumes();
133 for (Volume v : volList) {
134 if (!v.getId().equals(volId)) {
135 logger.info("Volume with " + volId + "detached successsfully");
138 logger.info("failed to detach volume with id" + volId);