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 org.opendaylight.controller.networkconfig.neutron.INeutronObject;
14 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
15 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
16 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
17 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
18 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
20 import javax.ws.rs.core.UriInfo;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.List;
26 public class PaginatedRequestFactory {
28 public static class PaginationResults<T extends INeutronObject> {
30 List<NeutronPageLink> links;
32 public PaginationResults(List<T> collection, List<NeutronPageLink> links) {
33 this.collection = collection;
38 public static <T extends INeutronObject> INeutronRequest createRequest(Integer limit, String marker,
43 PaginationResults results = _paginate(limit, marker, pageReverse, uriInfo, collection);
45 if (clazz.equals(NeutronNetwork.class)){
46 return new NeutronNetworkRequest(results.collection, results.links);
48 if (clazz.equals(NeutronSubnet.class)){
49 return new NeutronSubnetRequest(results.collection, results.links);
51 if (clazz.equals(NeutronPort.class)){
52 return new NeutronPortRequest(results.collection, results.links);
57 private static <T extends INeutronObject> PaginationResults _paginate(Integer limit, String marker, Boolean pageReverse, UriInfo uriInfo, List<T> collection) {
58 List<NeutronPageLink> links = new ArrayList<>();
59 Integer startPos = null;
62 Boolean firstPage = false;
63 Boolean lastPage = false;
65 Comparator<INeutronObject> neutronObjectComparator = new Comparator<INeutronObject>() {
67 public int compare(INeutronObject o1, INeutronObject o2) {
68 return o1.getID().compareTo(o2.getID());
72 Collections.sort(collection, neutronObjectComparator);
80 class MarkerObject implements INeutronObject {
83 public String getID() {
87 public void setID(String id) {
92 INeutronObject markerObject = new MarkerObject();
94 markerObject.setID(marker);
96 startPos = Collections.binarySearch(collection, markerObject, neutronObjectComparator);
99 startPos = startPos + 1;
102 startPos = startPos - limit;
107 if (startPos == null) {
108 throw new ResourceNotFoundException("UUID for marker:" + marker + " could not be found");
115 if (startPos + limit >= collection.size()) {
116 collection = collection.subList(startPos, collection.size());
117 startMarker = collection.get(0).getID();
118 endMarker = collection.get(collection.size() - 1).getID();
121 else if (startPos < 0) {
122 if (startPos + limit > 0) {
123 collection = collection.subList(0, startPos + limit);
124 startMarker = collection.get(0).getID();
125 endMarker = collection.get(collection.size() - 1).getID();
129 throw new BadRequestException("Requested page is out of bounds. Please check the supplied limit and marker");
133 collection = collection.subList(startPos, startPos + limit);
134 startMarker = collection.get(0).getID();
135 endMarker = collection.get(limit-1).getID();
139 NeutronPageLink next = new NeutronPageLink();
141 next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
146 NeutronPageLink previous = new NeutronPageLink();
147 previous.setRef("previous");
148 previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + startMarker + "&page_reverse=True");
152 return new PaginationResults(collection, links);