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