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.ImmutableList; |
| 20 | |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 25 | import org.onosproject.net.flow.FlowEntry; |
| 26 | import org.onosproject.net.flow.FlowRule; |
| 27 | import org.onosproject.net.flow.FlowRuleProgrammable; |
| 28 | |
| 29 | import org.snmp4j.smi.OID; |
| 30 | import org.snmp4j.smi.VariableBinding; |
| 31 | import org.snmp4j.util.TableEvent; |
| 32 | |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Collection; |
| 38 | import java.util.List; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static org.onosproject.drivers.polatis.snmp.PolatisOpticalUtility.fromFlowRule; |
| 42 | import static org.onosproject.drivers.polatis.snmp.PolatisSnmpUtility.getTable; |
| 43 | import static org.onosproject.drivers.polatis.snmp.PolatisSnmpUtility.set; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Flow rule programmable behaviour for Polatis optical snmp devices. |
| 48 | */ |
| 49 | public class PolatisFlowRuleProgrammable |
| 50 | extends AbstractHandlerBehaviour implements FlowRuleProgrammable { |
| 51 | |
| 52 | private static final String PORT_ENTRY_OID = ".1.3.6.1.4.1.26592.2.2.2.1.2"; |
| 53 | private static final String PORT_PATCH_OID = PORT_ENTRY_OID + ".1.2"; |
| 54 | |
| 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | @Override |
| 58 | public Collection<FlowEntry> getFlowEntries() { |
| 59 | List<TableEvent> events; |
| 60 | DeviceId deviceId = handler().data().deviceId(); |
| 61 | ImmutableList.Builder<FlowEntry> connectionsBuilder = ImmutableList.builder(); |
| 62 | |
| 63 | try { |
| 64 | OID[] columnOIDs = {new OID(PORT_PATCH_OID)}; |
| 65 | events = getTable(handler(), columnOIDs); |
| 66 | } catch (IOException e) { |
| 67 | log.error("Error reading ports table for device {} exception {}", deviceId, e); |
| 68 | return connectionsBuilder.build(); |
| 69 | } |
| 70 | |
| 71 | if (events == null) { |
| 72 | log.error("Error reading ports table for device {}", deviceId); |
| 73 | return connectionsBuilder.build(); |
| 74 | } |
| 75 | |
| 76 | for (TableEvent event : events) { |
| 77 | if (event == null) { |
| 78 | log.error("Error reading event for device {}", deviceId); |
| 79 | continue; |
| 80 | } |
| 81 | VariableBinding[] columns = event.getColumns(); |
| 82 | if (columns == null) { |
| 83 | log.error("Error reading columns for device {}", deviceId); |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | VariableBinding patchColumn = columns[0]; |
| 88 | if (patchColumn == null) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | int port = event.getIndex().last(); |
| 93 | int patch = patchColumn.getVariable().toInt(); |
| 94 | if (patch == 0) { |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | FlowRule flowRule = PolatisOpticalUtility.toFlowRule(this, |
| 99 | PortNumber.portNumber(port), PortNumber.portNumber(patch)); |
| 100 | connectionsBuilder.add(new DefaultFlowEntry(flowRule, FlowEntry.FlowEntryState.ADDED)); |
| 101 | } |
| 102 | |
| 103 | return connectionsBuilder.build(); |
| 104 | } |
| 105 | |
| 106 | private boolean editConnection(FlowRule rule, boolean delete) { |
| 107 | List<VariableBinding> vbs = new ArrayList<>(); |
| 108 | vbs.add(fromFlowRule(rule, delete)); |
| 109 | DeviceId deviceId = handler().data().deviceId(); |
| 110 | try { |
| 111 | set(handler(), vbs); |
| 112 | } catch (IOException e) { |
| 113 | log.error("Error writing ports table for device {} exception {}", deviceId, e); |
| 114 | return false; |
| 115 | } |
| 116 | // TODO: check for errors |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
| 122 | return rules.stream() |
| 123 | .filter(c -> editConnection(c, false)) |
| 124 | .collect(Collectors.toList()); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
| 129 | return rules.stream() |
| 130 | .filter(c -> editConnection(c, true)) |
| 131 | .collect(Collectors.toList()); |
| 132 | } |
| 133 | } |