1 package org.onap.champ.service;
3 import org.onap.aai.champcore.ChampGraph;
4 import org.onap.aai.champcore.ChampTransaction;
5 import org.onap.aai.champcore.exceptions.ChampTransactionException;
6 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
7 import org.onap.aai.champcore.model.ChampElement;
8 import org.onap.aai.champcore.model.ChampObject;
9 import org.onap.aai.champcore.model.ChampRelationship;
10 import org.onap.champ.exception.ChampServiceException;
11 import org.onap.champ.util.ChampProperties;
12 import org.onap.champ.util.ChampServiceConstants;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Stream;
21 public class ChampUUIDService {
22 private ChampGraph graphImpl;
23 private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME);
26 public ChampUUIDService(ChampGraph graphImpl) {
27 this.graphImpl = graphImpl;
30 public List populateUUIDKey(List<ChampElement> elements) {
32 List response = new ArrayList();
33 for (ChampElement e : elements) {
34 ChampElement item = populateUUIDKey(e);
44 public ChampElement populateUUIDKey(ChampElement e) {
46 ChampElement response = null;
49 if (e.asObject().getProperty(KEY_NAME).isPresent()) {
50 response = (ChampObject.create().from(e.asObject())
51 .withKey(e.asObject().getProperty(KEY_NAME).get().toString()).build());
54 if (e.asRelationship().getProperty(KEY_NAME).isPresent()
55 && e.asRelationship().getSource().getProperty(KEY_NAME).isPresent()
56 && e.asRelationship().getTarget().getProperty(KEY_NAME).isPresent()) {
57 ChampObject source = ChampObject.create().from(e.asRelationship().getSource())
58 .withKey(e.asRelationship().getSource().getProperty(KEY_NAME).get().toString()).build();
59 ChampObject target = ChampObject.create().from(e.asRelationship().getTarget())
60 .withKey(e.asRelationship().getTarget().getProperty(KEY_NAME).get().toString()).build();
61 ChampRelationship rel = new ChampRelationship.Builder(source, target, e.asRelationship().getType())
62 .key(e.asRelationship().getProperty(KEY_NAME).get().toString())
63 .properties(e.asRelationship().getProperties()).build();
74 public void populateUUIDProperty(ChampElement e, String uuid) {
75 e.getProperties().put(KEY_NAME, uuid);
79 public Optional<ChampObject> getObjectbyUUID(String uuid, ChampTransaction transaction)
80 throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
81 Optional<ChampObject> response = Optional.empty();
83 Stream<ChampObject> s;
84 Map<String, Object> filter = new HashMap<>();
85 filter.put(KEY_NAME, uuid);
87 s = graphImpl.queryObjects(filter, Optional.ofNullable(transaction));
88 Object[] objs = s.toArray();
89 if (objs.length == 0) {
92 response = graphImpl.retrieveObject(((ChampObject) objs[0]).getKey().get(), Optional.ofNullable(transaction));
96 public Optional<ChampRelationship> getRelationshipbyUUID(String uuid, ChampTransaction transaction)
97 throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
98 Optional<ChampRelationship> response = Optional.empty();
101 Stream<ChampRelationship> s;
102 Map<String, Object> filter = new HashMap<>();
103 filter.put(KEY_NAME, uuid);
105 s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction));
106 Object[] objs = s.toArray();
107 if (objs.length == 0) {
110 response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(),
111 Optional.ofNullable(transaction));