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