Update the aai-common with the latest code
[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 org.openecomp.aai.serialization.db.EdgeType;
35 import com.google.common.base.CaseFormat;
36 import com.google.common.base.Joiner;
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                 if (httpMethod.equals(HttpMethod.PUT)) {
122                         result += "Add";
123                 } else if (httpMethod.equals(HttpMethod.DELETE)) {
124                         result += "Del";
125                 } else {
126                         throw new IllegalArgumentException("http method not supported: " + httpMethod);
127                 }
128                 result += this.methodName;
129                 
130                 if (isPreprocess) {
131                         result += "PreProc";
132                 } else {
133                         result += "PostProc";
134                 }
135                 return result;
136         }
137
138         /**
139          * To upper camel.
140          *
141          * @param name the name
142          * @return the string
143          */
144         private String toUpperCamel(String name) {
145                 String result = "";
146                 result = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
147                 return result;
148         }
149
150         @Override
151         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
152                         throws AAIException {
153                 String upperCamel = toUpperCamel(obj.getDbName());
154                 if (topObject.equals("")) {
155                         topObject = upperCamel;
156                 }
157                 pieces.add(upperCamel);
158         }
159
160         @Override
161         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
162                         boolean isFinalContainer) throws AAIException {
163                 pieces.add(toUpperCamel(obj.getName()));
164         }
165 }