From 6e164bc0f1f6688714033e95731942a89f5f1868 Mon Sep 17 00:00:00 2001 From: Ravi Pendurty Date: Tue, 18 Aug 2020 15:30:11 +0200 Subject: Develop a VES Provider Common VES provider will be used by devicemanager bundles and other bundles for sending VES messages Issue-ID: SDNC-1188 Signed-off-by: Ravi Pendurty Change-Id: Ied23b82a528aac23d7bebab272a2f414e67d0866 Signed-off-by: Ravi Pendurty --- .../wt/devicemanager/impl/DeviceManagerImpl.java | 17 ++- .../impl/VESCollectorClient.java | 110 +++++++++++++++++++ .../impl/config/VESCollectorCfgImpl.java | 122 +++++++++++++++++++++ .../devicemanager/test/TestVESCollectorClient.java | 91 +++++++++++++++ 4 files changed, 337 insertions(+), 3 deletions(-) create mode 100644 sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/VESCollectorClient.java create mode 100644 sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/config/VESCollectorCfgImpl.java create mode 100644 sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestVESCollectorClient.java (limited to 'sdnr/wt/devicemanager/provider') diff --git a/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java b/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java index 4d731a5c9..f3112fbe3 100644 --- a/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java +++ b/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/impl/DeviceManagerImpl.java @@ -73,7 +73,9 @@ import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.MaintenanceService; import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NetconfNetworkElementService; import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService; import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.PerformanceManager; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService; import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.DevicemanagerNotificationDelayService; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.VESCollectorClient; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.MountPointService; @@ -123,6 +125,7 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa private ConnectionStatusHousekeepingService housekeepingService; private NetconfNodeStateService netconfNodeStateService; private DataProvider dataProvider; + private VESCollectorClient vesCollectorClient; // Handler private DeviceManagerNetconfConnectHandler deviceManagerNetconfConnectHandler; @@ -205,6 +208,8 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa this.dcaeProviderClient = new DcaeProviderClient(config, esConfig.getCluster(), this); this.aaiProviderClient = new AaiProviderClient(config, this); + + this.vesCollectorClient = new VESCollectorClient(config); // EM String myDbKeyNameExtended = MYDBKEYNAMEBASE + "-" + esConfig.getCluster(); @@ -268,6 +273,7 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa close(archiveCleanService); close(housekeepingService); close(deviceManagerNetconfConnectHandler); + close(vesCollectorClient); LOG.info("DeviceManagerImpl closing done"); } @@ -350,7 +356,7 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa /** * Used to close all Services, that should support AutoCloseable Pattern - * + * * @param toClose */ private void close(AutoCloseable... toCloseList) { @@ -384,7 +390,7 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa /** * Indication if init() of devicemanager successfully done. - * + * * @return true if init() was sucessfull. False if not done or not successfull. */ public boolean isDevicemanagerInitializationOk() { @@ -393,7 +399,7 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa /** * Get NE object. Used by DCAE Service - * + * * @param mountpoint mount point name * @return null or NE specific data */ @@ -408,5 +414,10 @@ public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceMa this.odlEventListenerHandler.writeEventLog(objectId, msg, value); } + @Override + public @NonNull VESCollectorService getVESCollectorService() { + return this.vesCollectorClient; + } + } diff --git a/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/VESCollectorClient.java b/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/VESCollectorClient.java new file mode 100644 index 000000000..3b1f71215 --- /dev/null +++ b/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/VESCollectorClient.java @@ -0,0 +1,110 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl; + +import java.io.IOException; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener; +import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient; +import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class VESCollectorClient implements VESCollectorService, IConfigChangedListener, AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(VESCollectorClient.class); + private final VESCollectorCfgImpl vesConfig; + private final ConfigurationFileRepresentation cfg; + private BaseHTTPClient httpClient; + private final Map headerMap; + + public VESCollectorClient(ConfigurationFileRepresentation config) { + this.vesConfig = new VESCollectorCfgImpl(config); + this.cfg = config; + this.cfg.registerConfigChangedListener(this); + + httpClient = new BaseHTTPClient(getBaseUrl()); + + this.headerMap = new HashMap<>(); + this.headerMap.put("Content-Type", "application/json"); + this.headerMap.put("Accept", "application/json"); + + setAuthorization(getConfig().getUsername(), getConfig().getPassword()); + } + + @Override + public VESCollectorCfgImpl getConfig() { + return this.vesConfig; + } + + public String getBaseUrl() { + LOG.debug("IP Address is - {}", getConfig().getIP()); + if (!getConfig().getTLSEnabled()) { + return "http://" + getConfig().getIP() + ":" + getConfig().getPort(); + } else { + return "https://" + getConfig().getIP() + ":" + getConfig().getPort(); + } + } + + private void setAuthorization(String username, String password) { + if (getConfig().getTLSEnabled()) { + String credentials = username + ":" + password; + this.headerMap.put("Authorization", + "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes()))); + } + + } + + + + @Override + public boolean publishVESMessage(String message) { + LOG.info("In VESClient - {} ", message); + BaseHTTPResponse response; + try { + String uri = "eventListener" + "/" + getConfig().getVersion(); + response = httpClient.sendRequest(uri, "POST", message, headerMap); + LOG.debug("finished with responsecode {}", response.code); + return response.code == 200; + } catch (IOException e) { + LOG.warn("problem publishing VES message {} ", e.getMessage()); + return false; + } + + } + + @Override + public void close() throws Exception { + this.cfg.unregisterConfigChangedListener(this); + } + + @Override + public void onConfigChanged() { + httpClient.setBaseUrl(getBaseUrl()); + setAuthorization(getConfig().getUsername(), getConfig().getPassword()); + } + +} diff --git a/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/config/VESCollectorCfgImpl.java b/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/config/VESCollectorCfgImpl.java new file mode 100644 index 000000000..1924a6759 --- /dev/null +++ b/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/vescollectorconnector/impl/config/VESCollectorCfgImpl.java @@ -0,0 +1,122 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config; + +import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService; + +public class VESCollectorCfgImpl implements VESCollectorCfgService, Configuration { + + private static final String SECTION_MARKER = "VESCollector"; + + private static final String PROPERTY_KEY_VES_COLLECTOR_IP = "VES_COLLECTOR_IP"; + private static final String DEFAULT_VALUE_VES_COLLECTOR_IP = "127.0.0.1"; + + private static final String PROPERTY_KEY_VES_COLLECTOR_PORT = "VES_COLLECTOR_PORT"; + private static final String DEFAULT_VALUE_VES_COLLECTOR_PORT = "8080"; + + private static final String PROPERTY_KEY_VES_COLLECTOR_TLS_ENABLED = "VES_COLLECTOR_TLS_ENABLED"; + + private static final String PROPERTY_KEY_VES_COLLECTOR_USERNAME = "VES_COLLECTOR_USERNAME"; + private static final String DEFAULT_VALUE_VES_COLLECTOR_USERNAME = "sample1"; + + private static final String PROPERTY_KEY_VES_COLLECTOR_PASSWORD = "VES_COLLECTOR_PASSWORD"; + private static final String DEFAULT_VALUE_VES_COLLECTOR_PASSWORD = "sample1"; + + private static final String PROPERTY_KEY_VES_COLLECTOR_VERSION = "VES_COLLECTOR_VERSION"; + private static final String DEFAULT_VALUE_VES_COLLECTOR_VERSION = "v7"; + + private static final String PROPERTY_KEY_REPORTING_ENTITY_NAME = "REPORTING_ENTITY_NAME"; + private static final String DEFAULT_VALUE_REPORTING_ENTITY_NAME = "ONAP SDN-R"; + + private static final String PROPERTY_KEY_EVENTLOG_DETAIL = "EVENTLOG_MSG_DETAIL"; + private static final String DEFAULT_VALUE_EVENTLOG_DETAIL = "SHORT"; // "SHORT", "MEDIUM", "LONG" + + + private static ConfigurationFileRepresentation configuration; + + public VESCollectorCfgImpl(ConfigurationFileRepresentation configuration) { + VESCollectorCfgImpl.configuration = configuration; + VESCollectorCfgImpl.configuration.addSection(SECTION_MARKER); + defaults(); + } + + @Override + public String getSectionName() { + return SECTION_MARKER; + } + + @Override + public String getReportingEntityName() { + return configuration != null ? configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_REPORTING_ENTITY_NAME) : "ONAP SDN-R"; + } + + @Override + public String getEventLogMsgDetail() { + return configuration != null ?configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_EVENTLOG_DETAIL) : DEFAULT_VALUE_EVENTLOG_DETAIL; + } + + public boolean getTLSEnabled() { + return configuration.getPropertyBoolean(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_TLS_ENABLED); + } + + public String getUsername() { + return configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_USERNAME); + } + + public String getPassword() { + return configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_PASSWORD); + } + + public String getIP() { + return configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_IP); + } + + public String getPort() { + return configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_PORT); + } + + public String getVersion() { + return configuration.getProperty(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_VERSION); + } + + @Override + public synchronized void defaults() { + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_TLS_ENABLED, Boolean.FALSE); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_USERNAME, + DEFAULT_VALUE_VES_COLLECTOR_USERNAME); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_PASSWORD, + DEFAULT_VALUE_VES_COLLECTOR_PASSWORD); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_IP, + DEFAULT_VALUE_VES_COLLECTOR_IP); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_PORT, + DEFAULT_VALUE_VES_COLLECTOR_PORT); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_VES_COLLECTOR_VERSION, + DEFAULT_VALUE_VES_COLLECTOR_VERSION); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_REPORTING_ENTITY_NAME, + DEFAULT_VALUE_REPORTING_ENTITY_NAME); + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_EVENTLOG_DETAIL, + DEFAULT_VALUE_EVENTLOG_DETAIL); + } + +} diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestVESCollectorClient.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestVESCollectorClient.java new file mode 100644 index 000000000..99e7d7bf9 --- /dev/null +++ b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestVESCollectorClient.java @@ -0,0 +1,91 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.devicemanager.test; + +import com.google.common.io.Files; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import org.junit.After; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.VESCollectorClient; + +public class TestVESCollectorClient { + + private static final String TESTCONFIG_CONTENT_NO_AUTH = "[VESCollector]\n" + "VES_COLLECTOR_IP=127.0.0.1\n" + + "VES_COLLECTOR_PORT=8080\n" + "VES_COLLECTOR_TLS_ENABLED=false\n" + "VES_COLLECTOR_USERNAME=sample1\n" + + "VES_COLLECTOR_PASSWORD=sample1\n" + "VES_COLLECTOR_VERSION=v7\n" + "REPORTING_ENTITY_NAME=ONAP SDN-R\n" + ""; + + private static final String TESTCONFIG_CONTENT_AUTH = "[VESCollector]\n" + "VES_COLLECTOR_IP=127.0.0.1\n" + + "VES_COLLECTOR_PORT=8080\n" + "VES_COLLECTOR_TLS_ENABLED=true\n" + "VES_COLLECTOR_USERNAME=sample1\n" + + "VES_COLLECTOR_PASSWORD=sample1\n" + "VES_COLLECTOR_VERSION=v7\n" + "REPORTING_ENTITY_NAME=ONAP SDN-R\n" + ""; + + private static final String message = "Test Message"; + private static final String CONFIG_FILE = "test.properties"; + + @Test + public void testNoAuth() throws Exception { + ConfigurationFileRepresentation vesCfg; + VESCollectorClient vesClient; + + Files.asCharSink(new File(CONFIG_FILE), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_NO_AUTH); + vesCfg = new ConfigurationFileRepresentation(CONFIG_FILE); + vesClient = new VESCollectorClient(vesCfg); + + vesClient.publishVESMessage(message); + vesClient.close(); + + } + + @Test + public void testAuth() throws Exception { + ConfigurationFileRepresentation vesCfg; + VESCollectorClient vesClient; + + Files.asCharSink(new File("test.properties"), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_AUTH); + vesCfg = new ConfigurationFileRepresentation("test.properties"); + + vesClient = new VESCollectorClient(vesCfg); + vesClient.publishVESMessage(message); + vesClient.close(); + } + + @After + public void after() throws InterruptedException, IOException { + + delete(new File(CONFIG_FILE)); + + } + + private static void delete(File f) throws IOException { + if (f.isDirectory()) { + for (File c : f.listFiles()) { + delete(c); + } + } + if (!f.delete()) { + throw new FileNotFoundException("Failed to delete file: " + f); + } + } +} -- cgit 1.2.3-korg