16
16
*/
17
17
package com .ericsson .ei .subscriptionhandler ;
18
18
19
- import com .ericsson .ei .jmespath .JmesPathInterface ;
20
- import com .fasterxml .jackson .databind .JsonNode ;
21
- import com .fasterxml .jackson .databind .node .ArrayNode ;
19
+ import java .util .HashMap ;
20
+ import java .util .Iterator ;
21
+ import java .util .concurrent .ConcurrentHashMap ;
22
+
22
23
import org .slf4j .Logger ;
23
24
import org .slf4j .LoggerFactory ;
24
25
import org .springframework .beans .factory .annotation .Autowired ;
25
26
import org .springframework .stereotype .Component ;
26
27
27
- import java .util .Iterator ;
28
+ import com .ericsson .ei .jmespath .JmesPathInterface ;
29
+ import com .fasterxml .jackson .databind .JsonNode ;
30
+ import com .fasterxml .jackson .databind .ObjectMapper ;
31
+ import com .fasterxml .jackson .databind .node .ArrayNode ;
28
32
29
33
30
34
/**
@@ -42,8 +46,11 @@ public class RunSubscription {
42
46
43
47
@ Autowired
44
48
private JmesPathInterface jmespath ;
45
-
46
- /**
49
+
50
+ @ Autowired
51
+ private SubscriptionRepeatDbHandler subscriptionRepeatDbHandler ;
52
+
53
+ /**
47
54
* This method matches every condition specified in the subscription Object
48
55
* and if all conditions are matched then only the aggregatedObject is
49
56
* eligible for notification via e-mail or REST POST.
@@ -54,34 +61,75 @@ public class RunSubscription {
54
61
* @param requirementIterator
55
62
* @return boolean
56
63
*/
57
- public boolean runSubscriptionOnObject (String aggregatedObject , Iterator <JsonNode > requirementIterator ) {
64
+ public boolean runSubscriptionOnObject (String aggregatedObject , Iterator <JsonNode > requirementIterator , JsonNode subscriptionJson ) {
58
65
boolean conditionFulfilled = false ;
59
- int countConditionFulfillment ;
60
- int countConditions ;
66
+ int count_condition_fulfillment = 0 ;
67
+ int count_conditions = 0 ;
68
+
69
+ int requirementIndex = 0 ;
70
+
61
71
while (requirementIterator .hasNext ()) {
72
+
73
+ JsonNode aggrObjJsonNode = null ;
74
+ ObjectMapper objectMapper = new ObjectMapper ();
75
+ try {
76
+ aggrObjJsonNode = objectMapper .readValue (aggregatedObject , JsonNode .class );
77
+ } catch (Exception e ) {
78
+ LOGGER .error (e .getMessage (), e );
79
+ }
80
+
81
+
82
+ String aggrObjId = aggrObjJsonNode .get ("id" ).asText ();
83
+ String subscriptionName = subscriptionJson .get ("subscriptionName" ).asText ();
84
+ String subscriptionRepeatFlag = subscriptionJson .get ("repeat" ).asText ();
85
+
86
+ if (subscriptionRepeatFlag == "false" && subscriptionRepeatDbHandler .checkIfAggrObjIdExistInSubscriptionAggrIdsMatchedList (subscriptionName , requirementIndex , aggrObjId )){
87
+ LOGGER .info ("Subscription has already matched with AggregatedObject Id: " + aggrObjId +
88
+ "\n SubscriptionName: " + subscriptionName +
89
+ "\n and has Subsctrion Repeat flag set to: " + subscriptionRepeatFlag );
90
+ break ;
91
+ }
92
+
62
93
JsonNode requirement = requirementIterator .next ();
63
- LOGGER .debug ("The fulfilled requirement which will condition checked is : " + requirement .toString ());
94
+
95
+ LOGGER .info ("The fulfilled requirement which will condition checked is : " + requirement .toString ());
64
96
ArrayNode conditions = (ArrayNode ) requirement .get ("conditions" );
65
- countConditionFulfillment = 0 ;
66
- countConditions = conditions .size ();
67
- LOGGER .debug ("Conditions of the subscription : " + conditions .toString ());
97
+
98
+ count_condition_fulfillment = 0 ;
99
+ count_conditions = conditions .size ();
100
+
101
+ LOGGER .info ("Conditions of the subscription : " + conditions .toString ());
68
102
Iterator <JsonNode > conditionIterator = conditions .elements ();
69
103
while (conditionIterator .hasNext ()) {
70
104
String rule = conditionIterator .next ().get ("jmespath" ).toString ().replaceAll ("^\" |\" $" , "" );
71
- String newRule = rule .replace ("'" , "\" " );
72
- LOGGER .debug ("Rule : " + rule );
73
- LOGGER .debug ("New Rule after replacing single quote : " + newRule );
105
+ String new_Rule = rule .replace ("'" , "\" " );
106
+ LOGGER .info ("Rule : " + rule );
107
+ LOGGER .info ("New Rule after replacing single quote : " + new_Rule );
74
108
JsonNode result = jmespath .runRuleOnEvent (rule , aggregatedObject );
75
- LOGGER .debug ("Result : " + result .toString ());
76
- if (result .toString () != null && ! result .toString (). equals ( "false" ) && !result .toString ().equals ("[]" )) {
77
- countConditionFulfillment ++;
109
+ LOGGER .info ("Result : " + result .toString ());
110
+ if (result .toString () != null && result .toString () != "false" && !result .toString ().equals ("[]" )){
111
+ count_condition_fulfillment ++;
78
112
}
79
113
}
80
- if (countConditions != 0 && countConditionFulfillment == countConditions ) {
114
+
115
+ if (count_conditions != 0 && count_condition_fulfillment == count_conditions ){
81
116
conditionFulfilled = true ;
117
+ if (subscriptionJson .get ("repeat" ).toString () == "false" ) {
118
+ LOGGER .info ("Adding matched AggrObj id to SubscriptionRepeatFlagHandlerDb." );
119
+ try {
120
+ subscriptionRepeatDbHandler .addMatchedAggrObjToSubscriptionId (subscriptionName , requirementIndex , aggrObjId );
121
+ } catch (Exception e ) {
122
+ LOGGER .error (e .getMessage ());
123
+ e .printStackTrace ();
124
+ }
125
+ }
82
126
}
127
+
128
+ requirementIndex ++;
83
129
}
84
- LOGGER .debug ("The final value of conditionFulfilled is : " + conditionFulfilled );
130
+
131
+ LOGGER .info ("The final value of conditionFulfilled is : " + conditionFulfilled );
132
+
85
133
return conditionFulfilled ;
86
134
}
87
135
}
0 commit comments