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.uri;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Introspector;
28 import org.onap.aai.introspection.Loader;
29 import org.onap.aai.introspection.Version;
30 import org.onap.aai.schema.enums.ObjectMetadata;
31 import org.onap.aai.serialization.db.EdgeType;
33 import javax.ws.rs.core.MultivaluedMap;
34 import java.io.UnsupportedEncodingException;
36 import java.util.HashMap;
37 import java.util.List;
40 * Given a URI this class returns an object, or series of nested objects
41 * with their keys populated based off the values in the URI.
43 * It populates the keys in the order they are listed in the model.
48 public class URIToObject implements Parsable {
49 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(URIToObject.class);
51 private Introspector topEntity = null;
53 private String topEntityName = null;
55 private String entityName = null;
57 private Introspector entity = null;
59 private Introspector previous = null;
61 private List<Object> parentList = null;
63 private Version version = null;
64 private Loader loader = null;
65 private final HashMap<String, Introspector> relatedObjects;
68 * Instantiates a new URI to object.
70 * @param loader the loader
72 * @throws IllegalArgumentException the illegal argument exception
73 * @throws AAIException the AAI exception
74 * @throws UnsupportedEncodingException the unsupported encoding exception
76 public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
78 URIParser parser = new URIParser(loader, uri);
79 this.relatedObjects = new HashMap<>();
82 this.loader = parser.getLoader();
83 this.version = loader.getVersion();
85 public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
87 URIParser parser = new URIParser(loader, uri);
88 this.relatedObjects = relatedObjects;
91 this.loader = parser.getLoader();
92 this.version = loader.getVersion();
100 public void processNamespace(Introspector obj) {
108 public String getCloudRegionTransform() {
116 public boolean useOriginalLoader() {
117 // TODO Auto-generated method stub
122 * Gets the top entity.
124 * @return the top entity
126 public Introspector getTopEntity() {
127 return this.topEntity;
135 public Introspector getEntity() {
140 * Gets the parent list.
142 * @return the parent list
144 public List<Object> getParentList() {
145 return this.parentList;
149 * Gets the entity name.
151 * @return the entity name
153 public String getEntityName() {
154 return this.entityName;
158 * Gets the top entity name.
160 * @return the top entity name
162 public String getTopEntityName() {
163 return this.topEntityName;
167 * Gets the object version.
169 * @return the object version
171 public Version getObjectVersion() {
172 return this.loader.getVersion();
174 public Loader getLoader() {
178 public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
179 throws AAIException {
181 if (this.entityName == null) {
182 this.topEntityName = obj.getDbName();
183 this.topEntity = obj;
185 this.entityName = obj.getDbName();
187 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
188 this.parentList.add(entity.getUnderlyingObject());
190 for (String key : uriKeys.keySet()) {
191 entity.setValue(key, uriKeys.getFirst(key));
194 if (relatedObjects.containsKey(entity.getObjectId())) {
195 Introspector relatedObject = relatedObjects.get(entity.getObjectId());
196 String nameProp = relatedObject.getMetadata(ObjectMetadata.NAME_PROPS);
197 if (nameProp == null) {
200 if (nameProp != null && !nameProp.equals("")) {
201 String[] nameProps = nameProp.split(",");
202 for (String prop : nameProps) {
203 entity.setValue(prop, relatedObject.getValue(prop));
207 } catch (UnsupportedEncodingException e) {
208 LOGGER.error(e.getMessage(),e);
210 this.previous = entity;
213 public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
214 boolean isFinalContainer) throws AAIException {
217 if (this.entity != null) {
218 this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
221 this.topEntity = obj;