blob: 526173d8a3d72ee5c9f39cb16ddb9bcda12a715c [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.
43 *
44 * 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
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}