diff options
Diffstat (limited to 'champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java')
-rw-r--r-- | champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java b/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java new file mode 100644 index 0000000..39c775b --- /dev/null +++ b/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java @@ -0,0 +1,115 @@ +package org.onap.champ.service; + +import org.onap.aai.champcore.ChampGraph; +import org.onap.aai.champcore.ChampTransaction; +import org.onap.aai.champcore.exceptions.ChampTransactionException; +import org.onap.aai.champcore.exceptions.ChampUnmarshallingException; +import org.onap.aai.champcore.model.ChampElement; +import org.onap.aai.champcore.model.ChampObject; +import org.onap.aai.champcore.model.ChampRelationship; +import org.onap.champ.exception.ChampServiceException; +import org.onap.champ.util.ChampProperties; +import org.onap.champ.util.ChampServiceConstants; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; + +public class ChampUUIDService { + private ChampGraph graphImpl; + private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME); + + + public ChampUUIDService(ChampGraph graphImpl) { + this.graphImpl = graphImpl; + } + + public List populateUUIDKey(List<ChampElement> elements) { + { + List response = new ArrayList(); + for (ChampElement e : elements) { + ChampElement item = populateUUIDKey(e); + if (item != null) { + response.add(item); + } + } + return response; + } + + } + + public ChampElement populateUUIDKey(ChampElement e) { + { + ChampElement response = null; + + if (e.isObject()) { + if (e.asObject().getProperty(KEY_NAME).isPresent()) { + response = (ChampObject.create().from(e.asObject()) + .withKey(e.asObject().getProperty(KEY_NAME).get().toString()).build()); + } + } else { + if (e.asRelationship().getProperty(KEY_NAME).isPresent() + && e.asRelationship().getSource().getProperty(KEY_NAME).isPresent() + && e.asRelationship().getTarget().getProperty(KEY_NAME).isPresent()) { + ChampObject source = ChampObject.create().from(e.asRelationship().getSource()) + .withKey(e.asRelationship().getSource().getProperty(KEY_NAME).get().toString()).build(); + ChampObject target = ChampObject.create().from(e.asRelationship().getTarget()) + .withKey(e.asRelationship().getTarget().getProperty(KEY_NAME).get().toString()).build(); + ChampRelationship rel = new ChampRelationship.Builder(source, target, e.asRelationship().getType()) + .key(e.asRelationship().getProperty(KEY_NAME).get().toString()) + .properties(e.asRelationship().getProperties()).build(); + response = rel; + } + + } + + return response; + } + + } + + public void populateUUIDProperty(ChampElement e, String uuid) { + e.getProperties().put(KEY_NAME, uuid); + } + + + public Optional<ChampObject> getObjectbyUUID(String uuid, ChampTransaction transaction) + throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException { + Optional<ChampObject> response = Optional.empty(); + + Stream<ChampObject> s; + Map<String, Object> filter = new HashMap<>(); + filter.put(KEY_NAME, uuid); + + s = graphImpl.queryObjects(filter, Optional.ofNullable(transaction)); + Object[] objs = s.toArray(); + if (objs.length == 0) { + return response; + } + response = graphImpl.retrieveObject(((ChampObject) objs[0]).getKey().get(), Optional.ofNullable(transaction)); + return response; + } + + public Optional<ChampRelationship> getRelationshipbyUUID(String uuid, ChampTransaction transaction) + throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException { + Optional<ChampRelationship> response = Optional.empty(); + + + Stream<ChampRelationship> s; + Map<String, Object> filter = new HashMap<>(); + filter.put(KEY_NAME, uuid); + + s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction)); + Object[] objs = s.toArray(); + if (objs.length == 0) { + return response; + } + response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(), + Optional.ofNullable(transaction)); + return response; + } + +} |