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