From e0d6ec6fc37bbcac731870a44c98c113efdf4e5c Mon Sep 17 00:00:00 2001
From: Christos Tranoris <tranoris@ece.upatras.gr>
Date: Mon, 6 Nov 2023 13:33:14 +0200
Subject: [PATCH] Initial commit

(cherry picked from commit 0ca7c8b9d82da8081f6dbd598fab8dd8e8710014)
---
 .gitignore                                    |   4 +
 Dockerfile.centrallog                         |   6 +
 pom.xml                                       | 209 ++++++++++++++++++
 .../centrallog/service/CentralLogService.java |  56 +++++
 .../service/CentralLoggerConfig.java          |  45 ++++
 .../service/CentralLoggerRouteBuilder.java    |  73 ++++++
 src/main/resources/application.yml            |  39 ++++
 src/main/resources/bootstrap.yml              |   0
 src/main/resources/logback.xml                |  60 +++++
 9 files changed, 492 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Dockerfile.centrallog
 create mode 100644 pom.xml
 create mode 100644 src/main/java/org/etsi/osl/centrallog/service/CentralLogService.java
 create mode 100644 src/main/java/org/etsi/osl/centrallog/service/CentralLoggerConfig.java
 create mode 100644 src/main/java/org/etsi/osl/centrallog/service/CentralLoggerRouteBuilder.java
 create mode 100644 src/main/resources/application.yml
 create mode 100644 src/main/resources/bootstrap.yml
 create mode 100644 src/main/resources/logback.xml

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5601d3b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+/target/
+.project
+.classpath
+/.settings
diff --git a/Dockerfile.centrallog b/Dockerfile.centrallog
new file mode 100644
index 0000000..3fe8ac2
--- /dev/null
+++ b/Dockerfile.centrallog
@@ -0,0 +1,6 @@
+FROM ibm-semeru-runtimes:open-17.0.7_7-jdk
+MAINTAINER openslice.io
+RUN mkdir /opt/shareclasses
+RUN mkdir -p /opt/openslice/lib/
+COPY target/org.etsi.osl.centrallog.service-1.2.0-SNAPSHOT.jar /opt/openslice/lib/
+CMD ["java", "-Xshareclasses:cacheDir=/opt/shareclasses", "-jar", "/opt/openslice/lib/org.etsi.osl.centrallog.service-1.2.0-SNAPSHOT.jar"]
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..e7c5e5b
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,209 @@
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<parent>
+		<groupId>org.etsi.osl</groupId>
+		<artifactId>org.etsi.osl.main</artifactId>
+		<version>1.2.0-SNAPSHOT</version>
+		<relativePath>../org.etsi.osl.main</relativePath>
+	</parent>
+
+	<artifactId>org.etsi.osl.centrallog.service</artifactId>
+	<name>org.etsi.osl.centrallog.service</name>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+		<spring.boot-version>${spring-boot-version}</spring.boot-version>
+		<spring.cloud-consul-version>${spring-cloud-consul-version}</spring.cloud-consul-version>
+	</properties>
+
+
+	<dependencyManagement>
+		<dependencies>
+			<!-- Spring Boot BOM -->
+			<dependency>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-dependencies</artifactId>
+				<version>${spring.boot-version}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+			<!-- Camel BOM -->
+			<dependency>
+				<groupId>org.apache.camel.springboot</groupId>
+				<artifactId>camel-spring-boot-dependencies</artifactId>
+				<version>${camel.version}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
+
+	<dependencies>
+		<!-- Spring Boot -->
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter</artifactId>
+			<exclusions>
+				<exclusion>
+					<groupId>org.springframework.boot</groupId>
+					<artifactId>spring-boot-starter-tomcat</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-cache</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-undertow-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-undertow</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-actuator</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-starter-bootstrap</artifactId>
+			<version>3.0.2</version>
+		</dependency>
+
+		<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> 
+			</dependency> -->
+
+
+		<!-- activeMQ -->
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-activemq</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.activemq</groupId>
+			<artifactId>activemq-amqp</artifactId>
+			<scope>test</scope>
+			<exclusions>
+				<exclusion>
+					<groupId>org.apache.qpid</groupId>
+					<artifactId>proton-j</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.messaginghub</groupId>
+			<artifactId>pooled-jms</artifactId>
+		</dependency>
+
+
+		<!-- Camel -->
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-spring-boot-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.activemq</groupId>
+			<artifactId>activemq-pool</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel</groupId>
+			<artifactId>camel-activemq</artifactId>
+			<exclusions>
+				<exclusion>
+					<groupId>org.apache.activemq</groupId>
+					<artifactId>activemq-broker</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-service-starter</artifactId>
+		</dependency>
+
+		<dependency>
+  			<groupId>org.apache.camel.springboot</groupId>
+			<artifactId>camel-http-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel</groupId>
+			<artifactId>camel-jackson</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel</groupId>
+			<artifactId>camel-http-common</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.camel</groupId>
+			<artifactId>camel-stream</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.h2database</groupId>
+			<artifactId>h2</artifactId>
+		</dependency>
+
+		<!-- models -->
+		<dependency>
+			<groupId>org.etsi.osl</groupId>
+			<artifactId>org.etsi.osl.model</artifactId>
+			<version>${project.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.etsi.osl</groupId>
+			<artifactId>org.etsi.osl.tmf.api</artifactId>
+			<version>${project.version}</version>
+		</dependency>
+
+
+		<dependency>
+			<groupId>com.fasterxml.jackson.core</groupId>
+			<artifactId>jackson-databind</artifactId>
+		</dependency>
+
+	</dependencies>
+
+	<build>
+		<pluginManagement>
+			<plugins>
+				<plugin>
+					<groupId>org.apache.maven.plugins</groupId>
+					<artifactId>maven-compiler-plugin</artifactId>
+					<configuration>
+						<source>${java.version}</source>
+						<target>${java.version}</target>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+				<version>${spring-boot-version}</version>
+				<executions>
+					<execution>
+						<goals>
+							<goal>repackage</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+
+	</build>
+
+
+
+</project>
diff --git a/src/main/java/org/etsi/osl/centrallog/service/CentralLogService.java b/src/main/java/org/etsi/osl/centrallog/service/CentralLogService.java
new file mode 100644
index 0000000..c4b34df
--- /dev/null
+++ b/src/main/java/org/etsi/osl/centrallog/service/CentralLogService.java
@@ -0,0 +1,56 @@
+package org.etsi.osl.centrallog.service;
+
+/*-
+ * ========================LICENSE_START=================================
+ * org.etsi.osl.bugzilla
+ * %%
+ * Copyright (C) 2019 openslice.io
+ * %%
+ * 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==================================
+ */
+
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
+
+
+/**
+ * @author ichatzis
+ *
+ * based on
+ * https://github.com/apache/camel/tree/master/examples/camel-example-spring-boot-activemq
+ * https://github.com/indrabasak/spring-consul-example 
+ */
+// This is equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan
+@SpringBootApplication
+// This annotation is used for consul
+@EnableDiscoveryClient
+//@EnableRetry
+// This is from spring-cloud to allow local configuration application
+@RefreshScope
+// @EnableAutoConfiguration annotation tells Spring Boot to "guess" how you will want to configure Spring, 
+// based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you 
+// have not manually configured any database connection beans, then Spring will auto-configure an in-memory database. 
+@EnableAutoConfiguration
+// This is enabled by default
+@EnableConfigurationProperties
+public class CentralLogService {
+	public static void main(String[] args) {		
+		SpringApplication.run( CentralLogService.class, args);	
+	}
+}
diff --git a/src/main/java/org/etsi/osl/centrallog/service/CentralLoggerConfig.java b/src/main/java/org/etsi/osl/centrallog/service/CentralLoggerConfig.java
new file mode 100644
index 0000000..756d141
--- /dev/null
+++ b/src/main/java/org/etsi/osl/centrallog/service/CentralLoggerConfig.java
@@ -0,0 +1,45 @@
+package org.etsi.osl.centrallog.service;
+
+/*-
+ * ========================LICENSE_START=================================
+ * org.etsi.osl.portal.api
+ * %%
+ * Copyright (C) 2019 openslice.io
+ * %%
+ * 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==================================
+ */
+/**
+ * @author ichatzis
+ **/
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@EnableConfigurationProperties
+@ConfigurationProperties
+public class CentralLoggerConfig {
+
+	
+    private String centrallogurl;
+
+	public String getCentrallogurl() {
+		return centrallogurl;
+	}
+
+	public void setCentrallogurl(String centrallogurl) {
+		this.centrallogurl = centrallogurl;
+	} 
+}
diff --git a/src/main/java/org/etsi/osl/centrallog/service/CentralLoggerRouteBuilder.java b/src/main/java/org/etsi/osl/centrallog/service/CentralLoggerRouteBuilder.java
new file mode 100644
index 0000000..60b6bc9
--- /dev/null
+++ b/src/main/java/org/etsi/osl/centrallog/service/CentralLoggerRouteBuilder.java
@@ -0,0 +1,73 @@
+package org.etsi.osl.centrallog.service;
+/*-
+ * ========================LICENSE_START=================================
+ * org.etsi.osl.portal.api
+ * %%
+ * Copyright (C) 2019 openslice.io
+ * %%
+ * 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==================================
+ */
+/**
+ * @author ichatzis
+ **/
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+
+@Configuration
+@Component
+public class CentralLoggerRouteBuilder  extends RouteBuilder{
+		
+	
+	private static String CENTRALLOGGERURL = "";
+	
+	@Autowired
+	private CentralLoggerConfig centrallogerconfig;
+	private static final transient Log logger = LogFactory.getLog( CentralLoggerRouteBuilder.class.getName());
+	
+	public void configure() {
+
+		
+		if ( centrallogerconfig.getCentrallogurl() != null) {
+			CENTRALLOGGERURL = centrallogerconfig.getCentrallogurl();
+		}
+		
+		if ( ( CENTRALLOGGERURL == null ) || CENTRALLOGGERURL.equals( "" ) ){
+			logger.info( "NO CENTRALLOGGERURL ROUTING. ELASTICURL = " + CENTRALLOGGERURL);
+			return; 
+		}
+		logger.info( "ENABLED CENTRALLOGGERURL ROUTING. ELASTICURL = " + CENTRALLOGGERURL);			
+	
+		String url = CENTRALLOGGERURL;
+
+		from("activemq:queue:centrallogger.log")
+		.log( "activemq:queue:centrallogger.log package with body ${body} !" )
+		.setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http.HttpMethods.POST))
+        .setHeader("Content-Type", constant("application/json"))
+        .to("log:DEBUG?showBody=true&showHeaders=true")
+        .doTry()
+			.toD(  url  )
+	        .to("log:DEBUG?showBody=true&showHeaders=true")			
+        .doCatch(Exception.class)
+	        .setBody(exceptionMessage().convertToString())
+	        .to("log:DEBUG?showBody=true&showHeaders=true")			
+	    .end();
+	}
+	
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
new file mode 100644
index 0000000..67fb3f7
--- /dev/null
+++ b/src/main/resources/application.yml
@@ -0,0 +1,39 @@
+
+# For Spring Actuator /info endpoint
+info:
+    artifact: centrallog-service
+    name: centrallog-service
+    description: Spring centrallog-service
+    version: 1.0.0
+
+server:
+  port: 13013
+    
+
+logging:
+  level:
+    org.apache.camel: INFO
+  file: logs/application-debug.log
+  pattern:
+    console: "%d %-5level %logger : %msg%n"
+    file: "%d %-5level [%thread] %logger : %msg%n"
+        
+   
+spring:
+  config:
+    activate:
+      on-profile: "default"
+  application:
+    name: centrallog-service
+  activemq:
+    brokerUrl: tcp://localhost:61616?jms.watchTopicAdvisories=false
+    user: artemis
+    password: artemis
+    pool:
+      enabled: true
+      max-connections: 100
+    packages:
+      trust-all: true
+
+centrallogurl: "http://elk_ip:elk_port/index_name/_doc"
+
diff --git a/src/main/resources/bootstrap.yml b/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..e69de29
diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml
new file mode 100644
index 0000000..208c635
--- /dev/null
+++ b/src/main/resources/logback.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ========================LICENSE_START=================================
+  org.etsi.osl.oauth.server
+  %%
+  Copyright (C) 2019 openslice.io
+  %%
+  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==================================
+  -->
+
+<configuration>
+    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
+
+    <springProfile name="default">
+        <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
+
+        <root level="INFO">
+            <appender-ref ref="CONSOLE"/>
+        </root>
+    </springProfile>
+
+    <springProfile name="prod">
+
+        <appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
+            <file>app.log</file>
+
+            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
+                <fileNamePattern>logs/archived/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+                <!-- each archived file, size max 10MB -->
+                <maxFileSize>10MB</maxFileSize>
+                <!-- total size of all archive files, if total size > 20GB, it will delete old archived file -->
+                <totalSizeCap>20GB</totalSizeCap>
+                <!-- 60 days to keep -->
+                <maxHistory>60</maxHistory>
+            </rollingPolicy>
+
+            <encoder>
+                <pattern>%d %p %c{1.} [%t] %m%n</pattern>
+            </encoder>
+        </appender>
+
+        <logger name="org.springframework" level="INFO"/>
+        <logger name="portal.api" level="DEBUG"/>
+        <root level="ERROR">
+            <appender-ref ref="FILE-ROLLING"/>
+        </root>
+    </springProfile>
+
+</configuration>
-- 
GitLab