Port champ-microservice project restructure
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / service / ChampUUIDService.java
1 package org.onap.champ.service;
2
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;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.stream.Stream;
20
21 public class ChampUUIDService {
22   private ChampGraph graphImpl;
23   private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME);
24
25
26   public ChampUUIDService(ChampGraph graphImpl) {
27     this.graphImpl = graphImpl;
28   }
29
30   public List populateUUIDKey(List<ChampElement> elements) {
31     {
32       List response = new ArrayList();
33       for (ChampElement e : elements) {
34         ChampElement item = populateUUIDKey(e);
35         if (item != null) {
36           response.add(item);
37         }
38       }
39       return response;
40     }
41
42   }
43
44   public ChampElement populateUUIDKey(ChampElement e) {
45     {
46       ChampElement response = null;
47
48       if (e.isObject()) {
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());
52         }
53       } else {
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();
64           response = rel;
65         }
66
67       }
68
69       return response;
70     }
71
72   }
73
74   public void populateUUIDProperty(ChampElement e, String uuid) {
75     e.getProperties().put(KEY_NAME, uuid);
76   }
77
78
79   public Optional<ChampObject> getObjectbyUUID(String uuid, ChampTransaction transaction)
80       throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
81     Optional<ChampObject> response = Optional.empty();
82
83     Stream<ChampObject> s;
84     Map<String, Object> filter = new HashMap<>();
85     filter.put(KEY_NAME, uuid);
86
87     s = graphImpl.queryObjects(filter, Optional.ofNullable(transaction));
88     Object[] objs = s.toArray();
89     if (objs.length == 0) {
90       return response;
91     }
92     response = graphImpl.retrieveObject(((ChampObject) objs[0]).getKey().get(), Optional.ofNullable(transaction));
93     return response;
94   }
95
96   public Optional<ChampRelationship> getRelationshipbyUUID(String uuid, ChampTransaction transaction)
97       throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
98     Optional<ChampRelationship> response = Optional.empty();
99
100
101     Stream<ChampRelationship> s;
102     Map<String, Object> filter = new HashMap<>();
103     filter.put(KEY_NAME, uuid);
104
105     s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction));
106     Object[] objs = s.toArray();
107     if (objs.length == 0) {
108       return response;
109     }
110     response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(),
111         Optional.ofNullable(transaction));
112     return response;
113   }
114
115 }