6f0ab87be665c9e727f10b67ff7ed59614fc8117
[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
29 import com.att.cdp.exceptions.ZoneException;
30 import com.att.cdp.zones.ComputeService;
31 import com.att.cdp.zones.Context;
32 import com.att.cdp.zones.VolumeService;
33 import com.att.cdp.zones.model.ModelObject;
34 import com.att.cdp.zones.model.Server;
35 import com.att.cdp.zones.model.Volume;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
39 import java.util.List;
40 import java.util.Map;
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.adapter.iaas.impl.VMURL;
48 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
49 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
50 import org.onap.appc.exceptions.APPCException;
51 import org.onap.appc.i18n.Msg;
52 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
53
54 public class DettachVolumeServer extends ProviderServerOperation {
55
56     private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
57
58     @Override
59     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
60         throws APPCException {
61         setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
62         logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
63         return dettachVolume(params, context);
64     }
65
66     private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
67         Server server = null;
68         RequestContext rc = new RequestContext(ctx);
69         rc.isAlive();
70         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
71         String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
72         String volumeId = params.get(ProviderAdapter.VOLUME_ID);
73         VMURL vm = VMURL.parseURL(vmUrl);
74         Context context;
75         String tenantName = "Unknown";//to be used also in case of exception
76         try {
77             if (validateVM(rc, appName, vmUrl, vm)) {
78                 return null;
79             }
80             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
81             String identStr = (ident == null) ? null : ident.toString();
82             context = getContext(rc, vmUrl, identStr);
83             if (context != null) {
84                 tenantName = context.getTenantName();//this variable also is used in case of exception
85                 rc.reset();
86                 server = lookupServer(rc, context, vm.getServerId());
87                 logger.debug(Msg.SERVER_FOUND, vmUrl, 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: " + volumeId);
92                 List<Volume> volList = vs.getVolumes();
93                 logger.info("Size of volume list: " + volList.size());
94                 if (!volList.isEmpty()) {
95                     for (Volume v : volList) {
96                         logger.info("list of volumesif exists: " + v.getId());
97                         if (v.getId().equals(volumeId)) {
98                             v.setId(volumeId);
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                             if (validateDetach(vs, volumeId)) {
103                                 doSuccess(rc);
104                             }
105                         } else {
106                             String msg =
107                                 "Volume with volume id " + volumeId + " cannot be detached as it doesn't exists";
108                             doFailure(rc, HttpStatus.NOT_IMPLEMENTED_501, msg);
109                         }
110                     }
111                 }
112                 context.close();
113                 doSuccess(rc);
114                 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
115             } else {
116                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
117             }
118         } catch (ZoneException e) {
119             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
120             logger.error(msg);
121             doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
122         } catch (RequestFailedException e) {
123             logger.error("An error occurred when processing the request", e);
124             doFailure(rc, e.getStatus(), e.getMessage());
125         } catch (Exception e) {
126             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e, e.getClass().getSimpleName(),
127                 ATTACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
128             logger.error(msg, e);
129             doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
130         }
131         return server;
132     }
133
134     protected boolean validateDetach(VolumeService vs, String volId) throws RequestFailedException, ZoneException {
135         boolean flag = false;
136         List<Volume> volList = vs.getVolumes();
137         for (Volume v : volList) {
138             if (!v.getId().equals(volId)) {
139                 logger.info("Volume with " + volId + "detached successsfully");
140                 flag = true;
141             } else {
142                 logger.info("failed to detach volume with id" + volId);
143                 flag = false;
144             }
145         }
146         return flag;
147     }
148 }