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