12953631291b41184281136a6ef55e68f37edda4
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / PaginatedRequestFactory.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.neutron.northbound.api;
10
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;
21
22 public final class PaginatedRequestFactory {
23
24     private static final Comparator<INeutronObject> NEUTRON_OBJECT_COMPARATOR = new Comparator<INeutronObject>() {
25         @Override
26         public int compare(INeutronObject o1, INeutronObject o2) {
27             return o1.getID().compareTo(o2.getID());
28         }
29     };
30
31     public static class PaginationResults<T extends INeutronObject<T>> {
32         List<T> collection;
33         List<NeutronPageLink> links;
34
35         public PaginationResults(List<T> collection, List<NeutronPageLink> links) {
36             this.collection = collection;
37             this.links = links;
38         }
39     }
40
41     private static final class MarkerObject extends NeutronObject<MarkerObject>
42             implements INeutronObject<MarkerObject> {
43         private final String id;
44
45         MarkerObject(String id) {
46             this.id = id;
47         }
48
49         @Override
50         public String getID() {
51             return id;
52         }
53
54         @Override
55         public void setID(String newId) {
56             throw new UnsupportedOperationException("Marker has constant ID");
57         }
58
59         @Override
60         public MarkerObject extractFields(List<String> fields) {
61             throw new UnsupportedOperationException("extractFields shouldn't be called for MarkerObject");
62         }
63     }
64
65     private PaginatedRequestFactory() {
66     }
67
68     /*
69      * SuppressWarnings is needed because the compiler does not understand that we
70      * are actually safe here.
71      *
72      * FIXME: the only caller performs a cast back, so this is not actually necessary.
73      */
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);
78
79         if (clazz.equals(NeutronNetwork.class)) {
80             return (INeutronRequest<T>) new NeutronNetworkRequest((List<NeutronNetwork>) results.collection,
81                     results.links);
82         }
83         if (clazz.equals(NeutronSubnet.class)) {
84             return (INeutronRequest<T>) new NeutronSubnetRequest((List<NeutronSubnet>) results.collection,
85                     results.links);
86         }
87         if (clazz.equals(NeutronPort.class)) {
88             return (INeutronRequest<T>) new NeutronPortRequest((List<NeutronPort>) results.collection, results.links);
89         }
90         return null;
91     }
92
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<>();
96         final int startPos;
97         String startMarker;
98         String endMarker;
99         Boolean firstPage;
100         Boolean lastPage = false;
101
102         Collections.sort(collection, NEUTRON_OBJECT_COMPARATOR);
103
104         if (marker != null) {
105             int offset = Collections.binarySearch(collection, new MarkerObject(marker), NEUTRON_OBJECT_COMPARATOR);
106             if (offset < 0) {
107                 throw new ResourceNotFoundException("UUID for marker: " + marker + " could not be found");
108             }
109
110             if (!pageReverse) {
111                 startPos = offset + 1;
112             } else {
113                 startPos = offset - limit;
114             }
115         } else {
116             startPos = 0;
117         }
118
119         firstPage = startPos == 0;
120
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();
125             lastPage = true;
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();
131                 firstPage = true;
132             } else {
133                 throw new BadRequestException(
134                         "Requested page is out of bounds. Please check the supplied limit and marker");
135             }
136         } else {
137             collection = collection.subList(startPos, startPos + limit);
138             startMarker = collection.get(0).getID();
139             endMarker = collection.get(limit - 1).getID();
140         }
141
142         if (!lastPage) {
143             NeutronPageLink next = new NeutronPageLink();
144             next.setRef("next");
145             next.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker=" + endMarker);
146             links.add(next);
147         }
148
149         if (!firstPage) {
150             NeutronPageLink previous = new NeutronPageLink();
151             previous.setRef("previous");
152             previous.setHref(uriInfo.getAbsolutePath().toString() + "?limit=" + limit.toString() + "&marker="
153                     + startMarker + "&page_reverse=True");
154             links.add(previous);
155         }
156
157         return new PaginationResults<>(collection, links);
158     }
159 }