Add a Polatis SNMP driver

Change-Id: I4ba986d9e7aebb33cbce413b70393db16fc9ffcc
diff --git a/drivers/polatis/snmp/BUCK b/drivers/polatis/snmp/BUCK
new file mode 100644
index 0000000..5ff6cdf
--- /dev/null
+++ b/drivers/polatis/snmp/BUCK
@@ -0,0 +1,35 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:org.apache.servicemix.bundles.snmp4j',
+    '//drivers/utilities:onos-drivers-utilities',
+    '//protocols/snmp/api:onos-protocols-snmp-api',
+    '//apps/optical-model:onos-apps-optical-model',
+    '//incubator/api:onos-incubator-api',
+]
+
+APPS = [
+    'org.onosproject.snmp',
+    'org.onosproject.optical-model',
+    'org.onosproject.faultmanagement',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+    '//core/api:onos-api-tests',
+]
+
+osgi_jar_with_tests(
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+    resources_root = 'src/main/resources',
+    resources = glob(['src/main/resources/**']),
+)
+
+onos_app(
+    app_name = 'org.onosproject.drivers.polatis.snmp',
+    title = 'Polatis SNMP Device Drivers',
+    category = 'Drivers',
+    url = 'http://www.polatis.com',
+    description = 'ONOS Polatis SNMP Device Drivers application.',
+    required_apps = APPS
+)
diff --git a/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisDeviceDescription.java b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisDeviceDescription.java
new file mode 100644
index 0000000..ff2862d
--- /dev/null
+++ b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisDeviceDescription.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.polatis.snmp;
+
+import com.google.common.collect.Lists;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+
+import org.onlab.packet.ChassisId;
+
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import static org.onosproject.drivers.polatis.snmp.PolatisSnmpUtility.getOid;
+
+/**
+ * Representation of device information and ports via SNMP for all Polatis
+ * optical circuit switches.
+ */
+public class PolatisDeviceDescription extends AbstractHandlerBehaviour
+    implements DeviceDescriptionDiscovery {
+
+    private static final String DEFAULT_MANUFACTURER = "Polatis";
+    private static final String DEFAULT_DESCRIPTION_DATA = "Unknown";
+
+    private static final String SOFTWARE_VERSION_OID = ".1.3.6.1.4.1.26592.2.1.2.2.3.0";
+    private static final String PRODUCT_CODE_OID = ".1.3.6.1.4.1.26592.2.1.2.2.1.0";
+    private static final String SERIAL_NUMBER_OID = ".1.3.6.1.4.1.26592.2.1.2.2.2.0";
+
+    private final Logger log = getLogger(getClass());
+
+    /**
+     * Discovers device details, for Polatis Snmp device by getting the system
+     * information.
+     *
+     * @return device description
+     */
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
+        DeviceId deviceId = handler().data().deviceId();
+        Device device = deviceService.getDevice(deviceId);
+        if (device == null) {
+            return new DefaultDeviceDescription(deviceId.uri(), Device.Type.FIBER_SWITCH,
+                    DEFAULT_MANUFACTURER, DEFAULT_DESCRIPTION_DATA,
+                    DEFAULT_DESCRIPTION_DATA, DEFAULT_DESCRIPTION_DATA,
+                    new ChassisId());
+        }
+        String hardwareVersion = DEFAULT_DESCRIPTION_DATA;
+        try {
+            hardwareVersion = hardwareVersion();
+        } catch (IOException e) {
+            log.error("Error reading hardware version for device {} exception {}", deviceId, e);
+        }
+
+        String softwareVersion = DEFAULT_DESCRIPTION_DATA;
+        try {
+            softwareVersion = softwareVersion();
+        } catch (IOException e) {
+            log.error("Error reading software version for device {} exception {}", deviceId, e);
+        }
+
+        String serialNumber = DEFAULT_DESCRIPTION_DATA;
+        try {
+            serialNumber = serialNumber();
+        } catch (IOException e) {
+            log.error("Error reading serial number for device {} exception {}", deviceId, e);
+        }
+
+        return new DefaultDeviceDescription(deviceId.uri(), Device.Type.FIBER_SWITCH,
+                                            DEFAULT_MANUFACTURER, hardwareVersion,
+                                            softwareVersion, serialNumber,
+                                            device.chassisId(), (SparseAnnotations) device.annotations());
+    }
+
+    /**
+     * Discovers port details, for Polatis Snmp device.
+     *
+     * @return port list
+     */
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        // TODO: Implement me
+        return Lists.newLinkedList();
+        // return ImmutableList.copyOf(this.getPorts());
+    }
+
+    private String hardwareVersion() throws IOException {
+        return getOid(handler(), PRODUCT_CODE_OID);
+    }
+
+    private String softwareVersion() throws IOException {
+        return getOid(handler(), SOFTWARE_VERSION_OID);
+    }
+
+    private String serialNumber() throws IOException {
+        return getOid(handler(), SERIAL_NUMBER_OID);
+    }
+}
diff --git a/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisDriversLoader.java b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisDriversLoader.java
new file mode 100644
index 0000000..df63dc3
--- /dev/null
+++ b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisDriversLoader.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2018 Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.polatis.snmp;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.net.driver.AbstractDriverLoader;
+import org.onosproject.net.optical.OpticalDevice;
+import org.onosproject.ui.UiGlyph;
+import org.onosproject.ui.UiGlyphFactory;
+import org.onosproject.ui.UiExtensionService;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Loader for Polatis Snmp device drivers.
+ */
+@Component(immediate = true)
+public class PolatisDriversLoader extends AbstractDriverLoader {
+
+    // OSGI: help bundle plugin discover runtime package dependency.
+    @SuppressWarnings("unused")
+    private OpticalDevice optical;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected UiExtensionService uiExtensionService;
+
+    private UiGlyphFactory glyphFactory =
+        () -> ImmutableList.of(
+            new UiGlyph("policon", "0 0 64 64",
+                "M 32.024746,2 30.163615,19.069136 24.258784,3.015638 "
+                + "26.879599,19.985033 17.021343,6.007051 23.943688,21.71947 "
+                + "10.8045,10.769161 21.557349,24.15439 6.031794,16.978659 "
+                + "19.883076,27.1245 3.027943,24.21114 19.033986,30.42674 "
+                + "2,31.97526 19.069136,33.83639 3.015638,39.74122 "
+                + "19.985033,37.12041 6.007051,46.97866 21.719466,40.05632 "
+                + "10.769161,53.19551 24.154391,42.44265 16.978659,57.96822 "
+                + "27.124504,44.11693 24.21114,60.97206 30.426738,44.96602 "
+                + "31.975259,62 33.83639,44.93086 39.74122,60.98437 "
+                + "37.120405,44.01497 46.978663,57.99296 40.056317,42.28054 "
+                + "53.195507,53.23084 42.442656,39.84561 57.968215,47.02135 "
+                + "44.116927,36.8755 60.972063,39.78886 44.966018,33.57327 "
+                + "62,32.02475 44.930865,30.16362 60.984369,24.25878 "
+                + "44.014972,26.8796 57.992959,17.021342 42.280539,23.94369 "
+                + "53.23084,10.8045 39.845614,21.55735 47.021349,6.031794 "
+                + "36.875501,19.883076 39.788865,3.027943 33.573267,19.033986 Z "
+                + "m -0.05497,19.23081 A 10.768943,10.768943 0 0 1 "
+                + "42.769201,31.96977 10.768943,10.768943 0 0 1 "
+                + "32.030235,42.7692 10.768943,10.768943 0 0 1 "
+                + "21.230812,32.03023 10.768943,10.768943 0 0 1 "
+                + "31.969778,21.23081 Z")
+            );
+
+    public PolatisDriversLoader() {
+        super("/polatis-snmp-drivers.xml");
+    }
+
+    @Activate
+    @Override
+    protected void activate() {
+        uiExtensionService.register(glyphFactory);
+        super.activate();
+    }
+
+    @Deactivate
+    @Override
+    protected void deactivate() {
+        uiExtensionService.unregister(glyphFactory);
+        super.deactivate();
+    }
+
+}
diff --git a/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisSnmpUtility.java b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisSnmpUtility.java
new file mode 100644
index 0000000..654294f
--- /dev/null
+++ b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/PolatisSnmpUtility.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.polatis.snmp;
+
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.snmp.SnmpDevice;
+import org.onosproject.snmp.SnmpController;
+
+import org.snmp4j.CommunityTarget;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.event.ResponseEvent;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.smi.Address;
+import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.VariableBinding;
+
+import java.io.IOException;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * SNP utility for Polatis SNMP drivers.
+ */
+public final class PolatisSnmpUtility {
+
+    private static final int MAX_SIZE_RESPONSE_PDU = 65535;
+
+    private PolatisSnmpUtility() {
+    }
+
+    private static SnmpDevice getDevice(DriverHandler handler) {
+        SnmpController controller = checkNotNull(handler.get(SnmpController.class));
+        SnmpDevice device = controller.getDevice(handler.data().deviceId());
+        return device;
+    }
+
+    private static CommunityTarget getTarget(DriverHandler handler) {
+        SnmpDevice device = getDevice(handler);
+        Address targetAddress = GenericAddress.parse(device.getProtocol() +
+                                                     ":" + device.getSnmpHost() +
+                                                     "/" + device.getSnmpPort());
+        CommunityTarget target = new CommunityTarget();
+        target.setCommunity(new OctetString(getDevice(handler).getCommunity()));
+        target.setAddress(targetAddress);
+        target.setRetries(3);
+        target.setTimeout(1000L * 3L);
+        target.setVersion(SnmpConstants.version2c);
+        target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU);
+        return target;
+    }
+
+    private static Snmp getSession(DriverHandler handler) {
+        Snmp session = getDevice(handler).getSession();
+        return session;
+    }
+
+    /**
+     * Sends a SET request.
+     *
+     * @param handler parent driver handler
+     * @param pdu SNMP protocol data unit
+     * @return the response event
+     * @throws IOException if unable to send a set request
+     */
+    public static ResponseEvent set(DriverHandler handler, PDU pdu) throws IOException {
+        Snmp session = getSession(handler);
+        CommunityTarget target = getTarget(handler);
+        return session.set(pdu, target);
+    }
+
+    /**
+     * Retrieves static object value.
+     *
+     * @param handler parent driver handler
+     * @param oid object identifier
+     * @return the string value
+     * @throws IOException if unable to retrieve the object value
+     */
+    public static String getOid(DriverHandler handler, String oid) throws IOException {
+        PDU pdu = new PDU();
+        pdu.add(new VariableBinding(new OID(oid)));
+        pdu.setType(PDU.GET);
+        Snmp session = getSession(handler);
+        CommunityTarget target = getTarget(handler);
+        ResponseEvent event = session.send(pdu, target);
+        return event.getResponse().get(0).getVariable().toString();
+    }
+}
diff --git a/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/package-info.java b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/package-info.java
new file mode 100644
index 0000000..5a6d5b7
--- /dev/null
+++ b/drivers/polatis/snmp/src/main/java/org/onosproject/drivers/polatis/snmp/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package for Polatis SNMP device drivers.
+ */
+package org.onosproject.drivers.polatis.snmp;
diff --git a/drivers/polatis/snmp/src/main/resources/polatis-snmp-drivers.xml b/drivers/polatis/snmp/src/main/resources/polatis-snmp-drivers.xml
new file mode 100644
index 0000000..43e4063
--- /dev/null
+++ b/drivers/polatis/snmp/src/main/resources/polatis-snmp-drivers.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2018-present Open Networking Foundation
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<drivers>
+    <driver name="polatis-snmp" extends="" manufacturer="Polatis"
+            hwVersion="" swVersion="">
+        <behaviour api="org.onosproject.net.optical.OpticalDevice"
+                   impl="org.onosproject.net.optical.DefaultOpticalDevice"/>
+        <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
+                   impl="org.onosproject.drivers.polatis.snmp.PolatisDeviceDescription"/>
+        <property name="uiType">policon</property>
+    </driver>
+</drivers>
+
diff --git a/drivers/polatis/snmp/src/test/java/org/onosproject/drivers/polatis/snmp/PolatisDriversLoaderTest.java b/drivers/polatis/snmp/src/test/java/org/onosproject/drivers/polatis/snmp/PolatisDriversLoaderTest.java
new file mode 100644
index 0000000..fe7596b
--- /dev/null
+++ b/drivers/polatis/snmp/src/test/java/org/onosproject/drivers/polatis/snmp/PolatisDriversLoaderTest.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2018 Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.polatis.snmp;
+
+import org.junit.Before;
+import org.onosproject.net.driver.AbstractDriverLoaderTest;
+import org.onosproject.ui.UiExtensionServiceAdapter;
+
+/**
+ * Polatis SNMP drivers loader test.
+ */
+public class PolatisDriversLoaderTest extends AbstractDriverLoaderTest {
+
+    @Before
+    public void setUp() {
+        loader = new PolatisDriversLoader();
+         ((PolatisDriversLoader) loader).uiExtensionService = new UiExtensionServiceAdapter();
+    }
+}
diff --git a/modules.defs b/modules.defs
index 7df940e..3c96b1a 100644
--- a/modules.defs
+++ b/modules.defs
@@ -117,6 +117,7 @@
     '//drivers/gnmi:onos-drivers-gnmi-oar',
     '//drivers/polatis/netconf:onos-drivers-polatis-netconf-oar',
     '//drivers/polatis/openflow:onos-drivers-polatis-openflow-oar',
+    '//drivers/polatis/snmp:onos-drivers-polatis-snmp-oar',
     '//drivers/odtn-driver:onos-drivers-odtn-driver-oar',
 ]