[ONOS-3943] SNMP protocol and provider refactoring according to ONOS architecture

Change-Id: Ie87ee6c181c9550ffac602397f2ee74a691bbdfa
diff --git a/protocols/snmp/ctl/BUCK b/protocols/snmp/ctl/BUCK
new file mode 100644
index 0000000..0c83b08
--- /dev/null
+++ b/protocols/snmp/ctl/BUCK
@@ -0,0 +1,35 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-snmp-ctl'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//incubator/api:onos-incubator-api',
+    '//protocols/snmp/api:onos-snmp-api',
+    '//lib:org.apache.servicemix.bundles.snmp4j',
+    '//lib:snmp-core',
+    '//lib:bti7000',
+    '//lib:mibs-net-snmp',
+    '//lib:mibs-rfc',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/protocols/snmp/ctl/pom.xml b/protocols/snmp/ctl/pom.xml
new file mode 100644
index 0000000..fb213d1
--- /dev/null
+++ b/protocols/snmp/ctl/pom.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016 Open Networking Laboratory
+  ~
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>onos-snmp-protocol</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.6.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-snmp-ctl</artifactId>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-snmp-api</artifactId>
+            <version>1.6.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-api</artifactId>
+            <version>1.6.0-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
+
+
+</project>
\ No newline at end of file
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
new file mode 100644
index 0000000..35556cd
--- /dev/null
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.snmp.ctl;
+
+import com.btisystems.pronx.ems.core.snmp.DefaultSnmpConfigurationFactory;
+import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
+import com.btisystems.pronx.ems.core.snmp.SnmpSessionFactory;
+import com.btisystems.pronx.ems.core.snmp.V2cSnmpConfiguration;
+import com.google.common.base.Preconditions;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+import org.onosproject.net.DeviceId;
+import org.onosproject.snmp.SnmpController;
+import org.onosproject.snmp.SnmpDevice;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Default implementation of the SNMP sub-controller.
+ */
+@Component(immediate = true)
+@Service
+public class DefaultSnmpController implements SnmpController {
+
+    private final Logger log = LoggerFactory
+            .getLogger(getClass());
+
+    private ISnmpSessionFactory sessionFactory;
+
+    private final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
+    protected Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
+
+    @Activate
+    public void activate(ComponentContext context) {
+        sessionFactory = new SnmpSessionFactory(
+                new DefaultSnmpConfigurationFactory(new V2cSnmpConfiguration()));
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        snmpDeviceMap.clear();
+        log.info("Stopped");
+    }
+
+    @Override
+    public ISnmpSession getSession(DeviceId deviceId) throws IOException {
+        if (!sessionMap.containsKey(deviceId)) {
+            SnmpDevice device = snmpDeviceMap.get(deviceId);
+            String ipAddress = null;
+            int port = -1;
+            if (device != null) {
+                ipAddress = device.getSnmpHost();
+                port = device.getSnmpPort();
+            } else {
+                String[] deviceComponents = deviceId.toString().split(":");
+                if (deviceComponents.length > 1) {
+                    ipAddress = deviceComponents[1];
+                    port = Integer.parseInt(deviceComponents[2]);
+
+                } else {
+                    log.error("Cannot obtain correct information from device id", deviceId);
+                }
+            }
+            Preconditions.checkNotNull(ipAddress, "ip address is empty, cannot start session");
+            Preconditions.checkArgument(port != -1, "port is incorrect, cannot start session");
+
+            ISnmpConfiguration config = new V2cSnmpConfiguration();
+            config.setPort(port);
+            sessionMap.put(deviceId, sessionFactory.createSession(config, ipAddress));
+        }
+        return sessionMap.get(deviceId);
+    }
+
+    @Override
+    public Collection<SnmpDevice> getDevices() {
+        return snmpDeviceMap.values();
+    }
+
+    @Override
+    public SnmpDevice getDevice(DeviceId did) {
+        return snmpDeviceMap.get(did);
+    }
+
+    @Override
+    public void removeDevice(DeviceId did) {
+        snmpDeviceMap.remove(did);
+    }
+
+    @Override
+    public void addDevice(DeviceId did, SnmpDevice device) {
+        snmpDeviceMap.put(did, device);
+    }
+
+    @Override
+    public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
+        return new DefaultAlarm.Builder(
+                deviceId, "SNMP alarm retrieval failed",
+                Alarm.SeverityLevel.CRITICAL,
+                System.currentTimeMillis()).build();
+    }
+}
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java
new file mode 100644
index 0000000..a76e86b
--- /dev/null
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.snmp.ctl;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.snmp.SnmpDevice;
+import org.slf4j.Logger;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * This is a logical representation of actual SNMP device, carrying all the necessary information to connect and execute
+ * SNMP operations.
+ */
+public class DefaultSnmpDevice implements SnmpDevice {
+
+    private final Logger log = getLogger(DefaultSnmpDevice.class);
+
+
+    private static final int DEFAULT_SNMP_PORT = 161;
+
+    private static final String SCHEME = "snmp";
+
+    private final String snmpHost;
+    private final DeviceId deviceId;
+    private int snmpPort = DEFAULT_SNMP_PORT;
+    private final String username;
+    //Community is a conventional name for password in SNMP.
+    private final String community;
+    private boolean reachable = false;
+
+
+    public DefaultSnmpDevice(String snmpHost, int snmpPort, String username, String community) {
+
+        this.snmpHost = checkNotNull(snmpHost, "SNMP Device IP cannot be null");
+        this.snmpPort = checkNotNull(snmpPort, "SNMP Device port cannot be null");
+        this.username = username;
+        this.community = community;
+        this.deviceId = createDeviceId();
+        reachable = true;
+    }
+
+    @Override
+    public String deviceInfo() {
+        return new StringBuilder("host: ").append(snmpHost).append(". port: ")
+                .append(snmpPort).toString();
+    }
+
+    @Override
+    public void disconnect() {
+        log.info("disconnect");
+        reachable = false;
+    }
+
+    @Override
+    public boolean isReachable() {
+        return reachable;
+    }
+
+    @Override
+    public String getSnmpHost() {
+        return snmpHost;
+    }
+
+
+    @Override
+    public int getSnmpPort() {
+        return snmpPort;
+    }
+
+    @Override
+    public String getUsername() {
+        return username;
+    }
+
+    @Override
+    public String getCommunity() {
+        return community;
+    }
+
+    @Override
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    private DeviceId createDeviceId() {
+        String additionalSsp = new StringBuilder(
+                snmpHost).append(":")
+                .append(snmpPort).toString();
+        try {
+            return DeviceId.deviceId(new URI(SCHEME, additionalSsp,
+                                             null));
+        } catch (URISyntaxException e) {
+            log.error("Syntax Error while creating URI for the device: "
+                              + additionalSsp
+                              + " couldn't persist the device onto the store", e);
+            throw new IllegalArgumentException("Can't create device ID from " + additionalSsp, e);
+        }
+    }
+}
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/package-info.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/package-info.java
new file mode 100644
index 0000000..455a73c
--- /dev/null
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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 implementation fo SNMP elements.
+ */
+package org.onosproject.snmp.ctl;