Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [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.roadm; |
| 17 | |
| 18 | import org.onosproject.net.OchSignal; |
| 19 | import org.onosproject.net.PortNumber; |
| 20 | import org.onosproject.net.flow.FlowRule; |
| 21 | import org.onosproject.net.flow.criteria.Criterion; |
| 22 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 23 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 24 | import org.onosproject.net.flow.instructions.Instruction; |
| 25 | import org.onosproject.net.flow.instructions.Instructions; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Representation of an internal ROADM connection. |
| 33 | */ |
| 34 | public final class ChannelData { |
| 35 | private PortNumber inPort; |
| 36 | private PortNumber outPort; |
| 37 | private OchSignal ochSignal; |
| 38 | |
| 39 | private ChannelData(PortNumber inPort, PortNumber outPort, OchSignal ochSignal) { |
| 40 | this.inPort = inPort; |
| 41 | this.outPort = outPort; |
| 42 | this.ochSignal = ochSignal; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns a ChannelData representation from a flow rule. The rule must contain |
| 47 | * a Criterion.Type.IN_PORT selector, Criterion.Type.OCH_SIGID selector, and |
| 48 | * Instruction.Type.OUTPUT instruction. |
| 49 | * |
| 50 | * @param rule the flow rule representing the connection |
| 51 | * @return ChannelData representation of the connection |
| 52 | */ |
| 53 | public static ChannelData fromFlow(FlowRule rule) { |
| 54 | checkNotNull(rule); |
| 55 | |
| 56 | Criterion in = rule.selector().getCriterion(Criterion.Type.IN_PORT); |
| 57 | checkNotNull(in); |
| 58 | PortNumber inPort = ((PortCriterion) in).port(); |
| 59 | |
| 60 | Criterion och = rule.selector().getCriterion(Criterion.Type.OCH_SIGID); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 61 | OchSignal ochSignal = och == null ? null : ((OchSignalCriterion) och).lambda(); |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 62 | |
| 63 | PortNumber outPort = null; |
| 64 | List<Instruction> instructions = rule.treatment().allInstructions(); |
| 65 | for (Instruction ins : instructions) { |
| 66 | if (ins.type() == Instruction.Type.OUTPUT) { |
| 67 | outPort = ((Instructions.OutputInstruction) ins).port(); |
| 68 | } |
| 69 | } |
| 70 | checkNotNull(outPort); |
| 71 | |
| 72 | return new ChannelData(inPort, outPort, ochSignal); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the input port. |
| 77 | * |
| 78 | * @return input port |
| 79 | */ |
| 80 | public PortNumber inPort() { |
| 81 | return inPort; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the output port. |
| 86 | * |
| 87 | * @return output port |
| 88 | */ |
| 89 | public PortNumber outPort() { |
| 90 | return outPort; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the channel signal. |
| 95 | * |
| 96 | * @return channel signal |
| 97 | */ |
| 98 | public OchSignal ochSignal() { |
| 99 | return ochSignal; |
| 100 | } |
| 101 | } |