Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.drivers.lumentum; |
| 17 | |
| 18 | import com.google.common.base.Preconditions; |
| 19 | import org.onlab.util.Frequency; |
| 20 | import org.onosproject.net.ChannelSpacing; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.GridType; |
| 23 | import org.snmp4j.CommunityTarget; |
| 24 | import org.snmp4j.PDU; |
| 25 | import org.snmp4j.Snmp; |
| 26 | import org.snmp4j.TransportMapping; |
| 27 | import org.snmp4j.event.ResponseEvent; |
| 28 | import org.snmp4j.mp.SnmpConstants; |
| 29 | import org.snmp4j.smi.Address; |
| 30 | import org.snmp4j.smi.GenericAddress; |
| 31 | import org.snmp4j.smi.OID; |
| 32 | import org.snmp4j.smi.OctetString; |
| 33 | import org.snmp4j.transport.DefaultUdpTransportMapping; |
| 34 | import org.snmp4j.util.DefaultPDUFactory; |
| 35 | import org.snmp4j.util.TreeEvent; |
| 36 | import org.snmp4j.util.TreeUtils; |
| 37 | |
| 38 | import java.io.IOException; |
| 39 | import java.util.List; |
| 40 | |
| 41 | /** |
| 42 | * Quick and dirty device abstraction for SNMP-based Lumentum devices. |
| 43 | * |
| 44 | * TODO: Refactor once SnmpDevice is finished |
| 45 | */ |
| 46 | public class LumentumSnmpDevice { |
| 47 | |
| 48 | private static final int MAX_SIZE_RESPONSE_PDU = 65535; |
| 49 | private static final int MAX_REPETITIONS = 50; // Only 42 directed ports on our devices |
| 50 | |
| 51 | public static final GridType GRID_TYPE = GridType.DWDM; |
| 52 | public static final ChannelSpacing CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ; |
| 53 | public static final Frequency START_CENTER_FREQ = Frequency.ofGHz(191_350); |
| 54 | public static final Frequency END_CENTER_FREQ = Frequency.ofGHz(196_100); |
| 55 | |
| 56 | // Lumentum SDN ROADM has shifted channel plan. |
| 57 | // Channel 36 corresponds to ITU-T center frequency, which has spacing multiplier 0. |
| 58 | public static final int MULTIPLIER_SHIFT = 36; |
| 59 | |
| 60 | private Snmp snmp; |
| 61 | private CommunityTarget target; |
| 62 | |
| 63 | public LumentumSnmpDevice(DeviceId did) throws IOException { |
| 64 | String[] deviceComponents = did.toString().split(":"); |
| 65 | Preconditions.checkArgument(deviceComponents.length > 1); |
| 66 | |
| 67 | String ipAddress = deviceComponents[1]; |
| 68 | String port = deviceComponents[2]; |
| 69 | |
| 70 | Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port); |
| 71 | TransportMapping transport = new DefaultUdpTransportMapping(); |
| 72 | transport.listen(); |
| 73 | snmp = new Snmp(transport); |
| 74 | |
| 75 | // setting up target |
| 76 | target = new CommunityTarget(); |
| 77 | target.setCommunity(new OctetString("public")); |
| 78 | target.setAddress(targetAddress); |
| 79 | target.setRetries(3); |
| 80 | target.setTimeout(1000 * 3); |
| 81 | target.setVersion(SnmpConstants.version2c); |
| 82 | target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU); |
| 83 | } |
| 84 | |
| 85 | public ResponseEvent set(PDU pdu) throws IOException { |
| 86 | return snmp.set(pdu, target); |
| 87 | } |
| 88 | |
| 89 | public List<TreeEvent> get(OID oid) { |
| 90 | TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory()); |
| 91 | treeUtils.setMaxRepetitions(MAX_REPETITIONS); |
| 92 | return treeUtils.getSubtree(target, oid); |
| 93 | } |
| 94 | } |