From 781b1a6df324419c846c84ea983c18fc8362bfd3 Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Wed, 13 Dec 2017 11:19:06 -0800 Subject: Third part of onap rename This part of the commit changes the folder structure on all other folders of appc. Change-Id: I8acfa11cdfcdcd36be0e137245d1dd7324f1abd3 Signed-off-by: Patrick Brady Issue-ID: APPC-13 --- .../onap/appc/dg/netconf/NetconfClientPlugin.java | 41 ++ .../org/onap/appc/dg/netconf/NetconfDBPlugin.java | 40 ++ .../dg/netconf/impl/NetconfClientPluginImpl.java | 314 ++++++++++ .../appc/dg/netconf/impl/NetconfDBPluginImpl.java | 123 ++++ .../appc/dg/netconf/NetconfClientPlugin.java | 41 -- .../openecomp/appc/dg/netconf/NetconfDBPlugin.java | 40 -- .../dg/netconf/impl/NetconfClientPluginImpl.java | 314 ---------- .../appc/dg/netconf/impl/NetconfDBPluginImpl.java | 123 ---- .../onap/appc/dg/netconf/impl/DAOServiceMock.java | 100 +++ .../impl/MockOperationalStateValidatorImpl.java | 64 ++ .../dg/netconf/impl/NetconfClientFactoryMock.java | 46 ++ .../dg/netconf/impl/NetconfClientJschMock.java | 103 ++++ .../netconf/impl/NetconfClientPluginImplTest.java | 681 +++++++++++++++++++++ .../dg/netconf/impl/NetconfDBPluginImplTest.java | 253 ++++++++ .../appc/dg/netconf/impl/ObjectMapperMock.java | 48 ++ .../impl/OperationStateValidatorFactoryMock.java | 50 ++ .../appc/dg/netconf/impl/DAOServiceMock.java | 100 --- .../impl/MockOperationalStateValidatorImpl.java | 64 -- .../dg/netconf/impl/NetconfClientFactoryMock.java | 46 -- .../dg/netconf/impl/NetconfClientJschMock.java | 103 ---- .../netconf/impl/NetconfClientPluginImplTest.java | 681 --------------------- .../dg/netconf/impl/NetconfDBPluginImplTest.java | 253 -------- .../appc/dg/netconf/impl/ObjectMapperMock.java | 48 -- .../impl/OperationStateValidatorFactoryMock.java | 50 -- 24 files changed, 1863 insertions(+), 1863 deletions(-) create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfClientPlugin.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfDBPlugin.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImpl.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImpl.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfClientPlugin.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfDBPlugin.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImpl.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImpl.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/DAOServiceMock.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientFactoryMock.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientJschMock.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImplTest.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImplTest.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/ObjectMapperMock.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/DAOServiceMock.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientFactoryMock.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientJschMock.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImplTest.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImplTest.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/ObjectMapperMock.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java (limited to 'appc-dg/appc-dg-shared/appc-dg-netconf') diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfClientPlugin.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfClientPlugin.java new file mode 100644 index 000000000..e1c563a4b --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfClientPlugin.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf; + +import java.util.Map; + +import org.onap.appc.exceptions.APPCException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; + + +public interface NetconfClientPlugin extends SvcLogicJavaPlugin { + void configure(Map params, SvcLogicContext ctx) throws APPCException; + void operationStateValidation(Map params, SvcLogicContext ctx) throws APPCException; + void modifyConfiguration(Map params, SvcLogicContext ctx) throws APPCException; + void backupConfiguration(Map params, SvcLogicContext ctx) throws APPCException; + void getConfig(Map params, SvcLogicContext ctx) throws APPCException; + void getRunningConfig(Map params, SvcLogicContext ctx) throws APPCException; +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfDBPlugin.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfDBPlugin.java new file mode 100644 index 000000000..a4086a521 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/NetconfDBPlugin.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf; + +import java.util.Map; + +import org.onap.appc.exceptions.APPCException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; + + + +public interface NetconfDBPlugin extends SvcLogicJavaPlugin { + void retrieveDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException; + void retrieveVMDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException; + void retrieveConfigFile(Map params, SvcLogicContext ctx) throws APPCException; + void retrieveConnectionDetails(Map params, SvcLogicContext ctx) throws APPCException; +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImpl.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImpl.java new file mode 100644 index 000000000..174226473 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImpl.java @@ -0,0 +1,314 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.appc.adapter.netconf.*; +import org.onap.appc.adapter.netconf.util.Constants; +import org.onap.appc.dg.netconf.NetconfClientPlugin; +import org.onap.appc.exceptions.APPCException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; + + + +public class NetconfClientPluginImpl implements NetconfClientPlugin { + + private static final String NETCONF_CLIENT_FACTORY_NAME = "org.onap.appc.adapter.netconf.NetconfClientFactory"; + private static ObjectMapper mapper = new ObjectMapper(); + private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger(); + + private NetconfDataAccessService dao; + private NetconfClientFactory clientFactory; + + public NetconfClientPluginImpl() { + BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + ServiceReference srefNetconfClientFactory = bctx.getServiceReference(NetconfClientFactory.class); + clientFactory = (NetconfClientFactory) bctx.getService(srefNetconfClientFactory); + } + + public void setDao(NetconfDataAccessService dao) { + this.dao = dao; + this.dao.setSchema(Constants.NETCONF_SCHEMA); + } + + public void configure(Map params, SvcLogicContext ctx) throws APPCException { + + try { + // by default, it uses the jsch Netconf Adapter implementation by calling GetNetconfClient(NetconfClientType.SSH). + NetconfClient client = clientFactory.GetNetconfClient(NetconfClientType.SSH); + try { + NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class); + String netconfMessage = params.get("file-content"); + client.connect(connectionDetails); + client.configure(netconfMessage); + } catch (IOException e) { + logger.error("Error " + e.getMessage()); + throw new APPCException(e); + } finally { + client.disconnect(); + } + } catch (Exception e) { + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + logger.error("Error " + e.getMessage()); + throw e; + } + } + + @Override + public void operationStateValidation(Map params, SvcLogicContext ctx) throws APPCException { + if (logger.isTraceEnabled()) { + logger.trace("Entering to operationStateValidation with params = "+ ObjectUtils.toString(params)+", SvcLogicContext = "+ObjectUtils.toString(ctx)); + } + try{ + String paramName = Constants.VNF_TYPE_FIELD_NAME; + String vfType = params.get(paramName); + validateMandatoryParam(paramName, vfType); + VnfType vnfType = VnfType.getVnfType(vfType); + + paramName = Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME; + String vnfHostIpAddress = params.get(paramName); + validateMandatoryParam(paramName, vnfHostIpAddress); + + //get connectionDetails + String connectionDetailsStr = params.get(Constants.CONNECTION_DETAILS_FIELD_NAME); + NetconfConnectionDetails connectionDetails = null; + if(StringUtils.isEmpty(connectionDetailsStr)){ + connectionDetails = retrieveConnectionDetails(vnfType); + connectionDetails.setHost(vnfHostIpAddress); + ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails)); + }else{ + connectionDetails = mapper.readValue(connectionDetailsStr, NetconfConnectionDetails.class); + } + if(connectionDetails == null){ + throw new IllegalStateException("missing connectionDetails for VnfType:"+vnfType.name()); + } + + //get operationsStateNetconfMessage + OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(vnfType); + String configurationFileName = operationalStateValidator.getConfigurationFileName(); + String operationsStateNetconfMessage = null; + if(!StringUtils.isEmpty(configurationFileName)){ + operationsStateNetconfMessage = retrieveConfigurationFileContent(configurationFileName); + } + + //connect checK Opertaions state and dissconnect + NetconfClient client = clientFactory.GetNetconfClient(NetconfClientType.SSH); + try { + client.connect(connectionDetails); + String response = null; + if(!StringUtils.isEmpty(operationsStateNetconfMessage)) { + response = client.exchangeMessage(operationsStateNetconfMessage); + } + operationalStateValidator.validateResponse(response); + } finally { + client.disconnect(); + } + } catch (APPCException e) { + logger.error(e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString()); + throw e; + } + catch (Exception e) { + logger.error(e.toString()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString()); + throw new APPCException(e); + } + } + + @Override + public void modifyConfiguration(Map params, SvcLogicContext ctx) throws APPCException { + this.configure(params, ctx); + } + + @Override + public void backupConfiguration(Map params, SvcLogicContext ctx) throws APPCException { + + NetconfClient client = null; + try { + if (logger.isDebugEnabled()) { + logger.debug("Entered backup to DEVICE_INTERFACE_LOG"); + } + + client = clientFactory.GetNetconfClient(NetconfClientType.SSH); + //get connection details + NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class); + //connect the client and get configuration + client.connect(connectionDetails); + String configuration = client.getConfiguration(); + + //store configuration in database + dao.logDeviceInteraction(null,null,getCurrentDateTime(),configuration); + + } catch (Exception e) { + logger.error("Error " + e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw new APPCException(e); + } finally { + //disconnect the client + if(client != null) { + client.disconnect(); + } + } + } + + @Override + public void getConfig(Map params, SvcLogicContext ctx) throws APPCException { + NetconfClient client = null; + String confId=params.get("conf-id"); + if(confId.equalsIgnoreCase("current")){ + try { + if (logger.isDebugEnabled()) { + logger.debug("Entered getConfig to DEVICE_INTERFACE_LOG"); + } + //get netconf client to get configuration + BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + ServiceReference sref = bctx.getServiceReference(NETCONF_CLIENT_FACTORY_NAME); + NetconfClientFactory clientFactory = (NetconfClientFactory) bctx.getService(sref); + client = clientFactory.GetNetconfClient(NetconfClientType.SSH); + //get connection details + NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class); + //connect the client and get configuration + client.connect(connectionDetails); + String configuration = client.getConfiguration(); + if(configuration !=null){ + String fullConfig = ctx.getAttribute("fullConfig"); + fullConfig = fullConfig==null?"":fullConfig; + ctx.setAttribute("fullConfig",fullConfig + configuration); + + ctx.setAttribute("getConfig_Result","Success"); + String entityName=ctx.getAttribute("entity");//VM name + if(entityName!=null){ + ctx.setAttribute(entityName+".Configuration",configuration); + } + }else{ + ctx.setAttribute("getConfig_Result","failure"); + } + } catch (Exception e) { + ctx.setAttribute("getConfig_Result","failure"); + logger.error("Error " + e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw new APPCException(e); + } finally { + //disconnect the client + if(client != null) { + client.disconnect(); + } + } + }else{ + logger.info("Current Conf id value is not supported"); + } + + } + + + @Override + public void getRunningConfig(Map params, SvcLogicContext ctx) throws APPCException { + NetconfClient client = null; + try { + logger.info("Entered getRunningConfig to DEVICE_INTERFACE_LOG"); + //get netconf client to get configuration + BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + ServiceReference sref = bctx.getServiceReference(NETCONF_CLIENT_FACTORY_NAME); + NetconfClientFactory clientFactory = (NetconfClientFactory) bctx.getService(sref); + client = clientFactory.GetNetconfClient(NetconfClientType.SSH); + //get connection details + NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); + connectionDetails.setHost(params.get("host-ip-address")); + connectionDetails.setUsername(params.get("user-name")); + connectionDetails.setPassword(params.get("password")); + connectionDetails.setPort(!("".equalsIgnoreCase(params.get("port-number")))?Integer.parseInt(params.get("port-number")):NetconfConnectionDetails.DEFAULT_PORT); + //connect the client and get configuration + client.connect(connectionDetails); + String configuration = client.getConfiguration(); + if(configuration !=null){ + // logger.info("*************************************Configuration Output*************************************"); + ctx.setAttribute("running-config", configuration); + + ctx.setAttribute("getRunningConfig_Result","Success"); + }else{ + ctx.setAttribute("getRunningConfig_Result","failure"); + } + } catch (Exception e) { + ctx.setAttribute("getRunningConfig_Result","failure"); + logger.error("Error " + e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw new APPCException(e); + } finally { + //disconnect the client + if(client != null) { + client.disconnect(); + } + } + } + + private String getCurrentDateTime() { + + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Date date = new Date(); + return dateFormat.format(date); + } + + private int getPort(String s) { + int port = 830; + if((s != null) && !s.isEmpty()) { + port = Integer.parseInt(s); + } + return port; + } + + void validateMandatoryParam(String paramName, String paramValue) { + if(StringUtils.isEmpty(paramValue)){ + throw new IllegalArgumentException("input "+paramName+" param is empty"); + } + } + + public NetconfConnectionDetails retrieveConnectionDetails( VnfType vnfType) throws APPCException{ + + NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); + if (!dao.retrieveNetconfConnectionDetails(vnfType.getFamilyType().name(), connectionDetails)) { + logger.error("Missing configuration for " + vnfType.getFamilyType().name()); + throw new APPCException("Missing configuration for " + vnfType.getFamilyType().name() + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME); + } + return connectionDetails; + } + + public String retrieveConfigurationFileContent(String configFileName){ + return dao.retrieveConfigFileName(configFileName); + } + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImpl.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImpl.java new file mode 100644 index 000000000..459ece9c1 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImpl.java @@ -0,0 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.Map; + +import org.onap.appc.adapter.netconf.NetconfConnectionDetails; +import org.onap.appc.adapter.netconf.NetconfDataAccessService; +import org.onap.appc.adapter.netconf.exception.DataAccessException; +import org.onap.appc.adapter.netconf.util.Constants; +import org.onap.appc.dg.netconf.NetconfDBPlugin; +import org.onap.appc.exceptions.APPCException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; + +public class NetconfDBPluginImpl implements NetconfDBPlugin { + + private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger(); + private static ObjectMapper mapper = new ObjectMapper(); + + // populated by blueprint framework + private NetconfDataAccessService daoService; + + public void setDaoService(NetconfDataAccessService daoService) { + this.daoService = daoService; + this.daoService.setSchema(Constants.NETCONF_SCHEMA); + } + + public NetconfDBPluginImpl() { + } + + public void retrieveDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException { + + try { + String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME)); + ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent); + } catch(DataAccessException e) { + logger.error("Error " + e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw e; + } + + getConnection(params, ctx); + } + + @Override + public void retrieveVMDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException { + logger.info("Setting entity value :" +params.get(Constants.RESOURCEKEY)); + ctx.setAttribute("entity", params.get(Constants.RESOURCEKEY)); + NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); + try { + if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.RESOURCEKEY), connectionDetails)) { + ctx.setAttribute("retrieveVMDSConfiguration_Result","failure"); + logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME)); + throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME); + } + ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails)); + ctx.setAttribute("retrieveVMDSConfiguration_Result","success"); + } catch(APPCException e) { + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw e; + } catch(DataAccessException | JsonProcessingException e) { + logger.error("Error " + e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw new APPCException(e); + } + } + + @Override + public void retrieveConfigFile(Map params, SvcLogicContext ctx) throws APPCException { + String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME)); + ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent); + } + + public void retrieveConnectionDetails(Map params, SvcLogicContext ctx) throws APPCException { + getConnection(params, ctx); + } + + private void getConnection(Map params, SvcLogicContext ctx) throws APPCException { + NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); + try { + if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.VNF_TYPE_FIELD_NAME), connectionDetails)) { + logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME)); + throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME); + } + connectionDetails.setHost(params.get(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME)); + ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails)); + } catch(APPCException e) { + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw e; + } catch(DataAccessException | JsonProcessingException e) { + logger.error("Error " + e.getMessage()); + ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); + throw new APPCException(e); + } + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfClientPlugin.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfClientPlugin.java deleted file mode 100644 index e1c563a4b..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfClientPlugin.java +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf; - -import java.util.Map; - -import org.onap.appc.exceptions.APPCException; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; - - -public interface NetconfClientPlugin extends SvcLogicJavaPlugin { - void configure(Map params, SvcLogicContext ctx) throws APPCException; - void operationStateValidation(Map params, SvcLogicContext ctx) throws APPCException; - void modifyConfiguration(Map params, SvcLogicContext ctx) throws APPCException; - void backupConfiguration(Map params, SvcLogicContext ctx) throws APPCException; - void getConfig(Map params, SvcLogicContext ctx) throws APPCException; - void getRunningConfig(Map params, SvcLogicContext ctx) throws APPCException; -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfDBPlugin.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfDBPlugin.java deleted file mode 100644 index a4086a521..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/NetconfDBPlugin.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf; - -import java.util.Map; - -import org.onap.appc.exceptions.APPCException; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; - - - -public interface NetconfDBPlugin extends SvcLogicJavaPlugin { - void retrieveDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException; - void retrieveVMDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException; - void retrieveConfigFile(Map params, SvcLogicContext ctx) throws APPCException; - void retrieveConnectionDetails(Map params, SvcLogicContext ctx) throws APPCException; -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImpl.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImpl.java deleted file mode 100644 index 174226473..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImpl.java +++ /dev/null @@ -1,314 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang3.StringUtils; -import org.onap.appc.adapter.netconf.*; -import org.onap.appc.adapter.netconf.util.Constants; -import org.onap.appc.dg.netconf.NetconfClientPlugin; -import org.onap.appc.exceptions.APPCException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; - -import java.io.IOException; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Map; - - - -public class NetconfClientPluginImpl implements NetconfClientPlugin { - - private static final String NETCONF_CLIENT_FACTORY_NAME = "org.onap.appc.adapter.netconf.NetconfClientFactory"; - private static ObjectMapper mapper = new ObjectMapper(); - private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger(); - - private NetconfDataAccessService dao; - private NetconfClientFactory clientFactory; - - public NetconfClientPluginImpl() { - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - ServiceReference srefNetconfClientFactory = bctx.getServiceReference(NetconfClientFactory.class); - clientFactory = (NetconfClientFactory) bctx.getService(srefNetconfClientFactory); - } - - public void setDao(NetconfDataAccessService dao) { - this.dao = dao; - this.dao.setSchema(Constants.NETCONF_SCHEMA); - } - - public void configure(Map params, SvcLogicContext ctx) throws APPCException { - - try { - // by default, it uses the jsch Netconf Adapter implementation by calling GetNetconfClient(NetconfClientType.SSH). - NetconfClient client = clientFactory.GetNetconfClient(NetconfClientType.SSH); - try { - NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class); - String netconfMessage = params.get("file-content"); - client.connect(connectionDetails); - client.configure(netconfMessage); - } catch (IOException e) { - logger.error("Error " + e.getMessage()); - throw new APPCException(e); - } finally { - client.disconnect(); - } - } catch (Exception e) { - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - logger.error("Error " + e.getMessage()); - throw e; - } - } - - @Override - public void operationStateValidation(Map params, SvcLogicContext ctx) throws APPCException { - if (logger.isTraceEnabled()) { - logger.trace("Entering to operationStateValidation with params = "+ ObjectUtils.toString(params)+", SvcLogicContext = "+ObjectUtils.toString(ctx)); - } - try{ - String paramName = Constants.VNF_TYPE_FIELD_NAME; - String vfType = params.get(paramName); - validateMandatoryParam(paramName, vfType); - VnfType vnfType = VnfType.getVnfType(vfType); - - paramName = Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME; - String vnfHostIpAddress = params.get(paramName); - validateMandatoryParam(paramName, vnfHostIpAddress); - - //get connectionDetails - String connectionDetailsStr = params.get(Constants.CONNECTION_DETAILS_FIELD_NAME); - NetconfConnectionDetails connectionDetails = null; - if(StringUtils.isEmpty(connectionDetailsStr)){ - connectionDetails = retrieveConnectionDetails(vnfType); - connectionDetails.setHost(vnfHostIpAddress); - ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails)); - }else{ - connectionDetails = mapper.readValue(connectionDetailsStr, NetconfConnectionDetails.class); - } - if(connectionDetails == null){ - throw new IllegalStateException("missing connectionDetails for VnfType:"+vnfType.name()); - } - - //get operationsStateNetconfMessage - OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(vnfType); - String configurationFileName = operationalStateValidator.getConfigurationFileName(); - String operationsStateNetconfMessage = null; - if(!StringUtils.isEmpty(configurationFileName)){ - operationsStateNetconfMessage = retrieveConfigurationFileContent(configurationFileName); - } - - //connect checK Opertaions state and dissconnect - NetconfClient client = clientFactory.GetNetconfClient(NetconfClientType.SSH); - try { - client.connect(connectionDetails); - String response = null; - if(!StringUtils.isEmpty(operationsStateNetconfMessage)) { - response = client.exchangeMessage(operationsStateNetconfMessage); - } - operationalStateValidator.validateResponse(response); - } finally { - client.disconnect(); - } - } catch (APPCException e) { - logger.error(e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString()); - throw e; - } - catch (Exception e) { - logger.error(e.toString()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.toString()); - throw new APPCException(e); - } - } - - @Override - public void modifyConfiguration(Map params, SvcLogicContext ctx) throws APPCException { - this.configure(params, ctx); - } - - @Override - public void backupConfiguration(Map params, SvcLogicContext ctx) throws APPCException { - - NetconfClient client = null; - try { - if (logger.isDebugEnabled()) { - logger.debug("Entered backup to DEVICE_INTERFACE_LOG"); - } - - client = clientFactory.GetNetconfClient(NetconfClientType.SSH); - //get connection details - NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class); - //connect the client and get configuration - client.connect(connectionDetails); - String configuration = client.getConfiguration(); - - //store configuration in database - dao.logDeviceInteraction(null,null,getCurrentDateTime(),configuration); - - } catch (Exception e) { - logger.error("Error " + e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw new APPCException(e); - } finally { - //disconnect the client - if(client != null) { - client.disconnect(); - } - } - } - - @Override - public void getConfig(Map params, SvcLogicContext ctx) throws APPCException { - NetconfClient client = null; - String confId=params.get("conf-id"); - if(confId.equalsIgnoreCase("current")){ - try { - if (logger.isDebugEnabled()) { - logger.debug("Entered getConfig to DEVICE_INTERFACE_LOG"); - } - //get netconf client to get configuration - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - ServiceReference sref = bctx.getServiceReference(NETCONF_CLIENT_FACTORY_NAME); - NetconfClientFactory clientFactory = (NetconfClientFactory) bctx.getService(sref); - client = clientFactory.GetNetconfClient(NetconfClientType.SSH); - //get connection details - NetconfConnectionDetails connectionDetails = mapper.readValue(params.get("connection-details"), NetconfConnectionDetails.class); - //connect the client and get configuration - client.connect(connectionDetails); - String configuration = client.getConfiguration(); - if(configuration !=null){ - String fullConfig = ctx.getAttribute("fullConfig"); - fullConfig = fullConfig==null?"":fullConfig; - ctx.setAttribute("fullConfig",fullConfig + configuration); - - ctx.setAttribute("getConfig_Result","Success"); - String entityName=ctx.getAttribute("entity");//VM name - if(entityName!=null){ - ctx.setAttribute(entityName+".Configuration",configuration); - } - }else{ - ctx.setAttribute("getConfig_Result","failure"); - } - } catch (Exception e) { - ctx.setAttribute("getConfig_Result","failure"); - logger.error("Error " + e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw new APPCException(e); - } finally { - //disconnect the client - if(client != null) { - client.disconnect(); - } - } - }else{ - logger.info("Current Conf id value is not supported"); - } - - } - - - @Override - public void getRunningConfig(Map params, SvcLogicContext ctx) throws APPCException { - NetconfClient client = null; - try { - logger.info("Entered getRunningConfig to DEVICE_INTERFACE_LOG"); - //get netconf client to get configuration - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - ServiceReference sref = bctx.getServiceReference(NETCONF_CLIENT_FACTORY_NAME); - NetconfClientFactory clientFactory = (NetconfClientFactory) bctx.getService(sref); - client = clientFactory.GetNetconfClient(NetconfClientType.SSH); - //get connection details - NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); - connectionDetails.setHost(params.get("host-ip-address")); - connectionDetails.setUsername(params.get("user-name")); - connectionDetails.setPassword(params.get("password")); - connectionDetails.setPort(!("".equalsIgnoreCase(params.get("port-number")))?Integer.parseInt(params.get("port-number")):NetconfConnectionDetails.DEFAULT_PORT); - //connect the client and get configuration - client.connect(connectionDetails); - String configuration = client.getConfiguration(); - if(configuration !=null){ - // logger.info("*************************************Configuration Output*************************************"); - ctx.setAttribute("running-config", configuration); - - ctx.setAttribute("getRunningConfig_Result","Success"); - }else{ - ctx.setAttribute("getRunningConfig_Result","failure"); - } - } catch (Exception e) { - ctx.setAttribute("getRunningConfig_Result","failure"); - logger.error("Error " + e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw new APPCException(e); - } finally { - //disconnect the client - if(client != null) { - client.disconnect(); - } - } - } - - private String getCurrentDateTime() { - - DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - Date date = new Date(); - return dateFormat.format(date); - } - - private int getPort(String s) { - int port = 830; - if((s != null) && !s.isEmpty()) { - port = Integer.parseInt(s); - } - return port; - } - - void validateMandatoryParam(String paramName, String paramValue) { - if(StringUtils.isEmpty(paramValue)){ - throw new IllegalArgumentException("input "+paramName+" param is empty"); - } - } - - public NetconfConnectionDetails retrieveConnectionDetails( VnfType vnfType) throws APPCException{ - - NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); - if (!dao.retrieveNetconfConnectionDetails(vnfType.getFamilyType().name(), connectionDetails)) { - logger.error("Missing configuration for " + vnfType.getFamilyType().name()); - throw new APPCException("Missing configuration for " + vnfType.getFamilyType().name() + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME); - } - return connectionDetails; - } - - public String retrieveConfigurationFileContent(String configFileName){ - return dao.retrieveConfigFileName(configFileName); - } - -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImpl.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImpl.java deleted file mode 100644 index 459ece9c1..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/main/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImpl.java +++ /dev/null @@ -1,123 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.util.Map; - -import org.onap.appc.adapter.netconf.NetconfConnectionDetails; -import org.onap.appc.adapter.netconf.NetconfDataAccessService; -import org.onap.appc.adapter.netconf.exception.DataAccessException; -import org.onap.appc.adapter.netconf.util.Constants; -import org.onap.appc.dg.netconf.NetconfDBPlugin; -import org.onap.appc.exceptions.APPCException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; - -public class NetconfDBPluginImpl implements NetconfDBPlugin { - - private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger(); - private static ObjectMapper mapper = new ObjectMapper(); - - // populated by blueprint framework - private NetconfDataAccessService daoService; - - public void setDaoService(NetconfDataAccessService daoService) { - this.daoService = daoService; - this.daoService.setSchema(Constants.NETCONF_SCHEMA); - } - - public NetconfDBPluginImpl() { - } - - public void retrieveDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException { - - try { - String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME)); - ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent); - } catch(DataAccessException e) { - logger.error("Error " + e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw e; - } - - getConnection(params, ctx); - } - - @Override - public void retrieveVMDSConfiguration(Map params, SvcLogicContext ctx) throws APPCException { - logger.info("Setting entity value :" +params.get(Constants.RESOURCEKEY)); - ctx.setAttribute("entity", params.get(Constants.RESOURCEKEY)); - NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); - try { - if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.RESOURCEKEY), connectionDetails)) { - ctx.setAttribute("retrieveVMDSConfiguration_Result","failure"); - logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME)); - throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME); - } - ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails)); - ctx.setAttribute("retrieveVMDSConfiguration_Result","success"); - } catch(APPCException e) { - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw e; - } catch(DataAccessException | JsonProcessingException e) { - logger.error("Error " + e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw new APPCException(e); - } - } - - @Override - public void retrieveConfigFile(Map params, SvcLogicContext ctx) throws APPCException { - String fileContent = daoService.retrieveConfigFileName(params.get(Constants.CONFIGURATION_FILE_FIELD_NAME)); - ctx.setAttribute(Constants.FILE_CONTENT_FIELD_NAME, fileContent); - } - - public void retrieveConnectionDetails(Map params, SvcLogicContext ctx) throws APPCException { - getConnection(params, ctx); - } - - private void getConnection(Map params, SvcLogicContext ctx) throws APPCException { - NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails(); - try { - if (!daoService.retrieveNetconfConnectionDetails(params.get(Constants.VNF_TYPE_FIELD_NAME), connectionDetails)) { - logger.error("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME)); - throw new APPCException("Missing configuration for " + params.get(Constants.VNF_TYPE_FIELD_NAME) + " in " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME); - } - connectionDetails.setHost(params.get(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME)); - ctx.setAttribute(Constants.CONNECTION_DETAILS_FIELD_NAME, mapper.writeValueAsString(connectionDetails)); - } catch(APPCException e) { - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw e; - } catch(DataAccessException | JsonProcessingException e) { - logger.error("Error " + e.getMessage()); - ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, e.getMessage()); - throw new APPCException(e); - } - } -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/DAOServiceMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/DAOServiceMock.java new file mode 100644 index 000000000..379e164b5 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/DAOServiceMock.java @@ -0,0 +1,100 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import java.util.HashMap; + +import org.onap.appc.adapter.netconf.ConnectionDetails; +import org.onap.appc.adapter.netconf.NetconfConnectionDetails; +import org.onap.appc.adapter.netconf.NetconfDataAccessService; +import org.onap.appc.adapter.netconf.exception.DataAccessException; +import org.onap.ccsdk.sli.core.dblib.DbLibService; + +class DAOServiceMock implements NetconfDataAccessService { + + private String configFile; + private ConnectionDetails connection; + private HashMap backupConf; + + @Override + public void setSchema(String schema) { + } + + @Override + public void setDbLibService(DbLibService dbLibService) { + } + + void setConfigFile(String configFile) { + this.configFile = configFile; + } + + public HashMap getBackupConf() { + return backupConf; + } + + public void setConnection(ConnectionDetails connection) { + this.connection = connection; + } + + @Override + public String retrieveConfigFileName(String xmlID) throws DataAccessException { + if (!xmlID.equals("wrong")) { + return configFile; + } else { + throw new DataAccessException(); + } + } + + @Override + public boolean retrieveConnectionDetails(String vnfType, ConnectionDetails connectionDetails) throws + DataAccessException { + return false; + } + + @Override + public boolean retrieveNetconfConnectionDetails(String vnfType, NetconfConnectionDetails connectionDetails) throws + DataAccessException { + if (vnfType.equals("VNF")) { + connectionDetails.setHost(connection.getHost()); + connectionDetails.setPassword(connection.getPassword()); + connectionDetails.setPort(connection.getPort()); + connectionDetails.setUsername(connection.getUsername()); + + return true; + } else { + return false; + } + } + + @Override + public boolean logDeviceInteraction(String instanceId, String requestId, String creationDate, String logText) throws + DataAccessException { + this.backupConf = new HashMap<>(); + backupConf.put("creationDate", creationDate); + backupConf.put("logText", logText); + return true; + } + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java new file mode 100644 index 000000000..802fe15e0 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import org.onap.appc.adapter.netconf.OperationalStateValidator; +import org.onap.appc.adapter.netconf.VnfType; +import org.onap.appc.exceptions.APPCException; + + +public class MockOperationalStateValidatorImpl implements OperationalStateValidator { + + private boolean validated; + private String configurationFileName; + + public boolean isValidated() { + return validated; + } + + @Override + public VnfType getVnfType() { + return null; + } + + @Override + public String getConfigurationFileName() { + return configurationFileName; + } + + @Override + public void validateResponse(String response) throws APPCException { + if (response.equals("wrong")) { + throw new APPCException(); + } else { + this.validated = true; + } + + } + + public void setConfigurationFileName(String configurationFileName) { + this.configurationFileName = configurationFileName; + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientFactoryMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientFactoryMock.java new file mode 100644 index 000000000..f61d188d9 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientFactoryMock.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import org.onap.appc.adapter.netconf.NetconfClient; +import org.onap.appc.adapter.netconf.NetconfClientFactory; +import org.onap.appc.adapter.netconf.NetconfClientType; +import org.onap.appc.adapter.netconf.jsch.NetconfClientJsch; +import org.onap.appc.adapter.netconf.odlconnector.NetconfClientRestconfImpl; + + +public class NetconfClientFactoryMock extends NetconfClientFactory { + + private final NetconfClientJschMock jschClient = new NetconfClientJschMock(); + + @Override + public NetconfClient GetNetconfClient(NetconfClientType type){ + + return jschClient; + + } +} + + diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientJschMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientJschMock.java new file mode 100644 index 000000000..82361a95e --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientJschMock.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import org.onap.appc.adapter.netconf.NetconfClient; +import org.onap.appc.adapter.netconf.NetconfConnectionDetails; +import org.onap.appc.exceptions.APPCException; + + +public class NetconfClientJschMock implements NetconfClient { + + private boolean connection; + private String lastMessage; + private String answer = "answer"; + private String configuration; + private NetconfConnectionDetails lastConnectionDetails; + + public boolean isConnection() { + return connection; + } + + public String getLastMessage() { + return lastMessage; + } + + public String getAnswer() { + return answer; + } + + public String getConf() { + return configuration; + } + + public void setConf(String configuration) { + this.configuration = configuration; + } + + public void setAnswer(String answer) { + this.answer = answer; + } + + public NetconfConnectionDetails getLastConnectionDetails() { + return lastConnectionDetails; + } + + @Override + public void connect(NetconfConnectionDetails connectionDetails) throws APPCException { + this.connection = true; + this.lastConnectionDetails = connectionDetails; + + } + + @Override + public String exchangeMessage(String message) throws APPCException { + if (connection) { + this.lastMessage = message; + return answer; + } else return null; + } + + @Override + public void configure(String configuration) throws APPCException { + if (connection) { + this.configuration = configuration; + } + + } + + @Override + public String getConfiguration() throws APPCException { + if (connection) { + return configuration; + } else return null; + } + + @Override + public void disconnect() throws APPCException { + this.connection = false; + + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImplTest.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImplTest.java new file mode 100644 index 000000000..0becff701 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfClientPluginImplTest.java @@ -0,0 +1,681 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import org.onap.appc.exceptions.APPCException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.onap.appc.adapter.netconf.*; +import org.onap.appc.adapter.netconf.util.Constants; +import org.onap.appc.dg.netconf.impl.NetconfClientPluginImpl; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.lang.reflect.Field; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static org.powermock.api.mockito.PowerMockito.when; + + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({OperationalStateValidatorFactory.class, NetconfClientPluginImpl.class, FrameworkUtil.class, ObjectMapper.class}) + +public class NetconfClientPluginImplTest { + private NetconfClientPluginImpl netconfClientPlugin; + private NetconfDataAccessService dao; + private NetconfClientFactory clientFactory; + private Map params; + + private final BundleContext bundleContext = Mockito.mock(BundleContext.class); + private final Bundle bundleService = Mockito.mock(Bundle.class); + private final ServiceReference sref1 = Mockito.mock(ServiceReference.class); + private final ServiceReference sref2 = Mockito.mock(ServiceReference.class); + private final ServiceReference sref3 = Mockito.mock(ServiceReference.class); + private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message"; + + + String host = "http://www.test.com"; + String host1 = "http://www.test1.com"; + String vnfType = "VNF"; + int port = 8080; + String username = "test"; + String password = "test"; + String connectionDetails = "{\"host\":\"" + host + "\",\"port\":" + port + ",\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"capabilities\":null,\"additionalProperties\":null}"; + String fileContent = "\n" + + "\n" + + "\t\n" + + "\t\t\n" + + "\t\t\t\n" + + "\t\t \n" + + "\t\n" + + "'"; + String operationalState = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n"; + + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + clientFactory = new NetconfClientFactoryMock(); + + } + + + @Test + public void testConfigure() throws Exception { + + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + + params = new HashMap<>(); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent); + + netconfClientPlugin.configure(params, ctx); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + + try { + Assert.assertEquals("wrong configuration", fileContent, client.getConf()); + Assert.assertEquals("wrong host", host, client.getLastConnectionDetails().getHost()); + Assert.assertEquals("wrong port", port, client.getLastConnectionDetails().getPort()); + Assert.assertEquals("wrong username", username, client.getLastConnectionDetails().getUsername()); + Assert.assertEquals("wrong password", password, client.getLastConnectionDetails().getPassword()); + Assert.assertFalse(client.isConnection()); + } catch (Exception e) { + Assert.fail("failed with because of " + e.getCause()); + } + + + } + + + @Test + public void testConfigureNegativeIOException() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + + params = new HashMap<>(); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails); + params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent); + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + + + try { + netconfClientPlugin.configure(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertNull(client.getLastConnectionDetails()); + Assert.assertNull(client.getConf()); + } + + } + + @Test + public void testOperationStateValidation() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setAnswer(operationalState); + + + params = new HashMap<>(); + params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); + params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); + validatorMock.setConfigurationFileName("VnfGetRunningConfig"); + + PowerMockito.mockStatic(OperationalStateValidatorFactory.class); + when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); + + netconfClientPlugin.operationStateValidation(params, ctx); + + Assert.assertTrue("validation process failed", validatorMock.isValidated()); + Assert.assertEquals(fileContent, client.getLastMessage()); + } + + + @Test + public void testOperationStateValidationNegativeJsonProcessingNullIllegalStateException() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setAnswer(operationalState); + + params = new HashMap<>(); + params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); + params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); + validatorMock.setConfigurationFileName("VnfGetRunningConfig"); + + PowerMockito.mockStatic(OperationalStateValidatorFactory.class); + when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); + substituteMapper(true); + + try { + netconfClientPlugin.operationStateValidation(params, ctx); + substituteMapper(false); + } catch (APPCException e) { + substituteMapper(false); + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + Assert.assertFalse(validatorMock.isValidated()); + Assert.assertNull(client.getLastMessage()); + } + } + + @Test + public void testOperationStateValidationNegativeConnectionDetailsAreNullNullPointerException() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setAnswer(operationalState); + + + params = new HashMap<>(); + params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); + params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null); + MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); + validatorMock.setConfigurationFileName("VnfGetRunningConfig"); + + PowerMockito.mockStatic(OperationalStateValidatorFactory.class); + when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); + ObjectMapper mapper = PowerMockito.mock(ObjectMapper.class); + final NetconfConnectionDetails netconfConnectionDetails = null; + when(mapper.readValue(Matchers.anyString(), Matchers.any(Class.class))).thenReturn(netconfConnectionDetails); + + + try { + netconfClientPlugin.operationStateValidation(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + Assert.assertFalse("validation process failed", validatorMock.isValidated()); + + } + } + + + @Test + public void testOperationStateValidationNegativeAppcException() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setAnswer("wrong"); + + + params = new HashMap<>(); + params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); + params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); + validatorMock.setConfigurationFileName("VnfGetRunningConfig"); + + PowerMockito.mockStatic(OperationalStateValidatorFactory.class); + when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); + + + try { + netconfClientPlugin.operationStateValidation(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + Assert.assertFalse("validation process failed", validatorMock.isValidated()); + + } + } + + + @Test + public void testOperationStateValidatioConnectionDetailsInParamsAreEmpty() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setAnswer(operationalState); + ((DAOServiceMock) dao).setConnection(getConnectionDetails()); + + + params = new HashMap<>(); + params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); + params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, ""); + MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); + validatorMock.setConfigurationFileName("VnfGetRunningConfig"); + + PowerMockito.mockStatic(OperationalStateValidatorFactory.class); + when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); + + netconfClientPlugin.operationStateValidation(params, ctx); + + Assert.assertTrue("validation process failed", validatorMock.isValidated()); + Assert.assertEquals(fileContent, client.getLastMessage()); + } + + @Test + public void testOperationStateValidatioConnectionDetailsInParamsAreNull() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setAnswer(operationalState); + ((DAOServiceMock) dao).setConnection(getConnectionDetails()); + + + params = new HashMap<>(); + params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); + params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null); + MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); + validatorMock.setConfigurationFileName("VnfGetRunningConfig"); + + PowerMockito.mockStatic(OperationalStateValidatorFactory.class); + when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); + + netconfClientPlugin.operationStateValidation(params, ctx); + + Assert.assertTrue("validation process failed", validatorMock.isValidated()); + Assert.assertEquals(fileContent, client.getLastMessage()); + } + + + @Test + public void testBackupConfiguration() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setConf(fileContent); + netconfClientPlugin.backupConfiguration(params, ctx); + + DAOServiceMock mockdao = (DAOServiceMock) dao; + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); + Date date = new Date(); + String creationDateExpected = dateFormat.format(date); + String creationDateActual = mockdao.getBackupConf().get("creationDate").substring(0, 10); + + + Assert.assertEquals("wrong configuration in db", fileContent, mockdao.getBackupConf().get("logText")); + Assert.assertEquals(creationDateExpected, creationDateActual); + + + } + + + @Test + public void testBackupConfigurationNegativeDgErrorFieldName() throws Exception { + shortInit(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails); + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setConf(fileContent); + try { + netconfClientPlugin.backupConfiguration(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + + DAOServiceMock mockdao = (DAOServiceMock) dao; + Assert.assertNull(mockdao.getBackupConf()); + } + + } + + @Test + public void testGetConfig() throws Exception { + fullInit(); + String entity = "123"; + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("entity", entity); + + params = new HashMap<>(); + params.put("conf-id", "current"); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setConf(fileContent); + + + netconfClientPlugin.getConfig(params, ctx); + + Assert.assertEquals("Success", ctx.getAttribute("getConfig_Result")); + Assert.assertEquals(fileContent, ctx.getAttribute("fullConfig")); + Assert.assertNotNull(ctx.getAttribute(entity + ".Configuration")); + Assert.assertEquals(fileContent, ctx.getAttribute(entity + ".Configuration")); + } + + + @Test + public void testGetConfigNegativeConfigurationNull() throws Exception { + fullInit(); + String entity = "123"; + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("entity", entity); + + params = new HashMap<>(); + params.put("conf-id", "current"); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + + + netconfClientPlugin.getConfig(params, ctx); + + Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result")); + Assert.assertNull(ctx.getAttribute("fullConfig")); + Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); + Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); + } + + + @Test + public void testGetConfigNegativeNotSupportedConfId() throws Exception { + fullInit(); + String entity = "123"; + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("entity", entity); + + params = new HashMap<>(); + params.put("conf-id", "current1"); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); + + + netconfClientPlugin.getConfig(params, ctx); + + Assert.assertNull(ctx.getAttribute("getConfig_Result")); + Assert.assertNull(ctx.getAttribute("fullConfig")); + Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); + Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); + } + + @Test + public void testGetConfigNegativeWronjJsonConnectionDetailsException() throws Exception { + fullInit(); + String entity = "123"; + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("entity", entity); + + params = new HashMap<>(); + params.put("conf-id", "current"); + params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails); + + + try { + netconfClientPlugin.getConfig(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result")); + Assert.assertNull(ctx.getAttribute("fullConfig")); + Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); + Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + } + + + } + + @Test + public void testGetRunningConfig() throws Exception { + fullInit(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("host-ip-address", host); + params.put("user-name", username); + params.put("password", password); + params.put("port-number", String.valueOf(port)); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setConf(fileContent); + + netconfClientPlugin.getRunningConfig(params, ctx); + + Assert.assertEquals("Success", ctx.getAttribute("getRunningConfig_Result")); + Assert.assertEquals(fileContent, ctx.getAttribute("running-config")); + Assert.assertEquals("success", ctx.getStatus()); + } + + @Test + public void testGetRunningConfigWithoutPortNumberDgErrorFieldNameException() throws Exception { + fullInit(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("host-ip-address", host); + params.put("user-name", username); + params.put("password", password); + + NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); + client.setConf(fileContent); + + try { + netconfClientPlugin.getRunningConfig(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result")); + Assert.assertNull(ctx.getAttribute("running-config")); + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + } + + + } + + @Test + public void testGetRunningConfigNegativeConfigurationNull() throws Exception { + fullInit(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("host-ip-address", host); + params.put("user-name", username); + params.put("password", password); + params.put("port-number", String.valueOf(port)); + + netconfClientPlugin.getRunningConfig(params, ctx); + + Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result")); + Assert.assertNull(ctx.getAttribute("running-config")); + } + + @Test + public void testValidateMandatoryParamNegativeEmptyParamValue() throws Exception { + shortInit(); + String paramName = "test"; + String paramValue = ""; + + try { + netconfClientPlugin.validateMandatoryParam(paramName, paramValue); + Assert.assertTrue(false); + } catch (Exception e) { + Assert.assertTrue(true); + } + } + + @Test + public void testRetrieveConnectionDetails() throws Exception { + shortInit(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + ConnectionDetails connectionDetails1 = getConnectionDetails(); + daoServiceMock.setConnection(connectionDetails1); + + NetconfConnectionDetails connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.VNF); + + + Assert.assertEquals("wrong host", connectionDetails1.getHost(), connectionDetailsActual.getHost()); + Assert.assertEquals("wrong password", connectionDetails1.getPassword(), connectionDetailsActual.getPassword()); + Assert.assertEquals("wrong port", connectionDetails1.getPort(), connectionDetailsActual.getPort()); + Assert.assertEquals("wrong usename", connectionDetails1.getUsername(), connectionDetailsActual.getUsername()); + } + + + @Test + public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception { + shortInit(); + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + ConnectionDetails connectionDetails1 = getConnectionDetails(); + daoServiceMock.setConnection(connectionDetails1); + + NetconfConnectionDetails connectionDetailsActual = null; + try { + connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.MOCK); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertNull(connectionDetailsActual); + } + + + } + + @Test + public void testRetrieveConfigurationFileContent() throws Exception { + shortInit(); + + DAOServiceMock daoServiceMock = (DAOServiceMock) dao; + daoServiceMock.setConfigFile(fileContent); + + Assert.assertEquals("wrong config in a database", fileContent, netconfClientPlugin.retrieveConfigurationFileContent("VnfGetRunningConfig")); + } + + private ConnectionDetails getConnectionDetails() { + + ConnectionDetails connectionDetails = new ConnectionDetails(); + connectionDetails.setPassword(password); + connectionDetails.setPort(port); + connectionDetails.setUsername(username); + connectionDetails.setHost(host); + return connectionDetails; + } + + + private void initDao() throws NoSuchFieldException, IllegalAccessException { + dao = new DAOServiceMock(); + PowerMockito.mockStatic(FrameworkUtil.class); + when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService); + when(bundleService.getBundleContext()).thenReturn(bundleContext); + when(bundleContext.getServiceReference(NetconfDataAccessService.class)).thenReturn(sref1); + when(bundleContext.getService(sref1)).thenReturn(dao); + + + } + + private void fullInit() throws NoSuchFieldException, IllegalAccessException { + initClientFactory(); + initClientFactory2(); + initDao(); + netconfClientPlugin = new NetconfClientPluginImpl(); + netconfClientPlugin.setDao(this.dao); + } + + private void shortInit() throws NoSuchFieldException, IllegalAccessException { + initClientFactory(); + initDao(); + netconfClientPlugin = new NetconfClientPluginImpl(); + netconfClientPlugin.setDao(this.dao); + } + + private void initClientFactory() throws NoSuchFieldException, IllegalAccessException { + + PowerMockito.mockStatic(FrameworkUtil.class); + when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService); + when(bundleService.getBundleContext()).thenReturn(bundleContext); + when(bundleContext.getServiceReference(NetconfClientFactory.class)).thenReturn(sref2); + when(bundleContext.getService(sref2)).thenReturn(clientFactory); + + } + + private void initClientFactory2() { + PowerMockito.mockStatic(FrameworkUtil.class); + when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService); + when(bundleService.getBundleContext()).thenReturn(bundleContext); + when(bundleContext.getServiceReference(Matchers.anyString())).thenReturn(sref3); + when(bundleContext.getService(sref3)).thenReturn(clientFactory); + } + + private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException { + ObjectMapper mapper = new ObjectMapperMock(); + ObjectMapper mapper2 = new ObjectMapper(); + Field field = NetconfClientPluginImpl.class.getDeclaredField("mapper"); + field.setAccessible(true); + if (command) { + field.set(netconfClientPlugin, mapper); + } else { + field.set(netconfClientPlugin, mapper2); + } + } + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImplTest.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImplTest.java new file mode 100644 index 000000000..5137b904b --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/NetconfDBPluginImplTest.java @@ -0,0 +1,253 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.*; +import org.onap.appc.adapter.netconf.ConnectionDetails; +import org.onap.appc.adapter.netconf.NetconfConnectionDetails; +import org.onap.appc.adapter.netconf.NetconfDataAccessService; +import org.onap.appc.adapter.netconf.exception.DataAccessException; +import org.onap.appc.dg.netconf.impl.NetconfDBPluginImpl; +import org.onap.appc.exceptions.APPCException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +import static org.powermock.api.support.SuppressCode.suppressConstructor; + +public class NetconfDBPluginImplTest { + private NetconfDBPluginImpl netconfDBPlugin; + private NetconfDataAccessService daoService; + private DAOServiceMock daoMock; + private Map params; + private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message"; + String host = "http://www.test.com"; + String host1 = "http://www.test1.com"; + int port = 8080; + String username = "test"; + String password = "test"; + String configContent = "\n" + + "\n" + + "\t\n" + + "\t\t\n" + + "\t\t\t\n" + + "\t\t \n" + + "\t\n" + + "'"; + + + @Test + public void testRetrieveDSConfiguration() throws Exception { + init(); + params = new HashMap<>(); + params.put("org.onap.appc.vftype", "VNF"); + params.put("configuration-file-name", "VnfGetRunningConfig"); + SvcLogicContext ctx = new SvcLogicContext(); + netconfDBPlugin.retrieveDSConfiguration(params, ctx); + + Assert.assertEquals("lack of success of status", "success", ctx.getStatus()); + Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content")); + } + + + @Test + public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception { + init(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("configuration-file-name", "wrong"); + + try { + netconfDBPlugin.retrieveDSConfiguration(params, ctx); + } catch (DataAccessException e) { + //Assert.assertNotNull(ctx.getAttribute("org.onap.appc.dg.error")); + Assert.assertNull(ctx.getAttribute("file-content")); + } + + + } + + @Test + public void testRetrieveVMDSConfiguration() throws Exception { + init(); + params = new HashMap<>(); + params.put("resourceKey", "VNF"); + SvcLogicContext ctx = new SvcLogicContext(); + netconfDBPlugin.retrieveVMDSConfiguration(params, ctx); + + Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result", "success", ctx.getAttribute("retrieveVMDSConfiguration_Result")); + Assert.assertEquals("wrong entity", "VNF", ctx.getAttribute("entity")); + assertConnectionDetails(ctx, host); + } + + @Test + public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception { + init(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("resourceKey", "MOCK"); + + try { + netconfDBPlugin.retrieveVMDSConfiguration(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + + Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result")); + } + } + + + @Test + public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception { + + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("resourceKey", "VNF"); + + init(); + substituteMapper(true); + try { + netconfDBPlugin.retrieveVMDSConfiguration(params, ctx); + substituteMapper(false); + Assert.assertTrue(false); + + } catch (APPCException e) { + substituteMapper(false); + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + + } + + } + + @Test + public void testRetrieveConfigFile() throws Exception { + init(); + SvcLogicContext ctx = new SvcLogicContext(); + params = new HashMap<>(); + params.put("configuration-file-name", "VnfGetRunningConfig"); + netconfDBPlugin.retrieveConfigFile(params, ctx); + + Assert.assertEquals("lack of success of status", "success", ctx.getStatus()); + Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content")); + } + + @Test + public void testRetrieveConnectionDetails() throws Exception { + init(); + params = new HashMap<>(); + params.put("org.onap.appc.vftype", "VNF"); + params.put("vnf-host-ip-address", host1); + SvcLogicContext ctx = new SvcLogicContext(); + netconfDBPlugin.retrieveConnectionDetails(params, ctx); + + assertConnectionDetails(ctx, host1); + } + + @Test + public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception { + init(); + params = new HashMap<>(); + params.put("org.onap.appc.vftype", "MOCK"); + params.put("vnf-host-ip-address", host1); + SvcLogicContext ctx = new SvcLogicContext(); + + try { + netconfDBPlugin.retrieveConnectionDetails(params, ctx); + Assert.assertTrue(false); + } catch (APPCException e) { + Assert.assertNull(ctx.getAttribute("connection-details")); + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + } + + } + + + @Test + public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception { + init(); + params = new HashMap<>(); + params.put("org.onap.appc.vftype", "VNF"); + params.put("vnf-host-ip-address", host1); + SvcLogicContext ctx = new SvcLogicContext(); + substituteMapper(true); + + try { + netconfDBPlugin.retrieveConnectionDetails(params, ctx); + substituteMapper(false); + Assert.assertTrue(false); + } catch (APPCException e) { + substituteMapper(false); + Assert.assertNull(ctx.getAttribute("connection-details")); + Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); + } + + } + + private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException { + String sConnectionDetails = ctx.getAttribute("connection-details"); + NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class); + Assert.assertEquals(host, connectionDetails.getHost()); + Assert.assertEquals(port, connectionDetails.getPort()); + Assert.assertEquals(username, connectionDetails.getUsername()); + Assert.assertEquals(password, connectionDetails.getPassword()); + Assert.assertNull(connectionDetails.getCapabilities()); + Assert.assertNull(connectionDetails.getAdditionalProperties()); + } + + private void init() { + netconfDBPlugin = new NetconfDBPluginImpl(); + daoService = new DAOServiceMock(); + netconfDBPlugin.setDaoService(daoService); + daoMock = (DAOServiceMock) daoService; + daoMock.setConfigFile(configContent); + daoMock.setConnection(getConnectionDetails()); + + } + + private ConnectionDetails getConnectionDetails() { + ConnectionDetails connectionDetails = new ConnectionDetails(); + connectionDetails.setHost(host); + connectionDetails.setUsername(username); + connectionDetails.setPort(port); + connectionDetails.setPassword(password); + return connectionDetails; + } + + private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException { + ObjectMapper mapper = new ObjectMapperMock(); + ObjectMapper mapper2 = new ObjectMapper(); + Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper"); + field.setAccessible(true); + if (command) { + field.set(netconfDBPlugin, mapper); + } else { + field.set(netconfDBPlugin, mapper2); + } + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/ObjectMapperMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/ObjectMapperMock.java new file mode 100644 index 000000000..cacc15eae --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/ObjectMapperMock.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; + + +public class ObjectMapperMock extends ObjectMapper { + + @Override + public String writeValueAsString(Object var1) throws JsonProcessingException { + throw new JsonProcessingException("") { + }; + + } + + @Override + public T readValue(String var1, Class var2) throws IOException, JsonParseException, JsonMappingException { + return null; + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java new file mode 100644 index 000000000..2194fc14c --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/onap/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.dg.netconf.impl; + +import org.apache.commons.lang3.NotImplementedException; +import org.onap.appc.adapter.netconf.*; + + +public class OperationStateValidatorFactoryMock extends OperationalStateValidatorFactory { + public static OperationalStateValidator getOperationalStateValidator(String vnfType) { + VnfType vnfTypeEnum = null; + try { + vnfTypeEnum = VnfType.getVnfType(vnfType); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Illegal value in vnfType. vnfType=" + vnfType, e); + } + return getOperationalStateValidator(vnfTypeEnum); + } + + public static OperationalStateValidator getOperationalStateValidator(VnfType vnfType) { + + return new MockOperationalStateValidatorImpl(); + + + } + + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/DAOServiceMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/DAOServiceMock.java deleted file mode 100644 index 379e164b5..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/DAOServiceMock.java +++ /dev/null @@ -1,100 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import java.util.HashMap; - -import org.onap.appc.adapter.netconf.ConnectionDetails; -import org.onap.appc.adapter.netconf.NetconfConnectionDetails; -import org.onap.appc.adapter.netconf.NetconfDataAccessService; -import org.onap.appc.adapter.netconf.exception.DataAccessException; -import org.onap.ccsdk.sli.core.dblib.DbLibService; - -class DAOServiceMock implements NetconfDataAccessService { - - private String configFile; - private ConnectionDetails connection; - private HashMap backupConf; - - @Override - public void setSchema(String schema) { - } - - @Override - public void setDbLibService(DbLibService dbLibService) { - } - - void setConfigFile(String configFile) { - this.configFile = configFile; - } - - public HashMap getBackupConf() { - return backupConf; - } - - public void setConnection(ConnectionDetails connection) { - this.connection = connection; - } - - @Override - public String retrieveConfigFileName(String xmlID) throws DataAccessException { - if (!xmlID.equals("wrong")) { - return configFile; - } else { - throw new DataAccessException(); - } - } - - @Override - public boolean retrieveConnectionDetails(String vnfType, ConnectionDetails connectionDetails) throws - DataAccessException { - return false; - } - - @Override - public boolean retrieveNetconfConnectionDetails(String vnfType, NetconfConnectionDetails connectionDetails) throws - DataAccessException { - if (vnfType.equals("VNF")) { - connectionDetails.setHost(connection.getHost()); - connectionDetails.setPassword(connection.getPassword()); - connectionDetails.setPort(connection.getPort()); - connectionDetails.setUsername(connection.getUsername()); - - return true; - } else { - return false; - } - } - - @Override - public boolean logDeviceInteraction(String instanceId, String requestId, String creationDate, String logText) throws - DataAccessException { - this.backupConf = new HashMap<>(); - backupConf.put("creationDate", creationDate); - backupConf.put("logText", logText); - return true; - } - -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java deleted file mode 100644 index 802fe15e0..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/MockOperationalStateValidatorImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import org.onap.appc.adapter.netconf.OperationalStateValidator; -import org.onap.appc.adapter.netconf.VnfType; -import org.onap.appc.exceptions.APPCException; - - -public class MockOperationalStateValidatorImpl implements OperationalStateValidator { - - private boolean validated; - private String configurationFileName; - - public boolean isValidated() { - return validated; - } - - @Override - public VnfType getVnfType() { - return null; - } - - @Override - public String getConfigurationFileName() { - return configurationFileName; - } - - @Override - public void validateResponse(String response) throws APPCException { - if (response.equals("wrong")) { - throw new APPCException(); - } else { - this.validated = true; - } - - } - - public void setConfigurationFileName(String configurationFileName) { - this.configurationFileName = configurationFileName; - } -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientFactoryMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientFactoryMock.java deleted file mode 100644 index f61d188d9..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientFactoryMock.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import org.onap.appc.adapter.netconf.NetconfClient; -import org.onap.appc.adapter.netconf.NetconfClientFactory; -import org.onap.appc.adapter.netconf.NetconfClientType; -import org.onap.appc.adapter.netconf.jsch.NetconfClientJsch; -import org.onap.appc.adapter.netconf.odlconnector.NetconfClientRestconfImpl; - - -public class NetconfClientFactoryMock extends NetconfClientFactory { - - private final NetconfClientJschMock jschClient = new NetconfClientJschMock(); - - @Override - public NetconfClient GetNetconfClient(NetconfClientType type){ - - return jschClient; - - } -} - - diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientJschMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientJschMock.java deleted file mode 100644 index 82361a95e..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientJschMock.java +++ /dev/null @@ -1,103 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import org.onap.appc.adapter.netconf.NetconfClient; -import org.onap.appc.adapter.netconf.NetconfConnectionDetails; -import org.onap.appc.exceptions.APPCException; - - -public class NetconfClientJschMock implements NetconfClient { - - private boolean connection; - private String lastMessage; - private String answer = "answer"; - private String configuration; - private NetconfConnectionDetails lastConnectionDetails; - - public boolean isConnection() { - return connection; - } - - public String getLastMessage() { - return lastMessage; - } - - public String getAnswer() { - return answer; - } - - public String getConf() { - return configuration; - } - - public void setConf(String configuration) { - this.configuration = configuration; - } - - public void setAnswer(String answer) { - this.answer = answer; - } - - public NetconfConnectionDetails getLastConnectionDetails() { - return lastConnectionDetails; - } - - @Override - public void connect(NetconfConnectionDetails connectionDetails) throws APPCException { - this.connection = true; - this.lastConnectionDetails = connectionDetails; - - } - - @Override - public String exchangeMessage(String message) throws APPCException { - if (connection) { - this.lastMessage = message; - return answer; - } else return null; - } - - @Override - public void configure(String configuration) throws APPCException { - if (connection) { - this.configuration = configuration; - } - - } - - @Override - public String getConfiguration() throws APPCException { - if (connection) { - return configuration; - } else return null; - } - - @Override - public void disconnect() throws APPCException { - this.connection = false; - - } -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImplTest.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImplTest.java deleted file mode 100644 index 0becff701..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfClientPluginImplTest.java +++ /dev/null @@ -1,681 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import org.onap.appc.exceptions.APPCException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.onap.appc.adapter.netconf.*; -import org.onap.appc.adapter.netconf.util.Constants; -import org.onap.appc.dg.netconf.impl.NetconfClientPluginImpl; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.lang.reflect.Field; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import static org.powermock.api.mockito.PowerMockito.when; - - - -@RunWith(PowerMockRunner.class) -@PrepareForTest({OperationalStateValidatorFactory.class, NetconfClientPluginImpl.class, FrameworkUtil.class, ObjectMapper.class}) - -public class NetconfClientPluginImplTest { - private NetconfClientPluginImpl netconfClientPlugin; - private NetconfDataAccessService dao; - private NetconfClientFactory clientFactory; - private Map params; - - private final BundleContext bundleContext = Mockito.mock(BundleContext.class); - private final Bundle bundleService = Mockito.mock(Bundle.class); - private final ServiceReference sref1 = Mockito.mock(ServiceReference.class); - private final ServiceReference sref2 = Mockito.mock(ServiceReference.class); - private final ServiceReference sref3 = Mockito.mock(ServiceReference.class); - private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message"; - - - String host = "http://www.test.com"; - String host1 = "http://www.test1.com"; - String vnfType = "VNF"; - int port = 8080; - String username = "test"; - String password = "test"; - String connectionDetails = "{\"host\":\"" + host + "\",\"port\":" + port + ",\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"capabilities\":null,\"additionalProperties\":null}"; - String fileContent = "\n" + - "\n" + - "\t\n" + - "\t\t\n" + - "\t\t\t\n" + - "\t\t \n" + - "\t\n" + - "'"; - String operationalState = "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "\n"; - - - @Before - public void setUp() throws NoSuchFieldException, IllegalAccessException { - clientFactory = new NetconfClientFactoryMock(); - - } - - - @Test - public void testConfigure() throws Exception { - - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - - params = new HashMap<>(); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent); - - netconfClientPlugin.configure(params, ctx); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - - try { - Assert.assertEquals("wrong configuration", fileContent, client.getConf()); - Assert.assertEquals("wrong host", host, client.getLastConnectionDetails().getHost()); - Assert.assertEquals("wrong port", port, client.getLastConnectionDetails().getPort()); - Assert.assertEquals("wrong username", username, client.getLastConnectionDetails().getUsername()); - Assert.assertEquals("wrong password", password, client.getLastConnectionDetails().getPassword()); - Assert.assertFalse(client.isConnection()); - } catch (Exception e) { - Assert.fail("failed with because of " + e.getCause()); - } - - - } - - - @Test - public void testConfigureNegativeIOException() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - - params = new HashMap<>(); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails); - params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent); - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - - - try { - netconfClientPlugin.configure(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertNull(client.getLastConnectionDetails()); - Assert.assertNull(client.getConf()); - } - - } - - @Test - public void testOperationStateValidation() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setAnswer(operationalState); - - - params = new HashMap<>(); - params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); - params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); - validatorMock.setConfigurationFileName("VnfGetRunningConfig"); - - PowerMockito.mockStatic(OperationalStateValidatorFactory.class); - when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); - - netconfClientPlugin.operationStateValidation(params, ctx); - - Assert.assertTrue("validation process failed", validatorMock.isValidated()); - Assert.assertEquals(fileContent, client.getLastMessage()); - } - - - @Test - public void testOperationStateValidationNegativeJsonProcessingNullIllegalStateException() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setAnswer(operationalState); - - params = new HashMap<>(); - params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); - params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); - validatorMock.setConfigurationFileName("VnfGetRunningConfig"); - - PowerMockito.mockStatic(OperationalStateValidatorFactory.class); - when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); - substituteMapper(true); - - try { - netconfClientPlugin.operationStateValidation(params, ctx); - substituteMapper(false); - } catch (APPCException e) { - substituteMapper(false); - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - Assert.assertFalse(validatorMock.isValidated()); - Assert.assertNull(client.getLastMessage()); - } - } - - @Test - public void testOperationStateValidationNegativeConnectionDetailsAreNullNullPointerException() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setAnswer(operationalState); - - - params = new HashMap<>(); - params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); - params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null); - MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); - validatorMock.setConfigurationFileName("VnfGetRunningConfig"); - - PowerMockito.mockStatic(OperationalStateValidatorFactory.class); - when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); - ObjectMapper mapper = PowerMockito.mock(ObjectMapper.class); - final NetconfConnectionDetails netconfConnectionDetails = null; - when(mapper.readValue(Matchers.anyString(), Matchers.any(Class.class))).thenReturn(netconfConnectionDetails); - - - try { - netconfClientPlugin.operationStateValidation(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - Assert.assertFalse("validation process failed", validatorMock.isValidated()); - - } - } - - - @Test - public void testOperationStateValidationNegativeAppcException() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setAnswer("wrong"); - - - params = new HashMap<>(); - params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); - params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); - validatorMock.setConfigurationFileName("VnfGetRunningConfig"); - - PowerMockito.mockStatic(OperationalStateValidatorFactory.class); - when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); - - - try { - netconfClientPlugin.operationStateValidation(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - Assert.assertFalse("validation process failed", validatorMock.isValidated()); - - } - } - - - @Test - public void testOperationStateValidatioConnectionDetailsInParamsAreEmpty() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setAnswer(operationalState); - ((DAOServiceMock) dao).setConnection(getConnectionDetails()); - - - params = new HashMap<>(); - params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); - params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, ""); - MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); - validatorMock.setConfigurationFileName("VnfGetRunningConfig"); - - PowerMockito.mockStatic(OperationalStateValidatorFactory.class); - when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); - - netconfClientPlugin.operationStateValidation(params, ctx); - - Assert.assertTrue("validation process failed", validatorMock.isValidated()); - Assert.assertEquals(fileContent, client.getLastMessage()); - } - - @Test - public void testOperationStateValidatioConnectionDetailsInParamsAreNull() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setAnswer(operationalState); - ((DAOServiceMock) dao).setConnection(getConnectionDetails()); - - - params = new HashMap<>(); - params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType); - params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null); - MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl(); - validatorMock.setConfigurationFileName("VnfGetRunningConfig"); - - PowerMockito.mockStatic(OperationalStateValidatorFactory.class); - when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock); - - netconfClientPlugin.operationStateValidation(params, ctx); - - Assert.assertTrue("validation process failed", validatorMock.isValidated()); - Assert.assertEquals(fileContent, client.getLastMessage()); - } - - - @Test - public void testBackupConfiguration() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setConf(fileContent); - netconfClientPlugin.backupConfiguration(params, ctx); - - DAOServiceMock mockdao = (DAOServiceMock) dao; - DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); - Date date = new Date(); - String creationDateExpected = dateFormat.format(date); - String creationDateActual = mockdao.getBackupConf().get("creationDate").substring(0, 10); - - - Assert.assertEquals("wrong configuration in db", fileContent, mockdao.getBackupConf().get("logText")); - Assert.assertEquals(creationDateExpected, creationDateActual); - - - } - - - @Test - public void testBackupConfigurationNegativeDgErrorFieldName() throws Exception { - shortInit(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails); - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setConf(fileContent); - try { - netconfClientPlugin.backupConfiguration(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - - DAOServiceMock mockdao = (DAOServiceMock) dao; - Assert.assertNull(mockdao.getBackupConf()); - } - - } - - @Test - public void testGetConfig() throws Exception { - fullInit(); - String entity = "123"; - - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("entity", entity); - - params = new HashMap<>(); - params.put("conf-id", "current"); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setConf(fileContent); - - - netconfClientPlugin.getConfig(params, ctx); - - Assert.assertEquals("Success", ctx.getAttribute("getConfig_Result")); - Assert.assertEquals(fileContent, ctx.getAttribute("fullConfig")); - Assert.assertNotNull(ctx.getAttribute(entity + ".Configuration")); - Assert.assertEquals(fileContent, ctx.getAttribute(entity + ".Configuration")); - } - - - @Test - public void testGetConfigNegativeConfigurationNull() throws Exception { - fullInit(); - String entity = "123"; - - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("entity", entity); - - params = new HashMap<>(); - params.put("conf-id", "current"); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - - - netconfClientPlugin.getConfig(params, ctx); - - Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result")); - Assert.assertNull(ctx.getAttribute("fullConfig")); - Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); - Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); - } - - - @Test - public void testGetConfigNegativeNotSupportedConfId() throws Exception { - fullInit(); - String entity = "123"; - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("entity", entity); - - params = new HashMap<>(); - params.put("conf-id", "current1"); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails); - - - netconfClientPlugin.getConfig(params, ctx); - - Assert.assertNull(ctx.getAttribute("getConfig_Result")); - Assert.assertNull(ctx.getAttribute("fullConfig")); - Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); - Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); - } - - @Test - public void testGetConfigNegativeWronjJsonConnectionDetailsException() throws Exception { - fullInit(); - String entity = "123"; - - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("entity", entity); - - params = new HashMap<>(); - params.put("conf-id", "current"); - params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails); - - - try { - netconfClientPlugin.getConfig(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result")); - Assert.assertNull(ctx.getAttribute("fullConfig")); - Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); - Assert.assertNull(ctx.getAttribute(entity + ".Configuration")); - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - } - - - } - - @Test - public void testGetRunningConfig() throws Exception { - fullInit(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("host-ip-address", host); - params.put("user-name", username); - params.put("password", password); - params.put("port-number", String.valueOf(port)); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setConf(fileContent); - - netconfClientPlugin.getRunningConfig(params, ctx); - - Assert.assertEquals("Success", ctx.getAttribute("getRunningConfig_Result")); - Assert.assertEquals(fileContent, ctx.getAttribute("running-config")); - Assert.assertEquals("success", ctx.getStatus()); - } - - @Test - public void testGetRunningConfigWithoutPortNumberDgErrorFieldNameException() throws Exception { - fullInit(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("host-ip-address", host); - params.put("user-name", username); - params.put("password", password); - - NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.GetNetconfClient(NetconfClientType.SSH); - client.setConf(fileContent); - - try { - netconfClientPlugin.getRunningConfig(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result")); - Assert.assertNull(ctx.getAttribute("running-config")); - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - } - - - } - - @Test - public void testGetRunningConfigNegativeConfigurationNull() throws Exception { - fullInit(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("host-ip-address", host); - params.put("user-name", username); - params.put("password", password); - params.put("port-number", String.valueOf(port)); - - netconfClientPlugin.getRunningConfig(params, ctx); - - Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result")); - Assert.assertNull(ctx.getAttribute("running-config")); - } - - @Test - public void testValidateMandatoryParamNegativeEmptyParamValue() throws Exception { - shortInit(); - String paramName = "test"; - String paramValue = ""; - - try { - netconfClientPlugin.validateMandatoryParam(paramName, paramValue); - Assert.assertTrue(false); - } catch (Exception e) { - Assert.assertTrue(true); - } - } - - @Test - public void testRetrieveConnectionDetails() throws Exception { - shortInit(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - ConnectionDetails connectionDetails1 = getConnectionDetails(); - daoServiceMock.setConnection(connectionDetails1); - - NetconfConnectionDetails connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.VNF); - - - Assert.assertEquals("wrong host", connectionDetails1.getHost(), connectionDetailsActual.getHost()); - Assert.assertEquals("wrong password", connectionDetails1.getPassword(), connectionDetailsActual.getPassword()); - Assert.assertEquals("wrong port", connectionDetails1.getPort(), connectionDetailsActual.getPort()); - Assert.assertEquals("wrong usename", connectionDetails1.getUsername(), connectionDetailsActual.getUsername()); - } - - - @Test - public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception { - shortInit(); - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - ConnectionDetails connectionDetails1 = getConnectionDetails(); - daoServiceMock.setConnection(connectionDetails1); - - NetconfConnectionDetails connectionDetailsActual = null; - try { - connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.MOCK); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertNull(connectionDetailsActual); - } - - - } - - @Test - public void testRetrieveConfigurationFileContent() throws Exception { - shortInit(); - - DAOServiceMock daoServiceMock = (DAOServiceMock) dao; - daoServiceMock.setConfigFile(fileContent); - - Assert.assertEquals("wrong config in a database", fileContent, netconfClientPlugin.retrieveConfigurationFileContent("VnfGetRunningConfig")); - } - - private ConnectionDetails getConnectionDetails() { - - ConnectionDetails connectionDetails = new ConnectionDetails(); - connectionDetails.setPassword(password); - connectionDetails.setPort(port); - connectionDetails.setUsername(username); - connectionDetails.setHost(host); - return connectionDetails; - } - - - private void initDao() throws NoSuchFieldException, IllegalAccessException { - dao = new DAOServiceMock(); - PowerMockito.mockStatic(FrameworkUtil.class); - when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService); - when(bundleService.getBundleContext()).thenReturn(bundleContext); - when(bundleContext.getServiceReference(NetconfDataAccessService.class)).thenReturn(sref1); - when(bundleContext.getService(sref1)).thenReturn(dao); - - - } - - private void fullInit() throws NoSuchFieldException, IllegalAccessException { - initClientFactory(); - initClientFactory2(); - initDao(); - netconfClientPlugin = new NetconfClientPluginImpl(); - netconfClientPlugin.setDao(this.dao); - } - - private void shortInit() throws NoSuchFieldException, IllegalAccessException { - initClientFactory(); - initDao(); - netconfClientPlugin = new NetconfClientPluginImpl(); - netconfClientPlugin.setDao(this.dao); - } - - private void initClientFactory() throws NoSuchFieldException, IllegalAccessException { - - PowerMockito.mockStatic(FrameworkUtil.class); - when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService); - when(bundleService.getBundleContext()).thenReturn(bundleContext); - when(bundleContext.getServiceReference(NetconfClientFactory.class)).thenReturn(sref2); - when(bundleContext.getService(sref2)).thenReturn(clientFactory); - - } - - private void initClientFactory2() { - PowerMockito.mockStatic(FrameworkUtil.class); - when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService); - when(bundleService.getBundleContext()).thenReturn(bundleContext); - when(bundleContext.getServiceReference(Matchers.anyString())).thenReturn(sref3); - when(bundleContext.getService(sref3)).thenReturn(clientFactory); - } - - private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapperMock(); - ObjectMapper mapper2 = new ObjectMapper(); - Field field = NetconfClientPluginImpl.class.getDeclaredField("mapper"); - field.setAccessible(true); - if (command) { - field.set(netconfClientPlugin, mapper); - } else { - field.set(netconfClientPlugin, mapper2); - } - } - -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImplTest.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImplTest.java deleted file mode 100644 index 5137b904b..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImplTest.java +++ /dev/null @@ -1,253 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.*; -import org.onap.appc.adapter.netconf.ConnectionDetails; -import org.onap.appc.adapter.netconf.NetconfConnectionDetails; -import org.onap.appc.adapter.netconf.NetconfDataAccessService; -import org.onap.appc.adapter.netconf.exception.DataAccessException; -import org.onap.appc.dg.netconf.impl.NetconfDBPluginImpl; -import org.onap.appc.exceptions.APPCException; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; - -import static org.powermock.api.support.SuppressCode.suppressConstructor; - -public class NetconfDBPluginImplTest { - private NetconfDBPluginImpl netconfDBPlugin; - private NetconfDataAccessService daoService; - private DAOServiceMock daoMock; - private Map params; - private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message"; - String host = "http://www.test.com"; - String host1 = "http://www.test1.com"; - int port = 8080; - String username = "test"; - String password = "test"; - String configContent = "\n" + - "\n" + - "\t\n" + - "\t\t\n" + - "\t\t\t\n" + - "\t\t \n" + - "\t\n" + - "'"; - - - @Test - public void testRetrieveDSConfiguration() throws Exception { - init(); - params = new HashMap<>(); - params.put("org.onap.appc.vftype", "VNF"); - params.put("configuration-file-name", "VnfGetRunningConfig"); - SvcLogicContext ctx = new SvcLogicContext(); - netconfDBPlugin.retrieveDSConfiguration(params, ctx); - - Assert.assertEquals("lack of success of status", "success", ctx.getStatus()); - Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content")); - } - - - @Test - public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception { - init(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("configuration-file-name", "wrong"); - - try { - netconfDBPlugin.retrieveDSConfiguration(params, ctx); - } catch (DataAccessException e) { - //Assert.assertNotNull(ctx.getAttribute("org.onap.appc.dg.error")); - Assert.assertNull(ctx.getAttribute("file-content")); - } - - - } - - @Test - public void testRetrieveVMDSConfiguration() throws Exception { - init(); - params = new HashMap<>(); - params.put("resourceKey", "VNF"); - SvcLogicContext ctx = new SvcLogicContext(); - netconfDBPlugin.retrieveVMDSConfiguration(params, ctx); - - Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result", "success", ctx.getAttribute("retrieveVMDSConfiguration_Result")); - Assert.assertEquals("wrong entity", "VNF", ctx.getAttribute("entity")); - assertConnectionDetails(ctx, host); - } - - @Test - public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception { - init(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("resourceKey", "MOCK"); - - try { - netconfDBPlugin.retrieveVMDSConfiguration(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - - Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result")); - } - } - - - @Test - public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception { - - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("resourceKey", "VNF"); - - init(); - substituteMapper(true); - try { - netconfDBPlugin.retrieveVMDSConfiguration(params, ctx); - substituteMapper(false); - Assert.assertTrue(false); - - } catch (APPCException e) { - substituteMapper(false); - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - - } - - } - - @Test - public void testRetrieveConfigFile() throws Exception { - init(); - SvcLogicContext ctx = new SvcLogicContext(); - params = new HashMap<>(); - params.put("configuration-file-name", "VnfGetRunningConfig"); - netconfDBPlugin.retrieveConfigFile(params, ctx); - - Assert.assertEquals("lack of success of status", "success", ctx.getStatus()); - Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content")); - } - - @Test - public void testRetrieveConnectionDetails() throws Exception { - init(); - params = new HashMap<>(); - params.put("org.onap.appc.vftype", "VNF"); - params.put("vnf-host-ip-address", host1); - SvcLogicContext ctx = new SvcLogicContext(); - netconfDBPlugin.retrieveConnectionDetails(params, ctx); - - assertConnectionDetails(ctx, host1); - } - - @Test - public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception { - init(); - params = new HashMap<>(); - params.put("org.onap.appc.vftype", "MOCK"); - params.put("vnf-host-ip-address", host1); - SvcLogicContext ctx = new SvcLogicContext(); - - try { - netconfDBPlugin.retrieveConnectionDetails(params, ctx); - Assert.assertTrue(false); - } catch (APPCException e) { - Assert.assertNull(ctx.getAttribute("connection-details")); - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - } - - } - - - @Test - public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception { - init(); - params = new HashMap<>(); - params.put("org.onap.appc.vftype", "VNF"); - params.put("vnf-host-ip-address", host1); - SvcLogicContext ctx = new SvcLogicContext(); - substituteMapper(true); - - try { - netconfDBPlugin.retrieveConnectionDetails(params, ctx); - substituteMapper(false); - Assert.assertTrue(false); - } catch (APPCException e) { - substituteMapper(false); - Assert.assertNull(ctx.getAttribute("connection-details")); - Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE)); - } - - } - - private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException { - String sConnectionDetails = ctx.getAttribute("connection-details"); - NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class); - Assert.assertEquals(host, connectionDetails.getHost()); - Assert.assertEquals(port, connectionDetails.getPort()); - Assert.assertEquals(username, connectionDetails.getUsername()); - Assert.assertEquals(password, connectionDetails.getPassword()); - Assert.assertNull(connectionDetails.getCapabilities()); - Assert.assertNull(connectionDetails.getAdditionalProperties()); - } - - private void init() { - netconfDBPlugin = new NetconfDBPluginImpl(); - daoService = new DAOServiceMock(); - netconfDBPlugin.setDaoService(daoService); - daoMock = (DAOServiceMock) daoService; - daoMock.setConfigFile(configContent); - daoMock.setConnection(getConnectionDetails()); - - } - - private ConnectionDetails getConnectionDetails() { - ConnectionDetails connectionDetails = new ConnectionDetails(); - connectionDetails.setHost(host); - connectionDetails.setUsername(username); - connectionDetails.setPort(port); - connectionDetails.setPassword(password); - return connectionDetails; - } - - private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapperMock(); - ObjectMapper mapper2 = new ObjectMapper(); - Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper"); - field.setAccessible(true); - if (command) { - field.set(netconfDBPlugin, mapper); - } else { - field.set(netconfDBPlugin, mapper2); - } - } -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/ObjectMapperMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/ObjectMapperMock.java deleted file mode 100644 index cacc15eae..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/ObjectMapperMock.java +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.IOException; - - -public class ObjectMapperMock extends ObjectMapper { - - @Override - public String writeValueAsString(Object var1) throws JsonProcessingException { - throw new JsonProcessingException("") { - }; - - } - - @Override - public T readValue(String var1, Class var2) throws IOException, JsonParseException, JsonMappingException { - return null; - } -} diff --git a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java b/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java deleted file mode 100644 index 2194fc14c..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/OperationStateValidatorFactoryMock.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.dg.netconf.impl; - -import org.apache.commons.lang3.NotImplementedException; -import org.onap.appc.adapter.netconf.*; - - -public class OperationStateValidatorFactoryMock extends OperationalStateValidatorFactory { - public static OperationalStateValidator getOperationalStateValidator(String vnfType) { - VnfType vnfTypeEnum = null; - try { - vnfTypeEnum = VnfType.getVnfType(vnfType); - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("Illegal value in vnfType. vnfType=" + vnfType, e); - } - return getOperationalStateValidator(vnfTypeEnum); - } - - public static OperationalStateValidator getOperationalStateValidator(VnfType vnfType) { - - return new MockOperationalStateValidatorImpl(); - - - } - - -} -- cgit 1.2.3-korg