Update schema service to fail to start
[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.config.SpringContextAware;
23 import org.onap.aai.exceptions.AAIException;
24 import org.onap.aai.introspection.Introspector;
25 import org.onap.aai.introspection.Loader;
26 import org.onap.aai.setup.SchemaVersion;
27 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
28 import org.onap.aai.edges.enums.EdgeType;
29 import org.onap.aai.setup.SchemaVersions;
30 import org.onap.aai.util.AAIConfig;
31 import org.onap.aai.util.AAIConstants;
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 SchemaVersion 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                 originalVersion = loader.getVersion();
75
76                 try {
77                         relationship = loader.introspectorFromName("relationship");
78                 } catch (AAIUnknownObjectException e1) {
79                         throw new RuntimeException("Fatal error - could not load relationship object!", e1);
80                 }
81
82                 this.baseURL = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);
83                 this.uri = uri;
84                 
85                 }
86                 
87         public URIToRelationshipObject(Loader loader, URI uri, String baseURL) throws AAIException {
88                 this(loader, uri);
89                 
90                 if (baseURL != null) {
91                         this.baseURL = baseURL;
92                 }
93         }
94
95         
96         /**
97          * @{inheritDoc}
98          */
99         @Override
100         public String getCloudRegionTransform(){
101                 return "remove";
102         }
103         
104         /**
105          * @{inheritDoc}
106          */
107         @Override
108         public void processNamespace(Introspector obj) {
109         
110         }
111         
112         /**
113          * @{inheritDoc}
114          */
115         @Override
116         public boolean useOriginalLoader() {
117                 return true;
118         }
119         
120         /**
121          * Gets the result.
122          *
123          * @return the result
124          * @throws AAIException 
125          * @throws UnsupportedEncodingException 
126          * @throws URISyntaxException 
127          */
128         public Introspector getResult() throws UnsupportedEncodingException, AAIException, URISyntaxException {
129                 URIParser parser = new URIParser(this.loader, this.uri);
130                 parser.parse(this);
131                 URI originalUri = parser.getOriginalURI();
132                 
133                 URI relatedLink = new URI(this.baseURL + this.originalVersion + "/" + originalUri);
134                 SchemaVersions schemaVersions = (SchemaVersions)SpringContextAware.getBean("schemaVersions");
135                 if (this.originalVersion.compareTo(schemaVersions.getRelatedLinkVersion()) >= 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                 for (String key : obj.getKeys()) {
149                         try {
150                                 Introspector data = loader.introspectorFromName("relationship-data");
151                                 data.setValue("relationship-key", obj.getDbName() + "." + key);
152                                 data.setValue("relationship-value", obj.getValue(key));
153                                 
154                                 ((List<Object>)relationship.getValue("relationship-data")).add(data.getUnderlyingObject());
155                         } catch (AAIUnknownObjectException e) {
156                                 throw new RuntimeException("Fatal error - relationship-data object not found!");
157                         }
158                 }
159                 relationship.setValue("related-to", obj.getDbName());
160         }
161
162         @Override
163         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
164                         boolean isFinalContainer) throws AAIException {
165         }
166 }