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