43b8e4b129dea985aa7be201524f4bbd219f1ff3
[onosfw.git] /
1 /*
2  * Copyright 2014 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.intent.constraint;
17
18 import com.google.common.annotations.Beta;
19 import org.onosproject.net.Link;
20 import org.onosproject.net.resource.link.BandwidthResource;
21 import org.onosproject.net.resource.link.BandwidthResourceRequest;
22 import org.onosproject.net.resource.link.LinkResourceService;
23 import org.onosproject.net.resource.ResourceRequest;
24 import org.onosproject.net.resource.ResourceType;
25
26 import java.util.Objects;
27
28 import static com.google.common.base.MoreObjects.toStringHelper;
29 import static com.google.common.base.Preconditions.checkNotNull;
30
31 /**
32  * Constraint that evaluates links based on available bandwidths.
33  */
34 @Beta
35 public class BandwidthConstraint extends BooleanConstraint {
36
37     private final BandwidthResource bandwidth;
38
39     /**
40      * Creates a new bandwidth constraint.
41      *
42      * @param bandwidth required bandwidth
43      */
44     public BandwidthConstraint(BandwidthResource bandwidth) {
45         this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
46     }
47
48     // Constructor for serialization
49     private BandwidthConstraint() {
50         this.bandwidth = null;
51     }
52
53     @Override
54     public boolean isValid(Link link, LinkResourceService resourceService) {
55         for (ResourceRequest request : resourceService.getAvailableResources(link)) {
56             if (request.type() == ResourceType.BANDWIDTH) {
57                 BandwidthResourceRequest brr = (BandwidthResourceRequest) request;
58                 if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) {
59                     return true;
60                 }
61             }
62         }
63         return false;
64     }
65
66     /**
67      * Returns the bandwidth required by this constraint.
68      *
69      * @return required bandwidth
70      */
71     public BandwidthResource bandwidth() {
72         return bandwidth;
73     }
74
75     @Override
76     public int hashCode() {
77         return Objects.hash(bandwidth);
78     }
79
80     @Override
81     public boolean equals(Object obj) {
82         if (this == obj) {
83             return true;
84         }
85         if (obj == null || getClass() != obj.getClass()) {
86             return false;
87         }
88         final BandwidthConstraint other = (BandwidthConstraint) obj;
89         return Objects.equals(this.bandwidth, other.bandwidth);
90     }
91
92     @Override
93     public String toString() {
94         return toStringHelper(this).add("bandwidth", bandwidth).toString();
95     }
96 }