b56d53cb7705223b8b14f15978916f118523d8a7
[appc.git] / appc-dg / appc-dg-shared / appc-dg-netconf / src / main / java / org / onap / appc / dg / netconf / impl / NetconfDBPluginImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dg.netconf.impl;
25
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 import java.util.Map;
30
31 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
32 import org.onap.appc.adapter.netconf.NetconfDataAccessService;
33 import org.onap.appc.adapter.netconf.exception.DataAccessException;
34 import org.onap.appc.adapter.netconf.util.Constants;
35 import org.onap.appc.dg.netconf.NetconfDBPlugin;
36 import org.onap.appc.exceptions.APPCException;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40
41 public class NetconfDBPluginImpl implements NetconfDBPlugin {
42
43     private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
44     private static ObjectMapper mapper = new ObjectMapper();
45
46     // populated by blueprint framework
47     private NetconfDataAccessService daoService;
48
49     public void setDaoService(NetconfDataAccessService daoService) {
50         this.daoService = daoService;
51         this.daoService.setSchema(Constants.NETCONF_SCHEMA);
52     }
53
54     public NetconfDBPluginImpl() {
55     }
56
57     public void retrieveDSConfiguration(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58
59         try {
60             String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME));
61             ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
62         } catch(DataAccessException e) {
63             logger.error("Error " + e.getMessage());
64             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
65             throw e;
66         }
67
68         getConnection(params, ctx);
69     }
70
71     @Override
72     public void retrieveVMDSConfiguration(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
73         logger.info("Setting entity value :" +params.get(Constants.RESOURCEKEY));
74         ctx.setAttribute("entity", params.get(Constants.RESOURCEKEY));
75         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
76         try {
77             if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.RESOURCEKEY), connectionDetails)) {
78                 ctx.setAttribute("retrieveVMDSConfiguration_Result","failure");
79                 logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME));
80                 throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME);
81             }
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 }