e0be5c9550789319c9417ec8c0077c3c4147440c
[appc.git] / appc-dg / appc-dg-shared / appc-dg-ssh / src / main / java / org / openecomp / appc / dg / ssh / impl / SshDBPluginImpl.java
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.dg.ssh.impl;
24
25 import com.att.eelf.i18n.EELFResourceManager;
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 import java.util.Map;
30
31 import org.openecomp.appc.adapter.ssh.Constants;
32 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
33 import org.openecomp.appc.adapter.ssh.SshDataAccessException;
34 import org.openecomp.appc.adapter.ssh.SshDataAccessService;
35 import org.openecomp.appc.dg.ssh.SshDBPlugin;
36 import org.openecomp.appc.exceptions.APPCException;
37 import org.openecomp.appc.i18n.Msg;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import org.openecomp.sdnc.sli.SvcLogicContext;
42
43 public class SshDBPluginImpl implements SshDBPlugin {
44
45     private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
46     private static ObjectMapper mapper = new ObjectMapper();
47
48     private SshDataAccessService dataAccessService;
49
50     public void setDataAccessService(SshDataAccessService dataAccessService) {
51         this.dataAccessService = dataAccessService;
52     }
53
54     public void retrieveConnectionDetails(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
55         SshConnectionDetails connectionDetails = new SshConnectionDetails();
56         //String vnfType = ctx.getAttribute("aai.prefix")+"."+"vnf-type";
57         String vnfType = params.get("vnf-type");
58         try {
59             if (!dataAccessService.retrieveConnectionDetails(vnfType, connectionDetails)) {
60                 logger.error("Missing connection details for VNF type: " + vnfType);
61                 throw new APPCException("Missing configuration for " + vnfType + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME);
62             }
63             connectionDetails.setHost(params.get(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME));
64             ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails));
65         } catch(APPCException e) {
66             String msg = EELFResourceManager.format(Msg.APPC_EXCEPTION, vnfType, e.getMessage());
67             logger.error(msg);
68             ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
69             throw e;
70         } catch(SshDataAccessException e) {
71             String msg = EELFResourceManager.format(Msg.SSH_DATA_EXCEPTION, e.getMessage());
72             logger.error(msg);
73             ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
74             throw e;
75         } catch (JsonProcessingException e) {
76             String msg = EELFResourceManager.format(Msg.JSON_PROCESSING_EXCEPTION, e.getMessage());
77             logger.error(msg);
78             ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
79             throw new APPCException(e);
80         }
81     }
82
83 }