Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 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 | */ |
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; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 22 | import org.onosproject.net.resource.link.LinkResourceService; |
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 |
| 35 | * @param resourceService resource service for checking available link resources |
| 36 | * @return true if link is viable |
| 37 | */ |
| 38 | public abstract boolean isValid(Link link, LinkResourceService resourceService); |
| 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} |
| 46 | * @param resourceService {@inheritDoc} |
| 47 | * @return {@inheritDoc} |
| 48 | */ |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 49 | @Override |
| 50 | public double cost(Link link, LinkResourceService resourceService) { |
| 51 | return isValid(link, resourceService) ? +1 : -1; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean validate(Path path, LinkResourceService resourceService) { |
Sho SHIMIZU | 36ef79f | 2016-02-15 18:47:08 -0800 | [diff] [blame] | 56 | return path.links().stream() |
| 57 | .allMatch(link -> isValid(link, resourceService)); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | } |