blob: ce0030869b94db1891549234d537b6a652137b60 [file] [log] [blame]
Priyanka Bb6963582016-05-20 20:21:20 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Priyanka Bb6963582016-05-20 20:21:20 +05303 *
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 */
16package org.onosproject.pce.pceservice.constraint;
17
18import org.onosproject.net.Link;
Satish K2eb5d842017-04-04 16:28:37 +053019import org.onosproject.net.LinkKey;
Priyanka Bb6963582016-05-20 20:21:20 +053020import org.onosproject.net.Path;
Satish K2eb5d842017-04-04 16:28:37 +053021import org.onosproject.net.config.NetworkConfigService;
Priyanka Bb6963582016-05-20 20:21:20 +053022import org.onosproject.net.intent.Constraint;
Satish K2eb5d842017-04-04 16:28:37 +053023import org.onosproject.net.intent.ResourceContext;
24import org.onosproject.pcep.api.TeLinkConfig;
Priyanka Bb6963582016-05-20 20:21:20 +053025
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Constraint that evaluates whether cost for a link is available, if yes return cost for that link.
33 */
34public final class CostConstraint implements Constraint {
35
36 /**
37 * Represents about cost types.
38 */
39 public enum Type {
40 /**
41 * Signifies that cost is IGP cost.
42 */
43 COST(1),
44
45 /**
46 * Signifies that cost is TE cost.
47 */
48 TE_COST(2);
49
50 int value;
51
52 /**
53 * Assign val with the value as the Cost type.
54 *
55 * @param val Cost type
56 */
57 Type(int val) {
58 value = val;
59 }
60
61 /**
62 * Returns value of Cost type.
63 *
64 * @return Cost type
65 */
66 public byte type() {
67 return (byte) value;
68 }
69 }
70
71 private final Type type;
72 public static final String TE_COST = "teCost";
73 public static final String COST = "cost";
74
75 // Constructor for serialization
76 private CostConstraint() {
77 this.type = null;
78 }
79
80 /**
81 * Creates a new cost constraint.
82 *
83 * @param type of a link
84 */
85 public CostConstraint(Type type) {
86 this.type = checkNotNull(type, "Type cannot be null");
87 }
88
89 /**
90 * Creates new CostConstraint with specified cost type.
91 *
92 * @param type of cost
93 * @return instance of CostConstraint
94 */
95 public static CostConstraint of(Type type) {
96 return new CostConstraint(type);
97 }
98
99 /**
100 * Returns the type of a cost specified in a constraint.
101 *
102 * @return required cost type
103 */
104 public Type type() {
105 return type;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(type);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118
119 if (obj instanceof CostConstraint) {
120 CostConstraint other = (CostConstraint) obj;
121 return Objects.equals(this.type, other.type);
122 }
123
124 return false;
125 }
126
127 @Override
128 public String toString() {
129 return toStringHelper(this)
130 .add("type", type)
131 .toString();
132 }
133
134 @Override
135 public double cost(Link link, ResourceContext context) {
Satish K2eb5d842017-04-04 16:28:37 +0530136 return 0;
137 }
138
139 /**
140 * Validates the link based on cost type specified.
141 *
142 * @param link to validate cost type constraint
143 * @param netCfgService instance of netCfgService
144 * @return true if link satisfies cost constraint otherwise false
145 */
146 public double isValidLink(Link link, NetworkConfigService netCfgService) {
147 if (netCfgService == null) {
148 return -1;
149 }
150
151 TeLinkConfig cfg = netCfgService.getConfig(LinkKey.linkKey(link.src(), link.dst()), TeLinkConfig.class);
152 if (cfg == null) {
153 //If cost configuration absent return -1[It is not L3 device]
154 return -1;
155 }
156
Priyanka Bb6963582016-05-20 20:21:20 +0530157 switch (type) {
Satish K2eb5d842017-04-04 16:28:37 +0530158 case COST:
159 //If IGP cost is zero then IGP cost is not assigned for that link
160 return cfg.igpCost() == 0 ? -1 : cfg.igpCost();
Priyanka Bb6963582016-05-20 20:21:20 +0530161
Satish K2eb5d842017-04-04 16:28:37 +0530162 case TE_COST:
163 //If TE cost is zero then TE cost is not assigned for that link
164 return cfg.teCost() == 0 ? -1 : cfg.teCost();
Priyanka Bb6963582016-05-20 20:21:20 +0530165
Satish K2eb5d842017-04-04 16:28:37 +0530166 default:
167 return -1;
Priyanka Bb6963582016-05-20 20:21:20 +0530168 }
169 }
170
171 @Override
172 public boolean validate(Path path, ResourceContext context) {
173 // TODO Auto-generated method stub
174 return false;
175 }
176}