Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 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.snmp; |
| 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.snmp4j.smi.OID; |
| 37 | import org.snmp4j.smi.UnsignedInteger32; |
| 38 | import org.snmp4j.smi.Variable; |
| 39 | import org.snmp4j.smi.VariableBinding; |
| 40 | |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | /** |
| 45 | * Polatis optical utilities. |
| 46 | */ |
| 47 | public final class PolatisOpticalUtility { |
| 48 | |
| 49 | private static final int DEFAULT_PRIORITY = 88; |
| 50 | private static final String DEFAULT_APP = "org.onosproject.drivers.polatis.snmp"; |
| 51 | public static final int POWER_MULTIPLIER = 100; |
| 52 | public static final int VOA_MULTIPLIER = 100; |
| 53 | public static final Range<Long> POWER_RANGE = Range.closed(-6000L, 2800L); |
| 54 | |
| 55 | private static final String PORT_ENTRY_OID = ".1.3.6.1.4.1.26592.2.2.2.1.2"; |
| 56 | private static final String PORT_PATCH_OID = PORT_ENTRY_OID + ".1.2"; |
| 57 | |
| 58 | private PolatisOpticalUtility() { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Transforms a flow FlowRule object to a variable binding. |
| 63 | * @param rule FlowRule object |
| 64 | * @param delete whether it is a delete or edit request |
| 65 | * @return variable binding |
| 66 | */ |
| 67 | public static VariableBinding fromFlowRule(FlowRule rule, boolean delete) { |
| 68 | Set<Criterion> criterions = rule.selector().criteria(); |
| 69 | PortNumber inPort = criterions.stream() |
| 70 | .filter(c -> c instanceof PortCriterion) |
| 71 | .map(c -> ((PortCriterion) c).port()) |
| 72 | .findAny() |
| 73 | .orElse(null); |
| 74 | long input = inPort.toLong(); |
| 75 | List<Instruction> instructions = rule.treatment().immediate(); |
| 76 | PortNumber outPort = instructions.stream() |
| 77 | .filter(c -> c instanceof Instructions.OutputInstruction) |
| 78 | .map(c -> ((Instructions.OutputInstruction) c).port()) |
| 79 | .findAny() |
| 80 | .orElse(null); |
| 81 | long output = outPort.toLong(); |
| 82 | OID oid = new OID(PORT_PATCH_OID + "." + input); |
| 83 | Variable var = new UnsignedInteger32(delete ? 0 : output); |
| 84 | return new VariableBinding(oid, var); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Finds the FlowRule from flow rule store by the given ports and channel. |
| 89 | * Returns an extra flow to remove the flow by ONOS if not found. |
| 90 | * @param behaviour the parent driver handler |
| 91 | * @param inPort the input port |
| 92 | * @param outPort the output port |
| 93 | * @return the flow rule |
| 94 | */ |
| 95 | public static FlowRule toFlowRule(HandlerBehaviour behaviour, PortNumber inPort, |
| 96 | PortNumber outPort) { |
| 97 | FlowRuleService service = behaviour.handler().get(FlowRuleService.class); |
| 98 | Iterable<FlowEntry> entries = service.getFlowEntries(behaviour.data().deviceId()); |
| 99 | // Try to Find the flow from flow rule store. |
| 100 | for (FlowEntry entry : entries) { |
| 101 | Set<Criterion> criterions = entry.selector().criteria(); |
| 102 | // input port |
| 103 | PortNumber ip = criterions.stream() |
| 104 | .filter(c -> c instanceof PortCriterion) |
| 105 | .map(c -> ((PortCriterion) c).port()) |
| 106 | .findAny() |
| 107 | .orElse(null); |
| 108 | // output port |
| 109 | PortNumber op = entry.treatment().immediate().stream() |
| 110 | .filter(c -> c instanceof Instructions.OutputInstruction) |
| 111 | .map(c -> ((Instructions.OutputInstruction) c).port()) |
| 112 | .findAny() |
| 113 | .orElse(null); |
| 114 | if (inPort.equals(ip) && outPort.equals(op)) { |
| 115 | // Find the flow. |
| 116 | return entry; |
| 117 | } |
| 118 | } |
| 119 | // Cannot find the flow from store. So report an extra flow to remove the flow by ONOS. |
| 120 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 121 | .matchInPort(inPort) |
| 122 | .build(); |
| 123 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 124 | .setOutput(outPort) |
| 125 | .build(); |
| 126 | return DefaultFlowRule.builder() |
| 127 | .forDevice(behaviour.data().deviceId()) |
| 128 | .withSelector(selector) |
| 129 | .withTreatment(treatment) |
| 130 | .makePermanent() |
| 131 | .withPriority(DEFAULT_PRIORITY) |
| 132 | .fromApp(behaviour.handler().get(CoreService.class).getAppId(DEFAULT_APP)) |
| 133 | .build(); |
| 134 | |
| 135 | } |
| 136 | } |