diff options
Diffstat (limited to 'aai-service')
-rwxr-xr-x | aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIDeclarations.java | 274 | ||||
-rwxr-xr-x | aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java | 91 |
2 files changed, 294 insertions, 71 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 0f7e99818..c5a4c22ee 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; @@ -56,6 +58,7 @@ import org.openecomp.aai.inventory.v11.LogicalLink; import org.openecomp.aai.inventory.v11.Metadata; import org.openecomp.aai.inventory.v11.Metadatum; import org.openecomp.aai.inventory.v11.Pnf; +import org.openecomp.aai.inventory.v11.RelatedToProperty; import org.openecomp.aai.inventory.v11.Relationship; import org.openecomp.aai.inventory.v11.RelationshipData; import org.openecomp.aai.inventory.v11.RelationshipList; @@ -503,9 +506,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 +671,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); + } } @@ -1295,18 +1306,17 @@ public abstract class AAIDeclarations implements AAIClient { if(relatedLink != null) { relationship.setRelatedLink(relatedLink); } else { -// List<RelationshipData> relData = relationship.getRelationshipData(); Map<String, String> relParams = new HashMap<>(); - while(true) { - String searchRelationshipKey = "relationship-list.relationship[" + i + "].relationship-data[" + j + "].relationship-key"; - String searchRelationshipValue = "relationship-list.relationship[" + i + "].relationship-data[" + j + "].relationship-value"; - if(!params.containsKey(searchRelationshipKey)) - break; + while(true) { + String searchRelationshipKey = "relationship-list.relationship[" + i + "].relationship-data[" + j + "].relationship-key"; + String searchRelationshipValue = "relationship-list.relationship[" + i + "].relationship-data[" + j + "].relationship-value"; + if(!params.containsKey(searchRelationshipKey)) + break; - relParams.put(params.get(searchRelationshipKey), params.get(searchRelationshipValue)); - j++; - } + relParams.put(params.get(searchRelationshipKey), params.get(searchRelationshipValue)); + j++; + } AAIRequest rlRequest = AAIRequest.createRequest(relatedTo, relParams); for(Map.Entry<String,String> entry : relParams.entrySet()) { rlRequest.addRequestProperty(entry.getKey(), entry.getValue()); @@ -1314,6 +1324,26 @@ public abstract class AAIDeclarations implements AAIClient { String path = rlRequest.updatePathDataValues(null); relationship.setRelatedLink(path); } + { + int k = 0; + // process related to properties + Map<String, String> relParams = new HashMap<String, String>(); + + while(true) { + String searchRelatedToKey = "relationship-list.relationship[" + i + "].related-to-property[" + k + "].property-key"; + String searchRelatedToValue = "relationship-list.relationship[" + i + "].related-to-property[" + k + "].property-value"; + if(!params.containsKey(searchRelatedToKey)) + break; + + RelatedToProperty relDatum = new RelatedToProperty(); + relDatum.setPropertyKey(params.get(searchRelatedToKey)); + relDatum.setPropertyValue(params.get(searchRelatedToValue)); + relationship.getRelatedToProperty().add(relDatum); + + relParams.put(params.get(searchRelatedToKey), params.get(searchRelatedToValue)); + k++; + } + } i++; } } @@ -1554,7 +1584,6 @@ public abstract class AAIDeclarations implements AAIClient { if(!params.containsKey(searchKey)) break; - int j = 0; String relatedTo = params.get(searchKey); String relatedLinkKey = "relationship-list.relationship[" + i + "].related-link"; String relatedLink = null; @@ -1562,30 +1591,121 @@ public abstract class AAIDeclarations implements AAIClient { relatedLink = params.get(relatedLinkKey); } - Relationship relationship = new Relationship(); - relationships.add(relationship); - relationship.setRelatedTo(relatedTo); - if(relatedLink != null) { - relationship.setRelatedLink(relatedLink); - } else { - Map<String, String> relParams = new HashMap<>(); + Relationship relationship = new Relationship(); + relationships.add(relationship); + relationship.setRelatedTo(relatedTo); + if (relatedLink != null) { + relationship.setRelatedLink(relatedLink); + } else { + Map<String, String> relParams = new HashMap<>(); + int j = 0; + + while (true) { + String searchRelationshipKey = "relationship-list.relationship[" + i + "].relationship-data[" + + j + "].relationship-key"; + String searchRelationshipValue = "relationship-list.relationship[" + i + "].relationship-data[" + + j + "].relationship-value"; + if (!params.containsKey(searchRelationshipKey)) + break; + + RelationshipData relDatum = new RelationshipData(); + relDatum.setRelationshipKey(params.get(searchRelationshipKey)); + relDatum.setRelationshipValue(params.get(searchRelationshipValue)); + relationship.getRelationshipData().add(relDatum); + + relParams.put(params.get(searchRelationshipKey), params.get(searchRelationshipValue)); + j++; + } + AAIRequest rlRequest = AAIRequest.createRequest(relatedTo, relParams); + for (Map.Entry<String, String> entry : relParams.entrySet()) { + rlRequest.addRequestProperty(entry.getKey(), entry.getValue()); + } + String path = rlRequest.updatePathDataValues(null); + relationship.setRelatedLink(path); + } + { + int k = 0; + // process related to properties + Map<String, String> relParams = new HashMap<String, String>(); + + while(true) { + String searchRelatedToKey = "relationship-list.relationship[" + i + "].related-to-property[" + k + "].property-key"; + String searchRelatedToValue = "relationship-list.relationship[" + i + "].related-to-property[" + k + "].property-value"; + if(!params.containsKey(searchRelatedToKey)) + break; + + RelatedToProperty relDatum = new RelatedToProperty(); + relDatum.setPropertyKey(params.get(searchRelatedToKey)); + relDatum.setPropertyValue(params.get(searchRelatedToValue)); + relationship.getRelatedToProperty().add(relDatum); + + relParams.put(params.get(searchRelatedToKey), params.get(searchRelatedToValue)); + k++; + } + } + + i++; + } + } - while(true) { - String searchRelationshipKey = "relationship-list.relationship[" + i + "].relationship-data[" + j + "].relationship-key"; - String searchRelationshipValue = "relationship-list.relationship[" + i + "].relationship-data[" + j + "].relationship-value"; - if(!params.containsKey(searchRelationshipKey)) - break; + 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); + } + } - relParams.put(params.get(searchRelationshipKey), params.get(searchRelationshipValue)); - j++; + // 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(); } - AAIRequest rlRequest = AAIRequest.createRequest(relatedTo, relParams); - for(Map.Entry<String,String> entry : relParams.entrySet()) { - rlRequest.addRequestProperty(entry.getKey(), entry.getValue()); + } + 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) { } - String path = rlRequest.updatePathDataValues(null); - relationship.setRelatedLink(path); } + } + + 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++; } @@ -1779,7 +1899,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 +1916,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 +2156,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 4781f2e2b..d2f90159c 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){ |