Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / entities / uri / SimpleUri.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.client.graphinventory.entities.uri;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.nio.charset.StandardCharsets;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.ws.rs.core.UriBuilder;
30
31 import org.apache.commons.lang3.builder.HashCodeBuilder;
32 import org.onap.so.client.graphinventory.Format;
33 import org.onap.so.client.aai.entities.uri.AAIUri;
34 import org.onap.so.client.graphinventory.entities.uri.Depth;
35 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser;
36 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
37 import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals;
38 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
39 import org.springframework.web.util.UriUtils;
40
41 public class SimpleUri implements GraphInventoryResourceUri {
42
43         protected UriBuilder internalURI;
44         protected final static String relationshipAPI = "/relationship-list/relationship";
45         protected final static String relatedTo = "/related-to";
46         protected final Object[] values;
47         protected final GraphInventoryObjectType type;
48         protected final GraphInventoryObjectPlurals pluralType;
49         protected SimpleUri(GraphInventoryObjectType type, Object... values) {
50                 this.type = type;
51                 this.pluralType = null;
52                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
53                 this.values = values;
54         }
55         protected SimpleUri(GraphInventoryObjectType type, URI uri) {
56                 this.type = type;
57                 this.pluralType = null;
58                 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
59                 this.values = new Object[0];
60         }
61         protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
62                 this.internalURI = builder;
63                 this.values = values;
64                 this.type = type;
65                 this.pluralType = null;
66         }
67         protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
68                 this.internalURI = builder;
69                 this.values = values;
70                 this.type = null;
71                 this.pluralType = type;
72         }
73         protected SimpleUri(GraphInventoryObjectPlurals type) {
74                 this.type = null;
75                 this.pluralType = type;
76                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
77                 this.values = new Object[0];
78         }
79         protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
80                 this.type = null;
81                 this.pluralType = type;
82                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
83                 this.values = values;
84         }
85         
86         @Override
87         public SimpleUri relationshipAPI() {
88                 this.internalURI = internalURI.path(relationshipAPI);
89                 return this;
90         }
91         
92         @Override
93         public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
94                 
95                 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
96                 return this;
97         }
98         @Override
99         public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
100                 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
101                 return this;
102         }
103         
104         @Override
105         public SimpleUri resourceVersion(String version) {
106                 this.internalURI = internalURI.replaceQueryParam("resource-version", version);
107                 return this;
108         }
109         
110         @Override
111         public SimpleUri queryParam(String name, String... values) {
112                 this.internalURI = internalURI.queryParam(name, values);
113                 return this;
114         }
115         
116         @Override
117         public SimpleUri replaceQueryParam(String name, String... values) {
118                 this.internalURI = internalURI.replaceQueryParam(name, values);
119                 return this;
120         }
121         
122         @Override
123         public URI build() {
124                 return build(this.values);
125         }
126         
127         protected URI build(Object... values) {
128                 //This is a workaround because resteasy does not encode URIs correctly
129                 final String[] encoded = new String[values.length];
130                 for (int i = 0; i < values.length; i++) {
131                         try {
132                                 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
133                         } catch (UnsupportedEncodingException e) {
134                                 encoded[i] = values[i].toString();
135                         }
136                 }
137                 return internalURI.buildFromEncoded(encoded);
138         }
139         
140         @Override
141         public Map<String, String> getURIKeys() {
142                 return this.getURIKeys(this.build().toString());
143         }
144         
145         protected Map<String, String> getURIKeys(String uri) {
146                 UriParser parser;
147                 if (this.type != null) {
148                         if (!("".equals(this.getTemplate(type)))) {
149                                 parser = new UriParserSpringImpl(this.getTemplate(type));
150                         } else {
151                                 return new HashMap<>();
152                         }
153                 } else {
154                         parser = new UriParserSpringImpl(this.getTemplate(pluralType));
155                 }
156                 
157                 
158                 return parser.parse(uri);
159         }
160         
161         @Override
162         public SimpleUri clone() {
163                 if (this.type != null) {
164                         return new SimpleUri(this.type, this.internalURI.clone(), values);
165                 } else {
166                         return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
167                 }
168         }
169         
170         @Override
171         public GraphInventoryObjectType getObjectType() {
172                 return this.type;
173         }
174         
175         @Override
176         public boolean equals(Object o) {
177                 if (o instanceof AAIUri) {
178                         return this.build().equals(((AAIUri)o).build());
179                 }
180                 return false;
181         }
182         
183         @Override
184         public int hashCode() {         
185                 return new HashCodeBuilder().append(this.build()).toHashCode();
186         }
187         
188         
189         @Override
190         public SimpleUri depth(Depth depth) {
191                 this.internalURI.replaceQueryParam("depth", depth.toString());
192                 return this;
193         }
194         @Override
195         public SimpleUri nodesOnly(boolean nodesOnly) {
196                 if (nodesOnly) {
197                         this.internalURI.replaceQueryParam("nodes-only", "");
198                 }
199                 return this;
200         }
201         
202         @Override
203         public SimpleUri format(Format format) {
204                 this.internalURI.replaceQueryParam("format", format);
205                 return this;
206         }
207         
208         protected String getTemplate(GraphInventoryObjectType type) {
209                 return type.uriTemplate();
210         }
211         
212         protected String getTemplate(GraphInventoryObjectPlurals type) {
213                 return type.uriTemplate();
214         }
215         
216 }