1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*-
* ============LICENSE_START=======================================================
* org.openecomp.aai
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.aai.dbgraphmap;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.map.MultiValueMap;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.dynamic.DynamicType;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.openecomp.aai.dbgen.DbMeth;
import org.openecomp.aai.dbgraphgen.DbEdgeGroup;
import org.openecomp.aai.domain.model.AAIResources;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.extensions.AAIExtensionMap;
import org.openecomp.aai.util.AAIConfig;
import org.openecomp.aai.util.AAIConstants;
import org.openecomp.aai.util.RestURL;
import com.thinkaurelius.titan.core.TitanTransaction;
import com.thinkaurelius.titan.core.TitanVertex;
public class RelationshipGraph {
/**
* this method processes the one relationship for the startVertex that is
* sent.
*
* @param g the g
* @param startVertex the start vertex
* @param jaxbContext the jaxb context
* @param rel the rel
* @param aaiExtMap the aai ext map
* @throws AAIException the AAI exception
*/
public static void updRelationship(TitanTransaction g, TitanVertex startVertex,
DynamicJAXBContext jaxbContext,
DynamicEntity rel,
AAIExtensionMap aaiExtMap)
throws AAIException {
String apiVersion = aaiExtMap.getApiVersion();
String transId = aaiExtMap.getTransId();
String fromAppId = aaiExtMap.getFromAppId();
MultiValueMap relatedNodesMap = new MultiValueMap();
if( rel != null ){
HashMap<String, Object> propFilterHash = new HashMap<String, Object>();
poplatePropertyHashWithRelData(rel, apiVersion, propFilterHash);
String relNodeType = (String)rel.get("relatedTo");
relatedNodesMap.put(relNodeType, propFilterHash);
}
DbEdgeGroup.replaceEdgeGroup(transId, fromAppId, g, startVertex,
"ONLY_PASSED_COUSINS_REL", relatedNodesMap, apiVersion);
}
/**
* Poplate property hash with rel data.
*
* @param rel the rel
* @param apiVersion the api version
* @param propFilterHash the prop filter hash
* @throws AAIException the AAI exception
*/
private static void poplatePropertyHashWithRelData(DynamicEntity rel, String apiVersion,
HashMap<String, Object> propFilterHash) throws AAIException {
for( DynamicEntity relData: (List<DynamicEntity>)rel.get("relationshipData")) {
String prop = ((String)relData.get("relationshipKey")).toLowerCase().trim();
propFilterHash.put(prop, ((String)relData.get("relationshipValue")).trim());
}
}
/**
* this method gets any relationships for the startVertex being processed
* and sets the related-link.
*
* @param g the g
* @param startVertex the start vertex
* @param apiVersion the api version
* @param aaiExtMap the aai ext map
* @return the relationships
* @throws AAIException the AAI exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
public static DynamicEntity getRelationships(TitanTransaction g, TitanVertex startVertex,
String apiVersion, AAIExtensionMap aaiExtMap)
throws AAIException, UnsupportedEncodingException {
DynamicType relationshipsType = null;
DynamicType relationshipType = null;
DynamicType relationshipDataType = null;
DynamicType relatedToPropertyType = null;
Boolean setRelatedToProperty = true;
AAIResources aaiResources = org.openecomp.aai.ingestModel.IngestModelMoxyOxm.aaiResourceContainer
.get(apiVersion);
DynamicJAXBContext jaxbContext = aaiResources.getJaxbContext();
//String apiVersion = aaiExtMap.getApiVersion();
String transId = aaiExtMap.getTransId();
String fromAppId = aaiExtMap.getFromAppId();
HashMap <String, String> vidToNodeTypeHash = new HashMap <String, String>();
HashMap <String, TitanVertex> vidToVertexHash = new HashMap <String, TitanVertex>();
if ("v2".equals( apiVersion)) {
relationshipsType = jaxbContext.getDynamicType("inventory.aai.openecomp.org.RelationshipList");
relationshipType = jaxbContext.getDynamicType(".org.Relationship");
relationshipDataType = jaxbContext.getDynamicType("inventory.aai.openecomp.org.RelationshipData");
setRelatedToProperty = false;
} else {
relationshipsType = jaxbContext.getDynamicType("inventory.aai.openecomp.org." + apiVersion + ".RelationshipList");
relationshipType = jaxbContext.getDynamicType("inventory.aai.openecomp.org." + apiVersion + ".Relationship");
relationshipDataType = jaxbContext.getDynamicType("inventory.aai.openecomp.org." + apiVersion + ".RelationshipData");
relatedToPropertyType = jaxbContext.getDynamicType("inventory.aai.openecomp.org." + apiVersion + ".RelatedToProperty");
if (relatedToPropertyType == null) {
setRelatedToProperty = false; // some versions do not support this
}
}
DynamicEntity relationships = relationshipsType.newDynamicEntity();
List<DynamicEntity> listOfRelationships = new ArrayList<DynamicEntity>();
DbEdgeGroup.getEdgeGroup(transId,
fromAppId,
g,
startVertex,
vidToNodeTypeHash,
vidToVertexHash,
"ONLY_COUSIN_REL",
AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP));
// Convert the found relationships to a RelationshipList DynamicEntity
for( Map.Entry<String, TitanVertex> entry : vidToVertexHash.entrySet() ){
List<DynamicEntity> relationshipDataList = new ArrayList<DynamicEntity>();
List<DynamicEntity> relatedToPropertyList = new ArrayList<DynamicEntity>();
DynamicEntity relationship = relationshipType.newDynamicEntity();
TitanVertex relNode = entry.getValue();
String relNodeVid = entry.getKey();
String relNodeType = vidToNodeTypeHash.get(relNodeVid);
String relNodeURL = RestURL.get(g, relNode, apiVersion);
HashMap <String, Object> nodeKeyPropsHash = RestURL.getKeyHashes(g, relNode, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP));
Iterator <Map.Entry<String,Object>>keyIterator = nodeKeyPropsHash.entrySet().iterator();
while( keyIterator.hasNext() ){
DynamicEntity relationshipData = relationshipDataType.newDynamicEntity();
Map.Entry <String,Object>pair = (Map.Entry<String,Object>)keyIterator.next();
String key = pair.getKey();
if (!key.contains(".")) {
key = relNodeType + "." + key;
}
String value = "";
if( pair.getValue() != null ){
value = pair.getValue().toString();
}
relationshipData.set("relationshipKey", key);
relationshipData.set("relationshipValue", value);
relationshipDataList.add(relationshipData);
}
if (setRelatedToProperty) {
HashMap <String, Object> nodeNamePropsHash = DbMeth.getNodeNamePropHash(transId, fromAppId, g, relNode, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP));
Iterator <Map.Entry<String,Object>>nameIterator = nodeNamePropsHash.entrySet().iterator();
while( nameIterator.hasNext() ){
DynamicEntity relatedToProperty = relatedToPropertyType.newDynamicEntity();
Map.Entry <String,Object>pair = (Map.Entry<String,Object>)nameIterator.next();
String key = pair.getKey();
if (!key.contains(".")) {
key = relNodeType + "." + key;
}
String value = "";
if( pair.getValue() != null ){
value = pair.getValue().toString();
}
relatedToProperty.set("propertyKey", key);
relatedToProperty.set("propertyValue", value);
relatedToPropertyList.add(relatedToProperty);
}
relationship.set("relatedToProperty", relatedToPropertyList);
}
relationship.set("relatedTo", relNodeType);
relationship.set("relatedLink", relNodeURL);
relationship.set("relationshipData", relationshipDataList);
listOfRelationships.add(relationship);
}
relationships.set("relationship", listOfRelationships);
return relationships;
}
/**
* this method processes any relationships for the startVertex being
* processed.
*
* @param g the g
* @param startVertex the start vertex
* @param jaxbContext the jaxb context
* @param relationshipList the relationship list
* @param aaiExtMap the aai ext map
* @throws AAIException the AAI exception
*/
public static void updRelationships(TitanTransaction g, TitanVertex startVertex,
DynamicJAXBContext jaxbContext,
DynamicEntity relationshipList,
AAIExtensionMap aaiExtMap)
throws AAIException {
String apiVersion = aaiExtMap.getApiVersion();
String transId = aaiExtMap.getTransId();
String fromAppId = aaiExtMap.getFromAppId();
MultiValueMap relatedNodesMap = new MultiValueMap();
if (relationshipList != null) {
if( relationshipList.get("relationship") != null ){
List <DynamicEntity> relListTmp = relationshipList.get("relationship");
for( DynamicEntity rel: relListTmp) {
HashMap<String, Object> propFilterHash = new HashMap<String, Object>();
poplatePropertyHashWithRelData(rel, apiVersion, propFilterHash);
String relNodeType = (String)rel.get("relatedTo");
relatedNodesMap.put(relNodeType, propFilterHash);
}
}
DbEdgeGroup.replaceEdgeGroup(transId, fromAppId, g, startVertex,
"ALL_COUSIN_REL", relatedNodesMap, apiVersion);
}
}
/**
* this method deletes the relationship sent in for the startVertex being
* processed.
*
* @param g the g
* @param startVertex the start vertex
* @param jaxbContext the jaxb context
* @param rel the rel
* @param aaiExtMap the aai ext map
* @throws AAIException the AAI exception
*/
public static void delRelationship(TitanTransaction g, TitanVertex startVertex,
DynamicJAXBContext jaxbContext,
DynamicEntity rel,
AAIExtensionMap aaiExtMap)
throws AAIException {
String apiVersion = aaiExtMap.getApiVersion();
String transId = aaiExtMap.getTransId();
String fromAppId = aaiExtMap.getFromAppId();
MultiValueMap relatedNodesMap = new MultiValueMap();
if( rel != null ){
HashMap<String, Object> propFilterHash = new HashMap<String, Object>();
poplatePropertyHashWithRelData(rel, apiVersion, propFilterHash);
String relNodeType = (String)rel.get("relatedTo");
relatedNodesMap.put(relNodeType, propFilterHash);
}
DbEdgeGroup.deleteEdgeGroup(transId, fromAppId, g, startVertex,
relatedNodesMap, apiVersion);
}
}
|