[AAI-2175] Change aai champ container processes to run as non-root on the host
[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-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
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  */
21 package org.onap.champ.service;
22
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.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.champ.ChampRESTAPI;
33 import org.onap.champ.exception.ChampServiceException;
34 import org.onap.champ.service.logging.ChampMsgs;
35 import org.onap.champ.util.ChampProperties;
36 import org.onap.champ.util.ChampServiceConstants;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Optional;
43 import java.util.stream.Stream;
44
45 public class ChampUUIDService {
46   private ChampGraph graphImpl;
47   private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME);
48   private Logger logger = LoggerFactory.getInstance().getLogger(ChampUUIDService.class);
49
50   public ChampUUIDService(ChampGraph graphImpl) {
51     this.graphImpl = graphImpl;
52   }
53
54   public List populateUUIDKey(List<ChampElement> elements) {
55     {
56       List response = new ArrayList();
57       for (ChampElement e : elements) {
58         ChampElement item = populateUUIDKey(e);
59         if (item != null) {
60           response.add(item);
61         }
62       }
63       return response;
64     }
65
66   }
67
68   public ChampElement populateUUIDKey(ChampElement e) {
69     {
70       ChampElement response = null;
71
72       if (e.isObject()) {
73         if (e.asObject().getProperty(KEY_NAME).isPresent()) {
74           response = (ChampObject.create().from(e.asObject())
75               .withKey(e.asObject().getProperty(KEY_NAME).get().toString()).build());
76         }
77       } else {
78         if (e.asRelationship().getProperty(KEY_NAME).isPresent()
79             && e.asRelationship().getSource().getProperty(KEY_NAME).isPresent()
80             && e.asRelationship().getTarget().getProperty(KEY_NAME).isPresent()) {
81           ChampObject source = ChampObject.create().from(e.asRelationship().getSource())
82               .withKey(e.asRelationship().getSource().getProperty(KEY_NAME).get().toString()).build();
83           ChampObject target = ChampObject.create().from(e.asRelationship().getTarget())
84               .withKey(e.asRelationship().getTarget().getProperty(KEY_NAME).get().toString()).build();
85           ChampRelationship rel = new ChampRelationship.Builder(source, target, e.asRelationship().getType())
86               .key(e.asRelationship().getProperty(KEY_NAME).get().toString())
87               .properties(e.asRelationship().getProperties()).build();
88           response = rel;
89         }
90
91       }
92
93       return response;
94     }
95
96   }
97
98   public void populateUUIDProperty(ChampElement e, String uuid) {
99     e.getProperties().put(KEY_NAME, uuid);
100   }
101
102
103   public Optional<ChampObject> getObjectbyUUID(String uuid, ChampTransaction transaction)
104       throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
105     Optional<ChampObject> response = Optional.empty();
106
107     Stream<ChampObject> s;
108     Map<String, Object> filter = new HashMap<>();
109     filter.put(KEY_NAME, uuid);
110
111     s = graphImpl.queryObjects(filter, Optional.ofNullable(transaction));
112     Object[] objs = s.toArray();
113     if (objs.length == 0) {
114       return response;
115     }
116     response = graphImpl.retrieveObject(((ChampObject) objs[0]).getKey().get(), Optional.ofNullable(transaction));
117     return response;
118   }
119
120   public Optional<ChampRelationship> getRelationshipbyUUID(String uuid, ChampTransaction transaction)
121       throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException {
122     Optional<ChampRelationship> response = Optional.empty();
123
124     Stream<ChampRelationship> s;
125     Map<String, Object> filter = new HashMap<>();
126     filter.put(KEY_NAME, uuid);
127
128     s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction));
129     
130     Object[] objs = s.toArray();
131     if (objs.length == 0) {
132       return response;
133     }
134     
135     response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(),
136         Optional.ofNullable(transaction));
137     
138     return response;
139   }
140
141 }