Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.pce.pceservice.constraint; |
| 17 | |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 18 | import org.onosproject.net.Device; |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 20 | import org.onosproject.net.Link; |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 21 | import org.onosproject.net.config.NetworkConfigService; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 22 | import org.onosproject.net.device.DeviceService; |
| 23 | import org.onosproject.net.intent.ResourceContext; |
| 24 | import org.onosproject.net.intent.constraint.BooleanConstraint; |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 25 | import org.onosproject.pcep.api.DeviceCapability; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 26 | |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 30 | |
| 31 | /** |
| 32 | * Constraint that evaluates whether devices satisfies capability. |
| 33 | */ |
| 34 | public final class CapabilityConstraint extends BooleanConstraint { |
| 35 | |
| 36 | private final CapabilityType capabilityType; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 37 | public static final String LSRID = "lsrId"; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 38 | 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-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 118 | * @param netCfgService instance of NetworkConfigService |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 119 | * @return true if link satisfies capability constraint otherwise false |
| 120 | */ |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 121 | public boolean isValidLink(Link link, DeviceService deviceService, NetworkConfigService netCfgService) { |
| 122 | if (deviceService == null || netCfgService == null) { |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | Device srcDevice = deviceService.getDevice(link.src().deviceId()); |
| 127 | Device dstDevice = deviceService.getDevice(link.dst().deviceId()); |
| 128 | |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 129 | //TODO: Usage of annotations are for transient solution. In future will be replaced with the |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 130 | // network config service / Projection model. |
| 131 | // L3 device |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 132 | if (srcDevice == null || dstDevice == null) { |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 136 | String srcLsrId = srcDevice.annotations().value(LSRID); |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 137 | String dstLsrId = dstDevice.annotations().value(LSRID); |
| 138 | |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 139 | DeviceCapability srcDeviceConfig = netCfgService.getConfig(DeviceId.deviceId(srcLsrId), |
| 140 | DeviceCapability.class); |
| 141 | DeviceCapability dstDeviceConfig = netCfgService.getConfig(DeviceId.deviceId(dstLsrId), |
| 142 | DeviceCapability.class); |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 143 | |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 144 | switch (capabilityType) { |
| 145 | case WITH_SIGNALLING: |
| 146 | return true; |
| 147 | case WITHOUT_SIGNALLING_AND_WITHOUT_SR: |
Priyanka B | dc93bee | 2016-06-15 09:07:30 +0530 | [diff] [blame^] | 148 | return srcDeviceConfig != null && dstDeviceConfig != null |
| 149 | && srcDeviceConfig.localLabelCap() && dstDeviceConfig.localLabelCap(); |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 150 | case SR_WITHOUT_SIGNALLING: |
Priyanka B | dc93bee | 2016-06-15 09:07:30 +0530 | [diff] [blame^] | 151 | return srcDeviceConfig != null && dstDeviceConfig != null && srcDeviceConfig.srCap() |
| 152 | && dstDeviceConfig.srCap() && srcDeviceConfig.labelStackCap() && dstDeviceConfig.labelStackCap(); |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 153 | 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 | } |