alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.drivers.lumentum; |
| 18 | |
| 19 | import org.onosproject.net.OchSignal; |
| 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.flow.DefaultFlowRule; |
| 22 | import org.onosproject.net.flow.FlowRule; |
| 23 | import org.onosproject.net.flow.criteria.Criterion; |
| 24 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 25 | import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; |
| 26 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 27 | import org.onosproject.net.flow.instructions.Instruction; |
| 28 | import org.onosproject.net.flow.instructions.Instructions; |
| 29 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.util.List; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkArgument; |
| 37 | |
| 38 | public class LumentumFlowRule extends DefaultFlowRule { |
| 39 | private static final Logger log = LoggerFactory.getLogger(LumentumFlowRule.class); |
| 40 | |
| 41 | protected enum Type { |
| 42 | OPTICAL_CONNECTIVITY_INTENT_RULE, |
| 43 | ROADM_APP_RULE |
| 44 | } |
| 45 | public Type type; |
| 46 | |
alessio | 1bf2a63 | 2019-06-04 15:47:39 +0200 | [diff] [blame] | 47 | private String connectionName; |
| 48 | private int connectionId; |
| 49 | private int connectionModule; |
alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 50 | private PortNumber addDrop; |
| 51 | |
| 52 | private PortNumber inputPort; |
| 53 | private PortNumber outputPort; |
| 54 | |
| 55 | private OchSignal ochSignal; |
| 56 | private boolean isAddRule; |
| 57 | |
alessio | 1bf2a63 | 2019-06-04 15:47:39 +0200 | [diff] [blame] | 58 | protected double attenuation; |
| 59 | protected double targetAttenuation; |
| 60 | protected double targetPower; |
| 61 | protected double inputPower; |
| 62 | protected double outputPower; |
| 63 | |
alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 64 | //As generated by the OpticalConnectivityIntentCompiler |
| 65 | private static final int NUM_CRITERIA_INTENT = 3; |
| 66 | private static final int NUM_INSTRUCTIONS_INTENT = 2; |
| 67 | |
| 68 | //As generated dy ROADM app |
| 69 | private static final int NUM_CRITERIA_ROADM = 3; |
| 70 | private static final int NUM_INSTRUCTIONS_ROADM = 1; |
| 71 | |
alessio | 1bf2a63 | 2019-06-04 15:47:39 +0200 | [diff] [blame] | 72 | |
alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 73 | public LumentumFlowRule(FlowRule rule, List<PortNumber> linePorts) { |
| 74 | super(rule); |
| 75 | |
| 76 | Set<Criterion> criteria = rule.selector().criteria(); |
| 77 | List<Instruction> instructions = rule.treatment().immediate(); |
| 78 | |
| 79 | // Proper cross connect has criteria for input port, OChSignal and OCh signal type. |
| 80 | // Instruction is only output to port (rule generated by ROADM app). |
| 81 | // Instruction includes output port and OChSignal (rule generated by OpticalConnectivityIntent). |
| 82 | checkArgument((criteria.size() == NUM_CRITERIA_INTENT) || |
| 83 | (criteria.size() == NUM_CRITERIA_ROADM), |
| 84 | "Wrong size of flow rule criteria for cross connect."); |
| 85 | checkArgument((instructions.size() == NUM_INSTRUCTIONS_INTENT) || |
| 86 | (instructions.size() == NUM_INSTRUCTIONS_ROADM), |
| 87 | "Wrong size of flow rule instructions for cross connect."); |
| 88 | |
| 89 | // FIXME: Ensure criteria has exactly one of each match type |
| 90 | criteria.forEach( |
| 91 | c -> checkArgument(c instanceof OchSignalCriterion || |
| 92 | c instanceof OchSignalTypeCriterion || |
| 93 | c instanceof PortCriterion, |
| 94 | "Incompatible flow rule criteria for LumentumROADM: " + criteria |
| 95 | ) |
| 96 | ); |
| 97 | |
| 98 | instructions.forEach( |
| 99 | c -> checkArgument(c instanceof Instructions.OutputInstruction || |
| 100 | c instanceof L0ModificationInstruction, |
| 101 | "Incompatible flow rule instruction for LumentumROADM: " + instructions |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | if (criteria.size() == NUM_CRITERIA_INTENT && instructions.size() == NUM_INSTRUCTIONS_INTENT) { |
| 106 | log.info("Lumentum device, FlowRule coming from OpticalConnectivityIntentCompiler"); |
| 107 | |
| 108 | type = Type.OPTICAL_CONNECTIVITY_INTENT_RULE; |
| 109 | } else { |
| 110 | log.info("Lumentum device, FlowRule coming from ROADM app"); |
| 111 | |
| 112 | type = Type.ROADM_APP_RULE; |
| 113 | } |
| 114 | |
| 115 | ochSignal = criteria.stream() |
| 116 | .filter(c -> c instanceof OchSignalCriterion) |
| 117 | .map(c -> ((OchSignalCriterion) c).lambda()) |
| 118 | .findAny() |
| 119 | .orElse(null); |
| 120 | |
| 121 | inputPort = criteria.stream() |
| 122 | .filter(c -> c instanceof PortCriterion) |
| 123 | .map(c -> ((PortCriterion) c).port()) |
| 124 | .findAny() |
| 125 | .orElse(null); |
| 126 | |
| 127 | outputPort = instructions.stream() |
| 128 | .filter(c -> c instanceof Instructions.OutputInstruction) |
| 129 | .map(c -> ((Instructions.OutputInstruction) c).port()) |
| 130 | .findAny() |
| 131 | .orElse(null); |
| 132 | |
| 133 | // Add or drop rule? Get the output instruction |
| 134 | Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instructions.stream() |
| 135 | .filter(c -> c instanceof Instructions.OutputInstruction) |
| 136 | .findAny() |
| 137 | .orElse(null); |
| 138 | |
| 139 | if (linePorts.contains(outInstruction.port())) { |
| 140 | addDrop = criteria.stream() |
| 141 | .filter(c -> c instanceof PortCriterion) |
| 142 | .map(c -> ((PortCriterion) c).port()) |
| 143 | .findAny() |
| 144 | .orElse(null); |
| 145 | isAddRule = true; |
| 146 | } else { |
| 147 | addDrop = outInstruction.port(); |
| 148 | isAddRule = false; |
| 149 | } |
alessio | 1bf2a63 | 2019-06-04 15:47:39 +0200 | [diff] [blame] | 150 | |
| 151 | connectionName = "inPort" + inputPort.toString() + "-ochSig-" + ochSignal.centralFrequency().toString(); |
alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public PortNumber addDrop() { |
| 155 | return addDrop; |
| 156 | } |
| 157 | |
| 158 | public OchSignal ochSignal() { |
| 159 | return ochSignal; |
| 160 | } |
| 161 | |
| 162 | public boolean isAddRule() { |
| 163 | return isAddRule; |
| 164 | } |
| 165 | |
| 166 | public PortNumber getInputPort() { |
| 167 | return inputPort; |
| 168 | } |
| 169 | |
| 170 | public PortNumber getOutputPort() { |
| 171 | return outputPort; |
| 172 | } |
alessio | 1bf2a63 | 2019-06-04 15:47:39 +0200 | [diff] [blame] | 173 | |
| 174 | public String getConnectionName() { |
| 175 | return connectionName; |
| 176 | } |
| 177 | |
| 178 | public int getConnectionId() { |
| 179 | return connectionId; |
| 180 | } |
| 181 | |
| 182 | public int getConnectionModule() { |
| 183 | return connectionModule; |
| 184 | } |
| 185 | |
| 186 | public void setConnectionId(int id) { |
| 187 | connectionId = id; |
| 188 | } |
| 189 | |
| 190 | public void setConnectionModule(int id) { |
| 191 | connectionModule = id; |
| 192 | } |
| 193 | |
| 194 | protected void setAttenuation(double att) { |
| 195 | attenuation = att; |
| 196 | } |
| 197 | |
| 198 | protected void setInputPower(double power) { |
| 199 | inputPower = power; |
| 200 | } |
| 201 | |
| 202 | protected void setOutputPower(double power) { |
| 203 | outputPower = power; |
| 204 | } |
alessio | d4a2b84 | 2019-04-30 18:43:17 +0200 | [diff] [blame] | 205 | } |