[AAI-178 Amsterdam] Make Edge Properties to be
[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 com.google.common.base.CaseFormat;
24 import com.google.common.base.Joiner;
25 import org.openecomp.aai.exceptions.AAIException;
26 import org.openecomp.aai.introspection.Introspector;
27 import org.openecomp.aai.introspection.Loader;
28 import org.openecomp.aai.restcore.HttpMethod;
29 import org.openecomp.aai.serialization.db.EdgeType;
30
31 import javax.ws.rs.core.MultivaluedMap;
32 import java.io.UnsupportedEncodingException;
33 import java.net.URI;
34 import java.util.ArrayList;
35 import java.util.List;
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 processNamespace(Introspector obj) {
72                 this.namespace = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, obj.getDbName());
73                 pieces.add(toUpperCamel(obj.getDbName()));
74
75         }
76
77         /**
78          * @{inheritDoc}
79          */
80         @Override
81         public String getCloudRegionTransform() {
82                 return "remove";
83         }
84         
85         /**
86          * @{inheritDoc}
87          */
88         @Override
89         public boolean useOriginalLoader() {
90                 return true;
91         }
92         
93         /**
94          * Gets the namespace.
95          *
96          * @return the namespace
97          */
98         public String getNamespace() {
99                 return this.namespace;
100         }
101         
102         /**
103          * Gets the top object.
104          *
105          * @return the top object
106          */
107         public String getTopObject() {
108                 return this.topObject;
109         }
110         
111         /**
112          * Gets the method name.
113          *
114          * @param httpMethod the http method
115          * @param isPreprocess the is preprocess
116          * @return the method name
117          */
118         public String getMethodName(HttpMethod httpMethod, boolean isPreprocess) {
119                 String result = "Dynamic";
120                 if (httpMethod.equals(HttpMethod.PUT)) {
121                         result += "Add";
122                 } else if (httpMethod.equals(HttpMethod.DELETE)) {
123                         result += "Del";
124                 } else {
125                         throw new IllegalArgumentException("http method not supported: " + httpMethod);
126                 }
127                 result += this.methodName;
128                 
129                 if (isPreprocess) {
130                         result += "PreProc";
131                 } else {
132                         result += "PostProc";
133                 }
134                 return result;
135         }
136
137         /**
138          * To upper camel.
139          *
140          * @param name the name
141          * @return the string
142          */
143         private String toUpperCamel(String name) {
144                 String result = "";
145                 result = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
146                 return result;
147         }
148
149         @Override
150         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
151                         throws AAIException {
152                 String upperCamel = toUpperCamel(obj.getDbName());
153                 if (topObject.equals("")) {
154                         topObject = upperCamel;
155                 }
156                 pieces.add(upperCamel);
157         }
158
159         @Override
160         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
161                         boolean isFinalContainer) throws AAIException {
162                 pieces.add(toUpperCamel(obj.getName()));
163         }
164 }