AAI-1523 Batch reformat aai-core
[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
21 package org.onap.aai.parsers.uri;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.util.HashMap;
26 import java.util.List;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.onap.aai.edges.enums.EdgeType;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.schema.enums.ObjectMetadata;
35 import org.onap.aai.setup.SchemaVersion;
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 public class URIToObject implements Parsable {
44
45     private Introspector topEntity = null;
46
47     private String topEntityName = null;
48
49     private String entityName = null;
50
51     private Introspector entity = null;
52
53     private Introspector previous = null;
54
55     private List<Object> parentList = null;
56
57     private SchemaVersion version = null;
58     private Loader loader = null;
59     private final HashMap<String, Introspector> relatedObjects;
60
61     /**
62      * Instantiates a new URI to object.
63      *
64      * @param loader the loader
65      * @param uri the uri
66      * @throws IllegalArgumentException the illegal argument exception
67      * @throws AAIException the AAI exception
68      * @throws UnsupportedEncodingException the unsupported encoding exception
69      */
70     public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
71
72         URIParser parser = new URIParser(loader, uri);
73         this.relatedObjects = new HashMap<>();
74
75         parser.parse(this);
76         this.loader = parser.getLoader();
77         this.version = loader.getVersion();
78     }
79
80     public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects)
81             throws AAIException, UnsupportedEncodingException {
82
83         URIParser parser = new URIParser(loader, uri);
84         this.relatedObjects = relatedObjects;
85
86         parser.parse(this);
87         this.loader = parser.getLoader();
88         this.version = loader.getVersion();
89
90     }
91
92     /**
93      * @{inheritDoc}
94      */
95     @Override
96     public void processNamespace(Introspector obj) {
97
98     }
99
100     /**
101      * @{inheritDoc}
102      */
103     @Override
104     public String getCloudRegionTransform() {
105         return "add";
106     }
107
108     /**
109      * @{inheritDoc}
110      */
111     @Override
112     public boolean useOriginalLoader() {
113         // TODO Auto-generated method stub
114         return false;
115     }
116
117     /**
118      * Gets the top entity.
119      *
120      * @return the top entity
121      */
122     public Introspector getTopEntity() {
123         return this.topEntity;
124     }
125
126     /**
127      * Gets the entity.
128      *
129      * @return the entity
130      */
131     public Introspector getEntity() {
132         return this.entity;
133     }
134
135     /**
136      * Gets the parent list.
137      *
138      * @return the parent list
139      */
140     public List<Object> getParentList() {
141         return this.parentList;
142     }
143
144     /**
145      * Gets the entity name.
146      *
147      * @return the entity name
148      */
149     public String getEntityName() {
150         return this.entityName;
151     }
152
153     /**
154      * Gets the top entity name.
155      *
156      * @return the top entity name
157      */
158     public String getTopEntityName() {
159         return this.topEntityName;
160     }
161
162     /**
163      * Gets the object version.
164      *
165      * @return the object version
166      */
167     public SchemaVersion getObjectVersion() {
168         return this.loader.getVersion();
169     }
170
171     public Loader getLoader() {
172         return this.loader;
173     }
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         }
207         this.previous = entity;
208     }
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 }