8d6e2dea288789b5dacb08b38a143434bb26f869
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
24
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
27 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
28 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
29 import org.openecomp.appc.adapter.iaas.impl.VMURL;
30 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
31 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
32 import org.openecomp.appc.configuration.Configuration;
33 import org.openecomp.appc.configuration.ConfigurationFactory;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.appc.i18n.Msg;
36 import com.att.cdp.exceptions.ZoneException;
37 import com.att.cdp.zones.Context;
38 import com.att.cdp.zones.model.ModelObject;
39 import com.att.cdp.zones.model.Server;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.att.eelf.i18n.EELFResourceManager;
43 import org.openecomp.sdnc.sli.SvcLogicContext;
44 import org.glassfish.grizzly.http.util.HttpStatus;
45
46 import java.io.IOException;
47 import java.util.Map;
48
49 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
50
51 /**
52  * @since September 26, 2016
53  */
54 public class LookupServer extends ProviderServerOperation {
55
56     private static final EELFLogger logger = EELFManager.getInstance().getLogger(EvacuateServer.class);
57     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
58
59
60     public Server lookupServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
61         Server server = null;
62         RequestContext rc = new RequestContext(ctx);
63         rc.isAlive(); //should we test the return and fail if false?
64
65         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
66
67         String vm_url = null;
68         VMURL vm = null;
69         try {
70
71             //process vm_url
72             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
73                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
74             vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
75             vm = VMURL.parseURL(vm_url);
76             if (validateVM(rc, appName, vm_url, vm)) return null;
77
78
79             //use try with resource to ensure context is closed (returned to pool)
80             try(Context context = resolveContext(rc, params, appName, vm_url)){
81                 //resloveContext & getContext call doFailure and log errors before returning null
82                 if (context != null){
83                     rc.reset();
84                     server = lookupServer(rc, context, vm.getServerId());
85                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
86                     ctx.setAttribute("serverFound", "success");
87                     String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "LookupServer", vm_url);
88                     ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
89                     doSuccess(rc);
90                 }
91             } catch (ZoneException e) {
92                 //server not found
93                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
94                 logger.error(msg);
95                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
96                 ctx.setAttribute("serverFound", "failure");
97             }  catch (IOException e) {
98                 //exception closing context
99                 String msg = EELFResourceManager.format(Msg.CLOSE_CONTEXT_FAILED, e, vm_url);
100                 logger.error(msg);
101             } catch (Throwable t) {
102                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
103                         Operation.LOOKUP_SERVICE.toString(), vm_url,  "Unknown" );
104                 logger.error(msg, t);
105                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
106             }
107
108         } catch (RequestFailedException e) {
109             // parameters not valid, unable to connect to provider
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             ctx.setAttribute("serverFound", "failure");
114         }
115         return server;
116     }
117
118     @Override
119     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
120
121         setMDC(Operation.LOOKUP_SERVICE.toString(), "App-C IaaS Adapter:LookupServer", ADAPTER_NAME);
122         logOperation(Msg.LOOKING_SERVER_UP, params, context);
123         return lookupServer(params, context);
124     }
125 }