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