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