d9b30cbb8400945f0acd8a6f2a60e6cfb9a9df9e
[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-2018 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.DETACHVOLUME_SERVICE;
27 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
28 import com.att.cdp.exceptions.ZoneException;
29 import com.att.cdp.zones.ComputeService;
30 import com.att.cdp.zones.Context;
31 import com.att.cdp.zones.VolumeService;
32 import com.att.cdp.zones.model.ModelObject;
33 import com.att.cdp.zones.model.Server;
34 import com.att.cdp.zones.model.Volume;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
38 import java.util.List;
39 import java.util.Map;
40 import org.glassfish.grizzly.http.util.HttpStatus;
41 import org.onap.appc.Constants;
42 import org.onap.appc.adapter.iaas.ProviderAdapter;
43 import org.onap.appc.adapter.iaas.impl.IdentityURL;
44 import org.onap.appc.adapter.iaas.impl.RequestContext;
45 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
46 import org.onap.appc.adapter.iaas.impl.VMURL;
47 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
48 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
49 import org.onap.appc.exceptions.APPCException;
50 import org.onap.appc.i18n.Msg;
51 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
52
53 public class DettachVolumeServer extends ProviderServerOperation {
54     private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
55
56     @Override
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);
62     }
63
64     private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
65         Server server = null;
66         RequestContext requestContext = new RequestContext(ctx);
67         requestContext.isAlive();
68         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
69         String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
70         String volumeId = params.get(ProviderAdapter.VOLUME_ID);
71         VMURL vm = VMURL.parseURL(vmUrl);
72         Context context;
73         String tenantName = "Unknown";// to be used also in case of exception
74         try {
75             if (validateVM(requestContext, appName, vmUrl, vm)) {
76                 return null;
77             }
78             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
79             String identStr = (ident == null) ? null : ident.toString();
80             context = getContext(requestContext, vmUrl, identStr);
81             if (context != null) {
82                 tenantName = context.getTenantName();// this variable also is
83                                                         // used in case of
84                                                         // exception
85                 requestContext.reset();
86                 server = lookupServer(requestContext, 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 volumeService = contx.getVolumeService();
91                 logger.info("collecting volume status for volume -id: " + volumeId);
92                 List<Volume> volumes = volumeService.getVolumes();
93                 Volume volume = new Volume();
94                 logger.info("Size of volume list: " + volumes.size());
95                 if (volumes != null && !volumes.isEmpty()) {
96                     if (volumes.contains(volumeId)) {
97                         volume.setId(volumeId);
98                         logger.info("Ready to Detach Volume from the server: " + Volume.Status.DETACHING);
99                         service.detachVolume(server, volume);
100                         logger.info("Volume status after performing detach: " + volume.getStatus());
101                         if (validateDetach(volumeService, volumeId)) {
102                             doSuccess(requestContext);
103                         } else {
104                             String msg = "Volume with volume id " + volumeId + " cannot be detached ";
105                             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
106                             doFailure(requestContext, HttpStatus.NOT_IMPLEMENTED_501, msg);
107                             logger.info("unable to detach volume  from the server");
108                         }
109                     } else {
110                         String msg = "Volume with volume id " + volumeId + " cannot be detached as it doesn't exists";
111                         ctx.setAttribute("VOLUME_STATUS", "FAILURE");
112                         doFailure(requestContext, HttpStatus.NOT_IMPLEMENTED_501, msg);
113                     }
114                     logger.info("volumestatus:" + ctx.getAttribute("VOLUME_STATUS"));
115                 }
116                 context.close();
117             } else {
118                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
119             }
120         } catch (ZoneException e) {
121             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
122             logger.error(msg);
123             doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
124         } catch (RequestFailedException e) {
125             logger.error("An error occurred when processing the request", e);
126             doFailure(requestContext, e.getStatus(), e.getMessage());
127         } catch (Exception e) {
128             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e, e.getClass().getSimpleName(),
129                     DETACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
130             logger.error(msg, e);
131             doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
132         }
133         return server;
134     }
135
136     protected boolean validateDetach(VolumeService volumeService, String volId)
137             throws RequestFailedException, ZoneException {
138         boolean flag = false;
139         List<Volume> volumes = volumeService.getVolumes();
140         if (!volumes.contains(volId)) {
141             flag = true;
142         } else {
143             flag = false;
144         }
145         logger.info("validateDetach flag-->" + flag);
146         return flag;
147     }
148 }