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