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