2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.parsers.uri;
23 import org.openecomp.aai.exceptions.AAIException;
24 import org.openecomp.aai.introspection.Introspector;
25 import org.openecomp.aai.introspection.Loader;
26 import org.openecomp.aai.introspection.Version;
27 import org.openecomp.aai.schema.enums.ObjectMetadata;
28 import org.openecomp.aai.serialization.db.EdgeType;
30 import javax.ws.rs.core.MultivaluedMap;
31 import java.io.UnsupportedEncodingException;
33 import java.util.HashMap;
34 import java.util.List;
37 * Given a URI this class returns an object, or series of nested objects
38 * with their keys populated based off the values in the URI.
40 * It populates the keys in the order they are listed in the model.
45 public class URIToObject implements Parsable {
48 private Introspector topEntity = null;
50 private String topEntityName = null;
52 private String entityName = null;
54 private Introspector entity = null;
56 private Introspector previous = null;
58 private List<Object> parentList = null;
60 private Version version = null;
61 private Loader loader = null;
62 private final HashMap<String, Introspector> relatedObjects;
65 * Instantiates a new URI to object.
67 * @param loader the loader
69 * @throws IllegalArgumentException the illegal argument exception
70 * @throws AAIException the AAI exception
71 * @throws UnsupportedEncodingException the unsupported encoding exception
73 public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
75 URIParser parser = new URIParser(loader, uri);
76 this.relatedObjects = new HashMap<>();
79 this.loader = parser.getLoader();
80 this.version = loader.getVersion();
82 public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
84 URIParser parser = new URIParser(loader, uri);
85 this.relatedObjects = relatedObjects;
88 this.loader = parser.getLoader();
89 this.version = loader.getVersion();
97 public void processNamespace(Introspector obj) {
105 public String getCloudRegionTransform() {
113 public boolean useOriginalLoader() {
114 // TODO Auto-generated method stub
119 * Gets the top entity.
121 * @return the top entity
123 public Introspector getTopEntity() {
124 return this.topEntity;
132 public Introspector getEntity() {
137 * Gets the parent list.
139 * @return the parent list
141 public List<Object> getParentList() {
142 return this.parentList;
146 * Gets the entity name.
148 * @return the entity name
150 public String getEntityName() {
151 return this.entityName;
155 * Gets the top entity name.
157 * @return the top entity name
159 public String getTopEntityName() {
160 return this.topEntityName;
164 * Gets the object version.
166 * @return the object version
168 public Version getObjectVersion() {
169 return this.loader.getVersion();
171 public Loader getLoader() {
175 public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
176 throws AAIException {
178 if (this.entityName == null) {
179 this.topEntityName = obj.getDbName();
180 this.topEntity = obj;
182 this.entityName = obj.getDbName();
184 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
185 this.parentList.add(entity.getUnderlyingObject());
187 for (String key : uriKeys.keySet()) {
188 entity.setValue(key, uriKeys.getFirst(key));
191 if (relatedObjects.containsKey(entity.getObjectId())) {
192 Introspector relatedObject = relatedObjects.get(entity.getObjectId());
193 String nameProp = relatedObject.getMetadata(ObjectMetadata.NAME_PROPS);
194 if (nameProp == null) {
197 if (nameProp != null && !nameProp.equals("")) {
198 String[] nameProps = nameProp.split(",");
199 for (String prop : nameProps) {
200 entity.setValue(prop, relatedObject.getValue(prop));
204 } catch (UnsupportedEncodingException e) {
206 this.previous = entity;
209 public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
210 boolean isFinalContainer) throws AAIException {
213 if (this.entity != null) {
214 this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
217 this.topEntity = obj;