bugfix for error handling while detaching volumes
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / AttachVolumeServer.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.ATTACHVOLUME_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 AttachVolumeServer extends ProviderServerOperation {
54
55     private final EELFLogger logger = EELFManager.getInstance().getLogger(AttachVolumeServer.class);
56
57     private Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58         Server server = null;
59         RequestContext requestContext = new RequestContext(ctx);
60         requestContext.isAlive();
61         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
62         String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
63         String volumeId = params.get(ProviderAdapter.VOLUME_ID);
64         String device = params.get(ProviderAdapter.DEVICE);
65         VMURL vm = VMURL.parseURL(vmUrl);
66         Context context;
67         String tenantName = "Unknown";// to be used also in case of exception
68         try {
69             if (validateVM(requestContext, appName, vmUrl, vm)) {
70                 return null;
71             }
72             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
73             String identStr = (ident == null) ? null : ident.toString();
74             context = getContext(requestContext, vmUrl, identStr);
75             if (context != null) {
76                 tenantName = context.getTenantName();// this variable also is
77                                                         // used in case of
78                                                         // exception
79                 requestContext.reset();
80                 server = lookupServer(requestContext, context, vm.getServerId());
81                 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
82                 Context contx = server.getContext();
83                 ComputeService service = contx.getComputeService();
84                 VolumeService volumeService = contx.getVolumeService();
85                 logger.info("collecting volume status for volume -id:" + volumeId);
86                 List<Volume> volumes = volumeService.getVolumes();
87                 Volume volume = new Volume();
88                 logger.info("Size of volume list :" + volumes.size());
89                 if (volumes != null && !volumes.isEmpty()) {
90                     if (!(volumes.contains(volumeId))) {
91                         volume.setId(volumeId);
92                         logger.info("Ready to Attach Volume to the server:");
93                         service.attachVolume(server, volume, device);
94                         logger.info("Volume status after performing attach:" + volume.getStatus());
95                         if (validateAttach(volumeService, volumeId)) {
96                             ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
97                             doSuccess(requestContext);
98                         } else {
99                             String msg = "Failed to attach Volume";
100                             logger.info("Volume with " + volumeId + " unable to attach");
101                             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
102                             doFailure(requestContext, HttpStatus.NOT_IMPLEMENTED_501, msg);
103                         }
104                     } else {
105                         String msg = "Volume with volume id " + volumeId + " cannot be attached as it already exists";
106                         logger.info("Alreday volumes exists:");
107                         ctx.setAttribute("VOLUME_STATUS", "FAILURE");
108                         doFailure(requestContext, HttpStatus.NOT_IMPLEMENTED_501, msg);
109                     }
110                 }
111                 context.close();
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, vmUrl);
117             logger.error(msg);
118             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
119             doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
120         } catch (RequestFailedException e) {
121             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
122             doFailure(requestContext, e.getStatus(), e.getMessage());
123         } catch (Exception ex) {
124             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
125                     ATTACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
126             ctx.setAttribute("VOLUME_STATUS", "FAILURE");
127             doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
128         }
129         return server;
130     }
131
132     @Override
133     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
134             throws APPCException {
135         setMDC(Operation.ATTACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:attachVolume", ADAPTER_NAME);
136         logOperation(Msg.ATTACHINGVOLUME_SERVER, params, context);
137         return attachVolume(params, context);
138     }
139
140     protected boolean validateAttach(VolumeService volumeService, String volumeId)
141             throws RequestFailedException, ZoneException {
142         boolean flag = false;
143         List<Volume> volumeList = volumeService.getVolumes();
144         if (volumeList.contains(volumeId)) {
145             flag = true;
146         } else {
147             flag = false;
148         }
149         logger.info("validateAttach flag-->" + flag);
150         return flag;
151     }
152 }