48a6324e9544dba438f7c69a58bbddfc3ef4f7d5
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / uri / URIToRelationshipObject.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 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.introspection.exceptions.AAIUnknownObjectException;
29 import org.onap.aai.serialization.db.EdgeType;
30 import org.onap.aai.util.AAIApiServerURLBase;
31 import org.onap.aai.workarounds.LegacyURITransformer;
32
33 import javax.ws.rs.core.MultivaluedMap;
34 import java.io.UnsupportedEncodingException;
35 import java.net.MalformedURLException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.List;
39
40 /**
41  * Given a URI a Relationship Object is returned.
42  * 
43  * The relationship-data objects are created from the keys in the model.
44  * The keys are processed in the order they appear in the model.
45  
46  *
47  */
48 public class URIToRelationshipObject implements Parsable {
49         
50         private Introspector result = null;
51                         
52         private LegacyURITransformer uriTransformer = null;
53         
54         private Version originalVersion = null;
55         
56         private Introspector relationship = null;
57         
58         private Loader loader = null;
59         
60         private String baseURL; 
61         
62         private final URI uri;
63         /**
64          * Instantiates a new URI to relationship object.
65          *
66          * @param loader the loader
67          * @param uri the uri
68          * @throws IllegalArgumentException the illegal argument exception
69          * @throws AAIException the AAI exception
70          * @throws UnsupportedEncodingException the unsupported encoding exception
71          * @throws MalformedURLException the malformed URL exception
72          */
73         public URIToRelationshipObject(Loader loader, URI uri) throws AAIException {
74                 
75                 this.loader = loader;
76                 uriTransformer = LegacyURITransformer.getInstance();
77                 originalVersion = loader.getVersion();
78
79                 try {
80                         relationship = loader.introspectorFromName("relationship");
81                 } catch (AAIUnknownObjectException e1) {
82                         throw new RuntimeException("Fatal error - could not load relationship object!", e1);
83                 }
84
85                 this.baseURL = AAIApiServerURLBase.get(originalVersion);
86                 this.uri = uri;
87                 
88                 }
89                 
90         public URIToRelationshipObject(Loader loader, URI uri, String baseURL) throws AAIException {
91                 this(loader, uri);
92                 
93                 if (baseURL != null) {
94                         this.baseURL = baseURL;
95                 }
96         }
97
98         
99         /**
100          * @{inheritDoc}
101          */
102         @Override
103         public String getCloudRegionTransform(){
104                 return "remove";
105         }
106         
107         /**
108          * @{inheritDoc}
109          */
110         @Override
111         public void processNamespace(Introspector obj) {
112         
113         }
114         
115         /**
116          * @{inheritDoc}
117          */
118         @Override
119         public boolean useOriginalLoader() {
120                 return true;
121         }
122         
123         /**
124          * Gets the result.
125          *
126          * @return the result
127          * @throws AAIException 
128          * @throws UnsupportedEncodingException 
129          * @throws URISyntaxException 
130          */
131         public Introspector getResult() throws UnsupportedEncodingException, AAIException, URISyntaxException {
132                 URIParser parser = new URIParser(this.loader, this.uri);
133                 parser.parse(this);
134                 URI originalUri = parser.getOriginalURI();
135                 
136                 URI relatedLink = new URI(this.baseURL + this.originalVersion + "/" + originalUri);
137                 if (this.originalVersion.compareTo(Version.v10) >= 0) {
138                         //only return the path section of the URI past v10
139                         relatedLink = new URI(relatedLink.getRawPath());
140                 }
141                 
142                 this.relationship.setValue("related-link", relatedLink.toString());
143                 
144                 this.result = relationship;
145                 return this.result;
146         }
147
148         @Override
149         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
150                         throws AAIException {
151                 for (String key : obj.getKeys()) {
152                         try {
153                                 Introspector data = loader.introspectorFromName("relationship-data");
154                                 data.setValue("relationship-key", obj.getDbName() + "." + key);
155                                 data.setValue("relationship-value", obj.getValue(key));
156                                 
157                                 ((List<Object>)relationship.getValue("relationship-data")).add(data.getUnderlyingObject());
158                         } catch (AAIUnknownObjectException e) {
159                                 throw new RuntimeException("Fatal error - relationship-data object not found!");
160                         }
161                 }
162                 relationship.setValue("related-to", obj.getDbName());
163         }
164
165         @Override
166         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
167                         boolean isFinalContainer) throws AAIException {
168         }
169 }