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