b1354c538dd4f33bda49efc1820c2826f68ac8bf
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / service / ChampUUIDService.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.champ.service;
23
24 import org.onap.aai.champcore.ChampGraph;
25 import org.onap.aai.champcore.ChampTransaction;
26 import org.onap.aai.champcore.exceptions.ChampTransactionException;
27 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
28 import org.onap.aai.champcore.model.ChampElement;
29 import org.onap.aai.champcore.model.ChampObject;
30 import org.onap.aai.champcore.model.ChampRelationship;
31 import org.onap.champ.exception.ChampServiceException;
32 import org.onap.champ.util.ChampProperties;
33 import org.onap.champ.util.ChampServiceConstants;
34
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Optional;
40 import java.util.stream.Stream;
41
42 public class ChampUUIDService {
43   private ChampGraph graphImpl;
44   private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME);
45
46
47   public ChampUUIDService(ChampGraph graphImpl) {
48     this.graphImpl = graphImpl;
49   }
50
51   public List populateUUIDKey(List<ChampElement> elements) {
52     {
53       List response = new ArrayList();
54       for (ChampElement e : elements) {
55         ChampElement item = populateUUIDKey(e);
56         if (item != null) {
57           response.add(item);
58         }
59       }
60       return response;
61     }
62
63   }
64
65   public ChampElement populateUUIDKey(ChampElement e) {
66     {
67       ChampElement response = null;
68
69       if (e.isObject()) {
70         if (e.asObject().getProperty(KEY_NAME).isPresent()) {
71           response = (ChampObject.create().from(e.asObject())
72               .withKey(e.asObject().getProperty(KEY_NAME).get().toString()).build());
73         }
74       } else {
75         if (e.asRelationship().getProperty(KEY_NAME).isPresent()
76             && e.asRelationship().getSource().getProperty(KEY_NAME).isPresent()
77             && e.asRelationship().getTarget().getProperty(KEY_NAME).isPresent()) {
78           ChampObject source = ChampObject.create().from(e.asRelationship().getSource())
79               .withKey(e.asRelationship().getSource().getProperty(KEY_NAME).get().toString()).build();
80           ChampObject target = ChampObject.create().from(e.asRelationship().getTarget())
81               .withKey(e.asRelationship().getTarget().getProperty(KEY_NAME).get().toString()).build();
82           ChampRelationship rel = new ChampRelationship.Builder(source, target, e.asRelationship().getType())
83               .key(e.asRelationship().getProperty(KEY_NAME).get().toString())
84               .properties(e.asRelationship().getProperties()).build();
85           response = rel;
86         }
87
88       }
89
90       return response;
91     }
92
93   }
94
95   public void populateUUIDProperty(ChampElement e, String uuid) {
96     e.getProperties().put(KEY_NAME, uuid);
97   }
98
99
100   public Optional<ChampObject> getObjectbyUUID(String uuid, ChampTransaction transaction)
101       throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
102     Optional<ChampObject> response = Optional.empty();
103
104     Stream<ChampObject> s;
105     Map<String, Object> filter = new HashMap<>();
106     filter.put(KEY_NAME, uuid);
107
108     s = graphImpl.queryObjects(filter, Optional.ofNullable(transaction));
109     Object[] objs = s.toArray();
110     if (objs.length == 0) {
111       return response;
112     }
113     response = graphImpl.retrieveObject(((ChampObject) objs[0]).getKey().get(), Optional.ofNullable(transaction));
114     return response;
115   }
116
117   public Optional<ChampRelationship> getRelationshipbyUUID(String uuid, ChampTransaction transaction)
118       throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
119     Optional<ChampRelationship> response = Optional.empty();
120
121
122     Stream<ChampRelationship> s;
123     Map<String, Object> filter = new HashMap<>();
124     filter.put(KEY_NAME, uuid);
125
126     s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction));
127     Object[] objs = s.toArray();
128     if (objs.length == 0) {
129       return response;
130     }
131     response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(),
132         Optional.ofNullable(transaction));
133     return response;
134   }
135
136 }