code fix for OS attach volumes and dettach volumes
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / DettachVolumeServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
25
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.Map;
29 import java.util.List;
30 import com.att.cdp.zones.ComputeService;
31 import org.glassfish.grizzly.http.util.HttpStatus;
32 import org.onap.appc.Constants;
33 import org.onap.appc.adapter.iaas.ProviderAdapter;
34 import org.onap.appc.adapter.iaas.impl.IdentityURL;
35 import org.onap.appc.adapter.iaas.impl.RequestContext;
36 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
37 import org.onap.appc.adapter.iaas.impl.VMURL;
38 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
39 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.i18n.Msg;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import com.att.cdp.exceptions.ZoneException;
44 import com.att.cdp.zones.ComputeService;
45 import com.att.cdp.zones.Context;
46 import com.att.cdp.zones.VolumeService;
47 import com.att.cdp.zones.model.ModelObject;
48 import com.att.cdp.zones.model.Server;
49 import com.att.cdp.zones.model.Volume;
50 import com.att.eelf.configuration.EELFLogger;
51 import com.att.eelf.configuration.EELFManager;
52 import com.att.eelf.i18n.EELFResourceManager;
53
54 public class DettachVolumeServer extends ProviderServerOperation {
55     private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
56
57     @Override
58     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
59             throws APPCException {
60         setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
61         logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
62         return dettachVolume(params, context);
63     }
64
65     private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
66         Server server = null;
67         RequestContext rc = new RequestContext(ctx);
68         rc.isAlive();
69         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
70         String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
71         String volumeid = params.get(ProviderAdapter.VOLUME_ID);
72         String device = params.get(ProviderAdapter.DEVICE);
73         VMURL vm = VMURL.parseURL(vm_url);
74         Context context = null;
75         String tenantName = "Unknown";//to be used also in case of exception
76         try {
77             if (validateVM(rc, appName, vm_url, vm))
78                 return null;
79             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
80             String identStr = (ident == null) ? null : ident.toString();
81             String vol_id = (volumeid == null) ? null : volumeid.toString();
82             context = getContext(rc, vm_url, identStr);
83             if (context != null) {
84                 tenantName = context.getTenantName();//this varaible also is used in case of exception
85                 rc.reset();
86                 server = lookupServer(rc, context, vm.getServerId());
87                 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
88                 Context contx = server.getContext();
89                 ComputeService service = contx.getComputeService();
90                 VolumeService vs = contx.getVolumeService();
91                 logger.info("collecting volume status for volume -id:" + vol_id);
92                 List<Volume> volList = vs.getVolumes();
93                 logger.info("Size of volume list :" + volList.size());
94                 if (volList != null && !volList.isEmpty()) {
95                     for (Volume v : volList) {
96                         logger.info("list of volumesif exists" + v.getId());
97                         if (v.getId().equals(vol_id)) {
98                             v.setId(vol_id);
99                             logger.info("Ready to Detach Volume from the server:" + Volume.Status.DETACHING);
100                             service.detachVolume(server, v);
101                             logger.info("Volume status after performing detach:" + v.getStatus());
102                             doSuccess(rc);
103                         } else {
104                             String msg = "Volume with volume id " + vol_id + " cannot be detached as it doesnot exists";
105                             doFailure(rc, HttpStatus.NOT_IMPLEMENTED_501, msg);
106                         }
107                     }
108                 }
109                 context.close();
110                 doSuccess(rc);
111                 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
112             } else {
113                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
114             }
115         } catch (ZoneException e) {
116             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
117             logger.error(msg);
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);
126         }
127         return server;
128     }
129
130 }