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