Skip to content

Commit 7ade041

Browse files
authored
Implemented changes to adapt routingkey template to sepia (#247)
* Implemented changes to adapt routingkey template to sepia
1 parent 2eeb088 commit 7ade041

File tree

13 files changed

+354
-10
lines changed

13 files changed

+354
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.1.0
2+
- Implemented new routing key template for Sepia.
3+
-
14
## 2.0.30
25
- Upgrading to OpenJDK 17
36

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<version>2.0.12</version>
1010
</parent>
1111
<properties>
12-
<eiffel-remrem-publish.version>2.0.30</eiffel-remrem-publish.version>
13-
<eiffel-remrem-semantics.version>2.2.6</eiffel-remrem-semantics.version>
12+
<eiffel-remrem-publish.version>2.1.0</eiffel-remrem-publish.version>
13+
<eiffel-remrem-semantics.version>2.3.0</eiffel-remrem-semantics.version>
1414
</properties>
1515
<artifactId>eiffel-remrem-publish</artifactId>
1616
<version>${eiffel-remrem-publish.version}</version>

publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CliOptions.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class CliOptions {
3333

3434
static private Options options=null;
3535
static private CommandLine commandLine;
36-
36+
private static final String SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH = "semanticsRoutingKeyTypeOverrideFilepath";
3737
//Used for testing purposes
3838
private static ArrayList<Integer> testErrorCodes = new ArrayList<>();
3939

@@ -85,6 +85,8 @@ public static void createCLIOptions() {
8585
options.addOption("tag", "tag", true, "tag to be used in routing key");
8686
options.addOption("rk", "routing_key", true, "routing key of the eiffel message. When provided routing key is not generated and the value provided is used.");
8787
options.addOption("tto", "tcp_time_out", true, "specifies tcp connection timeout, default time is 60000 milliseconds");
88+
options.addOption("srkt", SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH, true, "Default uses the routing key defined in Eiffel Sepia.To make it compatible to prior routing key structure provide the path to routing-key-overrides.properties.");
89+
8890
contentGroup = createContentGroup();
8991
options.addOptionGroup(contentGroup);
9092
}
@@ -252,6 +254,12 @@ public static void handleMessageBusOptions() throws HandleMessageBusException {
252254
System.setProperty(key, tls_ver);
253255
}
254256

257+
if (commandLine.hasOption(SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH)) {
258+
String semanticsRoutingKeyTypeOverrideFilepath =commandLine.getOptionValue(SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH);
259+
String key = PropertiesConfig.SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH;
260+
System.setProperty(key, semanticsRoutingKeyTypeOverrideFilepath);
261+
}
262+
255263
String usePersistance = "true";
256264
if (commandLine.hasOption("np")) {
257265
usePersistance = "false";
@@ -288,6 +296,7 @@ public static void clearSystemProperties() {
288296
System.clearProperty(PropertiesConfig.CHANNELS_COUNT);
289297
System.clearProperty(PropertiesConfig.TCP_TIMEOUT);
290298
System.clearProperty(PropertiesConfig.WAIT_FOR_CONFIRMS_TIME_OUT);
299+
System.clearProperty(PropertiesConfig.SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH);
291300
}
292301

293302
/**

publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/config/PropertiesConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class PropertiesConfig {
4242
public static final String SERVICE_UNAVAILABLE = "Service Unavailable";
4343

4444
public static final String CREATE_EXCHANGE_IF_NOT_EXISTING = "com.ericsson.eiffel.remrem.publish.messagebus.createExchange";
45+
public static final String SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH = "com.ericsson.eiffel.remrem.publish.messagebus.semanticsRoutingKeyTypeOverrideFilepath";
4546
public static final String INVALID_EXCHANGE = "Exchange not found, Please check exchange configuration and try again";
4647
public static final String INVALID_EXCHANGE_MESSAGE_CLI = " Unavailable. To create the exchange specify -ce or --create_exchange to true )";
4748
public static final String INVALID_EXCHANGE_MESSAGE_SERVICE = " ExchangeName is not present, To create the exchange specify createExchangeIfNotExisting in application configuration";

publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/config/RabbitMqPropertiesConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public class RabbitMqPropertiesConfig {
5454
@Value("${jasypt.encryptor.jasyptKeyFilePath:#{null}}")
5555
private String jasyptKeyFilePath;
5656

57+
@Value("${semanticsRoutingKeyTypeOverrideFilepath:#{null}}")
58+
private String semanticsRoutingKeyTypeOverrideFilepath;
59+
5760
private Map<String, RabbitMqProperties> rabbitMqPropertiesMap = new HashMap<String, RabbitMqProperties>();
5861

5962
@Autowired
@@ -154,6 +157,9 @@ private void readSpringProperties() {
154157
rabbitMqProperties.setChannelsCount(
155158
Integer.parseInt(channelsCount));
156159
}
160+
161+
rabbitMqProperties.setRoutingKeyTypeOverrideFilePath(semanticsRoutingKeyTypeOverrideFilepath);
162+
157163
String waitForConfirmsTimeOut = getPropertyAsText(rabbitmqInstanceObject, PROPERTY_WAIT_FOR_CONFIRMS_TIMEOUT);
158164
if (waitForConfirmsTimeOut != null) {
159165
rabbitMqProperties.setWaitForConfirmsTimeOut(Long.parseLong(waitForConfirmsTimeOut));

publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/PublishUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ public static String getRoutingKey(MsgService msgService, JsonObject json, RMQHe
6767
String domainId = rabbitMqProperties.getDomainId();
6868
if (rabbitMqProperties != null && rabbitMqProperties.getExchangeName() != null && rabbitMqProperties.getHost() != null
6969
&& (cliMode || (!cliMode && StringUtils.isNotBlank(domainId)))) {
70-
return StringUtils.defaultIfBlank(routingKey, msgService.generateRoutingKey(json, tag, domainId, userDomainSuffix));
70+
71+
if (StringUtils.isNotBlank(routingKey)) {
72+
return routingKey;
73+
} else if (StringUtils.isNotBlank(rabbitMqProperties.getRoutingKeyTypeOverrideFilePath())) {
74+
String type = rabbitMqProperties.getTypeRoutingKeyFromConfiguration(msgService.getEventType(json));
75+
if (StringUtils.isNotBlank(type)) {
76+
return msgService.generateRoutingKey(json, tag, domainId, userDomainSuffix, type);
77+
}
78+
}
79+
return msgService.generateRoutingKey(json, tag, domainId, userDomainSuffix);
7180
}
7281
return "";
7382
}

publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RabbitMqProperties.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
*/
1515
package com.ericsson.eiffel.remrem.publish.helper;
1616

17+
import java.io.FileInputStream;
1718
import java.io.IOException;
1819
import java.security.KeyManagementException;
1920
import java.security.NoSuchAlgorithmException;
2021
import java.util.ArrayList;
2122
import java.util.List;
23+
import java.util.MissingResourceException;
2224
import java.util.Random;
25+
import java.util.ResourceBundle;
2326
import java.util.concurrent.TimeoutException;
27+
import java.util.PropertyResourceBundle;
2428

29+
import org.apache.commons.lang3.StringUtils;
2530
import org.slf4j.LoggerFactory;
2631

2732
import com.ericsson.eiffel.remrem.publish.config.PropertiesConfig;
@@ -52,6 +57,7 @@ public class RabbitMqProperties {
5257
private String domainId;
5358
private Integer channelsCount;
5459
private boolean createExchangeIfNotExisting;
60+
private String routingKeyTypeOverrideFilePath;
5561
private Integer tcpTimeOut;
5662
private boolean hasExchange = false;
5763
// built in tcp connection timeout value for MB in milliseconds.
@@ -62,12 +68,17 @@ public class RabbitMqProperties {
6268
public static final String CONTENT_TYPE = "application/json";
6369
public static final String ENCODING_TYPE = "UTF-8";
6470
public static final BasicProperties PERSISTENT_BASIC_APPLICATION_JSON;
71+
public static final String SEMANTICS_MESSAGE_PROTOCOL = "eiffelsemantics";
6572

6673
private Connection rabbitConnection;
6774
private String protocol;
6875

6976
private List<Channel> rabbitChannels;
7077

78+
private ResourceBundle types;
79+
private final String TYPE = "type";
80+
private final String DOT = ".";
81+
7182
Logger log = (Logger) LoggerFactory.getLogger(RMQHelper.class);
7283

7384
static {
@@ -157,6 +168,14 @@ public void setCreateExchangeIfNotExisting(boolean createExchangeIfNotExisting)
157168
this.createExchangeIfNotExisting = createExchangeIfNotExisting;
158169
}
159170

171+
public String getRoutingKeyTypeOverrideFilePath() {
172+
return routingKeyTypeOverrideFilePath;
173+
}
174+
175+
public void setRoutingKeyTypeOverrideFilePath(String routingKeyTypeOverrideFilePath) {
176+
this.routingKeyTypeOverrideFilePath = routingKeyTypeOverrideFilePath;
177+
}
178+
160179
public Integer getChannelsCount() {
161180
return channelsCount;
162181
}
@@ -229,9 +248,6 @@ public void init() {
229248
factory.setUsername(username);
230249
factory.setPassword(password);
231250
}
232-
233-
234-
235251

236252
if (tlsVer != null && !tlsVer.isEmpty()) {
237253
if (tlsVer.contains("default")) {
@@ -259,6 +275,14 @@ public void init() {
259275
log.error("Error occured while setting up the RabbitMq Connection. "+e.getMessage());
260276
e.printStackTrace();
261277
}
278+
279+
if (StringUtils.isNotBlank(routingKeyTypeOverrideFilePath)) {
280+
try {
281+
types = new PropertyResourceBundle(new FileInputStream(routingKeyTypeOverrideFilePath));
282+
} catch (IOException e) {
283+
log.error("Cannot find routing key file. "+e.getMessage());
284+
}
285+
}
262286
}
263287

264288
/**
@@ -337,6 +361,12 @@ private void initService() {
337361
if (waitForConfirmsTimeOut == null ) {
338362
waitForConfirmsTimeOut = Long.getLong(getValuesFromSystemProperties(protocol + ".rabbitmq.waitForConfirmsTimeOut"));
339363
}
364+
365+
if (protocol.equalsIgnoreCase(SEMANTICS_MESSAGE_PROTOCOL)
366+
&& (routingKeyTypeOverrideFilePath == null || routingKeyTypeOverrideFilePath.isBlank())) {
367+
routingKeyTypeOverrideFilePath = getValuesFromSystemProperties(PropertiesConfig.SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH);
368+
}
369+
340370
}
341371

342372

@@ -352,6 +382,7 @@ private void setValues() {
352382
usePersitance = Boolean.getBoolean(PropertiesConfig.USE_PERSISTENCE);
353383
createExchangeIfNotExisting = Boolean.parseBoolean(getValuesFromSystemProperties(PropertiesConfig.CREATE_EXCHANGE_IF_NOT_EXISTING));
354384
tcpTimeOut = Integer.getInteger(PropertiesConfig.TCP_TIMEOUT);
385+
routingKeyTypeOverrideFilePath = getValuesFromSystemProperties(PropertiesConfig.SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH);
355386
}
356387

357388
private String getValuesFromSystemProperties(String propertyName) {
@@ -556,4 +587,35 @@ private Channel giveMeRandomChannel() throws RemRemPublishException {
556587
factory, e);
557588
}
558589
}
590+
591+
/**
592+
* This method is used to get routing key type based on the eventType from the configuration file
593+
*
594+
* @param eventType
595+
* Eiffel eventType
596+
* @return type based on eventType if provided in the configuration file else null
597+
*/
598+
public String getTypeRoutingKeyFromConfiguration(String eventType) {
599+
600+
if (types != null) {
601+
String key = eventType + DOT + TYPE;
602+
try {
603+
String routingKey = types.getString(key);
604+
if (!routingKey.isBlank()) {
605+
return routingKey;
606+
}else {
607+
log.warn("Routing key from configuration is empty for :"+ key);
608+
}
609+
} catch (MissingResourceException e) {
610+
log.warn("Routing key from configuration is null for :"+ key);
611+
return null;
612+
}
613+
}else {
614+
log.error("Uninitialized routing key configuration file ");
615+
}
616+
617+
return null;
618+
}
619+
620+
559621
}

publish-service/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jasypt.encryptor.jasyptKeyFilePath: ""
2424
rabbitmq.instances.jsonlist=[{ "mp": "eiffelsemantics", "host": "127.0.0.1", "port": "5672", "virtualHost": "", "username": "guest", "password": "guest", "tls": "", "exchangeName": "ertest1234", "channelsCount": "1", "domainId": "eiffelxxx", "createExchangeIfNotExisting":true,"waitForConfirmsTimeOut":"5000", "tcpTimeOut": "5000" }, \
2525
{ "mp": "eiffel3", "host": "127.0.0.1", "port": "5672", "virtualHost": "", "username": "guest", "password": "guest", "tls": "", "exchangeName": "eiffel3", "domainId": "eiffelxxx", "channelsCount": "1", "createExchangeIfNotExisting":true,"waitForConfirmsTimeOut":"5000", "tcpTimeOut": "5000" }]
2626

27+
#semanticsRoutingKeyTypeOverrideFilepath: <The complete file path to read properties of type and family to prepare routing key>
2728

2829
# properties for server used to generate messages
2930
generate.server.uri: http://127.0.0.1:8080
@@ -44,3 +45,4 @@ activedirectory.connectionTimeOut:
4445
spring.mvc.pathmatch.matching-strategy: ANT_PATH_MATCHER
4546

4647

48+

publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/integrationtest/EiffelRemRemPublishIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void testGenerateRoutingKey() throws Exception {
112112
JsonParser parser = new JsonParser();
113113
JsonElement json = parser.parse(new FileReader(file)).getAsJsonObject();
114114
String routingKey = messageService.generateRoutingKey(json.getAsJsonObject(), null, null, null);
115-
assertEquals("eiffel.activity.finished.notag.eiffeltest", routingKey);
115+
assertEquals("eiffel.activity.EiffelActivityFinishedEvent.notag.eiffeltest", routingKey);
116116
}
117117
}
118118
}

publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/MessageServiceRMQImplUnitTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.fail;
2121

2222
import java.io.File;
23+
import java.io.FileNotFoundException;
2324
import java.io.FileReader;
2425
import java.io.IOException;
2526
import java.util.HashMap;
@@ -39,6 +40,7 @@
3940
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4041

4142
import com.ericsson.eiffel.remrem.protocol.MsgService;
43+
import com.ericsson.eiffel.remrem.publish.config.PropertiesConfig;
4244
import com.ericsson.eiffel.remrem.publish.exception.NackException;
4345
import com.ericsson.eiffel.remrem.publish.exception.RemRemPublishException;
4446
import com.ericsson.eiffel.remrem.publish.helper.PublishUtils;
@@ -215,8 +217,37 @@ public void testRoutingKey() throws Exception {
215217
JsonElement json = parser.parse(new FileReader(file)).getAsJsonObject();
216218
routingKey = PublishUtils.getRoutingKey(msgService, json.getAsJsonObject(), rmqHelper, "fem001", null, null);
217219
if(routingKey != null) {
218-
assertEquals("eiffel.activity.finished.notag.eiffeltest.fem001", routingKey);
220+
assertEquals("eiffel.activity.EiffelActivityFinishedEvent.notag.eiffeltest.fem001", routingKey);
219221
}
220222
}
221223
}
224+
225+
@Test
226+
public void testRoutingKeyPriorSepia() throws FileNotFoundException {
227+
RabbitMqProperties semanticsProperties = rmqHelper.getRabbitMqPropertiesMap().get("eiffelsemantics");
228+
229+
try {
230+
// Simulate existence of routing key property file
231+
System.setProperty(PropertiesConfig.SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH,
232+
"src/test/resources/routing-key-overrides.properties");
233+
semanticsProperties.init();
234+
235+
MsgService msgService = PublishUtils.getMessageService(protocol, msgServices);
236+
String routingKey;
237+
if (msgService != null) {
238+
File file = new File("src/test/resources/EiffelActivityFinishedEvent.json");
239+
JsonParser parser = new JsonParser();
240+
JsonElement json = parser.parse(new FileReader(file)).getAsJsonObject();
241+
routingKey = PublishUtils.getRoutingKey(msgService, json.getAsJsonObject(), rmqHelper, "fem001", null, null);
242+
if (routingKey != null) {
243+
assertEquals("eiffel.activity.finished.notag.eiffeltest.fem001", routingKey);
244+
}
245+
}
246+
}
247+
finally {
248+
// Restore non-existence of routing key property file
249+
System.clearProperty(PropertiesConfig.SEMANTICS_ROUTINGKEY_TYPE_OVERRIDE_FILEPATH);
250+
semanticsProperties.init();
251+
}
252+
}
222253
}

0 commit comments

Comments
 (0)