From 2ccd393ecabee3ee19de68bb3573fa185c2ea0e7 Mon Sep 17 00:00:00 2001 From: flaviocirillo Date: Wed, 28 Jun 2017 11:56:35 +0200 Subject: [PATCH] Added IoT Registry module --- .../pom.xml | 1 + .../embeddediotagentregistry-context.xml | 27 + .../spring/embeddediotagentregistry-osgi.xml | 22 + .../pom.xml | 142 +++ .../EmbeddedAgentIdentifierFactory.java | 53 + .../LockableRegisterContextRequest.java | 73 ++ .../embeddediotagent/registry/Registry.java | 1008 +++++++++++++++++ .../registry/comparator/CircleComparator.java | 82 ++ .../comparator/ContextMetadataComparator.java | 154 +++ ...ontextRegistrationAttributeComparator.java | 187 +++ .../ContextRegistrationComparator.java | 112 ++ .../comparator/EntityIdComparator.java | 93 ++ .../registry/comparator/ListComparator.java | 94 ++ .../registry/comparator/PointComparator.java | 78 ++ .../comparator/PolygonComparator.java | 83 ++ .../comparator/SegmentComparator.java | 94 ++ .../registry/comparator/VertexComparator.java | 79 ++ 17 files changed, 2382 insertions(+) create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-context.xml create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-osgi.xml create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/pom.xml create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/EmbeddedAgentIdentifierFactory.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/LockableRegisterContextRequest.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/Registry.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/CircleComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextMetadataComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationAttributeComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/EntityIdComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ListComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PointComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PolygonComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/SegmentComparator.java create mode 100644 eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/VertexComparator.java diff --git a/eu.neclab.iotplatform.iotbroker.builder/pom.xml b/eu.neclab.iotplatform.iotbroker.builder/pom.xml index cfde1fa0..1ae66dc4 100644 --- a/eu.neclab.iotplatform.iotbroker.builder/pom.xml +++ b/eu.neclab.iotplatform.iotbroker.builder/pom.xml @@ -71,6 +71,7 @@ ../eu.neclab.iotplatform.iotbroker.embeddediotagent.couchdb ../eu.neclab.iotplatform.iotbroker.embeddediotagent.indexer ../eu.neclab.iotplatform.iotbroker.embeddediotagent.storage + ../eu.neclab.iotplatform.iotbroker.embeddediotagent.registry ../eu.neclab.iotplatform.knowledgebase diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-context.xml b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-context.xml new file mode 100644 index 00000000..a9c30283 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-context.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-osgi.xml b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-osgi.xml new file mode 100644 index 00000000..e1a06bc9 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/META-INF/spring/embeddediotagentregistry-osgi.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/pom.xml b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/pom.xml new file mode 100644 index 00000000..d5f5c116 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/pom.xml @@ -0,0 +1,142 @@ + + 4.0.0 + + IoTbrokerParent + eu.neclab.iotplatform + 6.1-SNAPSHOT + ../IoTbrokerParent + + + iotbroker.embeddediotagent.registry + bundle + + + + + EclipseLink Repo + http://download.eclipse.org/rt/eclipselink/maven.repo + + + + + + + src/main/resources + + + . + + META-INF/** + + + + + + org.apache.felix + maven-bundle-plugin + true + + META-INF + + eu.neclab.iotplatform.couchdb.* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.apache.httpcomponents + httpclient + 4.3.4 + + + + org.apache.httpcomponents + httpcore + 4.3.2 + + + + + org.json + json + 20140107 + + + + com.google.code.gson + gson + 2.2.2 + + + + eu.neclab.iotplatform + iotbroker.commons + + + + eu.neclab.iotplatform + ngsi.api + + + + eu.neclab.iotplatform + iotbroker.client + + + + eu.neclab.iotplatform + iotbroker.storage + + + + + + + + + + + javax.xml.bind + jaxb-api + 2.2.11 + + + + + + diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/EmbeddedAgentIdentifierFactory.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/EmbeddedAgentIdentifierFactory.java new file mode 100644 index 00000000..0b8508b4 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/EmbeddedAgentIdentifierFactory.java @@ -0,0 +1,53 @@ +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry; + +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextMetadata; +import eu.neclab.iotplatform.ngsi.api.datamodel.MetadataTypes; + +public class EmbeddedAgentIdentifierFactory { + + public static ContextMetadata getEmbeddedAgentIdentifier(String identifier) { + return new ContextMetadata( + MetadataTypes.EmbeddedAgentIdentifier.getName(), null, + identifier); + } + + public static boolean compare(ContextMetadata embeddedAgentIdentifier, + ContextMetadata contextMetadata) { + + if (embeddedAgentIdentifier == null) { + if (contextMetadata == null) { + return true; + } else { + return false; + } + } + + if (contextMetadata == null) { + return false; + } + +// System.out.println(MetadataTypes.EmbeddedAgentIdentifier.getName()); + + if (contextMetadata.getName().toLowerCase().equals( + MetadataTypes.EmbeddedAgentIdentifier.getName().toLowerCase())) { + + if (contextMetadata.getValue() instanceof String) { + + if (((String) embeddedAgentIdentifier.getValue()) + .equals((String) contextMetadata.getValue())) { + return true; + + } else { + return false; + } + + } else { + return false; + } + + } else { + return false; + } + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/LockableRegisterContextRequest.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/LockableRegisterContextRequest.java new file mode 100644 index 00000000..3d526fbf --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/LockableRegisterContextRequest.java @@ -0,0 +1,73 @@ +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.locks.ReentrantLock; + +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextMetadata; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistration; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistrationAttribute; +import eu.neclab.iotplatform.ngsi.api.datamodel.EntityId; +import eu.neclab.iotplatform.ngsi.api.datamodel.RegisterContextRequest; + +public class LockableRegisterContextRequest { + private ReentrantLock lock = new ReentrantLock(); + + private RegisterContextRequest registerContextRequest = new RegisterContextRequest(); + + public RegisterContextRequest getRegisterContextRequest() { + return registerContextRequest; + } + + public void setRegisterContextRequest( + RegisterContextRequest registerContextRequest) { + this.registerContextRequest = registerContextRequest; + } + + LockableRegisterContextRequest(ContextMetadata embeddedAgentIdentifier) { + super(); + List contextRegistrationList = new ArrayList(); + ContextRegistration contextRegistration = new ContextRegistration(); + contextRegistrationList.add(contextRegistration); + + List contextMetadataList = new ArrayList(); + contextRegistration.setListContextMetadata(contextMetadataList); + + if (embeddedAgentIdentifier != null) { + contextMetadataList.add(embeddedAgentIdentifier); + } + + List entityIdList = new ArrayList(); + contextRegistration.setListEntityId(entityIdList); + + List contextRegistrationAttributeList = new ArrayList(); + contextRegistration + .setListContextRegistrationAttribute(contextRegistrationAttributeList); + + registerContextRequest + .setContextRegistrationList(contextRegistrationList); + } + + public ContextRegistration getContextRegistration() { + return registerContextRequest.getContextRegistrationList().iterator() + .next(); + } + + public void setContextRegistration(ContextRegistration contextRegistration) { + List contextRegistrationList = new ArrayList(); + contextRegistrationList.add(contextRegistration); + registerContextRequest + .setContextRegistrationList(contextRegistrationList); + } + + public void lock() { + lock.lock(); + } + + public void unlock() { + lock.unlock(); + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/Registry.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/Registry.java new file mode 100644 index 00000000..f5486533 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/Registry.java @@ -0,0 +1,1008 @@ +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Value; + +import eu.neclab.iotplatform.iotbroker.commons.interfaces.EmbeddedAgentIndexerInterface; +import eu.neclab.iotplatform.iotbroker.commons.interfaces.EmbeddedAgentRegistryInterface; +import eu.neclab.iotplatform.iotbroker.commons.interfaces.PermanentRegistryInterface; +import eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator.ContextRegistrationAttributeComparator; +import eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator.ContextRegistrationComparator; +import eu.neclab.iotplatform.ngsi.api.datamodel.Circle; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextAttribute; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextElement; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextMetadata; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistration; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistrationAttribute; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistrationResponse; +import eu.neclab.iotplatform.ngsi.api.datamodel.DiscoverContextAvailabilityResponse; +import eu.neclab.iotplatform.ngsi.api.datamodel.EntityId; +import eu.neclab.iotplatform.ngsi.api.datamodel.MetadataTypes; +import eu.neclab.iotplatform.ngsi.api.datamodel.RegisterContextRequest; +import eu.neclab.iotplatform.ngsi.api.datamodel.RegisterContextResponse; +import eu.neclab.iotplatform.ngsi.api.ngsi9.Ngsi9Interface; + +public class Registry implements EmbeddedAgentRegistryInterface { + + /** The logger. */ + private static Logger logger = Logger.getLogger(Registry.class); + + @Value("${agentgeoscope_latitude}") + private String agentgeoscope_latitude = null; + @Value("${agentgeoscope_longitude}") + private String agentgeoscope_longitude = null; + @Value("${agentgeoscope_radius}") + private String agentgeoscope_radius = null; + + @Value("${registrations_folder}") + private String registrations_folder = null; + + @Value("${embeddedAgentId:agent1}") + private String embeddedAgentId = null; + + private Map registrationById = new HashMap(); + private boolean registrationByIdInitialized = false; + + // private final String dirConfig = System.getProperty("dir.config"); + // private final String path = new + // String("iotbrokerconfig/storageCouchDB/"); + private final String genericRegistrationIdFile = new String( + "genericRegistrationId.dat"); + + private ContextMetadata embeddedAgentIdentifier = null; + + // private final String registrationsFile = new String("registrations.dat"); + + private EmbeddedAgentIndexerInterface indexer; + + private Ngsi9Interface ngsi9Client; + + private PermanentRegistryInterface permanentRegistry; + + public PermanentRegistryInterface getPermanentRegistry() { + return permanentRegistry; + } + + public void setPermanentRegistry( + PermanentRegistryInterface permanentRegistry) { + this.permanentRegistry = permanentRegistry; + } + + public EmbeddedAgentIndexerInterface getIndexer() { + return indexer; + } + + public void setIndexer(EmbeddedAgentIndexerInterface indexer) { + this.indexer = indexer; + } + + public Ngsi9Interface getNgsi9Client() { + return ngsi9Client; + } + + public void setNgsi9Client(Ngsi9Interface ngsi9Client) { + this.ngsi9Client = ngsi9Client; + } + + private LockableRegisterContextRequest getLockableRegisterContextRequestAndLock( + String id) { + + if (!registrationByIdInitialized) { + initializeRegistrationById(); + } + + LockableRegisterContextRequest lockableRegisterContextRequest; + synchronized (registrationById) { + if (registrationById.containsKey(id)) { + lockableRegisterContextRequest = registrationById.get(id); + lockableRegisterContextRequest.lock(); + } else { + lockableRegisterContextRequest = new LockableRegisterContextRequest( + getEmbeddedAgentIdentifier()); + registrationById.put(id, lockableRegisterContextRequest); + lockableRegisterContextRequest.lock(); + } + } + return lockableRegisterContextRequest; + } + + private synchronized void initializeRegistrationById() { + + if (!registrationByIdInitialized) { + + for (Entry entry : permanentRegistry + .getAllRegistrations().entrySet()) { + + ContextRegistration contextRegistration = entry.getValue() + .getContextRegistrationList().iterator().next(); + + for (EntityId entityId : contextRegistration.getListEntityId()) { + + LockableRegisterContextRequest lockableRegisterContextRequest = new LockableRegisterContextRequest( + getEmbeddedAgentIdentifier()); + lockableRegisterContextRequest + .setContextRegistration(contextRegistration); + lockableRegisterContextRequest.getRegisterContextRequest() + .setRegistrationId( + entry.getValue().getRegistrationId()); + + registrationById.put(indexer.generateId(entityId), + lockableRegisterContextRequest); + } + + } + + registrationByIdInitialized = true; + + } + + } + + private ContextMetadata getEmbeddedAgentIdentifier() { + if (embeddedAgentIdentifier == null) { + embeddedAgentIdentifier = EmbeddedAgentIdentifierFactory + .getEmbeddedAgentIdentifier(embeddedAgentId); + } + return embeddedAgentIdentifier; + } + + /* + * (non-Javadoc) + * + * @see eu.neclab.iotplatform.iotbroker.embeddediotagent.registry. + * EmbeddedAgentRegistryInterface#makeGenericRegistration() + */ + @Override + public void makeGenericRegistration() { + + // Try to get the previous registrationId if any + String registrationId = getGenericRegistrationId(); + if (registrationId != null) { + + logger.info("Trying to refresh generic ResisterContext with id: " + + registrationId); + // Try to refresh the registration + registrationId = refreshRegistration(registrationId); + } + + if (registrationId == null) { + + logger.info("Trying to make a generic ResisterContext"); + + RegisterContextResponse response = ngsi9Client + .registerContext(createGenericRegistration()); + + if (response.getErrorCode() == null + || response.getErrorCode().getCode() == 200) { + + logger.info("Generic ResisterContext done: " + + response.getRegistrationId()); + + storeRegistrationIdOnFile(response.getRegistrationId()); + } else { + logger.info("Impossible to make a generic ResisterContext"); + + } + } else { + storeRegistrationIdOnFile(registrationId); + } + + } + + private RegisterContextRequest createGenericRegistration() { + RegisterContextRequest registerContextRequest = new RegisterContextRequest(); + + List contextRegistrationList = new ArrayList(); + ContextRegistration contextRegistration = new ContextRegistration(); + contextRegistrationList.add(contextRegistration); + + List contextMetadataList = new ArrayList(); + contextRegistration.setListContextMetadata(contextMetadataList); + + List entityIdList = new ArrayList(); + EntityId entityId = new EntityId(".*", null, true); + entityIdList.add(entityId); + contextRegistration.setListEntityId(entityIdList); + + List contextRegistrationAttributeList = new ArrayList(); + contextRegistration + .setListContextRegistrationAttribute(contextRegistrationAttributeList); + + if (agentgeoscope_latitude != null && !agentgeoscope_latitude.isEmpty() + && agentgeoscope_longitude != null + && !agentgeoscope_longitude.isEmpty() + && agentgeoscope_radius != null + && !agentgeoscope_radius.isEmpty()) { + logger.info("agentgeoscope_latitude " + agentgeoscope_latitude + + " agentgeoscope_longitude " + agentgeoscope_longitude + + " agentgeoscope_radius " + agentgeoscope_radius); + + ContextMetadata contextMetadata = new ContextMetadata(); + contextMetadata.setName(MetadataTypes.SimpleGeolocation.getName()); + contextMetadata.setType(MetadataTypes.SimpleGeolocation.getType()); + contextMetadata.setValue(new Circle(Float + .parseFloat(agentgeoscope_latitude), Float + .parseFloat(agentgeoscope_longitude), Float + .parseFloat(agentgeoscope_radius))); + contextMetadataList.add(contextMetadata); + + } + + registerContextRequest + .setContextRegistrationList(contextRegistrationList); + return registerContextRequest; + } + + private String refreshRegistration(String registrationId) { + // Create Generic registration + RegisterContextRequest registerContextRequest = createGenericRegistration(); + + registerContextRequest.setRegistrationId(registrationId); + + RegisterContextResponse response = ngsi9Client + .registerContext(registerContextRequest); + + if (response == null) { + logger.info("Remote Confman not reachable"); + return null; + } else if (response.getErrorCode() == null + || response.getErrorCode().getCode() == 200) { + logger.info("ResisterContext refreshed with id: " + + response.getRegistrationId()); + + return response.getRegistrationId(); + } else { + logger.info("ResisterContext not found in Confman: " + + response.getRegistrationId()); + + return null; + } + + } + + private void storeRegistrationIdOnFile(String registrationId) { + PrintWriter writer = null; + try { + // File file = new File(dirConfig + "/" + path + "/" + // + genericRegistrationIdFile); + File file = new File(registrations_folder + "/" + + genericRegistrationIdFile); + if (!file.exists()) { + file.createNewFile(); + } else { + file.delete(); + file.createNewFile(); + } + // writer = new PrintWriter(dirConfig + "/" + path + "/" + // + genericRegistrationIdFile, "UTF-8"); + writer = new PrintWriter(registrations_folder + "/" + + genericRegistrationIdFile, "UTF-8"); + writer.println(registrationId); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + if (writer != null) { + writer.close(); + } + } + + } + + private String getGenericRegistrationId() { + + String registrationId = null; + + // File file = new File(dirConfig + "/" + path + "/" + // + genericRegistrationIdFile); + File file = new File(registrations_folder + "/" + + genericRegistrationIdFile); + + if (!file.exists()) { + return null; + } else { + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); + String line = null; + while ((line = reader.readLine()) != null) { + registrationId = line; + } + reader.close(); + } catch (IOException x) { + System.err.format("IOException: %s%n", x); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + + return registrationId; + + } + + public void checkRegistration(List contextElementList) { + + if (logger.isDebugEnabled()){ + logger.debug("CheckingRegistration for contextElementList: "+contextElementList); + } + + if (contextElementList != null && !contextElementList.isEmpty()) { + if (contextElementList.size() == 1) { + checkRegistration(contextElementList.get(0)); + } else { + List compactedContextElement = compactContextElement(contextElementList); + + for (ContextElement contextElement : compactedContextElement) { + checkRegistration(contextElement); + } + } + } + + } + + private List compactContextElement( + List contextElementList) { + + Map contextElementById = new HashMap(); + + for (ContextElement contextElement : contextElementList) { + if (!contextElementById.containsKey(contextElement.getEntityId())) { + contextElementById.put(contextElement.getEntityId(), + contextElement); + } else { + ContextElement compactedContextElement = contextElementById + .get(contextElement.getEntityId()); + compactedContextElement.getContextAttributeList().addAll( + contextElement.getContextAttributeList()); + + if (contextElement.getAttributeDomainName() != null) { + compactedContextElement + .setAttributeDomainName(contextElement + .getAttributeDomainName()); + } + + if (contextElement.getDomainMetadata() != null + && !contextElement.getDomainMetadata().isEmpty()) { + + if (compactedContextElement.getDomainMetadata() != null + && !compactedContextElement.getDomainMetadata() + .isEmpty()) { + + compactedContextElement + .setDomainMetadata(compactContextMetadata( + contextElement.getDomainMetadata(), + compactedContextElement + .getDomainMetadata())); + + } else { + compactedContextElement + .setDomainMetadata(contextElement + .getDomainMetadata()); + } + } + + } + } + + return new ArrayList(contextElementById.values()); + + } + + private List compactContextMetadata( + List contextMetadataList1, + List contextMetadataList2) { + + Map contextElementByHash = new HashMap(); + + for (ContextMetadata contextMetadata : contextMetadataList1) { + + if (!contextElementByHash.containsKey(contextMetadata)) { + contextElementByHash.put(contextMetadata.getName(), + contextMetadata); + } + } + + for (ContextMetadata contextMetadata : contextMetadataList2) { + + if (!contextElementByHash.containsKey(contextMetadata)) { + contextElementByHash.put(contextMetadata.getName(), + contextMetadata); + } + } + + return new ArrayList(contextElementByHash.values()); + + } + + private boolean checkContextElementVsRegistrations( + ContextElement contextElement, RegisterContextRequest registration) { + + ContextRegistration contextRegistration1 = new ContextRegistration(); + contextRegistration1 + .setListContextMetadata(new ArrayList( + contextElement.getDomainMetadata())); + contextRegistration1.getListContextMetadata().add( + EmbeddedAgentIdentifierFactory + .getEmbeddedAgentIdentifier(embeddedAgentId)); + contextRegistration1.setListEntityId(new ArrayList()); + contextRegistration1.getListEntityId() + .add(contextElement.getEntityId()); + + ContextRegistrationComparator comparator = new ContextRegistrationComparator(); + + ContextRegistration contextRegistration2 = new ContextRegistration(); + contextRegistration2.setListContextMetadata(registration + .getContextRegistrationList().iterator().next() + .getListContextMetadata()); + contextRegistration2.setListEntityId(registration + .getContextRegistrationList().iterator().next() + .getListEntityId()); + + int comparison = comparator.compare(contextRegistration1, + contextRegistration2); + + if (logger.isDebugEnabled()) { + logger.debug("CheckingRegistration "+ (comparison==0) +" for ContextElement: " + + contextElement + " versus Registration" + registration); + } + + return comparison == 0; + + } + + public void checkRegistration(ContextElement contextElement) { + // List contextRegistrationAttributeList = + // generateContextRegistrationAttributeList(contextElement + // .getContextAttributeList()); + + String id = indexer.generateId(contextElement.getEntityId()); + + boolean newRegistration = true; + + LockableRegisterContextRequest lockableRegisterContextRequest = getLockableRegisterContextRequestAndLock(id); + + if (logger.isDebugEnabled()) { + logger.debug("CheckingRegistration for: " + contextElement); + } + + if (!checkContextElementVsRegistrations(contextElement, + lockableRegisterContextRequest.getRegisterContextRequest())) { + + if (lockableRegisterContextRequest.getContextRegistration() + .getListEntityId().isEmpty()) { + + if (logger.isDebugEnabled()) { + logger.debug("This needs a new registration ContextElement: " + contextElement); + } + + List entityIdList = new ArrayList(); + EntityId entityId = contextElement.getEntityId(); + entityIdList.add(entityId); + + lockableRegisterContextRequest + .getContextRegistration() + .getListContextMetadata() + .addAll(new ArrayList(contextElement + .getDomainMetadata())); + lockableRegisterContextRequest.getContextRegistration() + .setListEntityId(entityIdList); + // lockableRegisterContextRequest + // .getContextRegistration() + // .setListContextRegistrationAttribute( + // generateContextRegistrationAttributeList(contextElement + // .getContextAttributeList())); + + } else { + + newRegistration = false; + + if (contextElement.getDomainMetadata() != null + && !contextElement.getDomainMetadata().isEmpty()) { + + if (lockableRegisterContextRequest.getContextRegistration() + .getListContextMetadata() != null + && !lockableRegisterContextRequest + .getContextRegistration() + .getListContextMetadata().isEmpty()) { + + lockableRegisterContextRequest + .getContextRegistration() + .setListContextMetadata( + compactContextMetadata( + lockableRegisterContextRequest + .getContextRegistration() + .getListContextMetadata(), + contextElement + .getDomainMetadata())); + + } else { + lockableRegisterContextRequest.getContextRegistration() + .setListContextMetadata( + new ArrayList( + contextElement + .getDomainMetadata())); + } + } + } + + RegisterContextResponse registerContextResponse = ngsi9Client + .registerContext(lockableRegisterContextRequest + .getRegisterContextRequest()); + + lockableRegisterContextRequest.getRegisterContextRequest() + .setRegistrationId( + registerContextResponse.getRegistrationId()); + + if (!newRegistration) { + permanentRegistry.deleteRegistration(id); + } + permanentRegistry.storeRegistration(id, + lockableRegisterContextRequest.getRegisterContextRequest()); + + } + + lockableRegisterContextRequest.unlock(); + + } + + // public RegisterContextResponse registerContext( + // RegisterContextRequest request, URI uri) { + // + // /* + // * This is implemented analogously to queryContext. See the comments + // * there for clarification. + // */ + // + // RegisterContextResponse output = new RegisterContextResponse(); + // + // try { + // + // // get address of local host + // // InetAddress thisIp = InetAddress.getLocalHost(); + // + // // initialize http connection + // URL url = new URL(uri.toString()); + // HttpConnectionClient connection = new HttpConnectionClient(); + // + // String resource; + // if (url.toString().matches(".*/")) { + // resource = "registerContext"; + // } else { + // resource = "/registerContext"; + // } + // + // String respObj = connection.initializeConnection(url, resource, + // "POST", request, "application/xml", ""); + // + // if (respObj != null) { + // + // output = (RegisterContextResponse) xmlFactory + // .convertStringToXml(respObj, + // SubscribeContextResponse.class); + // + // return output; + // + // } + // + // } catch (MalformedURLException e) { + // e.printStackTrace(); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // + // return output; + // + // } + + // + // private void queueContextRegistration( + // String id, + // List + // actualContextRegistrationAttributeList, + // List + // receivedContextRegistrationAttributeList) { + // LockableRegisterContextRequest lockableRegisterContextRequest = + // getLockableRegisterContextRequestAndLock(id); + // + // + // + // } + + private List calculateNewContextRegistrationAttributes( + String id, + List actualContextRegistrationAttributeList, + List receivedContextRegistrationAttributeList) { + + /* + * This list will contain the new ContextRegistrationAttributeList seen + */ + List newContextRegistrationAttributeList = new ArrayList(); + + /* + * If there is not a previous registration just create an empty array + */ + List o1List = actualContextRegistrationAttributeList; + List o2List = receivedContextRegistrationAttributeList; + + /* + * Sort the lists + */ + ContextRegistrationAttributeComparator contextRegistrationAttributeComparator = new ContextRegistrationAttributeComparator(); + Collections.sort(o1List, contextRegistrationAttributeComparator); + Collections.sort(o2List, contextRegistrationAttributeComparator); + + /* + * Create the iterator + */ + Iterator contextRegistrationIterator1 = o1List + .iterator(); + Iterator contextRegistrationIterator2 = o2List + .iterator(); + + if (!contextRegistrationIterator1.hasNext()) { + /* + * Add all + */ + newContextRegistrationAttributeList = contextRegistrationAttributeSet( + contextRegistrationIterator2, + contextRegistrationAttributeComparator); + } else { + + ContextRegistrationAttribute contextRegistrationAttribute1 = contextRegistrationIterator1 + .next(); + ContextRegistrationAttribute contextRegistrationAttribute2 = contextRegistrationIterator2 + .next(); + + int comp = contextRegistrationAttributeComparator.compare( + contextRegistrationAttribute1, + contextRegistrationAttribute2); + + while (contextRegistrationIterator2.hasNext()) { + if (comp == 0) { + if (contextRegistrationIterator1.hasNext()) { + contextRegistrationAttribute1 = contextRegistrationIterator1 + .next(); + } else { + newContextRegistrationAttributeList + .addAll(contextRegistrationAttributeSet( + contextRegistrationIterator2, + contextRegistrationAttributeComparator)); + break; + } + contextRegistrationAttribute2 = nextDifferentContextRegistrationAttribute( + contextRegistrationAttribute2, + contextRegistrationIterator1, + contextRegistrationAttributeComparator); + if (contextRegistrationAttribute2 == null) { + break; + } + comp = contextRegistrationAttributeComparator.compare( + contextRegistrationAttribute1, + contextRegistrationAttribute2); + } else if (comp < 0) { + if (contextRegistrationIterator1.hasNext()) { + contextRegistrationAttribute1 = contextRegistrationIterator1 + .next(); + } else { + newContextRegistrationAttributeList + .addAll(contextRegistrationAttributeSet( + contextRegistrationIterator2, + contextRegistrationAttributeComparator)); + break; + } + comp = contextRegistrationAttributeComparator.compare( + contextRegistrationAttribute1, + contextRegistrationAttribute2); + } else { + newContextRegistrationAttributeList + .add(contextRegistrationAttribute2); + + contextRegistrationAttribute2 = nextDifferentContextRegistrationAttribute( + contextRegistrationAttribute2, + contextRegistrationIterator1, + contextRegistrationAttributeComparator); + + if (contextRegistrationAttribute2 == null) { + break; + } + comp = contextRegistrationAttributeComparator.compare( + contextRegistrationAttribute1, + contextRegistrationAttribute2); + } + + } + } + return newContextRegistrationAttributeList; + + } + + // private List calculateNewTs( + // String id, + // List actualList, + // List receivedList) { + // + // /* + // * This list will contain the new ContextRegistrationAttributeList seen + // */ + // List newList = new ArrayList(); + // + // /* + // * If there is not a previous registration just create an empty array + // */ + // List o1List = actualList; + // List o2List = receivedList; + // + // /* + // * Sort the lists + // */ + // ContextRegistrationAttributeComparator + // contextRegistrationAttributeComparator = new + // ContextRegistrationAttributeComparator(); + // Comparator comparator; + // if (receivedList.get(0) instanceof ContextRegistrationAttribute){ + // comparator = new ContextRegistrationAttributeComparator(); + // } else if (receivedList.get(0) instanceof ContextMetadata){ + // comparator = new ContextMetadataComparator(); + // } + // Collections.sort(o1List, comparator); + // Collections.sort(o2List, comparator); + // + // /* + // * Create the iterator + // */ + // Iterator iterator1 = o1List + // .iterator(); + // Iterator iterator2 = o2List + // .iterator(); + // + // if (!iterator1.hasNext()) { + // /* + // * Add all + // */ + // newList = contextRegistrationAttributeSet( + // iterator2, + // contextRegistrationAttributeComparator); + // } else { + // + // ContextRegistrationAttribute contextRegistrationAttribute1 = iterator1 + // .next(); + // ContextRegistrationAttribute contextRegistrationAttribute2 = iterator2 + // .next(); + // + // int comp = contextRegistrationAttributeComparator.compare( + // contextRegistrationAttribute1, + // contextRegistrationAttribute2); + // + // while (iterator2.hasNext()) { + // if (comp == 0) { + // if (iterator1.hasNext()) { + // contextRegistrationAttribute1 = iterator1 + // .next(); + // } else { + // newList + // .addAll(contextRegistrationAttributeSet( + // iterator2, + // contextRegistrationAttributeComparator)); + // break; + // } + // contextRegistrationAttribute2 = + // nextDifferentContextRegistrationAttribute( + // contextRegistrationAttribute2, + // iterator1, + // contextRegistrationAttributeComparator); + // if (contextRegistrationAttribute2 == null) { + // break; + // } + // comp = contextRegistrationAttributeComparator.compare( + // contextRegistrationAttribute1, + // contextRegistrationAttribute2); + // } else if (comp < 0) { + // if (iterator1.hasNext()) { + // contextRegistrationAttribute1 = iterator1 + // .next(); + // } else { + // newList + // .addAll(contextRegistrationAttributeSet( + // iterator2, + // contextRegistrationAttributeComparator)); + // break; + // } + // comp = contextRegistrationAttributeComparator.compare( + // contextRegistrationAttribute1, + // contextRegistrationAttribute2); + // } else { + // newList + // .add(contextRegistrationAttribute2); + // + // contextRegistrationAttribute2 = + // nextDifferentContextRegistrationAttribute( + // contextRegistrationAttribute2, + // iterator1, + // contextRegistrationAttributeComparator); + // + // if (contextRegistrationAttribute2 == null) { + // break; + // } + // comp = contextRegistrationAttributeComparator.compare( + // contextRegistrationAttribute1, + // contextRegistrationAttribute2); + // } + // + // } + // } + // return newList; + // + // } + + private List contextRegistrationAttributeSet( + Iterator contextRegistrationAttributeIterator, + ContextRegistrationAttributeComparator contextRegistrationAttributeComparator) { + /* + * Add all but check duplicates + */ + List newContextRegistrationAttributeList = new ArrayList(); + + ContextRegistrationAttribute newContextRegistrationAttribute = contextRegistrationAttributeIterator + .next(); + + newContextRegistrationAttributeList + .add(newContextRegistrationAttribute); + + while (contextRegistrationAttributeIterator.hasNext()) { + newContextRegistrationAttribute = nextDifferentContextRegistrationAttribute( + newContextRegistrationAttribute, + contextRegistrationAttributeIterator, + contextRegistrationAttributeComparator); + + if (newContextRegistrationAttribute != null) { + newContextRegistrationAttributeList + .add(newContextRegistrationAttribute); + } + } + + return newContextRegistrationAttributeList; + } + + // private List objectSet( + // Iterator iterator, + // Comparator comparator) { + // /* + // * Add all but check duplicates + // */ + // List newObjectList = new ArrayList(); + // + // Object newObject = iterator + // .next(); + // + // newObjectList + // .add(newObject); + // + // while (iterator.hasNext()) { + // newObject = nextDifferentContextRegistrationAttribute( + // newObject, + // iterator, + // comparator); + // + // if (newObject != null) { + // newObjectList + // .add(newObject); + // } + // } + // + // return newObjectList; + // } + + private ContextRegistrationAttribute nextDifferentContextRegistrationAttribute( + ContextRegistrationAttribute contextRegistrationAttribute, + Iterator contextRegistrationAttributeIterator, + ContextRegistrationAttributeComparator contextRegistrationAttributeComparator) { + + while (contextRegistrationAttributeIterator.hasNext()) { + + ContextRegistrationAttribute nextContextRegistrationAttribute = contextRegistrationAttributeIterator + .next(); + + if (contextRegistrationAttributeComparator.compare( + contextRegistrationAttribute, + nextContextRegistrationAttribute) != 0) { + + return nextContextRegistrationAttribute; + + } + } + + return null; + } + + // private List + // generateContextRegistrationAttributeList( + // List contextAttributeList) { + // + // List contextRegistrationAttributeList = new + // ArrayList(); + // for (ContextAttribute contextAttribute : contextAttributeList) { + // ContextRegistrationAttribute contextRegistrationAttribute = new + // ContextRegistrationAttribute(); + // + // contextRegistrationAttribute.setMetaData(contextAttribute + // .getMetadata()); + // contextRegistrationAttribute.setName(contextAttribute.getName()); + // contextRegistrationAttribute.setType(contextAttribute.getType()); + // + // contextRegistrationAttributeList.add(contextRegistrationAttribute); + // } + // + // return contextRegistrationAttributeList; + // } + + @Override + public List extractOwnContextRegistrations( + DiscoverContextAvailabilityResponse discoveryResponse) { + + List extractedContextRegistrations = new ArrayList(); + + if (discoveryResponse != null + && discoveryResponse.getErrorCode().getCode() == 200 + && discoveryResponse.getContextRegistrationResponse() != null + && !discoveryResponse.getContextRegistrationResponse() + .isEmpty()) { + + Iterator contextRegistrationResponseIterator = discoveryResponse + .getContextRegistrationResponse().iterator(); + + while (contextRegistrationResponseIterator.hasNext()) { + + ContextRegistrationResponse contextRegistrationResponse = contextRegistrationResponseIterator + .next(); + + for (ContextMetadata contextMetadata : contextRegistrationResponse + .getContextRegistration().getListContextMetadata()) { + + if (EmbeddedAgentIdentifierFactory.compare( + getEmbeddedAgentIdentifier(), contextMetadata)) { + + extractedContextRegistrations + .add(contextRegistrationResponse + .getContextRegistration()); + + contextRegistrationResponseIterator.remove(); + break; + + } + } + } + } + + return extractedContextRegistrations; + } +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/CircleComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/CircleComparator.java new file mode 100644 index 00000000..ff30718c --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/CircleComparator.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; + +import eu.neclab.iotplatform.ngsi.api.datamodel.Circle; + +/** + * @author Flavio Cirillo (flavio.cirillo@neclab.eu) + * + */ +public class CircleComparator implements Comparator { + + + private static final float epsilon = 0.0000000001f; + + @Override + /** + * The order of the Segment objects takes into account, in order: + * Radius -> Center_Latitude -> Center_Longitude + * + * Null object is order before a not-null object. + * + */ + public int compare(Circle o1, Circle o2) { + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if(Math.abs(o1.getRadius() - o2.getRadius()) > epsilon){ + return Float.compare(o1.getRadius(), o2.getRadius()); + } + + if(Math.abs(o1.getCenterLatitude() - o2.getCenterLatitude()) > epsilon){ + return Float.compare(o1.getCenterLatitude(), o2.getCenterLatitude()); + } + + if(Math.abs(o1.getCenterLongitude() - o2.getCenterLongitude()) > epsilon){ + return Float.compare(o1.getCenterLongitude(), o2.getCenterLongitude()); + } + + return 0; + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextMetadataComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextMetadataComparator.java new file mode 100644 index 00000000..8203dcd9 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextMetadataComparator.java @@ -0,0 +1,154 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import eu.neclab.iotplatform.ngsi.api.datamodel.Circle; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextMetadata; +import eu.neclab.iotplatform.ngsi.api.datamodel.Point; +import eu.neclab.iotplatform.ngsi.api.datamodel.Polygon; +import eu.neclab.iotplatform.ngsi.api.datamodel.Segment; + +public class ContextMetadataComparator implements Comparator { + + @Override + public int compare(ContextMetadata o1, ContextMetadata o2) { + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if (o1.getName() != o2.getName()) { + if (o1.getName() == null) { + return -1; + } else if (o2.getName() == null) { + return 1; + } else if (!o1.getName().toLowerCase().equals(o2.getName().toLowerCase())) { + return o1.getName().compareTo(o2.getName()); + } + } + + if (o1.getType() != o2.getType()) { + if (o1.getType() == null) { + return -1; + } else if (o2.getType() == null) { + return 1; + } else if (!o1.getType().equals(o2.getType())) { + return o1.getType().compareTo(o2.getType()); + } + } + + if (o1.getValue() != o2.getValue()) { + if (o1.getValue() == null) { + return -1; + } else if (o2.getValue() == null) { + return 1; + } else if (o1.getValue().getClass() != o2.getValue().getClass()) { + String value1 = extractValueString(o1); + String value2 = extractValueString(o2); + return value1.compareTo(value2); + } else if (o1.getValue().getClass() == String.class) { + int comp = ((String) o1.getValue()).compareTo(((String) o2 + .getValue())); + if (comp != 0) { + return comp; + } + } else if (isKnownMetadata(o1.getValue().getClass())) { + int comp = compareMetadataValue(o1.getValue(), o2.getValue()); + if (comp != 0) { + return comp; + } + } else if (o1.toJsonString() + .compareTo(o2.toJsonString()) != 0){ + return o1.toJsonString() + .compareTo(o2.toJsonString()); + } else { + return o1.getValue().toString() + .compareTo(o2.getValue().toString()); + } + } + + return 0; + + } + + private String extractValueString(ContextMetadata contextMetadata){ + + String contextMetadataString = contextMetadata.toString(); + String string = new String(contextMetadataString.replace("\n", "")); + + Pattern pattern = Pattern.compile("(.+?)?"); + Matcher matcher = pattern.matcher(string); + String value = null; + while (matcher.find()) { + value = matcher.group(1); + } + return value; + + } + + private boolean isKnownMetadata(Class clazz) { + + return (clazz == Segment.class) || (clazz == Point.class) + || (clazz == Circle.class) || (clazz == Polygon.class); + + } + + private int compareMetadataValue(Object value1, Object value2) { + + if (value1.getClass() == Segment.class) { + return new SegmentComparator().compare((Segment) value1, + (Segment) value2); + } else if (value1.getClass() == Circle.class) { + return new CircleComparator().compare((Circle) value1, + (Circle) value2); + } else if (value1.getClass() == Polygon.class) { + return new PolygonComparator().compare((Polygon) value1, + (Polygon) value2); + } else if (value1.getClass() == Point.class) { + return new PointComparator() + .compare((Point) value1, (Point) value2); + } + + return 0; + + } +} \ No newline at end of file diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationAttributeComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationAttributeComparator.java new file mode 100644 index 00000000..97b606b7 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationAttributeComparator.java @@ -0,0 +1,187 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextMetadata; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistrationAttribute; + +public class ContextRegistrationAttributeComparator implements + Comparator { + + @Override + public int compare(ContextRegistrationAttribute o1, + ContextRegistrationAttribute o2) { + + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if (o1.getIsDomain() != o2.getIsDomain()) { + if (!o1.getIsDomain()) { + return -1; + } else { + return 1; + } + } + + if (o1.getName() != o2.getName()) { + if (o1.getName() == null) { + return -1; + } else if (o2.getName() == null) { + return 1; + } else if (!o1.getName().equals(o2.getName())) { + return o1.getName().compareTo(o2.getName()); + } + } + + if (o1.getType() != o2.getType()) { + if (o1.getType() == null) { + return -1; + } else if (o2.getType() == null) { + return 1; + } else if (!o1.getType().equals(o2.getType())) { + return o1.getType().compareTo(o2.getType()); + } + } + + // List o1ContMetaList = o1.getMetaData(); + // List o2ConteMetaList = o2.getMetaData(); + // if (o1.getMetaData() != o2.getMetaData()){ + // if (o1.getMetaData() == null){ + // return -1; + // } else if (o2.getMetaData() == null){ + // return 1; + // } else if (o1.getMetaData().size() != o2.getMetaData().size()){ + // ContextMetadataComparator contextMetadataComparator = new + // ContextMetadataComparator(); + // Collections.sort(o1.getMetaData(),contextMetadataComparator); + // Collections.sort(o2.getMetaData(),contextMetadataComparator); + // Iterator iter1 = o1.getMetaData().iterator(); + // Iterator iter2 = o2.getMetaData().iterator(); + // while (iter1.hasNext()){ + // int comp = contextMetadataComparator.compare(iter1.next(), + // iter2.next()); + // if (comp != 0){ + // return comp; + // } + // } + // } + // } + + // if (o1.getMetaData() != o2.getMetaData()){ + // if (o1.getMetaData() == null){ + // return -1; + // } else if (o2.getMetaData() == null){ + // return 1; + // } else { + // + // if (o1.getMetaData().size() != o2.getMetaData().size()){ + // } + // ContextMetadataComparator contextMetadataComparator = new + // ContextMetadataComparator(); + // Collections.sort(o1.getMetaData(),contextMetadataComparator); + // Collections.sort(o2.getMetaData(),contextMetadataComparator); + // Iterator bigIter; + // Iterator smallIter; + // if (o1.getMetaData().size() > o2.getMetaData().size() ){ + // bigIter = o1.getMetaData().iterator(); + // smallIter = o2.getMetaData().iterator(); + // } else { + // smallIter = o1.getMetaData().iterator(); + // bigIter = o2.getMetaData().iterator(); + // } + // while (smallIter.hasNext()){ + // int comp = contextMetadataComparator.compare(smallIter.next(), + // bigIter.next()); + // if (comp != 0){ + // return comp; + // } + // } + // if (bigIter.hasNext()){ + // return o1.getMetaData().size() - o2.getMetaData().size(); + // } + // } + // } + + if (o1.getMetadata() != o2.getMetadata()) { + List o1List = new ArrayList(o1.getMetadata()); + List o2List = new ArrayList(o2.getMetadata()); + ContextMetadataComparator contextMetadataComparator = new ContextMetadataComparator(); + Collections.sort(o1List,contextMetadataComparator); + Collections.sort(o2List,contextMetadataComparator); + int comp = new ListComparator(contextMetadataComparator).compare(o1List,o2List); + if (comp != 0){ + return comp; + } + } + + return 0; + + // if (!arg0.getIsDomain() && arg0.getIsDomain()){ + // return -1; + // } else if(arg0.getIsDomain() && !arg0.getIsDomain()){ + // return 1; + // } else { + // if (arg0.getName() == null && arg1.getName() != null){ + // return -1; + // } else if (arg0.getName() != null && arg1.getName() == null){ + // return 1; + // } else if (arg0.getName() == null && arg1.getName() == null || + // arg0.getName().compareTo(arg1.getName()) == 0){ + // if(arg0.getType() == null && arg1.getType() != null){ + // return -1; + // } else if (arg0.getType() != null && arg1.getType() == null){ + // return 1; + // } else if (arg0.getType() == null && arg1.getType() == null || + // arg0.getType().compareTo(arg1.getType()) == 0){ + // if (arg0.get) + // } else { + // return arg0.getType().compareTo(arg1.getType()); + // } + // } else { + // return arg0.getName().compareTo(arg1.getName()); + // } + // } + } +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationComparator.java new file mode 100644 index 00000000..235fb421 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ContextRegistrationComparator.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextMetadata; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistration; +import eu.neclab.iotplatform.ngsi.api.datamodel.ContextRegistrationAttribute; +import eu.neclab.iotplatform.ngsi.api.datamodel.EntityId; + +public class ContextRegistrationComparator implements Comparator{ + + @Override + public int compare(ContextRegistration o1, ContextRegistration o2) { + if (o1 == o2){ + return 0; + } else if (o1 == null){ + return -1; + } else if (o2 == null){ + return 1; + } + + if (o1.getProvidingApplication() != o2.getProvidingApplication() ){ + if (o1.getProvidingApplication() == null){ + return -1; + } else if (o2.getProvidingApplication() == null){ + return 1; + } else if (!o1.getProvidingApplication().equals(o2.getProvidingApplication())){ + return o1.getProvidingApplication().compareTo(o2.getProvidingApplication()); + } + } + + + if (o1.getListEntityId() != o2.getListEntityId()) { + List o1List = new ArrayList(o1.getListEntityId()); + List o2List = new ArrayList(o2.getListEntityId()); + EntityIdComparator entityIdComparator = new EntityIdComparator(); + Collections.sort(o1List,entityIdComparator); + Collections.sort(o2List,entityIdComparator); + int comp = new ListComparator(entityIdComparator).compare(o1List,o2List); + if (comp != 0){ + return comp; + } + } + + + if (o1.getContextRegistrationAttribute() != o2.getContextRegistrationAttribute()) { + List o1List = new ArrayList(o1.getContextRegistrationAttribute()); + List o2List = new ArrayList(o2.getContextRegistrationAttribute()); + ContextRegistrationAttributeComparator contextRegistrationAttributeComparator = new ContextRegistrationAttributeComparator(); + Collections.sort(o1List,contextRegistrationAttributeComparator); + Collections.sort(o2List,contextRegistrationAttributeComparator); + int comp = new ListComparator(contextRegistrationAttributeComparator).compare(o1List,o2List); + if (comp != 0){ + return comp; + } + } + + + if (o1.getListContextMetadata() != o2.getListContextMetadata()) { + List o1List = new ArrayList(o1.getListContextMetadata()); + List o2List = new ArrayList(o2.getListContextMetadata()); + ContextMetadataComparator contextMetadataComparator = new ContextMetadataComparator(); + Collections.sort(o1List,contextMetadataComparator); + Collections.sort(o2List,contextMetadataComparator); + int comp = new ListComparator(contextMetadataComparator).compare(o1List,o2List); + if (comp != 0){ + return comp; + } + } + + + return 0; + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/EntityIdComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/EntityIdComparator.java new file mode 100644 index 00000000..2a54b17e --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/EntityIdComparator.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; + +import eu.neclab.iotplatform.ngsi.api.datamodel.EntityId; + +public class EntityIdComparator implements Comparator{ + + /** + * The order of the Segment objects takes into account, in order: + * isPattern -> Id -> Type + * + * Null object is order before a not-null object. + * If o1.Id is null and o2.Id is not-null the former is ordered + * before the latter. Similarly works for the other fields. + * + */ + @Override + public int compare(EntityId o1, EntityId o2) { + if (o1 == o2){ + return 0; + } else if (o1 == null){ + return -1; + } else if (o2 == null){ + return 1; + } + + if (o1.getIsPattern() != o2.getIsPattern()){ + if(!o1.getIsPattern()){ + return -1; + } else { + return 1; + } + } + + if (o1.getId() != o2.getId() ){ + if (o1.getId() == null){ + return -1; + } else if (o2.getId() == null){ + return 1; + } else if (!o1.getId().equals(o2.getId())){ + return o1.getId().compareTo(o2.getId()); + } + } + + if (o1.getType() != o2.getType() ){ + if (o1.getType() == null){ + return -1; + } else if (o2.getType() == null){ + return 1; + } else if (!o1.getType().equals(o2.getType())){ + return o1.getType().compareTo(o2.getType()); + } + } + + return 0; + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ListComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ListComparator.java new file mode 100644 index 00000000..6e5e5659 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/ListComparator.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +public class ListComparator implements Comparator>{ + + private Comparator comparator; + + public ListComparator(Comparator comparator){ + this.comparator = comparator; + + } + + /** + * Compare two lists. It returns 0 if the two objects are the same list, + * or both are null, or both have the same objects. How the elements are stored in the list + * matter, hence if you want to check regardless the order of the elements, submit the lists + * already sorted. + */ + @Override + public int compare(List o1, List o2) { + if (o1 != o2){ + if (o1 == null){ + return -1; + } else if (o2 == null){ + return 1; + } else { + + if (o1.size() != o2.size()){ + } +// Collections.sort(o1,comparator); +// Collections.sort(o2,comparator); + Iterator bigIter; + Iterator smallIter; + int factor = 1; + if (o1.size() > o2.size() ){ + bigIter = o1.iterator(); + smallIter = o2.iterator(); + factor = -1; + } else { + smallIter = o1.iterator(); + bigIter = o2.iterator(); + } + while (smallIter.hasNext()){ + int comp = comparator.compare(smallIter.next(), bigIter.next()); + if (comp != 0){ + return comp*factor; + } + } + if (bigIter.hasNext()){ + return o1.size() - o2.size(); + } + } + } + return 0; + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PointComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PointComparator.java new file mode 100644 index 00000000..d87560c8 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PointComparator.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; + +import eu.neclab.iotplatform.ngsi.api.datamodel.Point; + +/** + * @author Flavio Cirillo (flavio.cirillo@neclab.eu) + * + */ +public class PointComparator implements Comparator { + + + private static final float epsilon = 0.0000000001f; + + @Override + /** + * The order of the Segment objects takes into account, in order: + * Latitude -> sLongitude + * + * Null object is order before a not-null object. + * + */ + public int compare(Point o1, Point o2) { + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if(Math.abs(o1.getLatitude() - o2.getLatitude()) > epsilon){ + return Float.compare(o1.getLatitude(), o2.getLatitude()); + } + + if(Math.abs(o1.getLongitude() - o2.getLongitude()) > epsilon){ + return Float.compare(o1.getLongitude(), o2.getLongitude()); + } + + return 0; + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PolygonComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PolygonComparator.java new file mode 100644 index 00000000..d793e3bc --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/PolygonComparator.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import eu.neclab.iotplatform.ngsi.api.datamodel.Polygon; +import eu.neclab.iotplatform.ngsi.api.datamodel.Vertex; + +/** + * @author Flavio Cirillo (flavio.cirillo@neclab.eu) + * + */ +public class PolygonComparator implements Comparator { + + @Override + /** + * The order of the Segment objects takes into account, in order: + * Latitude -> sLongitude + * + * Null object is order before a not-null object. + * + */ + public int compare(Polygon o1, Polygon o2) { + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if (o1.getVertexList() != o2.getVertexList()) { + List o1List = new ArrayList(o1.getVertexList()); + List o2List = new ArrayList(o2.getVertexList()); + VertexComparator vertexComparator = new VertexComparator(); + Collections.sort(o1List,vertexComparator); + Collections.sort(o2List,vertexComparator); + int comp = new ListComparator(vertexComparator).compare(o1List,o2List); + if (comp != 0){ + return comp; + } + } + + return 0; + } + +} diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/SegmentComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/SegmentComparator.java new file mode 100644 index 00000000..7eb50b42 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/SegmentComparator.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; + +import eu.neclab.iotplatform.ngsi.api.datamodel.Segment; + +public class SegmentComparator implements Comparator { + + @Override + /** + * The order of the Segment objects takes into account, in order: + * NW_Corner -> SE_Corner -> height + * + * Null object is order before a not-null object. + * If o1.NW_Corner is null and o2.NW_Corner is not-null the former is ordered + * before the latter. Similarly works for the other fields. + * + */ + public int compare(Segment o1, Segment o2) { + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if (o1.getNW_Corner() != o2.getNW_Corner()) { + if (o1.getNW_Corner() == null) { + return -1; + } else if (o2.getNW_Corner() == null) { + return 1; + } else if (!o1.getNW_Corner().replaceAll("\\s","").equals(o2.getNW_Corner().replaceAll("\\s",""))) { + return o1.getNW_Corner().compareTo(o2.getNW_Corner()); + } + } + + if (o1.getSE_Corner() != o2.getSE_Corner()) { + if (o1.getSE_Corner() == null) { + return -1; + } else if (o2.getSE_Corner() == null) { + return 1; + } else if (!o1.getSE_Corner().replaceAll("\\s","").equals(o2.getSE_Corner().replaceAll("\\s",""))) { + return o1.getSE_Corner().compareTo(o2.getSE_Corner()); + } + } + + if (o1.getHeight() != o2.getHeight()) { + if (o1.getHeight() == null) { + return -1; + } else if (o2.getHeight() == null) { + return 1; + } else if (!o1.getHeight().equals(o2.getHeight())) { + return o1.getHeight().compareTo(o2.getHeight()); + } + } + return 0; + } + +} \ No newline at end of file diff --git a/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/VertexComparator.java b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/VertexComparator.java new file mode 100644 index 00000000..dadcfaa9 --- /dev/null +++ b/eu.neclab.iotplatform.iotbroker.embeddediotagent.registry/src/main/java/eu/neclab/iotplatform/iotbroker/embeddediotagent/registry/comparator/VertexComparator.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2015, NEC Europe Ltd. + * All rights reserved. + * + * Authors: + * * Salvatore Longo - salvatore.longo@neclab.eu + * * Tobias Jacobs - tobias.jacobs@neclab.eu + * * Flavio Cirillo - flavio.cirillo@neclab.eu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by NEC Europe Ltd. + * 4. Neither the name of NEC nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY NEC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEC BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *******************************************************************************/ + + +package eu.neclab.iotplatform.iotbroker.embeddediotagent.registry.comparator; + +import java.util.Comparator; + +import eu.neclab.iotplatform.ngsi.api.datamodel.Vertex; + +/** + * @author Flavio Cirillo (flavio.cirillo@neclab.eu) + * + */ +public class VertexComparator implements Comparator { + + + private static final float epsilon = 0.0000000001f; + + @Override + /** + * The order of the Segment objects takes into account, in order: + * Latitude -> sLongitude + * + * Null object is order before a not-null object. + * + */ + public int compare(Vertex o1, Vertex o2) { + if (o1 == o2) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } + + if(Math.abs(o1.getLatitude() - o2.getLatitude()) > epsilon){ + return Float.compare(o1.getLatitude(), o2.getLatitude()); + } + + if(Math.abs(o1.getLongitude() - o2.getLongitude()) > epsilon){ + return Float.compare(o1.getLongitude(), o2.getLongitude()); + } + + return 0; + } + +}