62707e3dfbee53e2e3f68595c616bae2ada6153c
[appc.git] / appc-dg / appc-dg-shared / appc-dg-netconf / src / main / java / org / openecomp / appc / dg / netconf / impl / NetconfDBPluginImpl.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.openecomp.appc.dg.netconf.impl;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 import java.util.Map;
31
32 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
33 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
34 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
35 import org.openecomp.appc.adapter.netconf.util.Constants;
36 import org.openecomp.appc.dg.netconf.NetconfDBPlugin;
37 import org.openecomp.appc.exceptions.APPCException;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41
42 public class NetconfDBPluginImpl implements NetconfDBPlugin {
43
44     private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
45     private static ObjectMapper mapper = new ObjectMapper();
46
47     // populated by blueprint framework
48     private NetconfDataAccessService daoService;
49
50     public void setDaoService(NetconfDataAccessService daoService) {
51         this.daoService = daoService;
52         this.daoService.setSchema(Constants.NETCONF_SCHEMA);
53     }
54
55     public NetconfDBPluginImpl() {
56     }
57
58     public void retrieveDSConfiguration(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59
60         try {
61             String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME));
62             ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
63         } catch(DataAccessException e) {
64             logger.error("Error " + e.getMessage());
65             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
66             throw e;
67         }
68
69         getConnection(params, ctx);
70     }
71
72     @Override
73     public void retrieveVMDSConfiguration(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
74         logger.info("Setting entity value :" +params.get(Constants.RESOURCEKEY));
75         ctx.setAttribute("entity", params.get(Constants.RESOURCEKEY));
76         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
77         try {
78             if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.RESOURCEKEY), connectionDetails)) {
79                 ctx.setAttribute("retrieveVMDSConfiguration_Result","failure");
80                 logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME));
81                 throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME);
82             }
83             ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails));
84             ctx.setAttribute("retrieveVMDSConfiguration_Result","success");
85         } catch(APPCException e) {
86             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
87             throw e;
88         } catch(DataAccessException | JsonProcessingException e) {
89             logger.error("Error " + e.getMessage());
90             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
91             throw new APPCException(e);
92         }
93     }
94
95     @Override
96     public void retrieveConfigFile(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
97         String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME));
98         ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
99     }
100
101     public void retrieveConnectionDetails(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
102         getConnection(params, ctx);
103     }
104
105             private void getConnection(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
106                 NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
107                 try {
108                     if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.VNF_TYPE_FIELD_NAME), connectionDetails)) {
109                         logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME));
110                         throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME);
111                     }
112                     connectionDetails.setHost(params.get(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME));
113                     ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails));
114                 } catch(APPCException e) {
115                     ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
116                     throw e;
117                 } catch(DataAccessException | JsonProcessingException e) {
118                     logger.error("Error " + e.getMessage());
119             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
120             throw new APPCException(e);
121         }
122     }
123 }