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