2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.champ.service;
23 import org.onap.aai.champcore.ChampGraph;
24 import org.onap.aai.champcore.ChampTransaction;
25 import org.onap.aai.champcore.exceptions.ChampTransactionException;
26 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
27 import org.onap.aai.champcore.model.ChampElement;
28 import org.onap.aai.champcore.model.ChampObject;
29 import org.onap.aai.champcore.model.ChampRelationship;
30 import org.onap.champ.exception.ChampServiceException;
31 import org.onap.champ.util.ChampProperties;
32 import org.onap.champ.util.ChampServiceConstants;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Optional;
39 import java.util.stream.Stream;
41 public class ChampUUIDService {
42 private ChampGraph graphImpl;
43 private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME);
46 public ChampUUIDService(ChampGraph graphImpl) {
47 this.graphImpl = graphImpl;
50 public List populateUUIDKey(List<ChampElement> elements) {
52 List response = new ArrayList();
53 for (ChampElement e : elements) {
54 ChampElement item = populateUUIDKey(e);
64 public ChampElement populateUUIDKey(ChampElement e) {
66 ChampElement response = null;
69 if (e.asObject().getProperty(KEY_NAME).isPresent()) {
70 response = (ChampObject.create().from(e.asObject())
71 .withKey(e.asObject().getProperty(KEY_NAME).get().toString()).build());
74 if (e.asRelationship().getProperty(KEY_NAME).isPresent()
75 && e.asRelationship().getSource().getProperty(KEY_NAME).isPresent()
76 && e.asRelationship().getTarget().getProperty(KEY_NAME).isPresent()) {
77 ChampObject source = ChampObject.create().from(e.asRelationship().getSource())
78 .withKey(e.asRelationship().getSource().getProperty(KEY_NAME).get().toString()).build();
79 ChampObject target = ChampObject.create().from(e.asRelationship().getTarget())
80 .withKey(e.asRelationship().getTarget().getProperty(KEY_NAME).get().toString()).build();
81 ChampRelationship rel = new ChampRelationship.Builder(source, target, e.asRelationship().getType())
82 .key(e.asRelationship().getProperty(KEY_NAME).get().toString())
83 .properties(e.asRelationship().getProperties()).build();
94 public void populateUUIDProperty(ChampElement e, String uuid) {
95 e.getProperties().put(KEY_NAME, uuid);
99 public Optional<ChampObject> getObjectbyUUID(String uuid, ChampTransaction transaction)
100 throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
101 Optional<ChampObject> response = Optional.empty();
103 Stream<ChampObject> s;
104 Map<String, Object> filter = new HashMap<>();
105 filter.put(KEY_NAME, uuid);
107 s = graphImpl.queryObjects(filter, Optional.ofNullable(transaction));
108 Object[] objs = s.toArray();
109 if (objs.length == 0) {
112 response = graphImpl.retrieveObject(((ChampObject) objs[0]).getKey().get(), Optional.ofNullable(transaction));
116 public Optional<ChampRelationship> getRelationshipbyUUID(String uuid, ChampTransaction transaction)
117 throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
118 Optional<ChampRelationship> response = Optional.empty();
121 Stream<ChampRelationship> s;
122 Map<String, Object> filter = new HashMap<>();
123 filter.put(KEY_NAME, uuid);
125 s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction));
126 Object[] objs = s.toArray();
127 if (objs.length == 0) {
130 response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(),
131 Optional.ofNullable(transaction));