2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.parsers.relationship;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.onap.aai.config.SpringContextAware;
26 import org.onap.aai.edges.EdgeIngestor;
27 import org.onap.aai.edges.EdgeRuleQuery;
28 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
29 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.*;
32 import org.onap.aai.setup.SchemaVersions;
34 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
35 import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException;
36 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
37 import org.onap.aai.parsers.uri.URIParser;
38 import org.onap.aai.schema.enums.ObjectMetadata;
39 import org.onap.aai.edges.enums.AAIDirection;
40 import org.onap.aai.edges.EdgeRule;
41 import org.onap.aai.edges.enums.EdgeType;
42 import org.springframework.context.ApplicationContext;
44 import javax.ws.rs.core.UriBuilder;
45 import java.io.UnsupportedEncodingException;
47 import java.net.URISyntaxException;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Optional;
54 * The Class RelationshipToURI.
56 public class RelationshipToURI {
58 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(RelationshipToURI.class);
60 private Introspector relationship = null;
62 private Loader loader = null;
64 private ModelType modelType = null;
66 private EdgeIngestor edgeRules = null;
68 private URI uri = null;
70 private SchemaVersions schemaVersions;
73 * Instantiates a new relationship to URI.
75 * @param loader the loader
76 * @param relationship the relationship
77 * @throws UnsupportedEncodingException the unsupported encoding exception
78 * @throws AAIException the AAI exception
80 public RelationshipToURI(Loader loader, Introspector relationship) throws UnsupportedEncodingException, AAIException {
81 this.relationship = relationship;
82 this.modelType = relationship.getModelType();
84 this.initEdgeIngestor();
89 protected void initEdgeIngestor() {
90 //TODO proper spring wiring, but that requires a lot of refactoring so for now we have this
91 ApplicationContext ctx = SpringContextAware.getApplicationContext();
92 edgeRules = ctx.getBean(EdgeIngestor.class);
93 schemaVersions = (SchemaVersions) ctx.getBean("schemaVersions");
100 * @throws UnsupportedEncodingException the unsupported encoding exception
101 * @throws AAIException the AAI exception
103 protected void parse() throws AAIException {
104 String relatedLink = (String)relationship.getValue("related-link");
105 Optional<URI> result;
107 if (loader.getVersion().compareTo(schemaVersions.getRelatedLinkVersion()) >= 0) {
108 result = processRelatedLink(relatedLink);
109 if (!result.isPresent()) {
110 result = processRelationshipData();
113 result = processRelationshipData();
114 if (!result.isPresent()) {
115 result = processRelatedLink(relatedLink);
118 if (result.isPresent()) {
119 this.uri = result.get();
121 throw new AAIIdentityMapParseException("nothing to parse");
123 } catch (AAIException e) {
125 } catch (Exception e) {
126 throw new AAIIdentityMapParseException("Could not parse relationship-list object: " + e.getMessage(), e);
131 private Optional<URI> processRelationshipData() throws AAIException, UnsupportedEncodingException {
132 Optional<URI> result = Optional.empty();
133 StringBuilder uriBuilder = new StringBuilder();
134 List<Object> data = (List<Object>)relationship.getValue("relationship-data");
135 Introspector wrapper;
140 String topLevelType = null;
142 HashMap<String, Introspector> map = new HashMap<>();
143 for (Object datum : data) {
144 wrapper = IntrospectorFactory.newInstance(modelType, datum);
145 key = (String)wrapper.getValue("relationship-key");
146 value = (String)wrapper.getValue("relationship-value");
147 split = key.split("\\.");
148 if (split == null || split.length != 2) {
149 throw new AAIIdentityMapParseException("incorrect format for key must be of the form {node-type}.{property-name}");
153 objectType = split[0];
154 propertyName = split[1];
157 Introspector wrappedObj = loader.introspectorFromName(objectType);
159 if (!wrappedObj.hasProperty(propertyName)) {
160 throw new AAIIdentityMapParseException("invalid property name in map: " + propertyName);
162 if (map.containsKey(objectType)) {
163 wrappedObj = map.get(objectType);
165 map.put(objectType, wrappedObj);
167 if (wrappedObj.getValue(propertyName) == null) {
168 wrappedObj.setValue(propertyName, value);
170 throw new AmbiguousMapAAIException("cannot determine where key/value goes: " + propertyName + "/" + value);
173 if (wrappedObj.getMetadata(ObjectMetadata.NAMESPACE) != null) {
174 if (topLevelType == null) {
175 topLevelType = objectType;
176 } else if (!topLevelType.equals(objectType)){
177 throw new AmbiguousMapAAIException("found two top level nodes of different types: " + topLevelType + " and " + objectType);
180 } catch (AAIUnknownObjectException e) {
181 throw new AAIIdentityMapParseException("invalid object name in map: " + objectType, e);
185 if (!map.isEmpty()) {
186 String startType = (String)relationship.getValue("related-to");
187 List<String> nodeTypes = new ArrayList<>();
188 nodeTypes.addAll(map.keySet());
190 String displacedType;
191 for (int i = 0; i < nodeTypes.size(); i++) {
192 if (nodeTypes.get(i).equals(startType)) {
193 displacedType = nodeTypes.set(nodeTypes.size() - 1, startType);
194 nodeTypes.set(i, displacedType);
198 sortRelationships(nodeTypes, startType, 1);
199 int startTypeIndex = nodeTypes.indexOf(startType);
200 int topLevelIndex = 0;
201 if (topLevelType != null) {
202 topLevelIndex = nodeTypes.indexOf(topLevelType);
204 //remove additional types not needed if they are there
205 List<String> nodeTypesSubList = nodeTypes;
206 if (topLevelIndex != 0) {
207 nodeTypesSubList = nodeTypes.subList(topLevelIndex, startTypeIndex+1);
209 for (String type : nodeTypesSubList) {
210 uriBuilder.append(map.get(type).getURI());
212 if (!nodeTypesSubList.isEmpty()) {
213 result = Optional.of(UriBuilder.fromPath(uriBuilder.toString()).build());
219 private Optional<URI> processRelatedLink(String relatedLink) throws URISyntaxException, UnsupportedEncodingException, AAIIdentityMapParseException {
220 Optional<URI> result = Optional.empty();
221 if (relatedLink != null) {
222 URI resultUri = new URI(relatedLink);
223 String path = resultUri.toString();
224 resultUri = UriBuilder.fromPath(resultUri.getRawPath()).build();
225 URIParser uriParser = new URIParser(this.loader, resultUri);
227 uriParser.validate();
228 } catch (AAIException e) {
229 throw new AAIIdentityMapParseException("related link is invalid: " + relatedLink, e);
231 result = Optional.of(resultUri);
238 * Sort relationships.
240 * @param data the data
241 * @param startType the start type
243 * @return true, if successful
244 * @throws AAIException
246 private boolean sortRelationships(List<String> data, String startType, int i) throws AAIException {
248 if (i == data.size()) {
253 String displacedObject;
256 for (j = (data.size() - i) - 1; j >= 0; j--) {
257 objectType = data.get(j);
259 rule = edgeRules.getRule(new EdgeRuleQuery.Builder(startType, objectType).edgeType(EdgeType.TREE).build());
260 direction = rule.getDirection();
261 if (direction != null) {
262 if ((rule.getContains().equals(AAIDirection.OUT.toString()) && direction.equals(Direction.IN)) || (rule.getContains().equals(AAIDirection.IN.toString()) && direction.equals(Direction.OUT))) {
263 displacedObject = data.set((data.size() - i) - 1, data.get(j));
264 data.set(j, displacedObject);
265 if (sortRelationships(data, objectType, i+1)) {
268 //continue to process
272 } catch (AAIException | EdgeRuleNotFoundException | AmbiguousRuleChoiceException e ) {
273 //ignore exceptions generated
287 public URI getUri() {