Evaluate to variable value
[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 import java.util.Map;
29 import org.glassfish.grizzly.http.util.HttpStatus;
30 import org.onap.appc.Constants;
31 import org.onap.appc.adapter.iaas.ProviderAdapter;
32 import org.onap.appc.adapter.iaas.impl.IdentityURL;
33 import org.onap.appc.adapter.iaas.impl.RequestContext;
34 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
35 import org.onap.appc.adapter.iaas.impl.VMURL;
36 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
37 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
38 import org.onap.appc.exceptions.APPCException;
39 import org.onap.appc.i18n.Msg;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import com.att.cdp.exceptions.ZoneException;
42 import com.att.cdp.zones.ComputeService;
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
52 public class DettachVolumeServer  extends ProviderServerOperation{
53     private final EELFLogger logger = EELFManager.getInstance().getLogger(DettachVolumeServer.class);
54     @Override
55     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
56             throws APPCException {
57         setMDC(Operation.DETACHVOLUME_SERVICE.toString(), "App-C IaaS Adapter:dettachVolume", ADAPTER_NAME);
58         logOperation(Msg.DETTACHINGVOLUME_SERVER, params, context);
59         return dettachVolume(params, context);
60     }
61     private Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) {
62         Server server = null;
63         RequestContext rc = new RequestContext(ctx);
64         rc.isAlive();
65         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
66         String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
67         String volumeid = params.get(ProviderAdapter.VOLUME_ID);
68         String device = params.get(ProviderAdapter.DEVICE);
69         VMURL vm = VMURL.parseURL(vm_url);
70         Context context = null;
71         String tenantName = "Unknown";//to be used also in case of exception
72         try {
73             if (validateVM(rc, appName, vm_url, vm))
74                 return null;
75             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
76             String identStr = (ident == null) ? null : ident.toString();
77             String vol_id = (volumeid == null) ? null : volumeid.toString();
78             String msg;
79             context = getContext(rc, vm_url, identStr);
80             if (context != null) {
81                 tenantName = context.getTenantName();//this varaible also is used in case of exception
82                 rc.reset();
83                 server = lookupServer(rc, context, vm.getServerId());
84                 logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
85                 Volume vol = new Volume();
86                 vol.setId(vol_id);
87                 Map volms = server.getVolumes();
88                     ComputeService cs = context.getComputeService();
89                     if(server.getVolumes().containsValue(vol_id))
90                     {
91                         logger.info("Alreday volumes exists:");
92                          logger.info( volms.size()+"volumes size if exists");
93                          cs.detachVolume(server, vol);
94                          server.detachVolume(device);
95                     }
96                     else
97                     {
98                         logger.info("volume is not available to detach");
99                         logger.info("Server status: RUNNING");
100                     }
101                 context.close();
102                 doSuccess(rc);
103                 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
104             } else {
105                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
106             }
107         } catch (ZoneException e) {
108             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
109             logger.error(msg);
110             doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
111         } catch (RequestFailedException e) {
112             doFailure(rc, e.getStatus(), e.getMessage());
113         } catch (Exception ex) {
114             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
115                     ATTACHVOLUME_SERVICE.toString(), vm_url, tenantName);
116             logger.error(msg, ex);
117             doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
118         }
119         return server;
120     }
121
122 }