Skip to content

Commit 270a117

Browse files
author
Raja Maragani
authored
Rest end point for run rules on list events and find aggregated object (#68)
* Implemented code for multiple users access EI finctionality, Implemented code for run rules on list events and find aggregated object * added test cases and aggregated enabled flag in prop file
1 parent d61bd76 commit 270a117

21 files changed

+745
-60
lines changed

src/main/java/com/ericsson/ei/App.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,51 @@
1616
*/
1717
package com.ericsson.ei;
1818

19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
24+
import org.springframework.beans.factory.config.Scope;
25+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
1926
import org.springframework.boot.SpringApplication;
2027
import org.springframework.boot.autoconfigure.SpringBootApplication;
2128
import org.springframework.boot.builder.SpringApplicationBuilder;
2229
import org.springframework.boot.web.support.SpringBootServletInitializer;
30+
import org.springframework.context.support.SimpleThreadScope;
2331
import org.springframework.scheduling.annotation.EnableAsync;
2432
import org.springframework.scheduling.annotation.EnableScheduling;
2533

26-
import java.util.ArrayList;
27-
import java.util.Collections;
28-
import java.util.List;
29-
3034
@SpringBootApplication
3135
@EnableAsync
3236
@EnableScheduling
3337
public class App extends SpringBootServletInitializer {
3438

35-
@Override
36-
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
37-
return application.sources(App.class);
38-
}
39+
@Override
40+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
41+
return application.sources(App.class);
42+
}
43+
44+
public static void main(String[] args) {
3945

40-
public static void main(String[] args) {
46+
List<String> logLevels = new ArrayList<>();
47+
Collections.addAll(logLevels, "ALL", "DEBUG", "ERROR", "FATAL", "INFO", "TRACE", "WARN");
4148

42-
List<String> logLevels = new ArrayList<>();
43-
Collections.addAll(logLevels, "ALL", "DEBUG", "ERROR", "FATAL", "INFO", "TRACE", "WARN");
49+
if (args != null && args.length > 0 && logLevels.contains(args[0])) {
50+
System.setProperty("logging.level.root", args[0]);
51+
System.setProperty("logging.level.org.springframework.web", args[0]);
52+
System.setProperty("logging.level.com.ericsson.ei", args[0]);
53+
} else {
54+
System.setProperty("logging.level.root", "OFF");
55+
System.setProperty("logging.level.org.springframework.web", "OFF");
56+
System.setProperty("logging.level.com.ericsson.ei", "OFF");
57+
}
4458

45-
if (args != null && args.length > 0 && logLevels.contains(args[0])) {
46-
System.setProperty("logging.level.root", args[0]);
47-
System.setProperty("logging.level.org.springframework.web", args[0]);
48-
System.setProperty("logging.level.com.ericsson.ei", args[0]);
49-
} else {
50-
System.setProperty("logging.level.root", "OFF");
51-
System.setProperty("logging.level.org.springframework.web", "OFF");
52-
System.setProperty("logging.level.com.ericsson.ei", "OFF");
53-
}
59+
ConfigurableBeanFactory beanFactory = new DefaultListableBeanFactory();
60+
Scope threadScope = new SimpleThreadScope();
61+
beanFactory.registerScope("thread", threadScope);
5462

55-
SpringApplication.run(App.class, args);
56-
}
63+
SpringApplication.run(App.class, args);
64+
}
5765

5866
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ericsson.ei.config.scope.register;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
5+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6+
import org.springframework.context.support.SimpleThreadScope;
7+
8+
/**
9+
* This class for the extend the "request" bean scope to thread level
10+
* This "thread" scope is implemented to EventHandler and RuleHandler
11+
* The spring provided "request" bean scope will work only request level, threads are not access the request bean scope without request
12+
* Using the SimpleThreadScope custom scope(scope name "thread") extend the request scope to threads.
13+
*
14+
*/
15+
public class CustomScopeRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
16+
@Override
17+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
18+
beanFactory.registerScope("thread", new SimpleThreadScope());
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ericsson.ei.config.scope.register;
2+
3+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* Register the SimpleThreadScope
9+
*
10+
*/
11+
@Configuration
12+
public class SpringBeanScopeRegisterConfig {
13+
14+
@Bean
15+
public static BeanFactoryPostProcessor beanFactoryPostProcessor() {
16+
return new CustomScopeRegisteringBeanFactoryPostProcessor();
17+
}
18+
19+
}

src/main/java/com/ericsson/ei/controller/RuleCheckController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@
1414
*
1515
*/
1616
@RestController
17-
@RequestMapping(value = "/jmespathrule/ruleCheck", produces = "application/json")
17+
@RequestMapping(value = "/rules/rule-check", produces = "application/json")
1818
public interface RuleCheckController {
1919

2020

2121
/**
22-
* No description
22+
* This call for run the jmespath rule object or rule on the JSON object
2323
*
2424
*/
2525
@RequestMapping(value = "", method = RequestMethod.POST)
26-
public ResponseEntity<?> updateJmespathruleRuleCheck(
26+
public ResponseEntity<?> updateRulesRuleCheck(
2727
@RequestParam
2828
String rule,
2929
@RequestParam
3030
String jsonContent);
3131

32+
/**
33+
* This call for run the jmespath rule objects on the Json array of objects, we get aggregation Object as output
34+
*
35+
*/
36+
@RequestMapping(value = "/aggregation", method = RequestMethod.POST)
37+
public ResponseEntity<?> updateAggregation(
38+
@RequestParam
39+
String listRulesJson,
40+
@RequestParam
41+
String listEventsJson);
42+
3243
}

src/main/java/com/ericsson/ei/controller/RuleCheckControllerImpl.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.ericsson.ei.controller;
22

3+
import java.io.IOException;
4+
5+
import org.json.JSONException;
36
import org.json.JSONObject;
47
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
69
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Value;
711
import org.springframework.http.HttpStatus;
812
import org.springframework.http.ResponseEntity;
913
import org.springframework.stereotype.Component;
1014
import org.springframework.web.bind.annotation.CrossOrigin;
11-
import org.springframework.web.bind.annotation.RequestMapping;
12-
import org.springframework.web.bind.annotation.RequestMethod;
1315

1416
import com.ericsson.ei.jmespath.JmesPathInterface;
17+
import com.ericsson.ei.services.IRuleCheckService;
1518

1619
import io.swagger.annotations.Api;
1720
import io.swagger.annotations.ApiOperation;
@@ -36,15 +39,20 @@
3639

3740
@Component
3841
@CrossOrigin
39-
@Api(value = "jmespath")
40-
@RequestMapping(value = "/jmespathrule/ruleCheck", produces = "application/json")
42+
@Api(value = "Check Rules", description = "This rest call for the execute the rule or rules(Rule object) on the Json, for checking output of rule")
4143
public class RuleCheckControllerImpl implements RuleCheckController {
4244

4345
private static final Logger LOG = LoggerFactory.getLogger(SubscriptionControllerImpl.class);
4446

4547
@Autowired
4648
JmesPathInterface jmesPathInterface;
4749

50+
@Autowired
51+
IRuleCheckService ruleCheckService;
52+
53+
@Value("${testaggregated.enabled:false}")
54+
private Boolean testEnable;
55+
4856
/**
4957
* This method interacts with JmesPathInterface class method runRuleOnEvent
5058
* to evaluate a rule on JSON object.
@@ -60,15 +68,15 @@ public class RuleCheckControllerImpl implements RuleCheckController {
6068
@Override
6169
@CrossOrigin
6270
@ApiOperation(value = "run rule on event")
63-
@RequestMapping(value = "", method = RequestMethod.POST)
64-
public ResponseEntity<?> updateJmespathruleRuleCheck(String rule, String jsonContent) {
71+
public ResponseEntity<?> updateRulesRuleCheck(String rule, String jsonContent) {
6572
String res = new String("[]");
6673

6774
try {
6875
JSONObject jsonObj = new JSONObject(jsonContent);
76+
6977
String jsonString = jsonObj.toString();
7078
res = jmesPathInterface.runRuleOnEvent(rule, jsonString).toString();
71-
LOG.info("Query :" + rule + " executed Successfully");
79+
LOG.debug("Query :" + rule + " executed Successfully");
7280
return new ResponseEntity<String>(res, HttpStatus.OK);
7381

7482
} catch (Exception e) {
@@ -77,4 +85,28 @@ public ResponseEntity<?> updateJmespathruleRuleCheck(String rule, String jsonCon
7785
}
7886
}
7987

88+
@Override
89+
@CrossOrigin
90+
@ApiOperation(value = "Run the list of rules on list of events and prepare the aggregation object. This endpoint for executing the rules on list of objects and return the aggregated objects.")
91+
public ResponseEntity<?> updateAggregation(String listRulesJson, String listEventsJson) {
92+
93+
if (testEnable) {
94+
try {
95+
String aggeObject = ruleCheckService.prepareAggregatedObject(listRulesJson, listEventsJson);
96+
if (aggeObject != null) {
97+
return new ResponseEntity<String>(aggeObject, HttpStatus.OK);
98+
} else {
99+
return new ResponseEntity<String>("invalid json content", HttpStatus.BAD_REQUEST);
100+
}
101+
102+
} catch (JSONException | IOException e) {
103+
LOG.error(e.getMessage(), e);
104+
return new ResponseEntity<String>("invalid json content", HttpStatus.BAD_REQUEST);
105+
}
106+
} else {
107+
LOG.debug("testaggregated.controller.enabled is not enabled in application.properties file, Unable to test the rules on list of events");
108+
return new ResponseEntity<String>("Please use the test environment for this execution", HttpStatus.BAD_REQUEST);
109+
}
110+
}
111+
80112
}

src/main/java/com/ericsson/ei/handlers/EventHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.slf4j.LoggerFactory;
2121
import org.springframework.amqp.core.Message;
2222
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Scope;
24+
import org.springframework.context.annotation.ScopedProxyMode;
2325
import org.springframework.scheduling.annotation.Async;
2426
import org.springframework.stereotype.Component;
2527

@@ -28,6 +30,7 @@
2830
import com.rabbitmq.client.Channel;
2931

3032
@Component
33+
@Scope(value="thread", proxyMode = ScopedProxyMode.TARGET_CLASS)
3134
public class EventHandler {
3235

3336
private static Logger log = LoggerFactory.getLogger(EventHandler.class);
@@ -40,6 +43,10 @@ public class EventHandler {
4043

4144
@Autowired
4245
DownstreamIdRulesHandler downstreamIdRulesHandler;
46+
47+
public RulesHandler getRulesHandler() {
48+
return rulesHandler;
49+
}
4350

4451
public void eventReceived(String event) {
4552
RulesObject eventRules = rulesHandler.getRulesForEvent(event);

src/main/java/com/ericsson/ei/handlers/EventToObjectMapHandler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ public ArrayList<String> getEventToObjectList(String eventId) {
135135
}
136136
return list;
137137
}
138+
139+
/**
140+
* The method is responsible for the delete the EventObjectMap by using the suffix template Name
141+
*
142+
* @param templateName
143+
* @return boolean
144+
*/
145+
public boolean deleteEventObjectMap(String templateName) {
146+
String condition = "{\"objects\": { \"$in\" : [/.*" + templateName + "/]} }";
147+
log.info("The Json condition for delete aggregated object is : " + condition);
148+
return mongodbhandler.dropDocument(databaseName, collectionName, condition);
149+
}
138150

139151

140152
}

src/main/java/com/ericsson/ei/queryservice/ProcessAggregatedObject.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ public ArrayList<String> processQueryAggregatedObject(String id) {
7373
jsonCondition.toString());
7474
return response;
7575
}
76+
77+
/**
78+
* The method is responsible to extract the aggregated data on the basis of
79+
* the ID from the aggregatedObject.
80+
*
81+
* @param id
82+
* @return ArrayList
83+
*/
84+
public ArrayList<String> getAggregatedObjectByTemplateName(String templateName) {
85+
String condition = "{\"_id\": /.*" + templateName + "/}";
86+
LOGGER.debug("The Json condition is : " + condition);
87+
return handler.find(aggregationDataBaseName, aggregationCollectionName, condition);
88+
}
89+
90+
/**
91+
* The method is responsible for the delete the aggregated object using template name suffix
92+
*
93+
* @param templateName
94+
* @return boolean
95+
*/
96+
public boolean deleteAggregatedObject(String templateName) {
97+
String condition = "{\"_id\": /.*" + templateName + "/}";
98+
LOGGER.debug("The Json condition for delete aggregated object is : " + condition);
99+
return handler.dropDocument(aggregationDataBaseName, aggregationCollectionName, condition);
100+
}
76101

77102
/**
78103
* This method is responsible for fetching all the aggregatedObjects from
@@ -109,4 +134,4 @@ public void init() {
109134
LOGGER.debug("The Aggregated Database is : " + aggregationDataBaseName);
110135
LOGGER.debug("The Aggregated Collection is : " + aggregationCollectionName);
111136
}
112-
}
137+
}

0 commit comments

Comments
 (0)