2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.parsers.relationship;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.onap.aai.exceptions.AAIException;
28 import org.onap.aai.introspection.*;
29 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
30 import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException;
31 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
32 import org.onap.aai.parsers.uri.URIParser;
33 import org.onap.aai.schema.enums.ObjectMetadata;
34 import org.onap.aai.serialization.db.AAIDirection;
35 import org.onap.aai.serialization.db.EdgeRule;
36 import org.onap.aai.serialization.db.EdgeRules;
37 import org.onap.aai.serialization.db.EdgeType;
38 import org.onap.aai.workarounds.LegacyURITransformer;
40 import javax.ws.rs.core.UriBuilder;
41 import java.io.UnsupportedEncodingException;
43 import java.net.URISyntaxException;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Optional;
50 * The Class RelationshipToURI.
52 public class RelationshipToURI {
54 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(RelationshipToURI.class);
56 private Introspector relationship = null;
58 private Loader loader = null;
60 private ModelType modelType = null;
62 private EdgeRules edgeRules = null;
64 private URI uri = null;
66 private LegacyURITransformer urlTransform = null;
69 * Instantiates a new relationship to URI.
71 * @param loader the loader
72 * @param relationship the relationship
73 * @throws UnsupportedEncodingException the unsupported encoding exception
74 * @throws AAIException the AAI exception
76 public RelationshipToURI(Loader loader, Introspector relationship) throws UnsupportedEncodingException, AAIException {
77 this.relationship = relationship;
78 this.modelType = relationship.getModelType();
79 this.edgeRules = EdgeRules.getInstance();
81 this.urlTransform = LegacyURITransformer.getInstance();
91 * @throws UnsupportedEncodingException the unsupported encoding exception
92 * @throws AAIException the AAI exception
94 protected void parse() throws AAIException {
95 String relatedLink = (String)relationship.getValue("related-link");
98 if (loader.getVersion().compareTo(Version.v10) >= 0) {
99 result = processRelatedLink(relatedLink);
100 if (!result.isPresent()) {
101 result = processRelationshipData();
104 result = processRelationshipData();
105 if (!result.isPresent()) {
106 result = processRelatedLink(relatedLink);
109 if (result.isPresent()) {
110 this.uri = result.get();
112 throw new AAIIdentityMapParseException("nothing to parse");
114 } catch (AAIException e) {
116 } catch (Exception e) {
117 throw new AAIIdentityMapParseException("Could not parse relationship-list object: " + e.getMessage(), e);
122 private Optional<URI> processRelationshipData() throws AAIException, UnsupportedEncodingException {
123 Optional<URI> result = Optional.empty();
124 StringBuilder uriBuilder = new StringBuilder();
125 List<Object> data = (List<Object>)relationship.getValue("relationship-data");
126 Introspector wrapper;
131 String topLevelType = null;
133 HashMap<String, Introspector> map = new HashMap<>();
134 for (Object datum : data) {
135 wrapper = IntrospectorFactory.newInstance(modelType, datum);
136 key = (String)wrapper.getValue("relationship-key");
137 value = (String)wrapper.getValue("relationship-value");
138 split = key.split("\\.");
139 if (split == null || split.length != 2) {
140 throw new AAIIdentityMapParseException("incorrect format for key must be of the form {node-type}.{property-name}");
144 objectType = split[0];
145 propertyName = split[1];
148 Introspector wrappedObj = loader.introspectorFromName(objectType);
150 if (!wrappedObj.hasProperty(propertyName)) {
151 throw new AAIIdentityMapParseException("invalid property name in map: " + propertyName);
153 if (map.containsKey(objectType)) {
154 wrappedObj = map.get(objectType);
156 map.put(objectType, wrappedObj);
158 if (wrappedObj.getValue(propertyName) == null) {
159 wrappedObj.setValue(propertyName, value);
161 throw new AmbiguousMapAAIException("cannot determine where key/value goes: " + propertyName + "/" + value);
164 if (wrappedObj.getMetadata(ObjectMetadata.NAMESPACE) != null) {
165 if (topLevelType == null) {
166 topLevelType = objectType;
167 } else if (!topLevelType.equals(objectType)){
168 throw new AmbiguousMapAAIException("found two top level nodes of different types: " + topLevelType + " and " + objectType);
171 } catch (AAIUnknownObjectException e) {
172 throw new AAIIdentityMapParseException("invalid object name in map: " + objectType, e);
176 if (!map.isEmpty()) {
177 String startType = (String)relationship.getValue("related-to");
178 List<String> nodeTypes = new ArrayList<>();
179 nodeTypes.addAll(map.keySet());
181 String displacedType;
182 for (int i = 0; i < nodeTypes.size(); i++) {
183 if (nodeTypes.get(i).equals(startType)) {
184 displacedType = nodeTypes.set(nodeTypes.size() - 1, startType);
185 nodeTypes.set(i, displacedType);
189 sortRelationships(nodeTypes, startType, 1);
190 int startTypeIndex = nodeTypes.indexOf(startType);
191 int topLevelIndex = 0;
192 if (topLevelType != null) {
193 topLevelIndex = nodeTypes.indexOf(topLevelType);
195 //remove additional types not needed if they are there
196 List<String> nodeTypesSubList = nodeTypes;
197 if (topLevelIndex != 0) {
198 nodeTypesSubList = nodeTypes.subList(topLevelIndex, startTypeIndex+1);
200 for (String type : nodeTypesSubList) {
201 uriBuilder.append(map.get(type).getURI());
203 if (!nodeTypesSubList.isEmpty()) {
204 result = Optional.of(UriBuilder.fromPath(uriBuilder.toString()).build());
210 private Optional<URI> processRelatedLink(String relatedLink) throws URISyntaxException, UnsupportedEncodingException, AAIIdentityMapParseException {
211 Optional<URI> result = Optional.empty();
212 if (relatedLink != null) {
213 URI resultUri = new URI(relatedLink);
214 String path = resultUri.toString();
215 resultUri = UriBuilder.fromPath(resultUri.getRawPath()).build();
216 URIParser uriParser = new URIParser(this.loader, resultUri);
218 uriParser.validate();
219 } catch (AAIException e) {
220 throw new AAIIdentityMapParseException("related link is invalid: " + relatedLink, e);
222 result = Optional.of(resultUri);
229 * Sort relationships.
231 * @param data the data
232 * @param startType the start type
234 * @return true, if successful
235 * @throws AAIException
237 private boolean sortRelationships(List<String> data, String startType, int i) throws AAIException {
239 if (i == data.size()) {
244 String displacedObject;
247 for (j = (data.size() - i) - 1; j >= 0; j--) {
248 objectType = data.get(j);
250 rule = edgeRules.getEdgeRule(EdgeType.TREE, startType, objectType);
251 direction = rule.getDirection();
252 if (direction != null) {
253 if ((rule.getContains().equals(AAIDirection.OUT.toString()) && direction.equals(Direction.IN)) || (rule.getContains().equals(AAIDirection.IN.toString()) && direction.equals(Direction.OUT))) {
254 displacedObject = data.set((data.size() - i) - 1, data.get(j));
255 data.set(j, displacedObject);
256 if (sortRelationships(data, objectType, i+1)) {
259 //continue to process
263 } catch (AAIException e) {
264 //ignore exceptions generated
278 public URI getUri() {