From: Daniel Silverthorn Date: Mon, 22 Jan 2018 16:28:42 +0000 (-0500) Subject: Add query parameters to get properties X-Git-Tag: 2.0.0-ONAP~36 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fgizmo.git;a=commitdiff_plain;h=a0e716dc093cd8a4a4ec8aaca7bc1635e518527a Add query parameters to get properties Issue-ID: AAI-685 Change-Id: Id06a08ef668591560d276ef8a79c095f31d8c85b Signed-off-by: Daniel Silverthorn --- diff --git a/VERTEX.md b/VERTEX.md index 603d748..b87e4bb 100644 --- a/VERTEX.md +++ b/VERTEX.md @@ -198,7 +198,52 @@ Optionally, a vertex can be created by posting to an endpoint which doesn't incl Code: 500 (Internal Server Error) Content: Error message describing the failure. - Situation: Any scenario not covered by the above error codes. + Situation: Any scenario not covered by the above error codes. + +### Get Vertices with Properties +Note: Adding query param of properties=all will return all properties + + URL: https://:9520/services/inventory/v11/pserver/ + Optional Query Param: ?equip-vendor=HP + Optional Query Param: ?properties=hostname&properties=equip-vendor + Method: GET + Success Response: + Code: 200 + Content: + [ + { + "idfdsa": "1263346e-372b-4681-8ce4-d40411620487", + "type": "pserver", + "url": "services/inventory/v11/pserver/1263346e-372b-4681-8ce4-d40411620487", + "properties": { + "equip-vendor": "HP", + "hostname": "mtanjasdf119snd" + } + }, + { + "idfdsa": "b57a9e54-bbb5-4e11-b537-aaa7bc8fd726", + "type": "pserver", + "url": "services/inventory/v11/pserver/b57a9e54-bbb5-4e11-b537-aaa7bc8fd726", + "properties": { + "equip-vendor": "HP", + "hostname": "mtanjasdf119snd" + } + } + ] + Error Response: + Code: 404 (NOT FOUND) + Situation: Resource Not found + + Code: 403 (FORBIDDEN) + Content: Error message describing the Authorization failure. + Situation: Authorization failure. + + Code: 415 (UNSUPPORTED MEDIA TYPE) + Situation: Unsupported content type . + + Code: 500 (Internal Server Error) + Content: Error message describing the failure. + Situation: Any scenario not covered by the above error codes. ### Update Vertex diff --git a/src/main/java/org/onap/crud/dao/GraphDao.java b/src/main/java/org/onap/crud/dao/GraphDao.java index bc42b15..c62a788 100644 --- a/src/main/java/org/onap/crud/dao/GraphDao.java +++ b/src/main/java/org/onap/crud/dao/GraphDao.java @@ -23,6 +23,7 @@ */ package org.onap.crud.dao; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -61,6 +62,21 @@ public interface GraphDao { */ public List getVertices(String type, Map filter) throws CrudException; + /** + * Retrieve a collection of {@link Vertex} objects which match the supplied + * type label and filter properties. + * + * @param type + * - The vertex type that we want to retrieve. + * @param filter + * - The parameters to filter our results by. + * @param properties + * - The properties to retrieve with the vertex + * @return - A collection of vertices. + * @throws CrudException + */ + public List getVertices(String type, Map filter, HashSet properties) throws CrudException; + /** * Retrieve an {@link Edge} from the graph database by specifying its unique * identifier. diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java index adf2a3a..ecbac36 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -50,6 +50,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import javax.ws.rs.core.MediaType; @@ -150,10 +151,17 @@ public class ChampDao implements GraphDao { @Override public List getVertices(String type, Map filter) throws CrudException { + return getVertices(type, filter, new HashSet()); + } + + @Override + public List getVertices(String type, Map filter, HashSet properties) throws CrudException { filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + List queryParams = convertToNameValuePair(filter); + queryParams.addAll(convertToNameValuePair("properties", properties)); String url = baseObjectUrl + "/filter" + "?" - + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); + + URLEncodedUtils.format(queryParams, Charset.defaultCharset()); OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); @@ -547,6 +555,15 @@ public class ChampDao implements GraphDao { return nvpList; } + + // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient + private List convertToNameValuePair(String key, HashSet values) { + List nvpList = new ArrayList<>(values.size()); + + values.forEach((value) -> nvpList.add(new BasicNameValuePair(key, value))); + + return nvpList; + } private Map> createHeader() { Map> headers = new HashMap<>(); diff --git a/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java b/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java index 62d1408..82fa4ba 100644 --- a/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java +++ b/src/main/java/org/onap/crud/parser/CrudResponseBuilder.java @@ -153,6 +153,13 @@ public class CrudResponseBuilder { item.addProperty("id", v.getId().get()); item.addProperty("type", v.getType()); item.addProperty("url", "services/inventory/" + version + "/" + v.getType() + "/" + v.getId().get()); + if (!v.getProperties().isEmpty()) { + JsonObject propertiesObject = new JsonObject(); + for (String key : v.getProperties().keySet()) { + propertiesObject.addProperty(key, v.getProperties().get(key).toString()); + } + item.add("properties", propertiesObject); + } arry.add(item); } diff --git a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java index 60241cc..ef276a3 100644 --- a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java +++ b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java @@ -25,6 +25,7 @@ package org.onap.crud.service; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -72,9 +73,9 @@ public abstract class AbstractGraphDataService { version); } - public String getVertices(String version, String type, Map filter) throws CrudException { + public String getVertices(String version, String type, Map filter, HashSet properties) throws CrudException { type = OxmModelValidator.resolveCollectionType(version, type); - List items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter)); + List items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties); return CrudResponseBuilder.buildGetVerticesResponse(items, version); } diff --git a/src/main/java/org/onap/crud/service/CrudRestService.java b/src/main/java/org/onap/crud/service/CrudRestService.java index 0ea07f0..2068709 100644 --- a/src/main/java/org/onap/crud/service/CrudRestService.java +++ b/src/main/java/org/onap/crud/service/CrudRestService.java @@ -9,18 +9,19 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * + *

* ECOMP is a trademark and service mark of AT&T Intellectual Property. */ + package org.onap.crud.service; import com.google.gson.JsonElement; @@ -32,6 +33,7 @@ import org.onap.aaiauth.auth.Auth; import org.onap.crud.exception.CrudException; import org.onap.crud.logging.CrudServiceMsgs; import org.onap.crud.logging.LoggingUtil; +import org.onap.crud.util.CrudProperties; import org.onap.crud.util.CrudServiceConstants; import org.onap.crud.util.CrudServiceUtil; import org.slf4j.MDC; @@ -39,13 +41,26 @@ import org.slf4j.MDC; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import javax.security.auth.x500.X500Principal; import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.*; -import javax.ws.rs.core.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.Encoded; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; +import javax.ws.rs.core.UriInfo; public class CrudRestService { @@ -115,13 +130,27 @@ public class CrudRestService { Response response = null; try { if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + String propertiesKey = CrudProperties.get(CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY); Map filter = new HashMap(); for (Map.Entry> e : uriInfo.getQueryParameters().entrySet()) { filter.put(e.getKey(), e.getValue().get(0)); } - String result = graphDataService.getVertices(version, type, filter); + for (Map.Entry> e : uriInfo.getQueryParameters().entrySet()) { + if (!e.getKey().equals(propertiesKey)) { + filter.put(e.getKey(), e.getValue().get(0)); + } + } + + HashSet properties; + if (uriInfo.getQueryParameters().containsKey(propertiesKey)) { + properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey)); + } else { + properties = new HashSet<>(); + } + + String result = graphDataService.getVertices(version, type, filter, properties); response = Response.status(Status.OK).entity(result).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); @@ -499,7 +528,7 @@ public class CrudRestService { } // check if ID is populate for modify/patch/delete operation if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify") - || opr.getValue().getAsString().equalsIgnoreCase("patch") + || opr.getValue().getAsString().equalsIgnoreCase("patch") || opr.getValue().getAsString().equalsIgnoreCase("delete"))) { throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); diff --git a/src/main/java/org/onap/crud/util/CrudServiceConstants.java b/src/main/java/org/onap/crud/util/CrudServiceConstants.java index 70db5e3..262623c 100644 --- a/src/main/java/org/onap/crud/util/CrudServiceConstants.java +++ b/src/main/java/org/onap/crud/util/CrudServiceConstants.java @@ -37,5 +37,6 @@ public class CrudServiceConstants { public static final String CRD_CHAMP_AUTH_FILE = CRD_HOME_AUTH + "champ-cert.p12"; public static final String CRD_AUTH_POLICY_NAME = "crud"; public static final String CRD_ASYNC_REQUEST_TIMEOUT = "crud.async.request.timeout"; - public static final String CRD_ASYNC_RESPONSE_PROCESS_POLL_INTERVAL = "crud.async.response.process.poll.interval"; + public static final String CRD_ASYNC_RESPONSE_PROCESS_POLL_INTERVAL = "crud.async.response.process.poll.interval"; + public static final String CRD_COLLECTION_PROPERTIES_KEY = "crud.collection.properties.key"; }