Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.Link; |
| 20 | import org.onosproject.net.Path; |
| 21 | import org.onosproject.net.intent.Constraint; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 22 | import org.onosproject.net.intent.ResourceContext; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Abstract base class for various constraints that evaluate link viability |
| 26 | * in a yes/no fashion. |
| 27 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 28 | @Beta |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 29 | public 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 35 | * @param context resource context for checking available resources |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 36 | * @return true if link is viable |
| 37 | */ |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 38 | public abstract boolean isValid(Link link, ResourceContext context); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 39 | |
Sho SHIMIZU | d6a18709 | 2014-11-05 09:06:29 -0800 | [diff] [blame] | 40 | /** |
| 41 | * {@inheritDoc} |
| 42 | * |
| 43 | * Negative return value means the specified link does not satisfy this constraint. |
| 44 | * |
| 45 | * @param link {@inheritDoc} |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 46 | * @param context {@inheritDoc} |
Sho SHIMIZU | d6a18709 | 2014-11-05 09:06:29 -0800 | [diff] [blame] | 47 | * @return {@inheritDoc} |
| 48 | */ |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 49 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 50 | public double cost(Link link, ResourceContext context) { |
| 51 | return isValid(link, context) ? +1 : -1; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 55 | public boolean validate(Path path, ResourceContext context) { |
Sho SHIMIZU | 36ef79f | 2016-02-15 18:47:08 -0800 | [diff] [blame] | 56 | return path.links().stream() |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 57 | .allMatch(link -> isValid(link, context)); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | } |