blob: 7b0579fecd010124c837ad90237eeb16f5fc6a7f [file] [log] [blame]
Marc De Leenheerc662d322016-02-18 16:05:10 -08001/*
Marc De Leenheer40a544b2017-04-25 14:16:02 -07002 * Copyright 2017-present Open Networking Laboratory
Marc De Leenheerc662d322016-02-18 16:05:10 -08003 *
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 */
Marc De Leenheer40a544b2017-04-25 14:16:02 -070016package org.onosproject.driver.optical.flowrule;
Marc De Leenheerc662d322016-02-18 16:05:10 -080017
18import org.onosproject.net.OchSignal;
19import org.onosproject.net.PortNumber;
20import org.onosproject.net.flow.DefaultFlowRule;
21import org.onosproject.net.flow.FlowRule;
22import org.onosproject.net.flow.criteria.Criterion;
23import org.onosproject.net.flow.criteria.OchSignalCriterion;
24import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
25import org.onosproject.net.flow.criteria.PortCriterion;
26import org.onosproject.net.flow.instructions.Instruction;
27import org.onosproject.net.flow.instructions.Instructions;
28
29import java.util.List;
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkArgument;
33
34/**
35 * Cross connect abstraction based on a flow rule.
36 */
37public class CrossConnectFlowRule extends DefaultFlowRule implements CrossConnect {
38 private PortNumber addDrop;
39 private OchSignal ochSignal;
40 private boolean isAddRule;
41
42 public CrossConnectFlowRule(FlowRule rule, List<PortNumber> linePorts) {
43 super(rule);
44
45 Set<Criterion> criteria = rule.selector().criteria();
46 List<Instruction> instructions = rule.treatment().immediate();
47
48 // Proper cross connect has criteria for input port, OChSignal and OCh signal type.
49 // Instruction must be output to port.
50 checkArgument(criteria.size() == 3, "Wrong size of flow rule criteria for cross connect.");
51 checkArgument(instructions.size() == 1, "Wrong size of flow rule instructions for cross connect.");
52 // FIXME: Ensure criteria has exactly one of each match type
53 criteria.forEach(
54 c -> checkArgument(c instanceof OchSignalCriterion ||
55 c instanceof OchSignalTypeCriterion ||
56 c instanceof PortCriterion,
57 "Incompatible flow rule criteria for cross connect: " + criteria
58 )
59 );
60 checkArgument(instructions.get(0).type() == Instruction.Type.OUTPUT,
61 "Incompatible flow rule instructions for cross connect: " + instructions);
62
63 ochSignal = criteria.stream()
64 .filter(c -> c instanceof OchSignalCriterion)
65 .map(c -> ((OchSignalCriterion) c).lambda())
66 .findAny()
67 .orElse(null);
68
69 // Add or drop rule?
70 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instructions.get(0);
71 if (linePorts.contains(outInstruction.port())) {
72 addDrop = criteria.stream()
73 .filter(c -> c instanceof PortCriterion)
74 .map(c -> ((PortCriterion) c).port())
75 .findAny()
76 .orElse(null);
77 isAddRule = true;
78 } else {
79 addDrop = outInstruction.port();
80 isAddRule = false;
81 }
82 }
83
84 @Override
85 public PortNumber addDrop() {
86 return addDrop;
87 }
88
89 @Override
90 public OchSignal ochSignal() {
91 return ochSignal;
92 }
93
94 @Override
95 public boolean isAddRule() {
96 return isAddRule;
97 }
98}