blob: c4630b8d06f79602c4d9556f2db336873ec6c39e [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
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
Priyanka Bb6963582016-05-20 20:21:20 +0530144 switch (capabilityType) {
145 case WITH_SIGNALLING:
146 return true;
147 case WITHOUT_SIGNALLING_AND_WITHOUT_SR:
Priyanka Bdc93bee2016-06-15 09:07:30 +0530148 return srcDeviceConfig != null && dstDeviceConfig != null
149 && srcDeviceConfig.localLabelCap() && dstDeviceConfig.localLabelCap();
Priyanka Bb6963582016-05-20 20:21:20 +0530150 case SR_WITHOUT_SIGNALLING:
Priyanka Bdc93bee2016-06-15 09:07:30 +0530151 return srcDeviceConfig != null && dstDeviceConfig != null && srcDeviceConfig.srCap()
152 && dstDeviceConfig.srCap() && srcDeviceConfig.labelStackCap() && dstDeviceConfig.labelStackCap();
Priyanka Bb6963582016-05-20 20:21:20 +0530153 default:
154 return false;
155 }
156 }
157
158 @Override
159 public boolean isValid(Link link, ResourceContext context) {
160 return false;
161 //Do nothing instead using isValidLink needs device service to validate link
162 }
163
164 @Override
165 public int hashCode() {
166 return Objects.hash(capabilityType);
167 }
168
169 @Override
170 public boolean equals(Object obj) {
171 if (this == obj) {
172 return true;
173 }
174
175 if (obj instanceof CapabilityConstraint) {
176 CapabilityConstraint other = (CapabilityConstraint) obj;
177 return Objects.equals(this.capabilityType, other.capabilityType);
178 }
179
180 return false;
181 }
182
183 @Override
184 public String toString() {
185 return toStringHelper(this)
186 .add("capabilityType", capabilityType)
187 .toString();
188 }
189}