blob: 229644dd6e05bc194fadb73137795743e95dfe86 [file] [log] [blame]
Priyanka Bb6963582016-05-20 20:21:20 +05301/*
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 */
16package org.onosproject.pce.pceservice.constraint;
17
18import org.onosproject.net.Link;
19import org.onosproject.net.Path;
20import org.onosproject.net.intent.ResourceContext;
21import org.onosproject.net.intent.Constraint;
22
23import java.util.Objects;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Constraint that evaluates whether cost for a link is available, if yes return cost for that link.
30 */
31public final class CostConstraint implements Constraint {
32
33 /**
34 * Represents about cost types.
35 */
36 public enum Type {
37 /**
38 * Signifies that cost is IGP cost.
39 */
40 COST(1),
41
42 /**
43 * Signifies that cost is TE cost.
44 */
45 TE_COST(2);
46
47 int value;
48
49 /**
50 * Assign val with the value as the Cost type.
51 *
52 * @param val Cost type
53 */
54 Type(int val) {
55 value = val;
56 }
57
58 /**
59 * Returns value of Cost type.
60 *
61 * @return Cost type
62 */
63 public byte type() {
64 return (byte) value;
65 }
66 }
67
68 private final Type type;
69 public static final String TE_COST = "teCost";
70 public static final String COST = "cost";
71
72 // Constructor for serialization
73 private CostConstraint() {
74 this.type = null;
75 }
76
77 /**
78 * Creates a new cost constraint.
79 *
80 * @param type of a link
81 */
82 public CostConstraint(Type type) {
83 this.type = checkNotNull(type, "Type cannot be null");
84 }
85
86 /**
87 * Creates new CostConstraint with specified cost type.
88 *
89 * @param type of cost
90 * @return instance of CostConstraint
91 */
92 public static CostConstraint of(Type type) {
93 return new CostConstraint(type);
94 }
95
96 /**
97 * Returns the type of a cost specified in a constraint.
98 *
99 * @return required cost type
100 */
101 public Type type() {
102 return type;
103 }
104
105 @Override
106 public int hashCode() {
107 return Objects.hash(type);
108 }
109
110 @Override
111 public boolean equals(Object obj) {
112 if (this == obj) {
113 return true;
114 }
115
116 if (obj instanceof CostConstraint) {
117 CostConstraint other = (CostConstraint) obj;
118 return Objects.equals(this.type, other.type);
119 }
120
121 return false;
122 }
123
124 @Override
125 public String toString() {
126 return toStringHelper(this)
127 .add("type", type)
128 .toString();
129 }
130
131 @Override
132 public double cost(Link link, ResourceContext context) {
133 //TODO: Usage of annotations are for transient solution. In future will be replaces with the
134 // network config service / Projection model.
135 switch (type) {
136 case COST:
137 if (link.annotations().value(COST) != null) {
138 return Double.parseDouble(link.annotations().value(COST));
139 }
140
141 //If cost annotations absent return -1[It is not L3 device]
142 return -1;
143 case TE_COST:
144 if (link.annotations().value(TE_COST) != null) {
145 return Double.parseDouble(link.annotations().value(TE_COST));
146 }
147
148 //If TE cost annotations absent return -1[It is not L3 device]
149 return -1;
150 default:
151 return -1;
152 }
153 }
154
155 @Override
156 public boolean validate(Path path, ResourceContext context) {
157 // TODO Auto-generated method stub
158 return false;
159 }
160}