2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.dg.netconf.impl;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.apache.commons.lang.ObjectUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openecomp.appc.adapter.netconf.*;
28 import org.openecomp.appc.adapter.netconf.util.Constants;
29 import org.openecomp.appc.dg.netconf.NetconfClientPlugin;
30 import org.openecomp.appc.exceptions.APPCException;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
38 import java.io.IOException;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
46 public class NetconfClientPluginImpl implements NetconfClientPlugin {
48 private static final String NETCONF_CLIENT_FACTORY_NAME = "org.openecomp.appc.adapter.netconf.NetconfClientFactory";
49 private static ObjectMapper mapper = new ObjectMapper();
50 private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
52 private NetconfDataAccessService dao;
53 private NetconfClientFactory clientFactory;
55 public NetconfClientPluginImpl() {
56 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
57 ServiceReference srefNetconfClientFactory = bctx.getServiceReference(NetconfClientFactory.class);
58 clientFactory = (NetconfClientFactory) bctx.getService(srefNetconfClientFactory);
61 public void setDao(NetconfDataAccessService dao) {
63 this.dao.setSchema(Constants.NETCONF_SCHEMA);
66 public void configure(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
69 // by default, it uses the jsch Netconf Adapter implementation by calling GetNetconfClient(NetconfClientType.SSH).
70 NetconfClient client = clientFactory.GetNetconfClient(NetconfClientType.SSH);
72 NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class);
73 String netconfMessage = params.get("file-content");
74 client.connect(connectionDetails);
75 client.configure(netconfMessage);
76 } catch (IOException e) {
77 logger.error("Error " + e.getMessage());
78 throw new APPCException(e);
82 } catch (Exception e) {
83 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
84 logger.error("Error " + e.getMessage());
90 public void operationStateValidation(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
91 if (logger.isTraceEnabled()) {
92 logger.trace("Entering to operationStateValidation with params = "+ ObjectUtils.toString(params)+", SvcLogicContext = "+ObjectUtils.toString(ctx));
95 String paramName = Constants.VNF_TYPE_FIELD_NAME;
96 String vfType = params.get(paramName);
97 validateMandatoryParam(paramName, vfType);
98 VnfType vnfType = VnfType.getVnfType(vfType);
100 paramName = Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME;
101 String vnfHostIpAddress = params.get(paramName);
102 validateMandatoryParam(paramName, vnfHostIpAddress);
104 //get connectionDetails
105 String connectionDetailsStr = params.get(Constants.CONNECTION_DETAILS_FIELD_NAME);
106 NetconfConnectionDetails connectionDetails = null;
107 if(StringUtils.isEmpty(connectionDetailsStr)){
108 connectionDetails = retrieveConnectionDetails(vnfType);
109 connectionDetails.setHost(vnfHostIpAddress);
110 ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails));
112 connectionDetails = mapper.readValue(connectionDetailsStr, NetconfConnectionDetails.class);
114 if(connectionDetails == null){
115 throw new IllegalStateException("missing connectionDetails for VnfType:"+vnfType.name());
118 //get operationsStateNetconfMessage
119 OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(vnfType);
120 String configurationFileName = operationalStateValidator.getConfigurationFileName();
121 String operationsStateNetconfMessage = null;
122 if(!StringUtils.isEmpty(configurationFileName)){
123 operationsStateNetconfMessage = retrieveConfigurationFileContent(configurationFileName);
126 //connect checK Opertaions state and dissconnect
127 NetconfClient client = clientFactory.GetNetconfClient(NetconfClientType.SSH);
129 client.connect(connectionDetails);
130 String response = null;
131 if(!StringUtils.isEmpty(operationsStateNetconfMessage)) {
132 response = client.exchangeMessage(operationsStateNetconfMessage);
134 operationalStateValidator.validateResponse(response);
138 } catch (APPCException e) {
139 logger.error(e.getMessage());
140 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString());
143 catch (Exception e) {
144 logger.error(e.toString());
145 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString());
146 throw new APPCException(e);
151 public void modifyConfiguration(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
152 this.configure(params, ctx);
156 public void backupConfiguration(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
158 NetconfClient client = null;
160 if (logger.isDebugEnabled()) {
161 logger.debug("Entered backup to DEVICE_INTERFACE_LOG");
164 client = clientFactory.GetNetconfClient(NetconfClientType.SSH);
165 //get connection details
166 NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class);
167 //connect the client and get configuration
168 client.connect(connectionDetails);
169 String configuration = client.getConfiguration();
171 //store configuration in database
172 dao.logDeviceInteraction(null,null,getCurrentDateTime(),configuration);
174 } catch (Exception e) {
175 logger.error("Error " + e.getMessage());
176 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
177 throw new APPCException(e);
179 //disconnect the client
187 public void getConfig(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
188 NetconfClient client = null;
189 String confId=params.get("conf-id");
190 if(confId.equalsIgnoreCase("current")){
192 if (logger.isDebugEnabled()) {
193 logger.debug("Entered getConfig to DEVICE_INTERFACE_LOG");
195 //get netconf client to get configuration
196 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
197 ServiceReference sref = bctx.getServiceReference(NETCONF_CLIENT_FACTORY_NAME);
198 NetconfClientFactory clientFactory = (NetconfClientFactory) bctx.getService(sref);
199 client = clientFactory.GetNetconfClient(NetconfClientType.SSH);
200 //get connection details
201 NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class);
202 //connect the client and get configuration
203 client.connect(connectionDetails);
204 String configuration = client.getConfiguration();
205 if(configuration !=null){
206 // logger.info("*************************************Configuration Output*************************************");
207 // logger.info(configuration);
208 String fullConfig = ctx.getAttribute("fullConfig");
209 fullConfig = fullConfig==null?"":fullConfig;
210 ctx.setAttribute("fullConfig",fullConfig + configuration);
212 ctx.setAttribute("getConfig_Result","Success");
213 String entityName=ctx.getAttribute("entity");//VM name
214 if(entityName!=null){
215 ctx.setAttribute(entityName+".Configuration",configuration);
218 ctx.setAttribute("getConfig_Result","failure");
220 //store configuration in database
221 /*NetconfJDBC dsImpl = new NetconfJDBCImpl();
222 dsImpl.logDeviceInteraction(null,null,getCurrentDateTime(),configuration);*/
224 } catch (Exception e) {
225 ctx.setAttribute("getConfig_Result","failure");
226 logger.error("Error " + e.getMessage());
227 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
228 throw new APPCException(e);
230 //disconnect the client
236 logger.info("Current Conf id value is not supported");
243 public void getRunningConfig(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
244 NetconfClient client = null;
246 logger.info("Entered getRunningConfig to DEVICE_INTERFACE_LOG");
247 //get netconf client to get configuration
248 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
249 ServiceReference sref = bctx.getServiceReference(NETCONF_CLIENT_FACTORY_NAME);
250 NetconfClientFactory clientFactory = (NetconfClientFactory) bctx.getService(sref);
251 client = clientFactory.GetNetconfClient(NetconfClientType.SSH);
252 //get connection details
253 NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
254 connectionDetails.setHost(params.get("host-ip-address"));
255 connectionDetails.setUsername(params.get("user-name"));
256 connectionDetails.setPassword(params.get("password"));
257 connectionDetails.setPort(!("".equalsIgnoreCase(params.get("port-number")))?Integer.parseInt(params.get("port-number")):NetconfConnectionDetails.DEFAULT_PORT);
258 //connect the client and get configuration
259 client.connect(connectionDetails);
260 String configuration = client.getConfiguration();
261 if(configuration !=null){
262 // logger.info("*************************************Configuration Output*************************************");
263 ctx.setAttribute("running-config", configuration);
265 ctx.setAttribute("getRunningConfig_Result","Success");
267 ctx.setAttribute("getRunningConfig_Result","failure");
269 } catch (Exception e) {
270 ctx.setAttribute("getRunningConfig_Result","failure");
271 logger.error("Error " + e.getMessage());
272 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage());
273 throw new APPCException(e);
275 //disconnect the client
282 private String getCurrentDateTime() {
284 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
285 Date date = new Date();
286 return dateFormat.format(date);
289 private int getPort(String s) {
291 if((s != null) && !s.isEmpty()) {
292 port = Integer.parseInt(s);
297 void validateMandatoryParam(String paramName, String paramValue) {
298 if(StringUtils.isEmpty(paramValue)){
299 throw new IllegalArgumentException("input "+paramName+" param is empty");
303 public NetconfConnectionDetails retrieveConnectionDetails( VnfType vnfType) throws APPCException{
305 NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
306 if (!dao.retrieveNetconfConnectionDetails(vnfType.getFamilyType().name(), connectionDetails)) {
307 logger.error("Missing configuration for " + vnfType.getFamilyType().name());
308 throw new APPCException("Missing configuration for " + vnfType.getFamilyType().name() + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME);
310 return connectionDetails;
313 public String retrieveConfigurationFileContent(String configFileName){
314 return dao.retrieveConfigFileName(configFileName);