Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / parsers / uri / URIToExtensionInformation.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.URI;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.openecomp.aai.exceptions.AAIException;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.Loader;
33 import org.openecomp.aai.restcore.HttpMethod;
34 import com.google.common.base.CaseFormat;
35 import com.google.common.base.Joiner;
36
37 /**
38  * The Class URIToExtensionInformation.
39  */
40 public class URIToExtensionInformation implements Parsable {
41
42         private String namespace = "";
43         
44         private String methodName = "";
45         
46         private String topObject = "";
47         
48         private List<String> pieces = null;
49         
50         /**
51          * Instantiates a new URI to extension information.
52          *
53          * @param loader the loader
54          * @param uri the uri
55          * @throws IllegalArgumentException the illegal argument exception
56          * @throws AAIException the AAI exception
57          * @throws UnsupportedEncodingException the unsupported encoding exception
58          */
59         public URIToExtensionInformation(Loader loader, URI uri) throws IllegalArgumentException, AAIException, UnsupportedEncodingException {
60                 pieces = new ArrayList<>();
61                 URIParser parser = new URIParser(loader, uri);
62                 parser.parse(this);
63                 
64                 this.methodName = Joiner.on("").join(this.pieces);
65         }
66         
67         /**
68          * @{inheritDoc}
69          */
70         @Override
71         public void processObject(Introspector obj, MultivaluedMap<String, String> uriKeys) {
72                 String upperCamel = toUpperCamel(obj.getDbName());
73                 if (topObject.equals("")) {
74                         topObject = upperCamel;
75                 }
76                 pieces.add(upperCamel);
77
78         }
79
80         /**
81          * @{inheritDoc}
82          */
83         @Override
84         public void processContainer(Introspector obj, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) {
85                 pieces.add(toUpperCamel(obj.getName()));
86         }
87         
88         /**
89          * @{inheritDoc}
90          */
91         @Override
92         public void processNamespace(Introspector obj) {
93                 this.namespace = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, obj.getDbName());
94                 pieces.add(toUpperCamel(obj.getDbName()));
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 boolean useOriginalLoader() {
111                 return true;
112         }
113         
114         /**
115          * Gets the namespace.
116          *
117          * @return the namespace
118          */
119         public String getNamespace() {
120                 return this.namespace;
121         }
122         
123         /**
124          * Gets the top object.
125          *
126          * @return the top object
127          */
128         public String getTopObject() {
129                 return this.topObject;
130         }
131         
132         /**
133          * Gets the method name.
134          *
135          * @param httpMethod the http method
136          * @param isPreprocess the is preprocess
137          * @return the method name
138          */
139         public String getMethodName(HttpMethod httpMethod, boolean isPreprocess) {
140                 String result = "Dynamic";
141                 if (httpMethod.equals(HttpMethod.PUT)) {
142                         result += "Add";
143                 } else if (httpMethod.equals(HttpMethod.DELETE)) {
144                         result += "Del";
145                 } else {
146                         throw new IllegalArgumentException("http method not supported: " + httpMethod);
147                 }
148                 result += this.methodName;
149                 
150                 if (isPreprocess) {
151                         result += "PreProc";
152                 } else {
153                         result += "PostProc";
154                 }
155                 return result;
156         }
157
158         /**
159          * To upper camel.
160          *
161          * @param name the name
162          * @return the string
163          */
164         private String toUpperCamel(String name) {
165                 String result = "";
166                 result = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
167                 return result;
168         }
169 }