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