validation fix for OS attach volumes & detach vols
[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.utils.Constants.ADAPTER_NAME;
27 import java.util.Map;
28 import java.util.List;
29 import com.att.cdp.zones.ComputeService;
30 import org.glassfish.grizzly.http.util.HttpStatus;
31 import org.onap.appc.Constants;
32 import org.onap.appc.adapter.iaas.ProviderAdapter;
33 import org.onap.appc.adapter.iaas.impl.IdentityURL;
34 import org.onap.appc.adapter.iaas.impl.RequestContext;
35 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
36 import org.onap.appc.adapter.iaas.impl.VMURL;
37 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
38 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
39 import org.onap.appc.exceptions.APPCException;
40 import org.onap.appc.i18n.Msg;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import com.att.cdp.exceptions.ZoneException;
43 import com.att.cdp.zones.Context;
44 import com.att.cdp.zones.VolumeService;
45 import com.att.cdp.zones.model.ModelObject;
46 import com.att.cdp.zones.model.Server;
47 import com.att.cdp.zones.model.Volume;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
50 import com.att.eelf.i18n.EELFResourceManager;
51 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.ATTACHVOLUME_SERVICE;;
52
53 public class AttachVolumeServer extends ProviderServerOperation {
54     private final EELFLogger logger = EELFManager.getInstance().getLogger(AttachVolumeServer.class);
55
56     private Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
57         Server server = null;
58         RequestContext rc = new RequestContext(ctx);
59         rc.isAlive();
60         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
61         String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
62         String volumeid = params.get(ProviderAdapter.VOLUME_ID);
63         String device = params.get(ProviderAdapter.DEVICE);
64         VMURL vm = VMURL.parseURL(vm_url);
65         Context context = null;
66         String tenantName = "Unknown";// to be used also in case of exception
67         try {
68             if (validateVM(rc, appName, vm_url, vm))
69                 return null;
70             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
71             String identStr = (ident == null) ? null : ident.toString();
72             String vol_id = (volumeid == null) ? null : volumeid.toString();
73             context = getContext(rc, vm_url, identStr);
74             if (context != null) {
75                 tenantName = context.getTenantName();// this varaible also is used in case of exception
76                 rc.reset();
77                 server = lookupServer(rc, context, vm.getServerId());
78                 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
79                 Context contx = server.getContext();
80                 ComputeService service = contx.getComputeService();
81                 VolumeService vs = contx.getVolumeService();
82                 logger.info("collecting volume status for volume -id:" + vol_id);
83                 List<Volume> volList = vs.getVolumes();
84                 logger.info("Size of volume list :" + volList.size());
85                 if (volList != null && !volList.isEmpty()) {
86                     for (Volume v : volList) {
87                         logger.info("list of volumesif exists" + v.getId());
88                         if (!v.getId().equals(vol_id)) {
89                             v.setId(vol_id);
90                             logger.info("Ready to Attach Volume to the server:" + Volume.Status.ATTACHING);
91                             service.attachVolume(server, v, device);
92                             logger.info("Volume status after performing attach:" + v.getStatus());
93                             if (validateAttach(vs, vol_id)) {
94                                 doSuccess(rc);
95                             }
96                         } else {
97                             String msg = "Volume with volume id " + vol_id + " cannot be attached as it already exists";
98                             logger.info("Alreday volumes exists:");
99                             doFailure(rc, HttpStatus.NOT_IMPLEMENTED_501, msg);
100                         }
101                     }
102                 }
103                 context.close();
104                 doSuccess(rc);
105                 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
106             } else {
107                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
108             }
109         } catch (ZoneException e) {
110             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
111             logger.error(msg);
112             doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
113         } catch (RequestFailedException e) {
114             doFailure(rc, 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(), vm_url, tenantName);
118             logger.error(msg, ex);
119             doFailure(rc, 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     protected boolean validateAttach(VolumeService vs, String volId) throws RequestFailedException, ZoneException {
133         boolean flag = false;
134         List<Volume> volList = vs.getVolumes();
135         for (Volume v : volList) {
136             if (v.getId().equals(volId)) {
137                 logger.info("Volume with " + volId + "attached successsfully");
138                 flag = true;
139             } else {
140                 logger.info("failed to attach volume with id" + volId);
141                 flag = false;
142             }
143         }
144         return flag;
145     }
146
147 }