6d5d98aec7d9c0823f82db894cc1696634512527
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / uri / URIToObject.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.parsers.uri;
23
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;
32
33 import javax.ws.rs.core.MultivaluedMap;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URI;
36 import java.util.HashMap;
37 import java.util.List;
38
39 /**
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.
42  * 
43  * It populates the keys in the order they are listed in the model.
44  * 
45  
46  *
47  */
48 public class URIToObject implements Parsable {
49         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(URIToObject.class);
50         
51         private Introspector topEntity = null;
52         
53         private String topEntityName = null;
54         
55         private String entityName = null;
56         
57         private Introspector entity = null;
58         
59         private Introspector previous = null;
60         
61         private List<Object> parentList = null;
62         
63         private Version version = null;
64         private Loader loader = null;
65         private final HashMap<String, Introspector> relatedObjects;
66         
67         /**
68          * Instantiates a new URI to object.
69          *
70          * @param loader the loader
71          * @param uri the uri
72          * @throws IllegalArgumentException the illegal argument exception
73          * @throws AAIException the AAI exception
74          * @throws UnsupportedEncodingException the unsupported encoding exception
75          */
76         public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
77                 
78                 URIParser parser = new URIParser(loader, uri);
79                 this.relatedObjects = new HashMap<>();
80                 
81                 parser.parse(this);
82                 this.loader = parser.getLoader();
83                 this.version = loader.getVersion();
84         }
85         public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
86                 
87                 URIParser parser = new URIParser(loader, uri);
88                 this.relatedObjects = relatedObjects;
89
90                 parser.parse(this);
91                 this.loader = parser.getLoader();
92                 this.version = loader.getVersion();
93
94         }
95         
96         /**
97          * @{inheritDoc}
98          */
99         @Override
100         public void processNamespace(Introspector obj) {
101         
102         }
103         
104         /**
105          * @{inheritDoc}
106          */
107         @Override
108         public String getCloudRegionTransform() {
109                 return "add";
110         }
111         
112         /**
113          * @{inheritDoc}
114          */
115         @Override
116         public boolean useOriginalLoader() {
117                 // TODO Auto-generated method stub
118                 return false;
119         }
120
121         /**
122          * Gets the top entity.
123          *
124          * @return the top entity
125          */
126         public Introspector getTopEntity() {
127                 return this.topEntity;
128         }
129         
130         /**
131          * Gets the entity.
132          *
133          * @return the entity
134          */
135         public Introspector getEntity() {
136                 return this.entity;
137         }
138         
139         /**
140          * Gets the parent list.
141          *
142          * @return the parent list
143          */
144         public List<Object> getParentList() {
145                 return this.parentList;
146         }
147         
148         /**
149          * Gets the entity name.
150          *
151          * @return the entity name
152          */
153         public String getEntityName() {
154                 return this.entityName;
155         }
156         
157         /**
158          * Gets the top entity name.
159          *
160          * @return the top entity name
161          */
162         public String getTopEntityName() {
163                 return this.topEntityName;
164         }
165         
166         /**
167          * Gets the object version.
168          *
169          * @return the object version
170          */
171         public Version getObjectVersion() {
172                 return this.loader.getVersion();
173         }
174         public Loader getLoader() {
175                 return this.loader;
176         }
177         @Override
178         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
179                         throws AAIException {
180
181                 if (this.entityName == null) {
182                         this.topEntityName = obj.getDbName();
183                         this.topEntity = obj;
184                 }
185                 this.entityName = obj.getDbName();
186                 this.entity = obj;
187                 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
188                 this.parentList.add(entity.getUnderlyingObject());
189                 
190                 for (String key : uriKeys.keySet()) {
191                         entity.setValue(key, uriKeys.getFirst(key));
192                 }
193                 try {
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) {
198                                         nameProp = "";
199                                 }
200                                 if (nameProp != null && !nameProp.equals("")) {
201                                         String[] nameProps = nameProp.split(",");
202                                         for (String prop : nameProps) {
203                                                 entity.setValue(prop, relatedObject.getValue(prop));
204                                         }
205                                 }
206                         }
207                 } catch (UnsupportedEncodingException e) {
208                         LOGGER.error(e.getMessage(),e);
209                 }
210                 this.previous = entity;
211         }
212         @Override
213         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
214                         boolean isFinalContainer) throws AAIException {
215                 this.previous = obj;
216
217                 if (this.entity != null) {
218                         this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
219                 } else {
220                         this.entity = obj;
221                         this.topEntity = obj;
222                 }
223         }
224 }