2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modification Copyright (C) 2019 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.iaas.provider.operation.impl;
28 import org.onap.appc.Constants;
29 import org.onap.appc.adapter.iaas.ProviderAdapter;
30 import org.onap.appc.adapter.iaas.impl.RequestContext;
31 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
32 import org.onap.appc.adapter.iaas.impl.VMURL;
33 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
34 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
35 import org.onap.appc.configuration.Configuration;
36 import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.appc.exceptions.APPCException;
38 import org.onap.appc.i18n.Msg;
39 import com.att.cdp.exceptions.ZoneException;
40 import com.att.cdp.zones.Context;
41 import com.att.cdp.zones.model.ModelObject;
42 import com.att.cdp.zones.model.Server;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import com.att.eelf.i18n.EELFResourceManager;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
47 import org.glassfish.grizzly.http.util.HttpStatus;
48 import java.io.IOException;
50 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
52 public class LookupServer extends ProviderServerOperation {
54 private static final EELFLogger logger = EELFManager.getInstance().getLogger(LookupServer.class);
55 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
56 private static final String SERVERFOUND = "serverFound";
58 public Server lookupServer(Map<String, String> params, SvcLogicContext ctx) {
60 RequestContext rc = new RequestContext(ctx);
61 rc.isAlive(); // should we test the return and fail if false?
66 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
67 ProviderAdapter.PROPERTY_PROVIDER_NAME);
69 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
70 vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
71 VMURL vm = VMURL.parseURL(vmUrl);
72 if (validateVM(rc, appName, vmUrl, vm)) {
75 server = lookupServerNested(params, server, rc, ctx, appName, vm, vmUrl);
76 } catch (RequestFailedException e) {
77 // parameters not valid, unable to connect to provider
78 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
80 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
81 ctx.setAttribute(SERVERFOUND, "failure");
86 private Server lookupServerNested(Map<String, String> params, Server server, RequestContext rqstCtx, SvcLogicContext ctx,
87 String appName, VMURL vm, String vmUrl) {
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) {
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);
101 } catch (ZoneException e) {
103 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
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);
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);
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);