2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.adaptors.aai;
24 import java.lang.annotation.Annotation;
25 import java.net.MalformedURLException;
27 import java.net.URISyntaxException;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
36 import javax.xml.bind.annotation.XmlType;
38 import org.apache.commons.lang.StringUtils;
39 import org.openecomp.aai.inventory.v11.Relationship;
40 import org.openecomp.aai.inventory.v11.RelationshipData;
41 import org.openecomp.aai.inventory.v11.RelationshipList;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class AAIServiceUtils {
49 private static final Logger LOG = LoggerFactory.getLogger(AAIService.class);
51 public static String getPrimaryIdFromClass(Class<? extends AAIDatum> resourceClass){
53 getLogger().debug(resourceClass.getName());
54 AAIDatum instance = null;
57 instance = resourceClass.newInstance();
59 Annotation[] annotations = resourceClass.getAnnotations();
60 for(Annotation annotation : annotations) {
61 Class<? extends Annotation> anotationType = annotation.annotationType();
62 String annotationName = anotationType.getName();
64 // 2. find string property setters and getters for the lists
65 if("javax.xml.bind.annotation.XmlType".equals(annotationName)){
66 XmlType order = (XmlType)annotation;
67 String[] values = order.propOrder();
68 for(String value : values) {
69 String id = camelCaseToDashedString(value);
74 } catch(Exception exc) {
80 public static String getSecondaryIdFromClass(Class<? extends AAIDatum> resourceClass){
82 getLogger().debug(resourceClass.getName());
83 AAIDatum instance = null;
86 instance = resourceClass.newInstance();
88 Annotation[] annotations = resourceClass.getAnnotations();
89 for(Annotation annotation : annotations) {
90 Class<? extends Annotation> anotationType = annotation.annotationType();
91 String annotationName = anotationType.getName();
93 // 2. find string property setters and getters for the lists
94 if("javax.xml.bind.annotation.XmlType".equals(annotationName)){
95 boolean primaryIdFound = false;
96 XmlType order = (XmlType)annotation;
97 String[] values = order.propOrder();
98 for(String value : values) {
99 String id = camelCaseToDashedString(value);
103 primaryIdFound = true;
108 } catch(Exception exc) {
115 private static Logger getLogger() {
120 private static final String regex = "([A-Z][a-z,0-9]+)";
121 private static final String replacement = "-$1";
123 public static String camelCaseToDashedString(String propOrder) {
124 return propOrder.replaceAll(regex, replacement).toLowerCase();
127 public static HashMap<String,String> keyToHashMap(String key, SvcLogicContext ctx) {
132 getLogger().debug("Converting key [" + key + "] to where clause");
134 if (key.startsWith("'") && key.endsWith("'")) {
135 key = key.substring(1, key.length() - 1);
137 getLogger().debug("Stripped outer single quotes - key is now [" + key + "]");
140 String[] keyTerms = key.split("\\s+");
142 StringBuffer whereBuff = new StringBuffer();
146 HashMap<String, String> results = new HashMap<String, String>();
148 for (int i = 0; i < keyTerms.length; i++) {
150 if ("and".equalsIgnoreCase(keyTerms[i])
151 || "or".equalsIgnoreCase(keyTerms[i])) {
154 term1 = resolveTerm(keyTerms[i], ctx);
156 } else if (op == null) {
157 if ("==".equals(keyTerms[i])) {
163 term2 = resolveTerm(keyTerms[i], ctx);
164 term2 = term2.trim().replace("'", "").replace("$", "").replace("'", "");
165 results.put(term1, term2);
176 private static String resolveTerm(String term, SvcLogicContext ctx) {
181 getLogger().debug("resolveTerm: term is " + term);
183 if (term.startsWith("$") && (ctx != null)) {
184 // Resolve any index variables.
186 return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
187 } else if (term.startsWith("'") || term.startsWith("\"")) {
190 return (term.replaceAll("-", "_"));
195 private static String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
197 if (ctxVarName.indexOf('[') == -1) {
198 // Ctx variable contains no arrays
199 return (ctx.getAttribute(ctxVarName));
202 // Resolve any array references
203 StringBuffer sbuff = new StringBuffer();
204 String[] ctxVarParts = ctxVarName.split("\\[");
205 sbuff.append(ctxVarParts[0]);
206 for (int i = 1; i < ctxVarParts.length; i++) {
207 if (ctxVarParts[i].startsWith("$")) {
208 int endBracketLoc = ctxVarParts[i].indexOf("]");
209 if (endBracketLoc == -1) {
210 // Missing end bracket ... give up parsing
211 getLogger().warn("Variable reference " + ctxVarName
212 + " seems to be missing a ']'");
213 return (ctx.getAttribute(ctxVarName));
216 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
217 String remainder = ctxVarParts[i].substring(endBracketLoc);
220 sbuff.append(ctx.getAttribute(idxVarName));
221 sbuff.append(remainder);
224 // Index is not a variable reference
226 sbuff.append(ctxVarParts[i]);
230 return (ctx.getAttribute(sbuff.toString()));
233 public static void populateRelationshipDataFromPath(RelationshipList rl) throws URISyntaxException {
234 List<Relationship> list = rl.getRelationship();
235 if(list != null && !list.isEmpty()) {
236 for(Relationship relationship : list) {
237 if(relationship.getRelationshipData().isEmpty()){
238 String link = relationship.getRelatedLink();
239 URI uri = new URI(link);
240 link = uri.getPath();
241 HashMap<String,String> contributors = pathToHashMap(link);
242 for(String key : contributors.keySet()) {
243 RelationshipData rd = new RelationshipData();
244 rd.setRelationshipKey(key);
245 rd.setRelationshipValue(contributors.get(key));
246 relationship.getRelationshipData().add(rd);
253 protected static HashMap<String,String> pathToHashMap(String path) {
254 HashMap<String, String> nameValues = new HashMap<String, String>();
256 String[] split = path.split("/");
258 LinkedList<String> list = new LinkedList<String>( Arrays.asList(split));
259 Iterator<String> it = list.iterator();
261 while(it.hasNext()) {
262 String tag = it.next();
264 if(AAIRequest.getResourceNames().contains(tag)){
266 // get the class from tag
267 Class<? extends AAIDatum> clazz = null;
269 clazz = AAIRequest.getClassFromResource(tag);
270 String fieldName = AAIServiceUtils.getPrimaryIdFromClass(clazz);
272 String value = it.next();
273 if(!StringUtils.isEmpty(value)){
274 nameValues.put(String.format("%s.%s", tag, fieldName), value);
280 case "service-capability":
282 String secondaryFieldName = AAIServiceUtils.getSecondaryIdFromClass(clazz);
283 if(secondaryFieldName != null) {
285 nameValues.put(String.format("%s.%s", tag, secondaryFieldName), value);
292 } catch (ClassNotFoundException exc) {
293 LOG.info("Caught exception", exc);
301 public static String getPathForResource(String resource, String key, SvcLogicContext ctx ) throws MalformedURLException{
302 HashMap<String, String> nameValues = AAIServiceUtils.keyToHashMap(key, ctx);
303 AAIRequest request = AAIRequest.createRequest(resource, nameValues);
305 for(String name : nameValues.keySet()) {
306 request.addRequestProperty(name, nameValues.get(name));
308 return request.getRequestPath();
311 public static boolean isValidFormat(String resource, HashMap<String, String> nameValues) {
314 case "formatted-query":
315 case "generic-query":
319 case "l2-bridge-sbg":
320 case "l2-bridge-bgf":
325 if(resource.contains(":")) {
326 resource = resource.substring(0, resource.indexOf(":"));
329 Set<String> keys = nameValues.keySet();
330 for(String key : keys) {
331 if(!key.contains(".")) {
332 if("depth".equals(key) || "related-to".equals(key) || "related_to".equals(key) || "related-link".equals(key) || "related_link".equals(key) || "selflink".equals(key))
335 getLogger().warn(String.format("key %s is incompatible with resource type '%s'", key, resource));
342 public static boolean containsResource(String resource, HashMap<String, String> nameValues) {
343 if(resource.contains(":")) {
349 case "formatted-query":
350 case "generic-query":
354 case "l2-bridge-sbg":
355 case "l2-bridge-bgf":
361 if(nameValues.containsKey("selflink")) {
366 Set<String> tags = new HashSet<>();
368 for(String key : nameValues.keySet()) {
369 key = key.replace("_", "-");
370 if(key.contains(".")) {
371 String[] split = key.split("\\.");
377 return tags.contains(resource);