Ramon Casellas | 99c1625 | 2019-05-31 14:29:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | * This work was partially supported by EC H2020 project METRO-HAUL (761727). |
| 17 | */ |
| 18 | package org.onosproject.drivers.odtn.openroadm; |
| 19 | |
| 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT; |
| 22 | import static org.onosproject.net.flow.criteria.Criterion.Type.OCH_SIGID; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | import org.onosproject.net.OchSignal; |
| 28 | import org.onosproject.net.OchSignalType; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 35 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 36 | import org.onosproject.net.flow.instructions.Instruction; |
| 37 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 38 | |
| 39 | /** |
| 40 | * Class that models a FlowRule in an OpenROADM context. |
| 41 | * |
| 42 | */ |
| 43 | public class OpenRoadmFlowRule extends DefaultFlowRule { |
| 44 | |
| 45 | /** |
| 46 | * Type of conenction. |
| 47 | */ |
| 48 | public enum Type { |
| 49 | EXPRESS_LINK, // Degree to Degree |
| 50 | ADD_LINK, // SRG to Degree |
| 51 | DROP_LINK, // Degree to SRG |
| 52 | LOCAL // SRG to SRG |
| 53 | } |
| 54 | |
| 55 | private Type type; |
| 56 | |
| 57 | private PortNumber inPortNumber; |
| 58 | |
| 59 | private PortNumber outPortNumber; |
| 60 | |
| 61 | private OchSignal ochSignal; |
| 62 | |
| 63 | private OchSignalType ochSignalType; |
| 64 | |
| 65 | /** |
| 66 | * Constructor. Build an OpenRoadm flow rule from the passed rule. |
| 67 | * |
| 68 | * @param rule ONOS flow rule that we have to process |
| 69 | * @param linePorts List of ports that are line ports (degrees) used |
| 70 | * to know the Type of this connection. |
| 71 | * |
| 72 | * We store and construct attributes like interface names to support |
| 73 | * this OpenROADM connection. |
| 74 | */ |
| 75 | public OpenRoadmFlowRule(FlowRule rule, List<PortNumber> linePorts) { |
| 76 | super(rule); |
| 77 | |
| 78 | TrafficSelector trafficSelector = rule.selector(); |
| 79 | PortCriterion pc = (PortCriterion) trafficSelector.getCriterion(IN_PORT); |
| 80 | checkArgument(pc != null, "Missing IN_PORT Criterion"); |
| 81 | inPortNumber = pc.port(); |
| 82 | |
| 83 | // Generally, Sigtype and ochSignal could be null. This would mean e.g. a |
| 84 | // port switching connection. |
| 85 | OchSignalCriterion osc = (OchSignalCriterion) trafficSelector.getCriterion(OCH_SIGID); |
| 86 | // checkArgument(osc != null, "Missing OCH_SIGID Criterion"); |
| 87 | if (osc != null) { |
| 88 | ochSignal = osc.lambda(); |
| 89 | } |
| 90 | |
| 91 | TrafficTreatment trafficTreatment = rule.treatment(); |
| 92 | List<Instruction> instructions = trafficTreatment.immediate(); |
| 93 | |
| 94 | outPortNumber = instructions.stream() |
| 95 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 96 | .map(i -> ((OutputInstruction) i).port()) |
| 97 | .findFirst() |
| 98 | .orElse(null); |
| 99 | checkArgument(outPortNumber != null, "Missing OUTPUT Instruction"); |
| 100 | |
| 101 | if (linePorts.contains(inPortNumber) && linePorts.contains(outPortNumber)) { |
| 102 | type = Type.EXPRESS_LINK; |
| 103 | } |
| 104 | if (!linePorts.contains(inPortNumber) && linePorts.contains(outPortNumber)) { |
| 105 | type = Type.ADD_LINK; |
| 106 | } |
| 107 | if (linePorts.contains(inPortNumber) && !linePorts.contains(outPortNumber)) { |
| 108 | type = Type.DROP_LINK; |
| 109 | } |
| 110 | if (!linePorts.contains(inPortNumber) && !linePorts.contains(outPortNumber)) { |
| 111 | type = Type.LOCAL; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the type of the connection. |
| 117 | * |
| 118 | * @return type (express, add, drop, local) |
| 119 | */ |
| 120 | public Type type() { |
| 121 | return type; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the input port. |
| 126 | * |
| 127 | * @return the input port (connection source). |
| 128 | */ |
| 129 | public PortNumber inPort() { |
| 130 | return inPortNumber; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Get the output port. |
| 136 | * |
| 137 | * @return the output port (connection destination). |
| 138 | */ |
| 139 | public PortNumber outPort() { |
| 140 | return outPortNumber; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Get the OchSignal for this connection. |
| 146 | * |
| 147 | * @return the OchSignal. |
| 148 | */ |
| 149 | public OchSignal ochSignal() { |
| 150 | return ochSignal; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Get the OchSignalType for this connection. |
| 156 | * |
| 157 | * @return the OchSignalType. |
| 158 | */ |
| 159 | public OchSignalType ochSignalType() { |
| 160 | return ochSignalType; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | @Override |
| 165 | public boolean equals(Object o) { |
| 166 | if (!(o instanceof OpenRoadmFlowRule)) { |
| 167 | return false; |
| 168 | } |
| 169 | OpenRoadmFlowRule that = (OpenRoadmFlowRule) o; |
| 170 | return Objects.equals(this.inPortNumber, that.inPortNumber) && |
| 171 | Objects.equals(this.outPortNumber, that.outPortNumber) && |
| 172 | Objects.equals(this.ochSignal, that.ochSignal) && |
| 173 | Objects.equals(this.ochSignalType, that.ochSignalType) && |
| 174 | Objects.equals(this.type, that.type); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | @Override |
| 179 | public int hashCode() { |
| 180 | return Objects.hash(inPortNumber, outPortNumber, ochSignal, ochSignalType, type); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | @Override |
| 185 | public String toString() { |
| 186 | return MoreObjects.toStringHelper(getClass()) |
| 187 | .add("type", type) |
| 188 | .add("inPortNumber", inPortNumber) |
| 189 | .add("outPortNumber", outPortNumber) |
| 190 | .add("ochSignal", ochSignal) |
| 191 | .add("ochSignalType", ochSignalType) |
| 192 | .toString(); |
| 193 | } |
| 194 | } |