--- /dev/null
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Authors : Dave Tucker
+ */
+
+package org.opendaylight.controller.networkconfig.neutron;
+
+/**
+ * This class contains behaviour common to Neutron configuration objects
+ */
+public interface INeutronObject {
+ public String getID();
+ public void setID(String id);
+}
@XmlRootElement(name = "network")
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronNetwork extends ConfigurationObject implements Serializable {
+public class NeutronNetwork extends ConfigurationObject implements Serializable, INeutronObject {
// See OpenStack Network API v2.0 Reference for description of
// annotated attributes
public String getID() { return networkUUID; }
+ public void setID(String id) { this.networkUUID = id; }
+
public String getNetworkUUID() {
return networkUUID;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronPort extends ConfigurationObject implements Serializable {
+public class NeutronPort extends ConfigurationObject implements Serializable, INeutronObject {
private static final long serialVersionUID = 1L;
// See OpenStack Network API v2.0 Reference for description of
public String getID() { return portUUID; }
+ public void setID(String id) { this.portUUID = id; }
+
public String getPortUUID() {
return portUUID;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronRouter extends ConfigurationObject implements Serializable {
+public class NeutronRouter extends ConfigurationObject implements Serializable, INeutronObject {
private static final long serialVersionUID = 1L;
// See OpenStack Network API v2.0 Reference for description of
public String getID() { return routerUUID; }
+ public void setID(String id) { this.routerUUID = id; }
+
public String getRouterUUID() {
return routerUUID;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronSubnet extends ConfigurationObject implements Serializable {
+public class NeutronSubnet extends ConfigurationObject implements Serializable, INeutronObject {
private static final long serialVersionUID = 1L;
// See OpenStack Network API v2.0 Reference for description of
public String getID() { return subnetUUID; }
+ public void setID(String id) { this.subnetUUID = id; }
+
public String getSubnetUUID() {
return subnetUUID;
}
--- /dev/null
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Authors : Dave Tucker
+ */
+
+package org.opendaylight.controller.networkconfig.neutron.northbound;
+
+import org.opendaylight.controller.networkconfig.neutron.INeutronObject;
+
+import java.util.List;
+
+public interface INeutronRequest<T extends INeutronObject> {
+ public T getSingleton();
+ public boolean isSingleton();
+ public List<T> getBulk();
+}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronNetworkRequest {
+public class NeutronNetworkRequest implements INeutronRequest {
// See OpenStack Network API v2.0 Reference for description of
// annotated attributes
@XmlElement(name="networks")
List<NeutronNetwork> bulkRequest;
+ @XmlElement(name="networks_links")
+ List<NeutronPageLink> links;
+
NeutronNetworkRequest() {
}
+ NeutronNetworkRequest(List<NeutronNetwork> bulkRequest, List<NeutronPageLink> links) {
+ this.bulkRequest = bulkRequest;
+ this.links = links;
+ this.singletonNetwork = null;
+ }
+
NeutronNetworkRequest(List<NeutronNetwork> bulk) {
bulkRequest = bulk;
singletonNetwork = null;
package org.opendaylight.controller.networkconfig.neutron.northbound;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
import org.codehaus.enunciate.jaxrs.TypeHint;
+
import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkAware;
import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
@QueryParam("provider_network_type") String queryProviderNetworkType,
@QueryParam("provider_physical_network") String queryProviderPhysicalNetwork,
@QueryParam("provider_segmentation_id") String queryProviderSegmentationID,
- // pagination
+ // linkTitle
@QueryParam("limit") Integer limit,
@QueryParam("marker") String marker,
@DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
// sorting not supported
) {
- INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
+ INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
if (networkInterface == null) {
throw new ServiceUnavailableException("Network CRUD Interface "
+ RestMessages.SERVICEUNAVAILABLE.toString());
}
}
- Comparator<NeutronNetwork> neutronNetworkComparator = new Comparator<NeutronNetwork>() {
- @Override
- public int compare(NeutronNetwork o1, NeutronNetwork o2) {
- return o1.getID().compareTo(o2.getID());
- }
- };
-
- Collections.sort(ans, neutronNetworkComparator);
-
if (limit != null && ans.size() > 1) {
- List<NeutronPageLink> links = new ArrayList<>();
- Integer startPos = null;
- String startMarker;
- String endMarker;
- Boolean firstPage = false;
- Boolean lastPage = false;
-
- if (marker == null) {
- startPos = 0;
- }
-
- else {
-
- NeutronNetwork markerNetwork = new NeutronNetwork();
- markerNetwork.setNetworkUUID(marker);
-
- startPos = Collections.binarySearch(ans, markerNetwork, neutronNetworkComparator);
-
- if (!pageReverse){
- startPos = startPos + 1;
- }
- else {
- startPos = startPos - limit;
- }
-
- }
-
- if (startPos == null) {
- throw new ResourceNotFoundException("UUID for marker:" + marker + " could not be found");
- }
-
- if (startPos == 0){
- firstPage = true;
- }
-
- if (startPos + limit >= ans.size()) {
- ans = ans.subList(startPos, ans.size());
- startMarker = ans.get(0).getID();
- endMarker = ans.get(ans.size() - 1).getID();
- lastPage = true;
- }
- else if (startPos < 0) {
- if (startPos + limit > 0) {
- ans = ans.subList(0, startPos + limit);
- startMarker = ans.get(0).getID();
- endMarker = ans.get(ans.size() - 1).getID();
- firstPage = true;
- }
- else {
- throw new BadRequestException("Requested page is out of bounds. Please check the supplied limit and marker");
- }
- }
- else {
- ans = ans.subList(startPos, startPos + limit);
- startMarker = ans.get(0).getID();
- endMarker = ans.get(limit-1).getID();
- }
-
- if (!lastPage) {
- NeutronPageLink next = new NeutronPageLink();
- next.setRef("next");
- next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
- links.add(next);
- }
-
- if (!firstPage) {
- NeutronPageLink previous = new NeutronPageLink();
- previous.setRef("previous");
- previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + startMarker + "&page_reverse=True");
- links.add(previous);
- }
-
- return Response.status(200).entity(new PaginatedNeutronNetworkRequest(ans, links)).build();
+ // Return a paginated request
+ NeutronNetworkRequest request = (NeutronNetworkRequest) PaginatedRequestFactory.createRequest(limit,
+ marker, pageReverse, uriInfo, ans, NeutronNetwork.class);
+ return Response.status(200).entity(request).build();
}
return Response.status(200).entity(new NeutronNetworkRequest(ans)).build();
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronPortRequest {
+public class NeutronPortRequest implements INeutronRequest {
// See OpenStack Network API v2.0 Reference for description of
// annotated attributes
@XmlElement(name="ports")
List<NeutronPort> bulkRequest;
+ @XmlElement(name="ports_links")
+ List<NeutronPageLink> links;
+
NeutronPortRequest() {
}
+ public NeutronPortRequest(List<NeutronPort> bulkRequest, List<NeutronPageLink> links) {
+ this.bulkRequest = bulkRequest;
+ this.links = links;
+ this.singletonPort = null;
+ }
+
NeutronPortRequest(List<NeutronPort> bulk) {
bulkRequest = bulk;
singletonPort = null;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
return o.extractFields(fields);
}
+ @Context
+ UriInfo uriInfo;
+
/**
* Returns a list of all Ports */
@QueryParam("device_id") String queryDeviceID,
@QueryParam("device_owner") String queryDeviceOwner,
@QueryParam("tenant_id") String queryTenantID,
- // pagination
- @QueryParam("limit") String limit,
+ // linkTitle
+ @QueryParam("limit") Integer limit,
@QueryParam("marker") String marker,
- @QueryParam("page_reverse") String pageReverse
+ @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
// sorting not supported
) {
INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
}
}
}
- //TODO: apply pagination to results
+
+ if (limit != null && ans.size() > 1) {
+ // Return a paginated request
+ NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit,
+ marker, pageReverse, uriInfo, ans, NeutronPort.class);
+ return Response.status(200).entity(request).build();
+ }
+
return Response.status(200).entity(
new NeutronPortRequest(ans)).build();
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
-public class NeutronSubnetRequest {
+public class NeutronSubnetRequest implements INeutronRequest {
// See OpenStack Network API v2.0 Reference for description of
// annotated attributes
@XmlElement(name="subnets")
List<NeutronSubnet> bulkRequest;
+ @XmlElement(name="subnets_links")
+ List<NeutronPageLink> links;
+
NeutronSubnetRequest() {
}
+ public NeutronSubnetRequest(List<NeutronSubnet> bulkRequest, List<NeutronPageLink> links) {
+ this.bulkRequest = bulkRequest;
+ this.links = links;
+ this.singletonSubnet = null;
+ }
+
NeutronSubnetRequest(List<NeutronSubnet> bulk) {
bulkRequest = bulk;
singletonSubnet = null;
+ links = null;
}
NeutronSubnetRequest(NeutronSubnet subnet) {
singletonSubnet = subnet;
+ bulkRequest = null;
+ links = null;
}
public NeutronSubnet getSingleton() {
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
return o.extractFields(fields);
}
+ @Context
+ UriInfo uriInfo;
/**
* Returns a list of all Subnets */
@QueryParam("tenant_id") String queryTenantID,
@QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
@QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
- // pagination
- @QueryParam("limit") String limit,
+ // linkTitle
+ @QueryParam("limit") Integer limit,
@QueryParam("marker") String marker,
- @QueryParam("page_reverse") String pageReverse
+ @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
// sorting not supported
) {
- INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
+ INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
if (subnetInterface == null) {
throw new ServiceUnavailableException("Subnet CRUD Interface "
+ RestMessages.SERVICEUNAVAILABLE.toString());
}
}
}
- //TODO: apply pagination to results
+
+ if (limit != null && ans.size() > 1) {
+ // Return a paginated request
+ NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit,
+ marker, pageReverse, uriInfo, ans, NeutronSubnet.class);
+ return Response.status(200).entity(request).build();
+ }
+
return Response.status(200).entity(
new NeutronSubnetRequest(ans)).build();
}
+++ /dev/null
-/*
- * Copyright (C) 2014 Hewlett-Packard Development Company L.P
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
- * Authors : Dave Tucker
- */
-
-package org.opendaylight.controller.networkconfig.neutron.northbound;
-
-import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
-
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import java.util.List;
-
-@XmlRootElement
-@XmlAccessorType(XmlAccessType.NONE)
-
-public class PaginatedNeutronNetworkRequest {
-
- @XmlElement (name="networks")
- List<NeutronNetwork> networks;
-
- @XmlElement (name="network_links")
- List<NeutronPageLink> networkLinks;
-
- public PaginatedNeutronNetworkRequest() {
- }
-
- public PaginatedNeutronNetworkRequest(List<NeutronNetwork> networks, List<NeutronPageLink> networkLinks) {
- this.networks = networks;
- this.networkLinks = networkLinks;
- }
-
- public List<NeutronNetwork> getNetworks() {
- return networks;
- }
-
- public void setNetworks(List<NeutronNetwork> networks) {
- this.networks = networks;
- }
-
- public List<NeutronPageLink> getNetworkLinks() {
- return networkLinks;
- }
-
- public void setNetworkLinks(List<NeutronPageLink> networkLinks) {
- this.networkLinks = networkLinks;
- }
-}
--- /dev/null
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Authors : Dave Tucker
+ */
+
+package org.opendaylight.controller.networkconfig.neutron.northbound;
+
+import org.opendaylight.controller.networkconfig.neutron.INeutronObject;
+import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
+import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
+import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
+import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
+import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
+
+import javax.ws.rs.core.UriInfo;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class PaginatedRequestFactory {
+
+ public static class PaginationResults<T extends INeutronObject> {
+ List<T> collection;
+ List<NeutronPageLink> links;
+
+ public PaginationResults(List<T> collection, List<NeutronPageLink> links) {
+ this.collection = collection;
+ this.links = links;
+ }
+ }
+
+ public static <T extends INeutronObject> INeutronRequest createRequest(Integer limit, String marker,
+ Boolean pageReverse,
+ UriInfo uriInfo,
+ List<T> collection,
+ Class<T> clazz) {
+ PaginationResults results = _paginate(limit, marker, pageReverse, uriInfo, collection);
+
+ if (clazz.equals(NeutronNetwork.class)){
+ return new NeutronNetworkRequest(results.collection, results.links);
+ }
+ if (clazz.equals(NeutronSubnet.class)){
+ return new NeutronSubnetRequest(results.collection, results.links);
+ }
+ if (clazz.equals(NeutronPort.class)){
+ return new NeutronPortRequest(results.collection, results.links);
+ }
+ return null;
+ }
+
+ private static <T extends INeutronObject> PaginationResults _paginate(Integer limit, String marker, Boolean pageReverse, UriInfo uriInfo, List<T> collection) {
+ List<NeutronPageLink> links = new ArrayList<>();
+ Integer startPos = null;
+ String startMarker;
+ String endMarker;
+ Boolean firstPage = false;
+ Boolean lastPage = false;
+
+ Comparator<INeutronObject> neutronObjectComparator = new Comparator<INeutronObject>() {
+ @Override
+ public int compare(INeutronObject o1, INeutronObject o2) {
+ return o1.getID().compareTo(o2.getID());
+ }
+ };
+
+ Collections.sort(collection, neutronObjectComparator);
+
+ if (marker == null) {
+ startPos = 0;
+ }
+
+ else {
+
+ class MarkerObject implements INeutronObject {
+ private String id;
+
+ public String getID() {
+ return id;
+ }
+
+ public void setID(String id) {
+ this.id = id;
+ }
+ }
+
+ INeutronObject markerObject = new MarkerObject();
+
+ markerObject.setID(marker);
+
+ startPos = Collections.binarySearch(collection, markerObject, neutronObjectComparator);
+
+ if (!pageReverse){
+ startPos = startPos + 1;
+ }
+ else {
+ startPos = startPos - limit;
+ }
+
+ }
+
+ if (startPos == null) {
+ throw new ResourceNotFoundException("UUID for marker:" + marker + " could not be found");
+ }
+
+ if (startPos == 0){
+ firstPage = true;
+ }
+
+ if (startPos + limit >= collection.size()) {
+ collection = collection.subList(startPos, collection.size());
+ startMarker = collection.get(0).getID();
+ endMarker = collection.get(collection.size() - 1).getID();
+ lastPage = true;
+ }
+ else if (startPos < 0) {
+ if (startPos + limit > 0) {
+ collection = collection.subList(0, startPos + limit);
+ startMarker = collection.get(0).getID();
+ endMarker = collection.get(collection.size() - 1).getID();
+ firstPage = true;
+ }
+ else {
+ throw new BadRequestException("Requested page is out of bounds. Please check the supplied limit and marker");
+ }
+ }
+ else {
+ collection = collection.subList(startPos, startPos + limit);
+ startMarker = collection.get(0).getID();
+ endMarker = collection.get(limit-1).getID();
+ }
+
+ if (!lastPage) {
+ NeutronPageLink next = new NeutronPageLink();
+ next.setRef("next");
+ next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
+ links.add(next);
+ }
+
+ if (!firstPage) {
+ NeutronPageLink previous = new NeutronPageLink();
+ previous.setRef("previous");
+ previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + startMarker + "&page_reverse=True");
+ links.add(previous);
+ }
+
+ return new PaginationResults(collection, links);
+ }
+}