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