refactor LookupServer for nesting try
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / LookupServer.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
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
26
27 import org.onap.appc.Constants;
28 import org.onap.appc.adapter.iaas.ProviderAdapter;
29 import org.onap.appc.adapter.iaas.impl.RequestContext;
30 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
31 import org.onap.appc.adapter.iaas.impl.VMURL;
32 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
33 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
34 import org.onap.appc.configuration.Configuration;
35 import org.onap.appc.configuration.ConfigurationFactory;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.i18n.Msg;
38 import com.att.cdp.exceptions.ZoneException;
39 import com.att.cdp.zones.Context;
40 import com.att.cdp.zones.model.ModelObject;
41 import com.att.cdp.zones.model.Server;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44 import com.att.eelf.i18n.EELFResourceManager;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.glassfish.grizzly.http.util.HttpStatus;
47 import java.io.IOException;
48 import java.util.Map;
49 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
50
51 public class LookupServer extends ProviderServerOperation {
52
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(EvacuateServer.class);
54     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
55
56
57     public Server lookupServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58         Server server = null;
59         RequestContext rc = new RequestContext(ctx);
60         rc.isAlive(); // should we test the return and fail if false?
61
62         String vmUrl = null;
63         try {
64             // process vmUrl
65             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
66                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
67
68             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
69             vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
70             VMURL vm = VMURL.parseURL(vmUrl);
71             if (validateVM(rc, appName, vmUrl, vm)) {
72                 return null;
73             }
74             server = lookupServerNested(params, server, rc, ctx, appName, vm, vmUrl);
75         } catch (RequestFailedException e) {
76             // parameters not valid, unable to connect to provider
77             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
78             logger.error(msg);
79             doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
80             ctx.setAttribute("serverFound", "failure");
81         }
82         return server;
83     }
84
85     private Server lookupServerNested(Map<String, String> params, Server server, RequestContext rqstCtx, SvcLogicContext ctx,
86             String appName, VMURL vm, String vmUrl)
87             throws APPCException {
88
89         // use try with resource to ensure context is closed (returned to pool)
90         try (Context context = resolveContext(rqstCtx, params, appName, vmUrl)) {
91             // resloveContext & getContext call doFailure and log errors before returning null
92             if (context != null) {
93                 rqstCtx.reset();
94                 server = lookupServer(rqstCtx, context, vm.getServerId());
95                 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
96                 ctx.setAttribute("serverFound", "success");
97                 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "LookupServer", vmUrl);
98                 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
99                 doSuccess(rqstCtx);
100             }
101         } catch (ZoneException e) {
102             // server not found
103             String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
104             logger.error(msg);
105             doFailure(rqstCtx, HttpStatus.NOT_FOUND_404, msg);
106             ctx.setAttribute("serverFound", "failure");
107         } catch (IOException e) {
108             // exception closing context
109             String msg = EELFResourceManager.format(Msg.CLOSE_CONTEXT_FAILED, e, vmUrl);
110             logger.error(msg);
111         } catch (Exception e1) {
112             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
113                     e1.getClass().getSimpleName(), Operation.LOOKUP_SERVICE.toString(), vmUrl, "Unknown");
114             logger.error(msg, e1);
115             doFailure(rqstCtx, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
116         }
117         return server;
118     }
119
120     @Override
121     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
122             throws APPCException {
123         setMDC(Operation.LOOKUP_SERVICE.toString(), "App-C IaaS Adapter:LookupServer", ADAPTER_NAME);
124         logOperation(Msg.LOOKING_SERVER_UP, params, context);
125         return lookupServer(params, context);
126     }
127 }