2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.neutron.northbound.api;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15 import javax.ws.rs.core.UriInfo;
16 import org.opendaylight.neutron.spi.INeutronObject;
17 import org.opendaylight.neutron.spi.NeutronNetwork;
18 import org.opendaylight.neutron.spi.NeutronObject;
19 import org.opendaylight.neutron.spi.NeutronPort;
20 import org.opendaylight.neutron.spi.NeutronSubnet;
22 public final class PaginatedRequestFactory {
24 private static final Comparator<INeutronObject> NEUTRON_OBJECT_COMPARATOR = new Comparator<INeutronObject>() {
26 public int compare(INeutronObject o1, INeutronObject o2) {
27 return o1.getID().compareTo(o2.getID());
31 public static class PaginationResults<T extends INeutronObject<T>> {
33 List<NeutronPageLink> links;
35 public PaginationResults(List<T> collection, List<NeutronPageLink> links) {
36 this.collection = collection;
41 private static final class MarkerObject extends NeutronObject<MarkerObject>
42 implements INeutronObject<MarkerObject> {
43 private final String id;
45 MarkerObject(String id) {
50 public String getID() {
55 public void setID(String newId) {
56 throw new UnsupportedOperationException("Marker has constant ID");
60 public MarkerObject extractFields(List<String> fields) {
61 throw new UnsupportedOperationException("extractFields shouldn't be called for MarkerObject");
65 private PaginatedRequestFactory() {
69 * SuppressWarnings is needed because the compiler does not understand that we
70 * are actually safe here.
72 * FIXME: the only caller performs a cast back, so this is not actually necessary.
74 @SuppressWarnings("unchecked")
75 public static <T extends INeutronObject<T>> INeutronRequest<T> createRequest(Integer limit, String marker,
76 Boolean pageReverse, UriInfo uriInfo, List<T> collection, Class<T> clazz) {
77 PaginationResults<T> results = paginate(limit, marker, pageReverse, uriInfo, collection);
79 if (clazz.equals(NeutronNetwork.class)) {
80 return (INeutronRequest<T>) new NeutronNetworkRequest((List<NeutronNetwork>) results.collection,
83 if (clazz.equals(NeutronSubnet.class)) {
84 return (INeutronRequest<T>) new NeutronSubnetRequest((List<NeutronSubnet>) results.collection,
87 if (clazz.equals(NeutronPort.class)) {
88 return (INeutronRequest<T>) new NeutronPortRequest((List<NeutronPort>) results.collection, results.links);
93 private static <T extends INeutronObject<T>> PaginationResults<T> paginate(Integer limit, String marker,
94 Boolean pageReverse, UriInfo uriInfo, List<T> collection) {
95 List<NeutronPageLink> links = new ArrayList<>();
100 Boolean lastPage = false;
102 Collections.sort(collection, NEUTRON_OBJECT_COMPARATOR);
104 if (marker != null) {
105 int offset = Collections.binarySearch(collection, new MarkerObject(marker), NEUTRON_OBJECT_COMPARATOR);
107 throw new ResourceNotFoundException("UUID for marker: " + marker + " could not be found");
111 startPos = offset + 1;
113 startPos = offset - limit;
119 firstPage = startPos == 0;
121 if (startPos + limit >= collection.size()) {
122 collection = collection.subList(startPos, collection.size());
123 startMarker = collection.get(0).getID();
124 endMarker = collection.get(collection.size() - 1).getID();
126 } else if (startPos < 0) {
127 if (startPos + limit > 0) {
128 collection = collection.subList(0, startPos + limit);
129 startMarker = collection.get(0).getID();
130 endMarker = collection.get(collection.size() - 1).getID();
133 throw new BadRequestException(
134 "Requested page is out of bounds. Please check the supplied limit and marker");
137 collection = collection.subList(startPos, startPos + limit);
138 startMarker = collection.get(0).getID();
139 endMarker = collection.get(limit - 1).getID();
143 NeutronPageLink next = new NeutronPageLink();
145 next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
150 NeutronPageLink previous = new NeutronPageLink();
151 previous.setRef("previous");
152 previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker="
153 + startMarker + "&page_reverse=True");
157 return new PaginationResults<>(collection, links);