AAI-1523 Batch reformat aai-core
[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
21 package org.onap.aai.parsers.uri;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.MalformedURLException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.List;
28
29 import javax.ws.rs.core.MultivaluedMap;
30
31 import org.onap.aai.config.SpringContextAware;
32 import org.onap.aai.edges.enums.EdgeType;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
37 import org.onap.aai.setup.SchemaVersion;
38 import org.onap.aai.setup.SchemaVersions;
39 import org.onap.aai.util.AAIConfig;
40 import org.onap.aai.util.AAIConstants;
41
42 /**
43  * Given a URI a Relationship Object is returned.
44  * 
45  * The relationship-data objects are created from the keys in the model.
46  * The keys are processed in the order they appear in the model.
47  *
48  * 
49  */
50 public class URIToRelationshipObject implements Parsable {
51
52     private Introspector result = null;
53
54     private SchemaVersion 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     /**
65      * Instantiates a new URI to relationship 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      * @throws MalformedURLException the malformed URL exception
73      */
74     public URIToRelationshipObject(Loader loader, URI uri) throws AAIException {
75
76         this.loader = loader;
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 = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);
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      * @{inheritDoc}
100      */
101     @Override
102     public String getCloudRegionTransform() {
103         return "remove";
104     }
105
106     /**
107      * @{inheritDoc}
108      */
109     @Override
110     public void processNamespace(Introspector obj) {
111
112     }
113
114     /**
115      * @{inheritDoc}
116      */
117     @Override
118     public boolean useOriginalLoader() {
119         return true;
120     }
121
122     /**
123      * Gets the result.
124      *
125      * @return the result
126      * @throws AAIException
127      * @throws UnsupportedEncodingException
128      * @throws URISyntaxException
129      */
130     public Introspector getResult() throws UnsupportedEncodingException, AAIException, URISyntaxException {
131         URIParser parser = new URIParser(this.loader, this.uri);
132         parser.parse(this);
133         URI originalUri = parser.getOriginalURI();
134
135         URI relatedLink = new URI(this.baseURL + this.originalVersion + "/" + originalUri);
136         SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
137         if (this.originalVersion.compareTo(schemaVersions.getRelatedLinkVersion()) >= 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         for (String key : obj.getKeys()) {
151             try {
152                 Introspector data = loader.introspectorFromName("relationship-data");
153                 data.setValue("relationship-key", obj.getDbName() + "." + key);
154                 data.setValue("relationship-value", obj.getValue(key));
155
156                 ((List<Object>) relationship.getValue("relationship-data")).add(data.getUnderlyingObject());
157             } catch (AAIUnknownObjectException e) {
158                 throw new RuntimeException("Fatal error - relationship-data object not found!");
159             }
160         }
161         relationship.setValue("related-to", obj.getDbName());
162     }
163
164     @Override
165     public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
166             boolean isFinalContainer) throws AAIException {
167     }
168 }