Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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.polatis.netconf; |
| 18 | |
| 19 | import com.google.common.collect.Range; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.driver.HandlerBehaviour; |
| 23 | import org.onosproject.net.flow.DefaultFlowRule; |
| 24 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 26 | import org.onosproject.net.flow.FlowEntry; |
| 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.FlowRuleService; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
| 31 | import org.onosproject.net.flow.criteria.Criterion; |
| 32 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 33 | import org.onosproject.net.flow.instructions.Instruction; |
| 34 | import org.onosproject.net.flow.instructions.Instructions; |
| 35 | |
| 36 | import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.CrossConnects; |
| 37 | import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.Pair; |
| 38 | import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.DefaultPair; |
| 39 | import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.DefaultCrossConnects; |
| 40 | import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.PortFormat; |
| 41 | |
| 42 | import java.util.List; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | /** |
| 46 | * Polatis optical utilities. |
| 47 | */ |
| 48 | public final class PolatisOpticalUtility { |
| 49 | |
| 50 | private static final int DEFAULT_PRIORITY = 88; |
| 51 | private static final String DEFAULT_APP = "org.onosproject.drivers.polatis.netconf"; |
| 52 | public static final int POWER_MULTIPLIER = 100; |
Laszlo Papp | 8b87a19 | 2017-10-19 18:47:12 +0100 | [diff] [blame] | 53 | public static final int VOA_MULTIPLIER = 100; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 54 | public static final Range<Long> POWER_RANGE = Range.closed(-6000L, 2800L); |
| 55 | |
| 56 | private PolatisOpticalUtility() { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Transforms a flow FlowRule object to a cross-connect object. |
| 61 | * @param behaviour the parent driver handler |
| 62 | * @param rule FlowRule object |
| 63 | * @return cross connect object |
| 64 | */ |
| 65 | public static CrossConnects fromFlowRule(HandlerBehaviour behaviour, FlowRule rule) { |
| 66 | // TrafficSelector |
| 67 | Set<Criterion> criterions = rule.selector().criteria(); |
| 68 | PortNumber inPort = criterions.stream() |
| 69 | .filter(c -> c instanceof PortCriterion) |
| 70 | .map(c -> ((PortCriterion) c).port()) |
| 71 | .findAny() |
| 72 | .orElse(null); |
| 73 | // TrafficTreatment |
| 74 | List<Instruction> instructions = rule.treatment().immediate(); |
| 75 | PortNumber outPort = instructions.stream() |
| 76 | .filter(c -> c instanceof Instructions.OutputInstruction) |
| 77 | .map(c -> ((Instructions.OutputInstruction) c).port()) |
| 78 | .findAny() |
| 79 | .orElse(null); |
| 80 | DefaultCrossConnects crossConnects = new DefaultCrossConnects(); |
| 81 | DefaultPair p = new DefaultPair(); |
| 82 | p.ingress(new PortFormat(inPort.toLong())); |
| 83 | p.egress(new PortFormat(outPort.toLong())); |
| 84 | crossConnects.addToPair(p); |
| 85 | return crossConnects; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Finds the FlowRule from flow rule store by the given cross connect information. |
| 90 | * Returns an extra flow to remove the flow by ONOS if not found. |
| 91 | * @param behaviour the parent driver handler |
| 92 | * @param cfg cross connect information |
| 93 | * @return the flow rule |
| 94 | */ |
| 95 | public static FlowRule toFlowRule(HandlerBehaviour behaviour, CrossConnects cfg) { |
| 96 | // Note: do we need to handle more than one pair? In any case, this |
| 97 | // looks strange. |
| 98 | Pair p = cfg.pair().get(0); |
| 99 | long i = p.ingress().uint32(); |
| 100 | long o = p.egress().uint32(); |
| 101 | PortNumber iPortNumber = PortNumber.portNumber(i); |
| 102 | PortNumber oPortNumber = PortNumber.portNumber(o); |
| 103 | return toFlowRule(behaviour, iPortNumber, oPortNumber); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Finds the FlowRule from flow rule store by the given ports and channel. |
| 108 | * Returns an extra flow to remove the flow by ONOS if not found. |
| 109 | * @param behaviour the parent driver handler |
| 110 | * @param inPort the input port |
| 111 | * @param outPort the output port |
| 112 | * @return the flow rule |
| 113 | */ |
| 114 | public static FlowRule toFlowRule(HandlerBehaviour behaviour, PortNumber inPort, |
| 115 | PortNumber outPort) { |
| 116 | FlowRuleService service = behaviour.handler().get(FlowRuleService.class); |
| 117 | Iterable<FlowEntry> entries = service.getFlowEntries(behaviour.data().deviceId()); |
| 118 | // Try to Find the flow from flow rule store. |
| 119 | for (FlowEntry entry : entries) { |
| 120 | Set<Criterion> criterions = entry.selector().criteria(); |
| 121 | // input port |
| 122 | PortNumber ip = criterions.stream() |
| 123 | .filter(c -> c instanceof PortCriterion) |
| 124 | .map(c -> ((PortCriterion) c).port()) |
| 125 | .findAny() |
| 126 | .orElse(null); |
| 127 | // output port |
| 128 | PortNumber op = entry.treatment().immediate().stream() |
| 129 | .filter(c -> c instanceof Instructions.OutputInstruction) |
| 130 | .map(c -> ((Instructions.OutputInstruction) c).port()) |
| 131 | .findAny() |
| 132 | .orElse(null); |
| 133 | if (inPort.equals(ip) && outPort.equals(op)) { |
| 134 | // Find the flow. |
| 135 | return entry; |
| 136 | } |
| 137 | } |
| 138 | // Cannot find the flow from store. So report an extra flow to remove the flow by ONOS. |
| 139 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 140 | .matchInPort(inPort) |
| 141 | .build(); |
| 142 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 143 | .setOutput(outPort) |
| 144 | .build(); |
| 145 | return DefaultFlowRule.builder() |
| 146 | .forDevice(behaviour.data().deviceId()) |
| 147 | .withSelector(selector) |
| 148 | .withTreatment(treatment) |
| 149 | .makePermanent() |
| 150 | .withPriority(DEFAULT_PRIORITY) |
| 151 | .fromApp(behaviour.handler().get(CoreService.class).getAppId(DEFAULT_APP)) |
| 152 | .build(); |
| 153 | |
| 154 | } |
| 155 | } |