blob: dd16f8b5668fc4cb6e7fcce91dc44e6f0d7ed8ad [file] [log] [blame]
Marc De Leenheerc662d322016-02-18 16:05:10 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Marc De Leenheerc662d322016-02-18 16:05:10 -08003 *
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 */
16package org.onosproject.drivers.lumentum;
17
18import com.google.common.base.Preconditions;
19import org.onlab.util.Frequency;
20import org.onosproject.net.ChannelSpacing;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.GridType;
23import org.snmp4j.CommunityTarget;
24import org.snmp4j.PDU;
25import org.snmp4j.Snmp;
26import org.snmp4j.TransportMapping;
27import org.snmp4j.event.ResponseEvent;
28import org.snmp4j.mp.SnmpConstants;
29import org.snmp4j.smi.Address;
30import org.snmp4j.smi.GenericAddress;
31import org.snmp4j.smi.OID;
32import org.snmp4j.smi.OctetString;
33import org.snmp4j.transport.DefaultUdpTransportMapping;
34import org.snmp4j.util.DefaultPDUFactory;
35import org.snmp4j.util.TreeEvent;
36import org.snmp4j.util.TreeUtils;
37
38import java.io.IOException;
39import java.util.List;
40
41/**
42 * Quick and dirty device abstraction for SNMP-based Lumentum devices.
Andrea Campanellabb8775b2016-04-12 10:32:14 -070043 * <p>
Marc De Leenheerc662d322016-02-18 16:05:10 -080044 * TODO: Refactor once SnmpDevice is finished
45 */
46public 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 Campanellabb8775b2016-04-12 10:32:14 -070070 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 Leenheerc662d322016-02-18 16:05:10 -080078 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}