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