blob: d4d2c74808c7b07c19b69a93fc09e06f443dcae8 [file] [log] [blame]
helenyrwu2a674902016-07-20 09:48:04 -07001/*
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
17package org.onosproject.net.intent.constraint;
18
19import com.google.common.annotations.Beta;
20import org.onosproject.net.Link;
21import org.onosproject.net.Path;
22import org.onosproject.net.intent.Constraint;
23import org.onosproject.net.intent.Intent;
24import org.onosproject.net.intent.PointToPointIntent;
25import org.onosproject.net.intent.ResourceContext;
26
27/**
28 * Constraint that determines whether to employ path protection.
29 */
30@Beta
31public class ProtectionConstraint implements Constraint {
32 // doesn't use LinkResourceService
33 @Override
34 public double cost(Link link, ResourceContext context) {
35 return 1;
36 }
37
38 // doesn't use LinkResourceService
39 @Override
40 public boolean validate(Path path, ResourceContext context) {
41 return true;
42 }
43
44 /**
45 * Determines whether to utilize path protection for the given intent.
46 *
47 * @param intent intent to be inspected
48 * @return whether the intent has a ProtectionConstraint
49 */
50 public static boolean requireProtectedPath(Intent intent) {
51 if (intent instanceof PointToPointIntent) {
52 PointToPointIntent pointToPointIntent = (PointToPointIntent) intent;
53 return pointToPointIntent.constraints().stream()
54 .anyMatch(p -> p instanceof ProtectionConstraint);
55 }
56 return false;
57 }
58}