7811a004d48ecd0ac090cc4a3558c7e2b1741d3a
[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.LambdaResource;
21 import org.onosproject.net.resource.link.LinkResourceService;
22 import org.onosproject.net.resource.ResourceRequest;
23 import org.onosproject.net.resource.ResourceType;
24
25 import java.util.Objects;
26
27 import static com.google.common.base.MoreObjects.toStringHelper;
28
29 /**
30  * Constraint that evaluates links based on available lambda.
31  */
32 @Beta
33 public class LambdaConstraint extends BooleanConstraint {
34
35     private final LambdaResource lambda;
36
37     /**
38      * Creates a new optical lambda constraint.
39      *
40      * @param lambda optional lambda to indicate a specific lambda
41      */
42     public LambdaConstraint(LambdaResource lambda) {
43         this.lambda = lambda;
44     }
45
46     // Constructor for serialization
47     private LambdaConstraint() {
48         this.lambda = null;
49     }
50
51     @Override
52     public boolean isValid(Link link, LinkResourceService resourceService) {
53         for (ResourceRequest request : resourceService.getAvailableResources(link)) {
54             if (request.type() == ResourceType.LAMBDA) {
55                 return true;
56             }
57         }
58         return false;
59     }
60
61     /**
62      * Returns the lambda required by this constraint.
63      *
64      * @return required lambda
65      */
66     public LambdaResource lambda() {
67         return lambda;
68     }
69
70     @Override
71     public int hashCode() {
72         return Objects.hashCode(lambda);
73     }
74
75     @Override
76     public boolean equals(Object obj) {
77         if (this == obj) {
78             return true;
79         }
80         if (obj == null || getClass() != obj.getClass()) {
81             return false;
82         }
83         final LambdaConstraint other = (LambdaConstraint) obj;
84         return Objects.equals(this.lambda, other.lambda);
85     }
86
87     @Override
88     public String toString() {
89         return toStringHelper(this).add("lambda", lambda).toString();
90     }
91 }