Skip to content

Commit ab7629e

Browse files
internal composition fixes (#14)
1 parent 634f4a5 commit ab7629e

16 files changed

+647
-617
lines changed

pom.xml

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -201,60 +201,7 @@
201201
<plugin>
202202
<groupId>org.springframework.boot</groupId>
203203
<artifactId>spring-boot-maven-plugin</artifactId>
204-
</plugin>
205-
206-
<!-- PhoenixNAP RAML Code Generator plugin used to generate sources
207-
from raml -->
208-
<plugin>
209-
<groupId>com.phoenixnap.oss</groupId>
210-
<artifactId>springmvc-raml-plugin</artifactId>
211-
<version>${plugin-version}</version>
212-
<configuration>
213-
<!-- path to raml file -->
214-
<ramlPath>${raml-path}</ramlPath>
215-
<!-- output of generated code -->
216-
<outputRelativePath>${output-relative-path}</outputRelativePath>
217-
<addTimestampFolder>false</addTimestampFolder>
218-
<!-- package for generated sources -->
219-
<basePackage>${base-package}</basePackage>
220-
<baseUri>/</baseUri>
221-
<seperateMethodsByContentType>false</seperateMethodsByContentType>
222-
<useJackson1xCompatibility>false</useJackson1xCompatibility>
223-
<rule>com.phoenixnap.oss.ramlapisync.generation.rules.Spring4ControllerInterfaceRule</rule>
224-
<generationConfig>
225-
<useCommonsLang3>true</useCommonsLang3>
226-
</generationConfig>
227-
</configuration>
228-
<executions>
229-
<execution>
230-
<id>generate-springmvc-controllers</id>
231-
<phase>generate-sources</phase>
232-
<goals>
233-
<goal>generate-springmvc-endpoints</goal>
234-
</goals>
235-
</execution>
236-
</executions>
237-
</plugin>
238-
239-
<!-- required for adding generated sources -->
240-
<plugin>
241-
<groupId>org.codehaus.mojo</groupId>
242-
<artifactId>build-helper-maven-plugin</artifactId>
243-
<executions>
244-
<execution>
245-
<phase>generate-sources</phase>
246-
<goals>
247-
<goal>add-source</goal>
248-
</goals>
249-
<configuration>
250-
<sources>
251-
<!-- where to find the generated sources -->
252-
<source>${output-relative-path}</source>
253-
</sources>
254-
</configuration>
255-
</execution>
256-
</executions>
257-
</plugin>
204+
</plugin>
258205

259206
<plugin>
260207
<groupId>org.apache.maven.plugins</groupId>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class SubscriptionControllerImpl implements SubscriptionController {
5858
public ResponseEntity<SubscriptionResponse> createSubscription(@RequestBody List<Subscription> subscriptions) {
5959
ResponseEntity<SubscriptionResponse> subResponse = null;
6060
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
61-
for (Subscription subscription : subscriptions){
61+
for (Subscription subscription : subscriptions) {
6262
subResponse = null;
6363
try {
6464
subscription.setCreated(Instant.now().toEpochMilli());
@@ -69,7 +69,8 @@ public ResponseEntity<SubscriptionResponse> createSubscription(@RequestBody List
6969
LOG.error(msg);
7070
subscriptionResponse.setMsg(msg);
7171
subscriptionResponse.setStatusCode(HttpStatus.PRECONDITION_FAILED.value());
72-
subResponse = new ResponseEntity<SubscriptionResponse>(subscriptionResponse, HttpStatus.PRECONDITION_FAILED);
72+
subResponse = new ResponseEntity<SubscriptionResponse>(subscriptionResponse,
73+
HttpStatus.PRECONDITION_FAILED);
7374
}
7475

7576
if (!subscriptionService.doSubscriptionExist(subscription.getSubscriptionName())) {

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
package com.ericsson.ei.handlers;
1616

17+
import org.json.JSONArray;
18+
import org.json.JSONException;
19+
import org.json.JSONObject;
1720
import org.slf4j.Logger;
1821
import org.slf4j.LoggerFactory;
1922
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,6 +26,10 @@
2326
import com.ericsson.ei.jsonmerge.MergePrepare;
2427
import com.ericsson.ei.rules.RulesObject;
2528
import com.fasterxml.jackson.databind.JsonNode;
29+
import com.fasterxml.jackson.databind.ObjectMapper;
30+
import com.fasterxml.jackson.databind.node.ArrayNode;
31+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
32+
import com.fasterxml.jackson.databind.node.ObjectNode;
2633
import com.github.wnameless.json.flattener.JsonFlattener;
2734

2835
import org.springframework.stereotype.Component;
@@ -67,11 +74,27 @@ public String runHistoryExtraction(String aggregatedObjectId, RulesObject rules,
6774
String ruleString = ruleJson.toString();
6875

6976
// if we need to add append to an array then array_path will not be
70-
// empty so we use it instead passed pathInAggregatedObject
77+
// empty so we use it instead of passed pathInAggregatedObject
7178
String aggregatedObject = mergeHandler.getAggregatedObject(aggregatedObjectId, false);
72-
String array_path = getPathFromExtractedContent(aggregatedObject, ruleString);
79+
String longRuleString = createLongMergeRule(pathInAggregatedObject, ruleJson);
80+
String objAtPathStr = "";
81+
String pathTrimmed = mergePrepare.trimLastInPath(pathInAggregatedObject, ".");
82+
try {
83+
// if (!pathInAggregatedObject.isEmpty()) {
84+
85+
pathTrimmed = mergePrepare.makeJmespathArrayIndexes(pathTrimmed);
86+
JsonNode objAtPath = jmesPathInterface.runRuleOnEvent(pathTrimmed, aggregatedObject);
87+
objAtPathStr = objAtPath.toString();
88+
// }
89+
} catch (Exception e) {
90+
log.error(e.getMessage(), e);
91+
}
92+
String array_path = getPathFromExtractedContent(objAtPathStr, ruleString);
93+
7394
if (!array_path.isEmpty()) {
74-
pathInAggregatedObject = array_path;
95+
// pathInAggregatedObject = array_path;
96+
pathInAggregatedObject = pathTrimmed + "." + array_path;
97+
pathInAggregatedObject = MergePrepare.destringify(pathInAggregatedObject);
7598
} else {
7699
String ruleKey = getRulePath(ruleString);
77100
if (pathInAggregatedObject.isEmpty()) {
@@ -90,6 +113,28 @@ public String runHistoryExtraction(String aggregatedObjectId, RulesObject rules,
90113
return pathInAggregatedObject;
91114
}
92115

116+
private String createLongMergeRule(String longPath, JsonNode ruleJson) {
117+
// longPath = mergePrepare.trimLastInPath(longPath, ".");
118+
String pathNoIndexes = mergePrepare.removeArrayIndexes(longPath);
119+
String[] pathSubstrings = pathNoIndexes.split("\\.");
120+
JsonNode mergeObject = ruleJson;
121+
122+
try {
123+
String pathString = "";
124+
for (int i = 1; i < pathSubstrings.length; i++) {
125+
int mergePathIndex = pathSubstrings.length - (1 + i);
126+
String pathElement = pathSubstrings[mergePathIndex];
127+
ObjectNode newObject = JsonNodeFactory.instance.objectNode();
128+
newObject.put(pathElement, mergeObject);
129+
mergeObject = newObject;
130+
}
131+
} catch (Exception e) {
132+
log.info(e.getMessage(), e);
133+
}
134+
135+
return mergeObject.toString();
136+
}
137+
93138
/**
94139
* Get the rule path as dot notation
95140
*

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

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ public class ObjectHandler {
4343

4444
static Logger log = (Logger) LoggerFactory.getLogger(ObjectHandler.class);
4545

46-
@Getter @Setter
46+
@Getter
47+
@Setter
4748
@Value("${aggregated.collection.name}")
4849
private String collectionName;
4950

50-
@Getter @Setter
51+
@Getter
52+
@Setter
5153
@Value("${database.name}")
5254
private String databaseName;
5355

@@ -66,7 +68,7 @@ public class ObjectHandler {
6668
@Setter
6769
@Autowired
6870
private SubscriptionHandler subscriptionHandler;
69-
71+
7072
@Getter
7173
@Value("${aggregated.collection.ttlValue}")
7274
private int ttlValue;
@@ -81,12 +83,11 @@ public boolean insertObject(String aggregatedObject, RulesObject rulesObject, St
8183
log.debug("ObjectHandler: Aggregated Object document to be inserted: " + document.toString());
8284

8385
mongoDbHandler.createTTLIndex(databaseName, collectionName, "Time", ttlValue);
84-
8586

8687
boolean result = mongoDbHandler.insertDocument(databaseName, collectionName, document.toString());
8788
if (result)
8889
eventToObjectMap.updateEventToObjectMapInMemoryDB(rulesObject, event, id);
89-
subscriptionHandler.checkSubscriptionForObject(aggregatedObject);
90+
subscriptionHandler.checkSubscriptionForObject(aggregatedObject);
9091
return result;
9192
}
9293

@@ -95,12 +96,18 @@ public boolean insertObject(JsonNode aggregatedObject, RulesObject rulesObject,
9596
}
9697

9798
/**
98-
* This method uses previously locked in database aggregatedObject (lock was set in lockDocument method)
99-
* and modifies this document with the new values and removes the lock in one query
100-
* @param aggregatedObject String to insert in database
101-
* @param rulesObject used for fetching id
102-
* @param event String to fetch id if it was not specified
103-
* @param id String
99+
* This method uses previously locked in database aggregatedObject (lock was
100+
* set in lockDocument method) and modifies this document with the new
101+
* values and removes the lock in one query
102+
*
103+
* @param aggregatedObject
104+
* String to insert in database
105+
* @param rulesObject
106+
* used for fetching id
107+
* @param event
108+
* String to fetch id if it was not specified
109+
* @param id
110+
* String
104111
* @return true if operation succeed
105112
*/
106113
public boolean updateObject(String aggregatedObject, RulesObject rulesObject, String event, String id) {
@@ -109,15 +116,15 @@ public boolean updateObject(String aggregatedObject, RulesObject rulesObject, St
109116
JsonNode idNode = jmespathInterface.runRuleOnEvent(idRules, event);
110117
id = idNode.textValue();
111118
}
112-
log.debug("ObjectHandler: Updating Aggregated Object:\n" + aggregatedObject +
113-
"\nEvent:\n" + event);
119+
log.debug("ObjectHandler: Updating Aggregated Object:\n" + aggregatedObject + "\nEvent:\n" + event);
114120
JsonNode document = prepareDocumentForInsertion(id, aggregatedObject);
115121
String condition = "{\"_id\" : \"" + id + "\"}";
116122
String documentStr = document.toString();
117123
boolean result = mongoDbHandler.updateDocument(databaseName, collectionName, condition, documentStr);
118-
if (result)
124+
if (result) {
119125
eventToObjectMap.updateEventToObjectMapInMemoryDB(rulesObject, event, id);
120126
subscriptionHandler.checkSubscriptionForObject(aggregatedObject);
127+
}
121128
return result;
122129
}
123130

@@ -132,9 +139,9 @@ public List<String> findObjectsByCondition(String condition) {
132139
public String findObjectById(String id) {
133140
String condition = "{\"_id\" : \"" + id + "\"}";
134141
String document = findObjectsByCondition(condition).get(0);
135-
// JsonNode result = getAggregatedObject(document);
136-
// if (result != null)
137-
// return result.asText();
142+
// JsonNode result = getAggregatedObject(document);
143+
// if (result != null)
144+
// return result.asText();
138145
return document;
139146
}
140147

@@ -158,45 +165,49 @@ public JsonNode prepareDocumentForInsertion(String id, String object) {
158165

159166
return jsonNode;
160167
} catch (Exception e) {
161-
log.info(e.getMessage(),e);
168+
log.info(e.getMessage(), e);
162169
}
163170
return null;
164171
}
165172

166173
public JsonNode getAggregatedObject(String dbDocument) {
167-
ObjectMapper mapper = new ObjectMapper();
168-
try {
169-
JsonNode documentJson = mapper.readValue(dbDocument, JsonNode.class);
170-
JsonNode objectDoc = documentJson.get("aggregatedObject");
171-
return objectDoc;
172-
} catch (Exception e) {
173-
log.info(e.getMessage(),e);
174-
}
175-
return null;
174+
ObjectMapper mapper = new ObjectMapper();
175+
try {
176+
JsonNode documentJson = mapper.readValue(dbDocument, JsonNode.class);
177+
JsonNode objectDoc = documentJson.get("aggregatedObject");
178+
return objectDoc;
179+
} catch (Exception e) {
180+
log.info(e.getMessage(), e);
181+
}
182+
return null;
176183
}
177184

178185
public String extractObjectId(JsonNode aggregatedDbObject) {
179186
return aggregatedDbObject.get("_id").textValue();
180187
}
181188

182189
/**
183-
* Locks the document in database to achieve pessimistic locking. Method findAndModify is used to optimize
184-
* the quantity of requests towards database.
185-
* @param id String to search
190+
* Locks the document in database to achieve pessimistic locking. Method
191+
* findAndModify is used to optimize the quantity of requests towards
192+
* database.
193+
*
194+
* @param id
195+
* String to search
186196
* @return String aggregated document
187197
*/
188-
public String lockDocument(String id){
198+
public String lockDocument(String id) {
189199
boolean documentLocked = true;
190200
String conditionId = "{\"_id\" : \"" + id + "\"}";
191201
String conditionLock = "[ { \"lock\" : null } , { \"lock\" : \"0\"}]";
192202
String setLock = "{ \"$set\" : { \"lock\" : \"1\"}}";
193203
ObjectMapper mapper = new ObjectMapper();
194-
while (documentLocked==true){
204+
while (documentLocked == true) {
195205
try {
196206
JsonNode documentJson = mapper.readValue(setLock, JsonNode.class);
197207
JsonNode queryCondition = mapper.readValue(conditionId, JsonNode.class);
198208
((ObjectNode) queryCondition).set("$or", mapper.readValue(conditionLock, JsonNode.class));
199-
Document result = mongoDbHandler.findAndModify(databaseName, collectionName, queryCondition.toString(), documentJson.toString());
209+
Document result = mongoDbHandler.findAndModify(databaseName, collectionName, queryCondition.toString(),
210+
documentJson.toString());
200211
if (result != null) {
201212
log.info("DB locked by " + Thread.currentThread().getId() + " thread");
202213
documentLocked = false;
@@ -205,7 +216,8 @@ public String lockDocument(String id){
205216
// To Remove
206217
log.info("Waiting by " + Thread.currentThread().getId() + " thread");
207218
} catch (Exception e) {
208-
log.info(e.getMessage(),e); }
219+
log.info(e.getMessage(), e);
220+
}
209221
}
210222
return null;
211223
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ private void traverseTree(final JsonNode jsonArray, final String aggregatedObjec
109109
final JsonNode parent = jsonArray.get(0);
110110
RulesObject rules = rulesHandler.getRulesForEvent(parent.toString());
111111

112-
final String np = historyExtractionHandler.runHistoryExtraction(aggregatedObjectId, rules, parent.toString(),
112+
String np = historyExtractionHandler.runHistoryExtraction(aggregatedObjectId, rules, parent.toString(),
113113
pathInAggregatedObject);
114114
String prevNp = null;
115115
for (int i = 1; i < jsonArray.size(); i++) {
116116
if (jsonArray.get(i).isObject()) {
117-
rules = rulesHandler.getRulesForEvent(jsonArray.get(i).toString());
118-
historyExtractionHandler.runHistoryExtraction(aggregatedObjectId, rules, jsonArray.get(i).toString(),
117+
String event = jsonArray.get(i).toString();
118+
rules = rulesHandler.getRulesForEvent(event);
119+
prevNp = historyExtractionHandler.runHistoryExtraction(aggregatedObjectId, rules, event,
119120
pathInAggregatedObject);
120121
} else {
121122
// if we have prevNp then we should use that because it is the

0 commit comments

Comments
 (0)