Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 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. |
Andrea Campanella | bb8775b | 2016-04-12 10:32:14 -0700 | [diff] [blame] | 43 | * <p> |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 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 | |
Andrea Campanella | bb8775b | 2016-04-12 10:32:14 -0700 | [diff] [blame] | 70 | createDevice(ipAddress, Integer.parseInt(port)); |
| 71 | } |
| 72 | |
| 73 | public LumentumSnmpDevice(String ipAddress, int port) throws IOException { |
| 74 | createDevice(ipAddress, port); |
| 75 | } |
| 76 | |
| 77 | private void createDevice(String ipAddress, int port) throws IOException { |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 78 | Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port); |
| 79 | TransportMapping transport = new DefaultUdpTransportMapping(); |
| 80 | transport.listen(); |
| 81 | snmp = new Snmp(transport); |
| 82 | |
| 83 | // setting up target |
| 84 | target = new CommunityTarget(); |
| 85 | target.setCommunity(new OctetString("public")); |
| 86 | target.setAddress(targetAddress); |
| 87 | target.setRetries(3); |
| 88 | target.setTimeout(1000 * 3); |
| 89 | target.setVersion(SnmpConstants.version2c); |
| 90 | target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU); |
| 91 | } |
| 92 | |
| 93 | public ResponseEvent set(PDU pdu) throws IOException { |
| 94 | return snmp.set(pdu, target); |
| 95 | } |
| 96 | |
| 97 | public List<TreeEvent> get(OID oid) { |
| 98 | TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory()); |
| 99 | treeUtils.setMaxRepetitions(MAX_REPETITIONS); |
| 100 | return treeUtils.getSubtree(target, oid); |
| 101 | } |
| 102 | } |