5153aebf45d7654470f3fe02e77a00a366b1d90c
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.net.resource.link;
17
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.Set;
21 import java.util.Objects;
22
23 import org.onlab.util.Bandwidth;
24 import org.onosproject.net.Link;
25 import org.onosproject.net.intent.Constraint;
26 import org.onosproject.net.intent.IntentId;
27
28 import com.google.common.collect.ImmutableSet;
29
30 import org.onosproject.net.intent.constraint.BandwidthConstraint;
31 import org.onosproject.net.intent.constraint.LambdaConstraint;
32 import org.onosproject.net.resource.ResourceRequest;
33 import org.onosproject.net.resource.ResourceType;
34
35 /**
36  * Implementation of {@link LinkResourceRequest}.
37  */
38 public final class DefaultLinkResourceRequest implements LinkResourceRequest {
39
40     private final IntentId intentId;
41     private final Collection<Link> links;
42     private final Set<ResourceRequest> resources;
43
44     /**
45      * Creates a new link resource request with the given ID, links, and
46      * resource requests.
47      *
48      * @param intentId intent ID related to this request
49      * @param links a set of links for the request
50      * @param resources a set of resources to be requested
51      */
52     private DefaultLinkResourceRequest(IntentId intentId,
53             Collection<Link> links,
54             Set<ResourceRequest> resources) {
55         this.intentId = intentId;
56         this.links = ImmutableSet.copyOf(links);
57         this.resources = ImmutableSet.copyOf(resources);
58     }
59
60
61     @Override
62     public ResourceType type() {
63         return null;
64     }
65
66     @Override
67     public IntentId intentId() {
68         return intentId;
69     }
70
71     @Override
72     public Collection<Link> links() {
73         return links;
74     }
75
76     @Override
77     public Set<ResourceRequest> resources() {
78         return resources;
79     }
80
81     /**
82      * Returns builder of link resource request.
83      *
84      * @param intentId intent ID related to this request
85      * @param links a set of links for the request
86      * @return builder of link resource request
87      */
88     public static LinkResourceRequest.Builder builder(
89             IntentId intentId, Collection<Link> links) {
90         return new Builder(intentId, links);
91     }
92
93     /**
94      * Builder of link resource request.
95      */
96     public static final class Builder implements LinkResourceRequest.Builder {
97         private IntentId intentId;
98         private Collection<Link> links;
99         private Set<ResourceRequest> resources;
100
101         /**
102          * Creates a new link resource request.
103          *
104          * @param intentId intent ID related to this request
105          * @param links a set of links for the request
106          */
107         private Builder(IntentId intentId, Collection<Link> links) {
108             this.intentId = intentId;
109             this.links = links;
110             this.resources = new HashSet<>();
111         }
112
113         /**
114          * Adds lambda request.
115          *
116          * @return self
117          */
118         @Override
119         public Builder addLambdaRequest() {
120             resources.add(new LambdaResourceRequest());
121             return this;
122         }
123
124         /**
125          * Adds Mpls request.
126          *
127          * @return self
128          */
129         @Override
130         public Builder addMplsRequest() {
131             resources.add(new MplsLabelResourceRequest());
132             return this;
133         }
134
135         /**
136          * Adds bandwidth request with bandwidth value.
137          *
138          * @param bandwidth bandwidth value in bits per second to be requested
139          * @return self
140          */
141         @Override
142         public Builder addBandwidthRequest(double bandwidth) {
143             resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
144             return this;
145         }
146
147         @Override
148         public LinkResourceRequest.Builder addConstraint(Constraint constraint) {
149             if (constraint instanceof LambdaConstraint) {
150                 return addLambdaRequest();
151             } else if (constraint instanceof BandwidthConstraint) {
152                 BandwidthConstraint bw = (BandwidthConstraint) constraint;
153                 return addBandwidthRequest(bw.bandwidth().toDouble());
154             }
155             return this;
156         }
157
158         /**
159          * Returns link resource request.
160          *
161          * @return link resource request
162          */
163         @Override
164         public LinkResourceRequest build() {
165             return new DefaultLinkResourceRequest(intentId, links, resources);
166         }
167     }
168
169     @Override
170     public int hashCode() {
171         return Objects.hash(intentId, links);
172     }
173
174     @Override
175     public boolean equals(Object obj) {
176         if (this == obj) {
177             return true;
178         }
179         if (obj == null || getClass() != obj.getClass()) {
180             return false;
181         }
182         final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
183         return Objects.equals(this.intentId, other.intentId)
184                 && Objects.equals(this.links, other.links);
185     }
186 }