blob: e123eeabb29c22f54be04d5a831e6b9d91a5e54d [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
Priyanka Bb6963582016-05-20 20:21:20 +053018import org.onosproject.net.Device;
Avantika-Huawei032a9872016-05-27 22:57:38 +053019import org.onosproject.net.DeviceId;
Priyanka Bb6963582016-05-20 20:21:20 +053020import org.onosproject.net.Link;
Avantika-Huawei032a9872016-05-27 22:57:38 +053021import org.onosproject.net.config.NetworkConfigService;
Priyanka Bb6963582016-05-20 20:21:20 +053022import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.intent.ResourceContext;
24import org.onosproject.net.intent.constraint.BooleanConstraint;
Avantika-Huawei032a9872016-05-27 22:57:38 +053025import org.onosproject.pcep.api.DeviceCapability;
Priyanka Bb6963582016-05-20 20:21:20 +053026
27import java.util.Objects;
28
29import static com.google.common.base.MoreObjects.toStringHelper;
30
31/**
32 * Constraint that evaluates whether devices satisfies capability.
33 */
34public final class CapabilityConstraint extends BooleanConstraint {
35
36 private final CapabilityType capabilityType;
Priyanka Bb6963582016-05-20 20:21:20 +053037 public static final String LSRID = "lsrId";
Priyanka Bb6963582016-05-20 20:21:20 +053038 public static final String TRUE = "true";
39
40 /**
41 * Represents about capability type.
42 */
43 public enum CapabilityType {
44 /**
45 * Signifies that path is created via signaling mode.
46 */
47 WITH_SIGNALLING(0),
48
49 /**
50 * Signifies that path is created via SR mode.
51 */
52 SR_WITHOUT_SIGNALLING(1),
53
54 /**
55 * Signifies that path is created via without signaling and without SR mode.
56 */
57 WITHOUT_SIGNALLING_AND_WITHOUT_SR(2);
58
59 int value;
60
61 /**
62 * Assign val with the value as the capability type.
63 *
64 * @param val capability type
65 */
66 CapabilityType(int val) {
67 value = val;
68 }
69
70 /**
71 * Returns value of capability type.
72 *
73 * @return capability type
74 */
75 public byte type() {
76 return (byte) value;
77 }
78 }
79
80 // Constructor for serialization
81 private CapabilityConstraint() {
82 capabilityType = null;
83 }
84
85 /**
86 * Creates a new capability constraint.
87 *
88 * @param capabilityType type of capability device supports
89 */
90 public CapabilityConstraint(CapabilityType capabilityType) {
91 this.capabilityType = capabilityType;
92 }
93
94 /**
95 * Creates a new capability constraint.
96 *
97 * @param capabilityType type of capability device supports
98 * @return instance of CapabilityConstraint for specified capability type
99 */
100 public static CapabilityConstraint of(CapabilityType capabilityType) {
101 return new CapabilityConstraint(capabilityType);
102 }
103
104 /**
105 * Obtains type of capability.
106 *
107 * @return type of capability
108 */
109 public CapabilityType capabilityType() {
110 return capabilityType;
111 }
112
113 /**
114 * Validates the link based on capability constraint.
115 *
116 * @param link to validate source and destination based on capability constraint
117 * @param deviceService instance of DeviceService
Avantika-Huawei032a9872016-05-27 22:57:38 +0530118 * @param netCfgService instance of NetworkConfigService
Priyanka Bb6963582016-05-20 20:21:20 +0530119 * @return true if link satisfies capability constraint otherwise false
120 */
Avantika-Huawei032a9872016-05-27 22:57:38 +0530121 public boolean isValidLink(Link link, DeviceService deviceService, NetworkConfigService netCfgService) {
122 if (deviceService == null || netCfgService == null) {
Priyanka Bb6963582016-05-20 20:21:20 +0530123 return false;
124 }
125
126 Device srcDevice = deviceService.getDevice(link.src().deviceId());
127 Device dstDevice = deviceService.getDevice(link.dst().deviceId());
128
Avantika-Huawei032a9872016-05-27 22:57:38 +0530129 //TODO: Usage of annotations are for transient solution. In future will be replaced with the
Priyanka Bb6963582016-05-20 20:21:20 +0530130 // network config service / Projection model.
131 // L3 device
Avantika-Huawei032a9872016-05-27 22:57:38 +0530132 if (srcDevice == null || dstDevice == null) {
Priyanka Bb6963582016-05-20 20:21:20 +0530133 return false;
134 }
135
Avantika-Huawei032a9872016-05-27 22:57:38 +0530136 String srcLsrId = srcDevice.annotations().value(LSRID);
Priyanka Bb6963582016-05-20 20:21:20 +0530137 String dstLsrId = dstDevice.annotations().value(LSRID);
138
Avantika-Huawei032a9872016-05-27 22:57:38 +0530139 DeviceCapability srcDeviceConfig = netCfgService.getConfig(DeviceId.deviceId(srcLsrId),
140 DeviceCapability.class);
141 DeviceCapability dstDeviceConfig = netCfgService.getConfig(DeviceId.deviceId(dstLsrId),
142 DeviceCapability.class);
Priyanka Bb6963582016-05-20 20:21:20 +0530143
Avantika-Huawei032a9872016-05-27 22:57:38 +0530144 if (srcDeviceConfig == null || dstDeviceConfig == null) {
Priyanka Bb6963582016-05-20 20:21:20 +0530145 return false;
146 }
147
148 switch (capabilityType) {
149 case WITH_SIGNALLING:
150 return true;
151 case WITHOUT_SIGNALLING_AND_WITHOUT_SR:
Avantika-Huawei032a9872016-05-27 22:57:38 +0530152 return srcDeviceConfig.localLabelCap() && dstDeviceConfig.localLabelCap();
153
Priyanka Bb6963582016-05-20 20:21:20 +0530154 case SR_WITHOUT_SIGNALLING:
Avantika-Huawei032a9872016-05-27 22:57:38 +0530155 return srcDeviceConfig.srCap() && dstDeviceConfig.srCap()
156 && srcDeviceConfig.labelStackCap() && dstDeviceConfig.labelStackCap();
Priyanka Bb6963582016-05-20 20:21:20 +0530157 default:
158 return false;
159 }
160 }
161
162 @Override
163 public boolean isValid(Link link, ResourceContext context) {
164 return false;
165 //Do nothing instead using isValidLink needs device service to validate link
166 }
167
168 @Override
169 public int hashCode() {
170 return Objects.hash(capabilityType);
171 }
172
173 @Override
174 public boolean equals(Object obj) {
175 if (this == obj) {
176 return true;
177 }
178
179 if (obj instanceof CapabilityConstraint) {
180 CapabilityConstraint other = (CapabilityConstraint) obj;
181 return Objects.equals(this.capabilityType, other.capabilityType);
182 }
183
184 return false;
185 }
186
187 @Override
188 public String toString() {
189 return toStringHelper(this)
190 .add("capabilityType", capabilityType)
191 .toString();
192 }
193}