helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | |
| 17 | package org.onosproject.net.intent.constraint; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onosproject.net.Link; |
| 21 | import org.onosproject.net.Path; |
| 22 | import org.onosproject.net.intent.Constraint; |
| 23 | import org.onosproject.net.intent.Intent; |
| 24 | import org.onosproject.net.intent.PointToPointIntent; |
| 25 | import org.onosproject.net.intent.ResourceContext; |
| 26 | |
| 27 | /** |
| 28 | * Constraint that determines whether to employ path protection. |
| 29 | */ |
| 30 | @Beta |
| 31 | public class ProtectionConstraint implements Constraint { |
Yuta HIGUCHI | 23547b3 | 2016-09-01 16:02:40 -0700 | [diff] [blame] | 32 | private static final ProtectionConstraint PROTECTION_CONSTRAINT = new ProtectionConstraint(); |
| 33 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 34 | // doesn't use LinkResourceService |
| 35 | @Override |
| 36 | public double cost(Link link, ResourceContext context) { |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | // doesn't use LinkResourceService |
| 41 | @Override |
| 42 | public boolean validate(Path path, ResourceContext context) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Determines whether to utilize path protection for the given intent. |
| 48 | * |
| 49 | * @param intent intent to be inspected |
| 50 | * @return whether the intent has a ProtectionConstraint |
| 51 | */ |
| 52 | public static boolean requireProtectedPath(Intent intent) { |
| 53 | if (intent instanceof PointToPointIntent) { |
| 54 | PointToPointIntent pointToPointIntent = (PointToPointIntent) intent; |
| 55 | return pointToPointIntent.constraints().stream() |
| 56 | .anyMatch(p -> p instanceof ProtectionConstraint); |
| 57 | } |
| 58 | return false; |
| 59 | } |
Yuta HIGUCHI | 23547b3 | 2016-09-01 16:02:40 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Returns protection constraint. |
| 63 | * |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 64 | * @return protection constraint |
Yuta HIGUCHI | 23547b3 | 2016-09-01 16:02:40 -0700 | [diff] [blame] | 65 | */ |
| 66 | public static ProtectionConstraint protection() { |
| 67 | return PROTECTION_CONSTRAINT; |
| 68 | } |
| 69 | |
| 70 | protected ProtectionConstraint() { |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String toString() { |
| 75 | return "Protection"; |
| 76 | } |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 77 | } |