diff options
author | Dan Timoney <dtimoney@att.com> | 2017-10-30 14:10:45 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2017-10-30 14:10:45 +0000 |
commit | 825e3e0df023078fd926218f4ba74fb26d766e6b (patch) | |
tree | ff97234f863fa824c34c84b4fc39044900becd24 /aai-service/provider/src | |
parent | f2e639b1a3c7ee762dfc13a94d444435f42b1d95 (diff) | |
parent | 530fd439bd7f10a5e1e72736ce965b2040578060 (diff) |
Merge "Support AAI adapter to store metadata on service"
Diffstat (limited to 'aai-service/provider/src')
-rwxr-xr-x | aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIDeclarations.java | 169 | ||||
-rwxr-xr-x | aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java | 91 |
2 files changed, 218 insertions, 42 deletions
diff --git a/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIDeclarations.java b/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIDeclarations.java index 0f7e9981..53906c87 100755 --- a/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIDeclarations.java +++ b/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIDeclarations.java @@ -30,6 +30,7 @@ import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -48,6 +49,7 @@ import org.apache.commons.lang.StringUtils; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus; +import org.openecomp.aai.inventory.v11.Image; import org.openecomp.aai.inventory.v11.GenericVnf; import org.openecomp.aai.inventory.v11.InventoryResponseItem; import org.openecomp.aai.inventory.v11.InventoryResponseItems; @@ -503,9 +505,12 @@ public abstract class AAIDeclarations implements AAIClient { } switch(dependency){ - case "relationship-list": - newModelProcessRelationshipList(instance, params, prefix, ctx); - break; + case "relationship-list": + newModelProcessRelationshipList(instance, params, prefix, ctx); + break; + case "metadata": + newModelProcessMetadata(instance, params, prefix, ctx); + break; } // create a method to update relationship-list AAIRequest request = AAIRequest.createRequest(localResource, nameValues); @@ -665,7 +670,12 @@ public abstract class AAIDeclarations implements AAIClient { if(AAIRequest.createRequest(resource, nameValues) != null) { if(resource.contains(":")) { - return processDeleteRelationshipList(resource, key, ctx, nameValues); + switch (resource.split(":")[1]){ + case "relationship-list": + return processDeleteRelationshipList(resource, key, ctx, nameValues); + case "metadata": + return processDeleteMetadata(resource, key, ctx, nameValues); + } } @@ -1594,6 +1604,69 @@ public abstract class AAIDeclarations implements AAIClient { return QueryStatus.SUCCESS; } + private QueryStatus newModelProcessMetadata(Object instance, Map<String, String> params, String prefix, SvcLogicContext ctx) throws Exception { + + if (!(instance instanceof ServiceInstance) && !(instance instanceof Image)) { + throw new IllegalArgumentException("request is not applicable for selected request"); + } + + Class resourceClass = instance.getClass(); + Set<String> metadataKeys = new TreeSet<String>(); + Set<String> set = params.keySet(); + for(String attribute : set) { + if(attribute.startsWith("metadata")) { + metadataKeys.add(attribute); + } + } + + // 3. Process Metadata + // add metadata + if(!metadataKeys.isEmpty()) { + Metadata metadata = null; + Object obj = null; + Method getMetadataMethod = resourceClass.getMethod("getMetadata"); + if(getMetadataMethod != null){ + try { + getMetadataMethod.setAccessible(true); + obj = getMetadataMethod.invoke(instance); + } catch (InvocationTargetException x) { + Throwable cause = x.getCause(); + } + } + if(obj != null && obj instanceof Metadata){ + metadata = (Metadata)obj; + } else { + metadata = new Metadata(); + Method setMetadataMethod = resourceClass.getMethod("setMetadata", Metadata.class); + if(setMetadataMethod != null){ + try { + setMetadataMethod.setAccessible(true); + setMetadataMethod.invoke(instance, metadata); + } catch (InvocationTargetException x) { + } + } + } + + List<Metadatum> metadatumList = metadata.getMetadatum(); + int i = 0; + while(true){ + String metaNameKey = "metadata.metadatum[" + i + "].metaname"; + String metaValueKey = "metadata.metadatum[" + i + "].metaval"; + if(!params.containsKey(metaNameKey) || !params.containsKey(metaValueKey)) + break; + + Metadatum metadatum = new Metadatum(); + metadatum.setMetaname(params.get(metaNameKey)); + metadatum.setMetaval(params.get(metaValueKey)); + metadatumList.add(metadatum); + + i++; + } + } + + return QueryStatus.SUCCESS; + } + private Relationship findRelationship(List<Relationship> relationships, String relatedTo) { if(relatedTo == null) return null; @@ -1779,7 +1852,7 @@ public abstract class AAIDeclarations implements AAIClient { for(Relationship targetRelationship : relationshipsToDelete) { String json_text = mapper.writeValueAsString(targetRelationship); - boolean response = deleteRelationshipList(deleteUrl, json_text); + boolean response = deleteList(deleteUrl, json_text); if(!response) cumulativeResponse = response; @@ -1796,6 +1869,90 @@ public abstract class AAIDeclarations implements AAIClient { } } + private QueryStatus processDeleteMetadata(String resource, String key, SvcLogicContext ctx, HashMap<String, String> nameValues) { + try { + AAIRequest request = AAIRequest.createRequest(resource, nameValues); + if(request == null) { + return QueryStatus.FAILURE; + } + + request.processRequestPathValues(nameValues); + URL url = request.getRequestUrl("GET", null); + + Class resourceClass = request.getModelClass(); + Object instance = getResource(url.toString(), resourceClass); + + // get resource version + String resourceVersion = null; + Method getResourceVersionMethod = resourceClass.getMethod("getResourceVersion"); + if(getResourceVersionMethod != null){ + try { + getResourceVersionMethod.setAccessible(true); + resourceVersion = (String) getResourceVersionMethod.invoke(instance); + } catch (InvocationTargetException x) { + } + } + + Metadata metadata = null; + Object obj = null; + Method getMetadataMethod = resourceClass.getMethod("getMetadata"); + if(getMetadataMethod != null){ + try { + getMetadataMethod.setAccessible(true); + obj = getMetadataMethod.invoke(instance); + } catch (InvocationTargetException x) { + Throwable cause = x.getCause(); + } + } + if(obj != null && obj instanceof Metadata){ + metadata = (Metadata)obj; + } else { + getLogger().debug("No metadata found to process."); + return QueryStatus.NOT_FOUND; + } + + if(metadata.getMetadatum() == null || metadata.getMetadatum().isEmpty()) { + return QueryStatus.NOT_FOUND; + } + + List<Metadatum> metadatumList = metadata.getMetadatum(); + Metadatum metadatumToDelete = null; + + final String metaname = nameValues.get("metaname"); + + for(Metadatum metadatum : metadatumList) { + getLogger().debug(String.format("Comparing existing metadatum of '%s' to keyword '%s'", metadatum.getMetaname(), metaname)); + if(metaname.equals(metadatum.getMetaname())) { + metadatumToDelete = metadatum; + break; + } + } + if(metadatumToDelete == null) { + getLogger().info(String.format("Metadatum has not been found for %s", key)); + return QueryStatus.NOT_FOUND; + } + + String path = url.toString(); + path = path + "/metadata/metadatum/" + encodeQuery( metadatumToDelete.getMetaname() ) + + "?resource-version=" + metadatumToDelete.getResourceVersion(); + URL deleteUrl = new URL(path); + boolean response = deleteList(deleteUrl, null); + + if(!response) + return QueryStatus.FAILURE; + + return QueryStatus.SUCCESS; + + } catch(Exception exc) { + getLogger().warn("processDelete", exc); + return QueryStatus.FAILURE; + } + } + + protected String encodeQuery(String param) throws UnsupportedEncodingException { + return URLEncoder.encode(param, "UTF-8").replace("+", "%20"); + } + static final Map<String, String> ctxGetBeginsWith( SvcLogicContext ctx, String prefix ) { Map<String, String> tmpPrefixMap = new HashMap<>(); @@ -1952,5 +2109,5 @@ public abstract class AAIDeclarations implements AAIClient { } public abstract <T> T getResource(String key, Class<T> type) throws AAIServiceException ; - protected abstract boolean deleteRelationshipList(URL url, String caller) throws AAIServiceException; + protected abstract boolean deleteList(URL url, String caller) throws AAIServiceException; } diff --git a/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java b/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java index 4781f2e2..d2f90159 100755 --- a/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java +++ b/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java @@ -381,6 +381,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe } else { LOG.debug("MetricLogger requestId is null"); } + con.setRequestProperty("Transfer-Encoding","chunked"); if(user_name != null && !user_name.isEmpty() && user_password != null && !user_password.isEmpty()) { String basicAuth = "Basic " + new String(Base64.encodeBase64((user_name + ":" + user_password).getBytes())); @@ -1088,11 +1089,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.PUT); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); - osw.close(); - + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + osw.close(); + } LOGwriteFirstTrace("PUT", request_url); LOGwriteDateTrace("hostname", hostname); @@ -1199,11 +1201,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.PUT); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); - osw.close(); - + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + osw.close(); + } LOGwriteFirstTrace("PUT", request_url); LOGwriteDateTrace("link-name", linkName); @@ -1311,11 +1314,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.PUT); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); - osw.close(); - + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + osw.close(); + } LOGwriteFirstTrace("PUT", request_url); LOGwriteDateTrace("service-id", linkName); @@ -1963,11 +1967,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.PUT); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); - osw.close(); - + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + osw.close(); + } LOGwriteFirstTrace("PUT", request_url); LOGwriteDateTrace("NotifyEvent", json_text); @@ -2330,6 +2335,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe LOG.warn(errorStringBuilder.toString(), exc); throw new AAIServiceException(exc); } finally { + if (con != null) { + con.disconnect(); + } if(inputStream != null){ try { inputStream.close(); @@ -2361,6 +2369,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe public String post(AAIRequest request) throws AAIServiceException { InputStream inputStream = null; String requestId = UUID.randomUUID().toString(); + HttpURLConnection con = null; try { String resourceVersion = null; @@ -2379,17 +2388,18 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe } URL requestUrl = null; - HttpURLConnection con = getConfiguredConnection(requestUrl = request.getRequestUrl(HttpMethod.PUT, resourceVersion), HttpMethod.PUT); + con = getConfiguredConnection(requestUrl = request.getRequestUrl(HttpMethod.PUT, resourceVersion), HttpMethod.PUT); ObjectMapper mapper = getObjectMapper(); String json_text = request.toJSONString(); LOGwriteDateTrace("data", json_text); logMetricRequest(requestId, "PUT "+requestUrl.getPath(), json_text, requestUrl.getPath()); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); - + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + } // Check for errors String responseMessage = con.getResponseMessage(); int responseCode = con.getResponseCode(); @@ -2428,6 +2438,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe LOG.warn("AAIRequestExecutor.post", exc); throw new AAIServiceException(exc); } finally { + if (con != null) { + con.disconnect(); + } try { if(inputStream != null) inputStream.close(); @@ -2558,7 +2571,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe } } - con = null; + if (con != null) { + con.disconnect(); + } } return response; } @@ -2582,9 +2597,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe LOGwriteDateTrace("data", json_text); logMetricRequest(requestId, "PATCH "+requestUrl.getPath(), json_text, requestUrl.getPath()); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + osw.close(); + } // Check for errors String responseMessage = con.getResponseMessage(); @@ -2892,7 +2910,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe } - protected boolean deleteRelationshipList(URL httpReqUrl, String json_text) throws AAIServiceException { + protected boolean deleteList(URL httpReqUrl, String json_text) throws AAIServiceException { if(httpReqUrl == null) { throw new NullPointerException(); } @@ -2905,11 +2923,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe // SSLSocketFactory sockFact = CTX.getSocketFactory(); // con.setSSLSocketFactory( sockFact ); - OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); - osw.write(json_text); - osw.flush(); - osw.close(); - + if (json_text != null) { + OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); + osw.write(json_text); + osw.flush(); + osw.close(); + } LOGwriteFirstTrace("DELETE", httpReqUrl.toString()); LOGwriteDateTrace("data", json_text); @@ -2950,7 +2969,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe } catch(AAIServiceException aaiexc) { throw aaiexc; } catch (Exception exc) { - LOG.warn("deleteRelationshipList", exc); + LOG.warn("deleteList", exc); throw new AAIServiceException(exc); } finally { if(inputStream != null){ |