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