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 java.io.UnsupportedEncodingException;
25 import java.util.HashMap;
26 import java.util.List;
28 import javax.ws.rs.core.MultivaluedMap;
30 import org.openecomp.aai.exceptions.AAIException;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.Loader;
33 import org.openecomp.aai.introspection.Version;
34 import org.openecomp.aai.schema.enums.ObjectMetadata;
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 IllegalArgumentException, 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 IllegalArgumentException, 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 processObject(Introspector obj, MultivaluedMap<String, String> uriKeys) {
99 if (this.entityName == null) {
100 this.topEntityName = obj.getDbName();
101 this.topEntity = obj;
103 this.entityName = obj.getDbName();
105 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
106 this.parentList.add(entity.getUnderlyingObject());
108 for (String key : uriKeys.keySet()) {
109 entity.setValue(key, uriKeys.getFirst(key));
112 if (relatedObjects.containsKey(entity.getObjectId())) {
113 Introspector relatedObject = relatedObjects.get(entity.getObjectId());
114 String nameProp = relatedObject.getMetadata(ObjectMetadata.NAME_PROPS);
115 if (nameProp == null) {
118 if (nameProp != null && !nameProp.equals("")) {
119 String[] nameProps = nameProp.split(",");
120 for (String prop : nameProps) {
121 entity.setValue(prop, relatedObject.getValue(prop));
125 } catch (UnsupportedEncodingException e) {
127 this.previous = entity;
135 public void processContainer(Introspector obj, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) {
139 if (this.entity != null) {
140 this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
143 this.topEntity = obj;
152 public void processNamespace(Introspector obj) {
160 public String getCloudRegionTransform() {
168 public boolean useOriginalLoader() {
169 // TODO Auto-generated method stub
174 * Gets the top entity.
176 * @return the top entity
178 public Introspector getTopEntity() {
179 return this.topEntity;
187 public Introspector getEntity() {
192 * Gets the parent list.
194 * @return the parent list
196 public List<Object> getParentList() {
197 return this.parentList;
201 * Gets the entity name.
203 * @return the entity name
205 public String getEntityName() {
206 return this.entityName;
210 * Gets the top entity name.
212 * @return the top entity name
214 public String getTopEntityName() {
215 return this.topEntityName;
219 * Gets the object version.
221 * @return the object version
223 public Version getObjectVersion() {
224 return this.loader.getVersion();
226 public Loader getLoader() {