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 {
26 private static final Comparator<INeutronObject> NEUTRON_OBJECT_COMPARATOR = new Comparator<INeutronObject>() {
28 public int compare(INeutronObject o1, INeutronObject o2) {
29 return o1.getID().compareTo(o2.getID());
33 public static class PaginationResults<T extends INeutronObject> {
35 List<NeutronPageLink> links;
37 public PaginationResults(List<T> collection, List<NeutronPageLink> links) {
38 this.collection = collection;
44 * SuppressWarnings is needed because the compiler does not understand that we
45 * are actually safe here.
47 * FIXME: the only caller performs a cast back, so this is not actually necessary.
49 @SuppressWarnings("unchecked")
50 public static <T extends INeutronObject> INeutronRequest<T> createRequest(Integer limit, String marker,
55 PaginationResults<T> results = _paginate(limit, marker, pageReverse, uriInfo, collection);
57 if (clazz.equals(NeutronNetwork.class)){
58 return (INeutronRequest<T>) new NeutronNetworkRequest((List<NeutronNetwork>) results.collection, results.links);
60 if (clazz.equals(NeutronSubnet.class)){
61 return (INeutronRequest<T>) new NeutronSubnetRequest((List<NeutronSubnet>) results.collection, results.links);
63 if (clazz.equals(NeutronPort.class)){
64 return (INeutronRequest<T>) new NeutronPortRequest((List<NeutronPort>) results.collection, results.links);
69 private static <T extends INeutronObject> PaginationResults<T> _paginate(Integer limit, String marker, Boolean pageReverse, UriInfo uriInfo, List<T> collection) {
70 List<NeutronPageLink> links = new ArrayList<>();
71 Integer startPos = null;
74 Boolean firstPage = false;
75 Boolean lastPage = false;
77 Collections.sort(collection, NEUTRON_OBJECT_COMPARATOR);
85 class MarkerObject implements INeutronObject {
89 public String getID() {
94 public void setID(String id) {
99 INeutronObject markerObject = new MarkerObject();
101 markerObject.setID(marker);
103 startPos = Collections.binarySearch(collection, markerObject, NEUTRON_OBJECT_COMPARATOR);
106 startPos = startPos + 1;
109 startPos = startPos - limit;
114 if (startPos == null) {
115 throw new ResourceNotFoundException("UUID for marker:" + marker + " could not be found");
122 if (startPos + limit >= collection.size()) {
123 collection = collection.subList(startPos, collection.size());
124 startMarker = collection.get(0).getID();
125 endMarker = collection.get(collection.size() - 1).getID();
128 else if (startPos < 0) {
129 if (startPos + limit > 0) {
130 collection = collection.subList(0, startPos + limit);
131 startMarker = collection.get(0).getID();
132 endMarker = collection.get(collection.size() - 1).getID();
136 throw new BadRequestException("Requested page is out of bounds. Please check the supplied limit and marker");
140 collection = collection.subList(startPos, startPos + limit);
141 startMarker = collection.get(0).getID();
142 endMarker = collection.get(limit-1).getID();
146 NeutronPageLink next = new NeutronPageLink();
148 next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
153 NeutronPageLink previous = new NeutronPageLink();
154 previous.setRef("previous");
155 previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + startMarker + "&page_reverse=True");
159 return new PaginationResults<T>(collection, links);