a11b36f0f1561b27f3ce29d8c7e0ccc3dc70724c
[appc.git] /
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             ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails));
81             ctx.setAttribute("retrieveVMDSConfiguration_Result","success");
82         } catch(APPCException e) {
83             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
84             throw e;
85         } catch(DataAccessException | JsonProcessingException e) {
86             logger.error("Error " + e.getMessage());
87             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
88             throw new APPCException(e);
89         }
90     }
91
92     @Override
93     public void retrieveConfigFile(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
94         String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME));
95         ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
96     }
97
98     public void retrieveConnectionDetails(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
99         getConnection(params, ctx);
100     }
101
102             private void getConnection(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
103                 NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
104                 try {
105                     if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.VNF_TYPE_FIELD_NAME), connectionDetails)) {
106                         logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME));
107                         throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME);
108                     }
109                     connectionDetails.setHost(params.get(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME));
110                     ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails));
111                 } catch(APPCException e) {
112                     ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
113                     throw e;
114                 } catch(DataAccessException | JsonProcessingException e) {
115                     logger.error("Error " + e.getMessage());
116             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
117             throw new APPCException(e);
118         }
119     }
120 }