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.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.Reader;
28 import java.io.UnsupportedEncodingException;
29 import java.lang.reflect.Method;
30 import java.net.MalformedURLException;
32 import java.net.URLDecoder;
33 import java.net.URLEncoder;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.BitSet;
37 import java.util.HashSet;
38 import java.util.HashMap;
39 import java.util.LinkedHashMap;
40 import java.util.List;
42 import java.util.Properties;
44 import java.util.TreeSet;
46 import org.apache.commons.lang.StringUtils;
47 import org.openecomp.aai.inventory.v11.GenericVnf;
48 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
49 import org.osgi.framework.Bundle;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.FrameworkUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.fasterxml.jackson.core.JsonParseException;
56 import com.fasterxml.jackson.core.JsonProcessingException;
57 import com.fasterxml.jackson.databind.JsonMappingException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
60 public abstract class AAIRequest {
61 protected static final Logger LOG = LoggerFactory.getLogger(AAIRequest.class);
63 protected static final String TARGET_URI = "org.onap.ccsdk.sli.adaptors.aai.uri";
65 protected static final String MASTER_REQUEST = "master-request";
67 public static final String RESOURCE_VERSION = "resource-version";
69 public static final String DEPTH = "depth";
71 protected static Properties configProperties;
72 protected final String target_uri;
73 protected static AAIService aaiService;
75 protected AAIDatum requestDatum;
77 protected final Properties requestProperties = new Properties();
80 public static AAIRequest createRequest(String resoourceName, Map<String, String> nameValues){
82 String resoource = resoourceName;
87 if(resoource.contains(":")) {
88 String[] tokens = resoource.split(":");
89 if(tokens != null && tokens.length > 0) {
90 resoource = tokens[0];
94 if(nameValues.containsKey("selflink")){
95 Class<? extends AAIDatum> clazz = null;
97 clazz = getClassFromResource(resoource) ;
98 } catch (ClassNotFoundException e) {
99 LOG.warn("AAIRequest does not support class: " + e.getMessage());
104 return new SelfLinkRequest(clazz);
110 case "generic-query":
111 return new GenericQueryRequest();
113 return new NamedQueryRequest();
115 return new NodesQueryRequest();
117 case "formatted-query":
118 return new CustomQueryRequest();
120 return new LInterfaceRequest(LInterfaceRequest.TYPE.L2_BRIDGE_SBG);
121 case "l2-bridge-sbg":
122 return new SubInterfaceRequest(SubInterfaceRequest.TYPE.L2_BRIDGE_SBG);
123 case "l2-bridge-bgf":
124 return new SubInterfaceRequest(SubInterfaceRequest.TYPE.L2_BRIDGE_BGF);
127 return new EchoRequest();
131 AAIRequest request = getRequestFromResource(resoource);
132 if(request == null) {
142 * Map containing resource tag to its bit position in bitset mapping
144 private static Map<String, String> tagValues = new LinkedHashMap<>();
146 * Map containing bitset value of the path to its path mapping
148 private static Map<BitSet, String> bitsetPaths = new LinkedHashMap<>();
151 public static Set<String> getResourceNames() {
152 return tagValues.keySet();
156 public static void setProperties(Properties props, AAIService aaiService) {
157 AAIRequest.configProperties = props;
158 AAIRequest.aaiService = aaiService;
163 Bundle bundle = FrameworkUtil.getBundle(AAIServiceActivator.class);
165 BundleContext ctx = bundle.getBundleContext();
169 url = ctx.getBundle().getResource(AAIService.PATH_PROPERTIES);
171 url = aaiService.getClass().getResource("/aai-path.properties");
174 InputStream in = url.openStream();
175 Reader reader = new InputStreamReader(in, "UTF-8");
177 Properties properties = new Properties();
178 properties.load(reader);
179 LOG.info("loaded " + properties.size());
181 Set<String> keys = properties.stringPropertyNames();
184 Set<String> resourceNames = new TreeSet<>();
186 for(String key : keys) {
187 String[] tags = key.split("\\|");
188 for(String tag : tags) {
189 if(!resourceNames.contains(tag)) {
190 resourceNames.add(tag);
191 tagValues.put(tag, Integer.toString(++index));
194 BitSet bs = new BitSet(256);
195 for(String tag : tags) {
196 String value = tagValues.get(tag);
197 Integer bitIndex = Integer.parseInt(value) ;
200 String path = properties.getProperty(key);
201 LOG.info(String.format("bitset %s\t\t%s", bs.toString(), path));
202 bitsetPaths.put(bs, path);
204 LOG.info("loaded " + resourceNames.toString());
208 LOG.error("Caught exception", e);
212 public AAIRequest() {
213 target_uri = configProperties.getProperty(TARGET_URI);
216 public void addRequestProperty(String key, String value) {
217 requestProperties.put(key, value);
220 public final void setRequestObject(AAIDatum value) {
221 requestDatum = value;
224 public final AAIDatum getRequestObject() {
228 public final void addMasterRequest(AAIRequest masterRequest) {
229 requestProperties.put(MASTER_REQUEST, masterRequest);
232 protected static String encodeQuery(String param) throws UnsupportedEncodingException {
233 return URLEncoder.encode(param, "UTF-8").replace("+", "%20");
236 protected void handleException(AAIRequest lInterfaceRequest, JsonProcessingException exc) {
237 aaiService.getLogger().warn("Could not deserialize object of type " + lInterfaceRequest.getClass().getSimpleName(), exc) ;
240 public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
242 String request_url = null;
244 request_url = target_uri + updatePathDataValues(resourceVersion);
246 URL http_req_url = new URL(request_url);
248 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
253 public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
254 String request_url = getRequestPath();
256 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
258 for(String resoourceName:uniqueResources) {
259 AAIRequest locRequest = AAIRequest.createRequest(resoourceName, new HashMap<String, String>());
260 if(locRequest != null) {
261 Class<?> clazz = locRequest.getClass();
262 Method function = null;
264 function = clazz.getMethod("processPathData", request_url.getClass(), requestProperties.getClass());
265 request_url = (String) function.invoke(null, request_url, requestProperties);
266 } catch (Exception e) {
272 if(resourceVersion != null) {
273 request_url = request_url +"?resource-version="+resourceVersion;
279 protected String getRequestPath() throws MalformedURLException {
280 return getRequestPath(null);
283 protected String getRequestPath(String resource) throws MalformedURLException {
284 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
285 if(resource != null) {
286 // for group search add itself, but remove singular version of itself
287 if(!uniqueResources.contains(resource)) {
288 boolean replaced = false;
289 Set<String> tmpUniqueResources = new HashSet<String>();
290 tmpUniqueResources.addAll(uniqueResources);
291 for(String item : tmpUniqueResources){
292 String plural = item +"s";
293 if(item.endsWith("y")){
294 plural = item.substring(0, item.length()-1)+ "ies";
296 if(plural.equals(resource)) {
297 uniqueResources.remove(item);
298 uniqueResources.add(resource);
304 if(!uniqueResources.contains(resource)) {
305 uniqueResources.add(resource);
310 BitSet bitset = new BitSet();
311 for(String key : uniqueResources) {
312 if(tagValues.containsKey(key)) {
313 Object tmpValue = tagValues.get(key);
314 if(tmpValue != null) {
315 String value = tmpValue.toString();
316 int bitIndex = Integer.parseInt(value);
317 bitset.set(bitIndex);
322 String path = bitsetPaths.get(bitset);
324 throw new MalformedURLException("PATH not found for key string containing valies :" +requestProperties.toString());
329 public abstract URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException;
331 public abstract String toJSONString();
333 public abstract String[] getArgsList();
335 public abstract Class<? extends AAIDatum> getModelClass() ;
337 public String getPrimaryResourceName(String resource) {
341 public String formatKey(String argument) {
345 public AAIDatum jsonStringToObject(String jsonData) throws JsonParseException, JsonMappingException, IOException {
346 if(jsonData == null) {
350 AAIDatum response = null;
351 ObjectMapper mapper = getObjectMapper();
352 response = mapper.readValue(jsonData, getModelClass());
356 protected static Set<String> extractUniqueResourceSetFromKeys(Set<String> keySet) {
357 Set<String> uniqueResources = new TreeSet<>();
358 List<String> keys = new ArrayList<>(keySet);
359 for(String resource : keys) {
360 if(resource.contains(".")) {
361 String [] split = resource.split("\\.");
362 uniqueResources.add(split[0].replaceAll("_", "-"));
365 return uniqueResources;
368 public void processRequestPathValues(Map<String, String> nameValues) {
369 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(nameValues.keySet());
371 Set<String> tokens = new TreeSet<>();
373 tokens.addAll(Arrays.asList(this.getArgsList()));
375 for(String resoourceName:uniqueResources) {
376 AAIRequest locRequest = AAIRequest.createRequest(resoourceName, nameValues);
377 if(locRequest != null)
378 tokens.addAll(Arrays.asList(locRequest.getArgsList()));
381 String[] arguments = tokens.toArray(new String[0]);
382 for(String name : arguments) {
383 String tmpName = name.replaceAll("-", "_");
384 String value = nameValues.get(tmpName);
385 if(value != null && !value.isEmpty()) {
386 value = value.trim().replace("'", "").replace("$", "").replace("'", "");
387 this.addRequestProperty(name, value);
392 public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
396 public boolean isDeleteDataRequired() {
400 ObjectMapper getObjectMapper() {
401 return AAIService.getObjectMapper();
404 public static Class<? extends AAIDatum> getClassFromResource(String resoourceName) throws ClassNotFoundException {
405 String className = GenericVnf.class.getName();
406 String[] split = resoourceName.split("-");
407 for(int i = 0; i < split.length; i++) {
408 split[i] = StringUtils.capitalize(split[i]);
411 String caps = StringUtils.join(split);
412 className = className.replace("GenericVnf", caps);
413 Class<? extends AAIDatum> clazz = null;
415 clazz = (Class<? extends AAIDatum>)Class.forName(className);
416 } catch (ClassNotFoundException e) {
417 LOG.warn("AAIRequest does not support class: " + e.getMessage());
424 protected static AAIRequest getRequestFromResource(String resoourceName) {
426 Class<? extends AAIDatum> clazz = null;
428 clazz = getClassFromResource(resoourceName);
429 } catch (ClassNotFoundException e) {
430 LOG.warn("AAIRequest does not support class: " + e.getMessage());
436 GenericRequest request = new GenericRequest(clazz);
440 public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException {
441 Map<String, String> query_pairs = new LinkedHashMap<>();
443 if(query != null && !query.isEmpty()) {
444 String[] pairs = query.split("&");
445 for (String pair : pairs) {
446 int idx = pair.indexOf('=');
447 query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
453 public static Map<String, String> splitPath(String path) throws UnsupportedEncodingException {
454 Map<String, String> query_pairs = new LinkedHashMap<>();
456 if(path != null && !path.isEmpty()) {
457 String[] pairs = path.split("/");
458 for (String pair : pairs) {
459 int idx = pair.indexOf('=');
460 query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
466 protected boolean expectsDataFromPUTRequest() {
471 public String getTargetUri() {