blob: 9ce788d206806479fcdc31674d2b1763eb2e8616 [file] [log] [blame]
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.Link;
20import org.onosproject.net.Path;
21import org.onosproject.net.intent.Constraint;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080022import org.onosproject.net.intent.ResourceContext;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080023
24/**
25 * Abstract base class for various constraints that evaluate link viability
26 * in a yes/no fashion.
27 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040028@Beta
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080029public abstract class BooleanConstraint implements Constraint {
30
31 /**
32 * Returns true if the specified link satisfies the constraint.
33 *
34 * @param link link to be validated
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080035 * @param context resource context for checking available resources
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080036 * @return true if link is viable
37 */
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080038 public abstract boolean isValid(Link link, ResourceContext context);
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080039
Sho SHIMIZUd6a187092014-11-05 09:06:29 -080040 /**
41 * {@inheritDoc}
42 *
43 * Negative return value means the specified link does not satisfy this constraint.
44 *
45 * @param link {@inheritDoc}
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080046 * @param context {@inheritDoc}
Sho SHIMIZUd6a187092014-11-05 09:06:29 -080047 * @return {@inheritDoc}
48 */
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080049 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080050 public double cost(Link link, ResourceContext context) {
51 return isValid(link, context) ? +1 : -1;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080052 }
53
54 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080055 public boolean validate(Path path, ResourceContext context) {
Sho SHIMIZU36ef79f2016-02-15 18:47:08 -080056 return path.links().stream()
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080057 .allMatch(link -> isValid(link, context));
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080058 }
59
60}