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 | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.Link; |
| 19 | import org.onosproject.net.Path; |
| 20 | import org.onosproject.net.intent.Constraint; |
| 21 | import org.onosproject.net.resource.LinkResourceService; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Abstract base class for various constraints that evaluate link viability |
| 25 | * in a yes/no fashion. |
| 26 | */ |
| 27 | public 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 SHIMIZU | d6a18709 | 2014-11-05 09:06:29 -0800 | [diff] [blame] | 38 | /** |
| 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 Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 47 | @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 SHIMIZU | c672d7d1 | 2014-11-10 19:20:47 -0800 | [diff] [blame] | 55 | if (!isValid(link, resourceService)) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | } |