2 * Copyright (C) 2014 Red Hat, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Dave Tucker
11 package org.opendaylight.controller.networkconfig.neutron.northbound;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.List;
17 import javax.ws.rs.core.UriInfo;
18 import org.opendaylight.controller.networkconfig.neutron.INeutronObject;
19 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
20 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
21 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
22 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
23 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
25 public class PaginatedRequestFactory {
27 public static class PaginationResults<T extends INeutronObject> {
29 List<NeutronPageLink> links;
31 public PaginationResults(List<T> collection, List<NeutronPageLink> links) {
32 this.collection = collection;
38 * SuppressWarnings is needed because the compiler does not understand that we
39 * are actually safe here.
41 * FIXME: the only caller performs a cast back, so this is not actually necessary.
43 @SuppressWarnings("unchecked")
44 public static <T extends INeutronObject> INeutronRequest<T> createRequest(Integer limit, String marker,
49 PaginationResults<T> results = _paginate(limit, marker, pageReverse, uriInfo, collection);
51 if (clazz.equals(NeutronNetwork.class)){
52 return (INeutronRequest<T>) new NeutronNetworkRequest((List<NeutronNetwork>) results.collection, results.links);
54 if (clazz.equals(NeutronSubnet.class)){
55 return (INeutronRequest<T>) new NeutronSubnetRequest((List<NeutronSubnet>) results.collection, results.links);
57 if (clazz.equals(NeutronPort.class)){
58 return (INeutronRequest<T>) new NeutronPortRequest((List<NeutronPort>) results.collection, results.links);
63 private static <T extends INeutronObject> PaginationResults<T> _paginate(Integer limit, String marker, Boolean pageReverse, UriInfo uriInfo, List<T> collection) {
64 List<NeutronPageLink> links = new ArrayList<>();
65 Integer startPos = null;
68 Boolean firstPage = false;
69 Boolean lastPage = false;
71 Comparator<INeutronObject> neutronObjectComparator = new Comparator<INeutronObject>() {
73 public int compare(INeutronObject o1, INeutronObject o2) {
74 return o1.getID().compareTo(o2.getID());
78 Collections.sort(collection, neutronObjectComparator);
86 class MarkerObject implements INeutronObject {
90 public String getID() {
95 public void setID(String id) {
100 INeutronObject markerObject = new MarkerObject();
102 markerObject.setID(marker);
104 startPos = Collections.binarySearch(collection, markerObject, neutronObjectComparator);
107 startPos = startPos + 1;
110 startPos = startPos - limit;
115 if (startPos == null) {
116 throw new ResourceNotFoundException("UUID for marker:" + marker + " could not be found");
123 if (startPos + limit >= collection.size()) {
124 collection = collection.subList(startPos, collection.size());
125 startMarker = collection.get(0).getID();
126 endMarker = collection.get(collection.size() - 1).getID();
129 else if (startPos < 0) {
130 if (startPos + limit > 0) {
131 collection = collection.subList(0, startPos + limit);
132 startMarker = collection.get(0).getID();
133 endMarker = collection.get(collection.size() - 1).getID();
137 throw new BadRequestException("Requested page is out of bounds. Please check the supplied limit and marker");
141 collection = collection.subList(startPos, startPos + limit);
142 startMarker = collection.get(0).getID();
143 endMarker = collection.get(limit-1).getID();
147 NeutronPageLink next = new NeutronPageLink();
149 next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
154 NeutronPageLink previous = new NeutronPageLink();
155 previous.setRef("previous");
156 previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + startMarker + "&page_reverse=True");
160 return new PaginationResults<T>(collection, links);