blob: 224184f1521987fe349c2ac7489835e77acf6d99 [file] [log] [blame]
alessiod4a2b842019-04-30 18:43:17 +02001/*
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
17package org.onosproject.drivers.lumentum;
18
19import org.onosproject.net.OchSignal;
20import org.onosproject.net.PortNumber;
21import org.onosproject.net.flow.DefaultFlowRule;
22import org.onosproject.net.flow.FlowRule;
23import org.onosproject.net.flow.criteria.Criterion;
24import org.onosproject.net.flow.criteria.OchSignalCriterion;
25import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
26import org.onosproject.net.flow.criteria.PortCriterion;
27import org.onosproject.net.flow.instructions.Instruction;
28import org.onosproject.net.flow.instructions.Instructions;
29import org.onosproject.net.flow.instructions.L0ModificationInstruction;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.List;
34import java.util.Set;
35
36import static com.google.common.base.Preconditions.checkArgument;
37
38public 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
alessio1bf2a632019-06-04 15:47:39 +020047 private String connectionName;
48 private int connectionId;
49 private int connectionModule;
alessiod4a2b842019-04-30 18:43:17 +020050 private PortNumber addDrop;
51
52 private PortNumber inputPort;
53 private PortNumber outputPort;
54
55 private OchSignal ochSignal;
56 private boolean isAddRule;
57
alessio1bf2a632019-06-04 15:47:39 +020058 protected double attenuation;
59 protected double targetAttenuation;
60 protected double targetPower;
61 protected double inputPower;
62 protected double outputPower;
63
alessiod4a2b842019-04-30 18:43:17 +020064 //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
alessio1bf2a632019-06-04 15:47:39 +020072
alessiod4a2b842019-04-30 18:43:17 +020073 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
alessioa9bcacc2021-09-24 17:32:57 +020079 log.debug("Lumentum device received criteria {} - treatment {}",
80 criteria,
81 instructions);
82
alessiod4a2b842019-04-30 18:43:17 +020083 // Proper cross connect has criteria for input port, OChSignal and OCh signal type.
84 // Instruction is only output to port (rule generated by ROADM app).
85 // Instruction includes output port and OChSignal (rule generated by OpticalConnectivityIntent).
86 checkArgument((criteria.size() == NUM_CRITERIA_INTENT) ||
87 (criteria.size() == NUM_CRITERIA_ROADM),
88 "Wrong size of flow rule criteria for cross connect.");
89 checkArgument((instructions.size() == NUM_INSTRUCTIONS_INTENT) ||
90 (instructions.size() == NUM_INSTRUCTIONS_ROADM),
91 "Wrong size of flow rule instructions for cross connect.");
92
93 // FIXME: Ensure criteria has exactly one of each match type
94 criteria.forEach(
95 c -> checkArgument(c instanceof OchSignalCriterion ||
96 c instanceof OchSignalTypeCriterion ||
97 c instanceof PortCriterion,
98 "Incompatible flow rule criteria for LumentumROADM: " + criteria
99 )
100 );
101
102 instructions.forEach(
103 c -> checkArgument(c instanceof Instructions.OutputInstruction ||
104 c instanceof L0ModificationInstruction,
105 "Incompatible flow rule instruction for LumentumROADM: " + instructions
106 )
107 );
108
109 if (criteria.size() == NUM_CRITERIA_INTENT && instructions.size() == NUM_INSTRUCTIONS_INTENT) {
alessioa9bcacc2021-09-24 17:32:57 +0200110 log.debug("Lumentum device, FlowRule coming from OpticalConnectivityIntentCompiler");
alessiod4a2b842019-04-30 18:43:17 +0200111
112 type = Type.OPTICAL_CONNECTIVITY_INTENT_RULE;
113 } else {
alessioa9bcacc2021-09-24 17:32:57 +0200114 log.debug("Lumentum device, FlowRule coming from ROADM app");
alessiod4a2b842019-04-30 18:43:17 +0200115
116 type = Type.ROADM_APP_RULE;
117 }
118
119 ochSignal = criteria.stream()
120 .filter(c -> c instanceof OchSignalCriterion)
121 .map(c -> ((OchSignalCriterion) c).lambda())
122 .findAny()
123 .orElse(null);
124
125 inputPort = criteria.stream()
126 .filter(c -> c instanceof PortCriterion)
127 .map(c -> ((PortCriterion) c).port())
128 .findAny()
129 .orElse(null);
130
131 outputPort = instructions.stream()
132 .filter(c -> c instanceof Instructions.OutputInstruction)
133 .map(c -> ((Instructions.OutputInstruction) c).port())
134 .findAny()
135 .orElse(null);
136
137 // Add or drop rule? Get the output instruction
138 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instructions.stream()
139 .filter(c -> c instanceof Instructions.OutputInstruction)
140 .findAny()
141 .orElse(null);
142
143 if (linePorts.contains(outInstruction.port())) {
144 addDrop = criteria.stream()
145 .filter(c -> c instanceof PortCriterion)
146 .map(c -> ((PortCriterion) c).port())
147 .findAny()
148 .orElse(null);
149 isAddRule = true;
150 } else {
151 addDrop = outInstruction.port();
152 isAddRule = false;
153 }
alessio1bf2a632019-06-04 15:47:39 +0200154
alessioa280f032024-01-30 02:52:01 +0100155 connectionName = "inPort-" + getOchPort().toString() + "-centralFreq-" + ochSignal.centralFrequency().asGHz();
alessiod4a2b842019-04-30 18:43:17 +0200156 }
157
158 public PortNumber addDrop() {
159 return addDrop;
160 }
161
162 public OchSignal ochSignal() {
163 return ochSignal;
164 }
165
166 public boolean isAddRule() {
167 return isAddRule;
168 }
169
170 public PortNumber getInputPort() {
171 return inputPort;
172 }
173
174 public PortNumber getOutputPort() {
175 return outputPort;
176 }
alessio1bf2a632019-06-04 15:47:39 +0200177
alessioa280f032024-01-30 02:52:01 +0100178 public PortNumber getOchPort() {
179 if (isAddRule) {
180 return inputPort;
181 } else {
182 return outputPort;
183 }
184 }
185
alessio1bf2a632019-06-04 15:47:39 +0200186 public String getConnectionName() {
187 return connectionName;
188 }
189
190 public int getConnectionId() {
191 return connectionId;
192 }
193
194 public int getConnectionModule() {
195 return connectionModule;
196 }
197
198 public void setConnectionId(int id) {
199 connectionId = id;
200 }
201
202 public void setConnectionModule(int id) {
203 connectionModule = id;
204 }
205
206 protected void setAttenuation(double att) {
207 attenuation = att;
208 }
209
210 protected void setInputPower(double power) {
211 inputPower = power;
212 }
213
214 protected void setOutputPower(double power) {
215 outputPower = power;
216 }
alessiod4a2b842019-04-30 18:43:17 +0200217}