Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbinteraction #228

Merged
merged 25 commits into from Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
58569c5
removed block jdbc template handling, cleaning up some duplicates and…
ScorpioBroker Jun 9, 2021
f293b27
fixed tenantid
ScorpioBroker Jun 10, 2021
49853cf
fixed append handling in subs
ScorpioBroker Jun 11, 2021
aac9144
Multitenant rst file for documentation
pawannec Jun 15, 2021
8308052
fixed subscription problem on append
ScorpioBroker Jun 15, 2021
6c26c69
Merge branch 'dbinteraction' of https://github.com/scorpiobroker/scor…
ScorpioBroker Jun 15, 2021
0203cb9
fixed nullpointer
ScorpioBroker Jun 15, 2021
f94e790
forward all headers in a remote query except tenant which comes from …
ScorpioBroker Jun 15, 2021
73c6f26
Modify images
pawannec Jun 16, 2021
1a289a7
Merge branch 'dbinteraction' of https://github.com/ScorpioBroker/Scor…
pawannec Jun 16, 2021
43b22c8
first changes for different id handling
ScorpioBroker Jun 17, 2021
877dff7
Merge branch 'dbinteraction' of https://github.com/scorpiobroker/scor…
ScorpioBroker Jun 17, 2021
7d5a288
fixed a problem with table handling in subs
ScorpioBroker Jun 18, 2021
4d78ccc
change timertask hashmap to table to reflect tenant
ScorpioBroker Jun 18, 2021
828971c
fixed unsubscribe
ScorpioBroker Jun 18, 2021
47b31fd
Resolve response code issue in query manager
pawannec Jun 22, 2021
f49de92
Fix error message issue in entity manager.
amitrnec Jun 24, 2021
0d5001c
Fix response error message issue in registry manager.
amitrnec Jun 25, 2021
15798d7
fixed delete for history and getbyid
ScorpioBroker Jun 29, 2021
6a44cb4
Remove unused code for fetch tenantvalue from database
pawannec Jul 6, 2021
8ab2654
only delete entity on explicit delete request
ScorpioBroker Jul 8, 2021
591842f
merged
ScorpioBroker Jul 8, 2021
cf7de0a
Merge branch 'development' into dbinteraction
ScorpioBroker Jul 9, 2021
3f65656
Release notes
ScorpioBroker Jul 9, 2021
03b63ee
Release notes
ScorpioBroker Jul 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,44 @@
package eu.neclab.ngsildbroker.commons.datatypes;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;

public class DBWriteTemplates {


private JdbcTemplate writerJdbcTemplateWithTransaction;
private TransactionTemplate writerTransactionTemplate;
private JdbcTemplate writerJdbcTemplate;

public DBWriteTemplates() {

}
public DBWriteTemplates(JdbcTemplate writerJdbcTemplateWithTransaction,
TransactionTemplate writerTransactionTemplate, JdbcTemplate writerJdbcTemplate) {
super();
this.writerJdbcTemplateWithTransaction = writerJdbcTemplateWithTransaction;
this.writerTransactionTemplate = writerTransactionTemplate;
this.writerJdbcTemplate = writerJdbcTemplate;
}
public JdbcTemplate getWriterJdbcTemplateWithTransaction() {
return writerJdbcTemplateWithTransaction;
}
public void setWriterJdbcTemplateWithTransaction(JdbcTemplate writerJdbcTemplateWithTransaction) {
this.writerJdbcTemplateWithTransaction = writerJdbcTemplateWithTransaction;
}
public TransactionTemplate getWriterTransactionTemplate() {
return writerTransactionTemplate;
}
public void setWriterTransactionTemplate(TransactionTemplate writerTransactionTemplate) {
this.writerTransactionTemplate = writerTransactionTemplate;
}
public JdbcTemplate getWriterJdbcTemplate() {
return writerJdbcTemplate;
}
public void setWriterJdbcTemplate(JdbcTemplate writerJdbcTemplate) {
this.writerJdbcTemplate = writerJdbcTemplate;
}



}
Expand Up @@ -28,10 +28,11 @@ public UpdateHistoryEntityRequest(ArrayListMultimap<String, String> headers, Str
}

public UpdateHistoryEntityRequest(EntityRequest entityRequest) {

logger.trace("Listener handleEntityUpdate...");
// logger.debug("Received key: " + key);
// String payload = new String(message);

setHeaders(entityRequest.getHeaders());
final JsonObject jsonObject = parser.parse(entityRequest.getWithSysAttrs()).getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
logger.debug("Key = " + entry.getKey() + " Value = " + entry.getValue());
Expand Down
Expand Up @@ -18,6 +18,6 @@ public interface NotificationHandler {
* @param notification
* @param ldContext
*/
public void notify(Notification notification, URI callback, String acceptHeader, String subId, List<Object> context, int throttling, Map<String, String> clientSettings);
public void notify(Notification notification, URI callback, String acceptHeader, String subId, List<Object> context, int throttling, Map<String, String> clientSettings, String tenantId);

}
Expand Up @@ -46,14 +46,15 @@
import eu.neclab.ngsildbroker.commons.tools.SerializationTools;

@Component
@SuppressWarnings({"unchecked", "rawtypes"})//Map<String, Object>/List<Object> is always returned here. Json LD lib uses rawtypes
@SuppressWarnings({ "unchecked", "rawtypes" }) // Map<String, Object>/List<Object> is always returned here. Json LD lib
// uses rawtypes
public class ContextResolverBasic {
private final static Logger logger = LogManager.getLogger(ContextResolverBasic.class);
private URI CORE_CONTEXT_URL;
@Value("${context.coreurl:https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld}")
private String CORE_CONTEXT_URL_STR;
//private String USED_CORE_CONTEXT_URL_STR;

// private String USED_CORE_CONTEXT_URL_STR;
// private URI DEFAULT_CONTEXT_URL;

@Autowired
Expand Down Expand Up @@ -107,7 +108,6 @@ private void setup() {
}
}


public ContextResolverBasic(String atContextBaseUrl) {
this();
if (atContextBaseUrl != null) {
Expand All @@ -132,6 +132,26 @@ public ContextResolverBasic() {

}

public static List<Object> getAtContextFromBody(String body) throws ResponseException {
Map<String, Object> bodyMap;
try {
bodyMap = (Map<String, Object>) JsonUtils.fromString(body);

Object context = bodyMap.get(NGSIConstants.JSON_LD_CONTEXT);
if (context == null) {
return new ArrayList<Object>();
}
if (context instanceof List) {
return (List<Object>) context;
}
ArrayList<Object> result = new ArrayList<Object>();
result.add(context);
return result;
} catch (IOException e) {
throw new ResponseException(ErrorType.InvalidRequest, e.getMessage());
}
}

public String expand(String body, List<Object> contextLinks, boolean check, int endPoint) throws ResponseException {
try {
Object obj = JsonUtils.fromString(body);
Expand All @@ -154,7 +174,7 @@ public String expand(String body, List<Object> contextLinks, boolean check, int
throw new ResponseException(ErrorType.InvalidRequest);
} catch (IOException e) {
e.printStackTrace();
throw new ResponseException(ErrorType.InvalidRequest);
throw new ResponseException(ErrorType.InvalidRequest, e.getMessage());
}
}

Expand All @@ -174,7 +194,7 @@ private Map<Integer, List<Object>> expand(Map<String, Object> json, List<Object>
context.addAll(contextLinks);
}
ArrayList<Object> usedContext = new ArrayList<Object>();

usedContext.addAll(context);
usedContext.remove(CORE_CONTEXT_URL_STR);
usedContext.add(BASE_CONTEXT);
Expand Down Expand Up @@ -230,7 +250,7 @@ private boolean preFlightCheck(List<Object> expanded, List<Object> usedContext,
}
return hasAttributes;
}

private boolean preFlightCheck(Map<String, Object> objMap, List<Object> usedContext, boolean root,
int calledEndpoint, boolean customKey) throws ResponseException, JsonGenerationException, IOException {

Expand Down Expand Up @@ -339,12 +359,11 @@ private void checkHasObject(Object mapValue) throws ResponseException {
}
if (mapValue instanceof List) {
List tempList = (List) mapValue;
if(tempList.size() != 1) {
if (tempList.size() != 1) {
throw new ResponseException(ErrorType.BadRequestData, "Only one entry per relationship is allowed");
}
}



}

public Subscription expandSubscription(String body, List<Object> contextLinks) throws ResponseException {
Expand All @@ -357,7 +376,7 @@ public Subscription expandSubscription(String body, List<Object> contextLinks) t
throw new ResponseException(ErrorType.BadRequestData, "Failed to parse document. JSON is invalid");
}
Map<String, Object> rawSub = (Map<String, Object>) expanded.get(1).get(0);

boolean hasEntities = false;
boolean hasWatchedAttributes = false;
boolean hasNotificaition = false;
Expand Down Expand Up @@ -455,7 +474,8 @@ public Subscription expandSubscription(String body, List<Object> contextLinks) t
subscription.setNotification(notification);
hasNotificaition = true;
} catch (Exception e) {
throw new ResponseException(ErrorType.BadRequestData, "Failed to parse notification parameter.\n" + e.getMessage());
throw new ResponseException(ErrorType.BadRequestData,
"Failed to parse notification parameter.\n" + e.getMessage());
}
} else if (keyType == 14) {
// NGSI_LD_QUERY
Expand Down Expand Up @@ -541,10 +561,10 @@ private NotificationParam getNotificationParam(Map<String, Object> map) throws E
String accept = AppConstants.NGB_APPLICATION_JSONLD;
Format format = Format.normalized;
List<String> watchedAttribs = new ArrayList<String>();
String mqttVersion= null;
String mqttVersion = null;
Integer qos = null;
NotificationParam notifyParam = new NotificationParam();
Map<String,String>notifierInfo = new HashMap<String,String>();
Map<String, String> notifierInfo = new HashMap<String, String>();
for (Entry<String, Object> entry : map.entrySet()) {
switch (entry.getKey()) {
case NGSIConstants.NGSI_LD_ATTRIBUTES:
Expand All @@ -565,7 +585,7 @@ private NotificationParam getNotificationParam(Map<String, Object> map) throws E
.get(0).get(NGSIConstants.JSON_LD_VALUE));
endPoint.setUri(endPointURI);
break;

case NGSIConstants.NGSI_LD_NOTIFIERINFO:

for (Entry<String, Object> endPointNotifier : ((List<Map<String, Object>>) endPointEntry
Expand All @@ -585,18 +605,19 @@ private NotificationParam getNotificationParam(Map<String, Object> map) throws E
break;
default:
notifierInfo.put(NGSIConstants.MQTT_VERSION, NGSIConstants.DEFAULT_MQTT_VERSION);
notifierInfo.put(NGSIConstants.MQTT_QOS,String.valueOf(NGSIConstants.DEFAULT_MQTT_QOS));
notifierInfo.put(NGSIConstants.MQTT_QOS,
String.valueOf(NGSIConstants.DEFAULT_MQTT_QOS));
}
}
endPoint.setNotifierInfo(notifierInfo);
break;

default:
throw new ResponseException(ErrorType.BadRequestData, "Unkown entry for endpoint");
}
}
endPoint.setAccept(accept);
//endPoint.setNotifierInfo(notifierInfo);
// endPoint.setNotifierInfo(notifierInfo);
notifyParam.setEndPoint(endPoint);
break;
case NGSIConstants.NGSI_LD_FORMAT:
Expand Down Expand Up @@ -816,18 +837,18 @@ private void protectGeoProp(Map<String, Object> objMap, Object value, List<Objec
return;
}


Map<String, Object> compactedFull = JsonLdProcessor.compact(value, usedContext, defaultOptions);
compactedFull.remove(NGSIConstants.JSON_LD_CONTEXT);
String geoType = (String) compactedFull.get(NGSIConstants.GEO_JSON_TYPE);
//This is needed because one context could map from type which wouldn't work with the used context.
//Used context is needed because something could map point
//This is not good but new geo type will come so this can go away at some time
if(geoType == null) {
// This is needed because one context could map from type which wouldn't work
// with the used context.
// Used context is needed because something could map point
// This is not good but new geo type will come so this can go away at some time
if (geoType == null) {
compactedFull = JsonLdProcessor.compact(value, CORE_CONTEXT, defaultOptions);
compactedFull.remove(NGSIConstants.JSON_LD_CONTEXT);
geoType = (String) compactedFull.get(NGSIConstants.GEO_JSON_TYPE);

}
List geoValues = (List) compactedFull.get(NGSIConstants.GEO_JSON_COORDINATES);
Object entry1, entry2;
Expand All @@ -836,7 +857,8 @@ private void protectGeoProp(Map<String, Object> objMap, Object value, List<Objec
// nothing to be done here point is ok like this
entry1 = geoValues.get(0);
entry2 = geoValues.get(1);
if((!(entry1 instanceof Double) && !(entry1 instanceof Integer)) || (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
if ((!(entry1 instanceof Double) && !(entry1 instanceof Integer))
|| (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData, "Provided coordinate entry is not a float value");
}
break;
Expand All @@ -846,8 +868,10 @@ private void protectGeoProp(Map<String, Object> objMap, Object value, List<Objec
ArrayList<Object> container = new ArrayList<Object>();
entry1 = geoValues.get(i);
entry2 = geoValues.get(i + 1);
if((!(entry1 instanceof Double) && !(entry1 instanceof Integer)) || (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData, "Provided coordinate entry is not a float value");
if ((!(entry1 instanceof Double) && !(entry1 instanceof Integer))
|| (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData,
"Provided coordinate entry is not a float value");
}
container.add(entry1);
container.add(entry2);
Expand All @@ -859,15 +883,18 @@ private void protectGeoProp(Map<String, Object> objMap, Object value, List<Objec
case NGSIConstants.GEO_TYPE_POLYGON:
ArrayList<Object> topLevelContainerList = new ArrayList<Object>();
ArrayList<Object> polyContainerList = new ArrayList<Object>();
if(!geoValues.get(0).equals(geoValues.get(geoValues.size()-2)) || !geoValues.get(1).equals(geoValues.get(geoValues.size()-1))) {
if (!geoValues.get(0).equals(geoValues.get(geoValues.size() - 2))
|| !geoValues.get(1).equals(geoValues.get(geoValues.size() - 1))) {
throw new ResponseException(ErrorType.BadRequestData, "Polygon does not close");
}
for (int i = 0; i < geoValues.size(); i += 2) {
ArrayList<Object> container = new ArrayList<Object>();
entry1 = geoValues.get(i);
entry2 = geoValues.get(i + 1);
if((!(entry1 instanceof Double) && !(entry1 instanceof Integer)) || (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData, "Provided coordinate entry is not a float value");
if ((!(entry1 instanceof Double) && !(entry1 instanceof Integer))
|| (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData,
"Provided coordinate entry is not a float value");
}
container.add(entry1);
container.add(entry2);
Expand All @@ -884,11 +911,14 @@ private void protectGeoProp(Map<String, Object> objMap, Object value, List<Objec
ArrayList<Object> container = new ArrayList<Object>();
entry1 = geoValues.get(i);
entry2 = geoValues.get(i + 1);
if((!(entry1 instanceof Double) && !(entry1 instanceof Integer)) || (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData, "Provided coordinate entry is not a float value");
if ((!(entry1 instanceof Double) && !(entry1 instanceof Integer))
|| (!(entry2 instanceof Double) && !(entry2 instanceof Integer))) {
throw new ResponseException(ErrorType.BadRequestData,
"Provided coordinate entry is not a float value");
}
container.add(entry1);
container.add(entry2);multiPolyContainerList.add(container);
container.add(entry2);
multiPolyContainerList.add(container);
}
multiMidLevelContainerList.add(multiPolyContainerList);
multiTopLevelContainerList.add(multiMidLevelContainerList);
Expand Down Expand Up @@ -1065,7 +1095,7 @@ private CompactedJson compact(Object json, Map<String, Object> context, List<Obj
// unprotectLocationFromRegistry(tempResult);
if (tempResult.containsKey("@graph")) {
// we are in a multiresult set
//Object atContext = tempResult.get(NGSIConstants.JSON_LD_CONTEXT);
// Object atContext = tempResult.get(NGSIConstants.JSON_LD_CONTEXT);
List<Map<String, Object>> toCompact = (List<Map<String, Object>>) tempResult.get("@graph");
result.setCompacted(JsonUtils.toPrettyString(toCompact));
for (Map<String, Object> entry : toCompact) {
Expand All @@ -1088,28 +1118,29 @@ private CompactedJson compact(Object json, Map<String, Object> context, List<Obj
}

private void cleanExpandedJson(Object json) {
if(json instanceof List) {
List tempList = (List)json;
for(Object entry: tempList) {
if (json instanceof List) {
List tempList = (List) json;
for (Object entry : tempList) {
cleanExpandedJson(entry);
}
}else if(json instanceof Map) {
Map tempMap = (Map)json;
} else if (json instanceof Map) {
Map tempMap = (Map) json;
Iterator<Entry> it = tempMap.entrySet().iterator();
while(it.hasNext()) {
while (it.hasNext()) {
Entry next = it.next();
Object key = next.getKey();
Object value = next.getValue();
if(NGSIConstants.NGSI_LD_DATA_SET_ID.equals(key) && NGSIConstants.DEFAULT_DATA_SET_ID.equals(((Map)((List)value).get(0)).get(NGSIConstants.JSON_LD_ID))) {
if (NGSIConstants.NGSI_LD_DATA_SET_ID.equals(key) && NGSIConstants.DEFAULT_DATA_SET_ID
.equals(((Map) ((List) value).get(0)).get(NGSIConstants.JSON_LD_ID))) {
it.remove();
continue;
}
if(value instanceof Map || value instanceof List) {
if (value instanceof Map || value instanceof List) {
cleanExpandedJson(value);
}
}
}

}

private String generateAtContextServing(List<Object> rawContext, int hash) {
Expand Down Expand Up @@ -1193,6 +1224,7 @@ private Map<String, Object> getRemoteContext(String url) throws ResponseExceptio
}

}

//Check not used anymore
/*
* private void validateAndCleanContext(Map<String, Object> contextToMerge)
Expand All @@ -1208,7 +1240,7 @@ private Map<String, Object> getRemoteContext(String url) throws ResponseExceptio
* }
*
* } }
*/
*/
private String validateSubNotifierInfoMqttVersion(String string) throws ResponseException {
try {
if (!Arrays.asList(NGSIConstants.VALID_MQTT_VERSION).contains(string)) {
Expand Down