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