blob: 4e9fc9a146202635bb3613abdd407b60367a0e4c [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.Path;
20import org.onlab.onos.net.intent.Constraint;
21import org.onlab.onos.net.resource.LinkResourceService;
22
23/**
24 * Abstract base class for various constraints that evaluate link viability
25 * in a yes/no fashion.
26 */
27public abstract class BooleanConstraint implements Constraint {
28
29 /**
30 * Returns true if the specified link satisfies the constraint.
31 *
32 * @param link link to be validated
33 * @param resourceService resource service for checking available link resources
34 * @return true if link is viable
35 */
36 public abstract boolean isValid(Link link, LinkResourceService resourceService);
37
Sho SHIMIZUd6a187092014-11-05 09:06:29 -080038 /**
39 * {@inheritDoc}
40 *
41 * Negative return value means the specified link does not satisfy this constraint.
42 *
43 * @param link {@inheritDoc}
44 * @param resourceService {@inheritDoc}
45 * @return {@inheritDoc}
46 */
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080047 @Override
48 public double cost(Link link, LinkResourceService resourceService) {
49 return isValid(link, resourceService) ? +1 : -1;
50 }
51
52 @Override
53 public boolean validate(Path path, LinkResourceService resourceService) {
54 for (Link link : path.links()) {
Sho SHIMIZUc672d7d12014-11-10 19:20:47 -080055 if (!isValid(link, resourceService)) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080056 return false;
57 }
58 }
59 return true;
60 }
61
62}