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.uri;
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.introspection.Introspector;
24 import org.onap.aai.introspection.Loader;
25 import org.onap.aai.schema.enums.ObjectMetadata;
26 import org.onap.aai.edges.enums.EdgeType;
27 import org.onap.aai.setup.SchemaVersion;
29 import javax.ws.rs.core.MultivaluedMap;
30 import java.io.UnsupportedEncodingException;
32 import java.util.HashMap;
33 import java.util.List;
36 * Given a URI this class returns an object, or series of nested objects
37 * with their keys populated based off the values in the URI.
39 * It populates the keys in the order they are listed in the model.
41 public class URIToObject implements Parsable {
44 private Introspector topEntity = null;
46 private String topEntityName = null;
48 private String entityName = null;
50 private Introspector entity = null;
52 private Introspector previous = null;
54 private List<Object> parentList = null;
56 private SchemaVersion version = null;
57 private Loader loader = null;
58 private final HashMap<String, Introspector> relatedObjects;
61 * Instantiates a new URI to object.
63 * @param loader the loader
65 * @throws IllegalArgumentException the illegal argument exception
66 * @throws AAIException the AAI exception
67 * @throws UnsupportedEncodingException the unsupported encoding exception
69 public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
71 URIParser parser = new URIParser(loader, uri);
72 this.relatedObjects = new HashMap<>();
75 this.loader = parser.getLoader();
76 this.version = loader.getVersion();
78 public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
80 URIParser parser = new URIParser(loader, uri);
81 this.relatedObjects = relatedObjects;
84 this.loader = parser.getLoader();
85 this.version = loader.getVersion();
93 public void processNamespace(Introspector obj) {
101 public String getCloudRegionTransform() {
109 public boolean useOriginalLoader() {
110 // TODO Auto-generated method stub
115 * Gets the top entity.
117 * @return the top entity
119 public Introspector getTopEntity() {
120 return this.topEntity;
128 public Introspector getEntity() {
133 * Gets the parent list.
135 * @return the parent list
137 public List<Object> getParentList() {
138 return this.parentList;
142 * Gets the entity name.
144 * @return the entity name
146 public String getEntityName() {
147 return this.entityName;
151 * Gets the top entity name.
153 * @return the top entity name
155 public String getTopEntityName() {
156 return this.topEntityName;
160 * Gets the object version.
162 * @return the object version
164 public SchemaVersion getObjectVersion() {
165 return this.loader.getVersion();
167 public Loader getLoader() {
171 public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
172 throws AAIException {
174 if (this.entityName == null) {
175 this.topEntityName = obj.getDbName();
176 this.topEntity = obj;
178 this.entityName = obj.getDbName();
180 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
181 this.parentList.add(entity.getUnderlyingObject());
183 for (String key : uriKeys.keySet()) {
184 entity.setValue(key, uriKeys.getFirst(key));
187 if (relatedObjects.containsKey(entity.getObjectId())) {
188 Introspector relatedObject = relatedObjects.get(entity.getObjectId());
189 String nameProp = relatedObject.getMetadata(ObjectMetadata.NAME_PROPS);
190 if (nameProp == null) {
193 if (nameProp != null && !nameProp.equals("")) {
194 String[] nameProps = nameProp.split(",");
195 for (String prop : nameProps) {
196 entity.setValue(prop, relatedObject.getValue(prop));
200 } catch (UnsupportedEncodingException e) {
202 this.previous = entity;
205 public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
206 boolean isFinalContainer) throws AAIException {
209 if (this.entity != null) {
210 this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
213 this.topEntity = obj;