[ONOS-6076] Moving tl1,snmp,rest netcfg to devices key

Change-Id: I332d6b9a3afad5bc8461f6bb94e2d0a3c2ca643e
diff --git a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceConfig.java b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceConfig.java
new file mode 100644
index 0000000..293f5cd
--- /dev/null
+++ b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceConfig.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2017-present 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.provider.snmp.device.impl;
+
+import com.google.common.annotations.Beta;
+import org.apache.commons.lang3.tuple.Pair;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.Config;
+
+/**
+ * Configuration to push devices to the SNMP provider.
+ */
+@Beta
+public class SnmpDeviceConfig extends Config<DeviceId> {
+
+    private static final String IP = "ip";
+    private static final String PORT = "port";
+    private static final String USERNAME = "username";
+    private static final String PASSWORD = "password";
+
+    @Override
+    public boolean isValid() {
+        return hasOnlyFields(IP, PORT, USERNAME, PASSWORD) &&
+                ip() != null;
+    }
+
+    /**
+     * Gets the Ip of the SNMP device.
+     *
+     * @return ip
+     */
+    public IpAddress ip() {
+        return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
+    }
+
+    /**
+     * Gets the port of the SNMP device.
+     *
+     * @return port
+     */
+    public int port() {
+        return get(PORT, extractIpPort().getValue());
+    }
+
+    /**
+     * Gets the username of the SNMP device.
+     *
+     * @return username
+     */
+    public String username() {
+        return get(USERNAME, "");
+    }
+
+    /**
+     * Gets the password of the SNMP device.
+     *
+     * @return password
+     */
+    public String password() {
+        return get(PASSWORD, "");
+    }
+
+
+    private Pair<String, Integer> extractIpPort() {
+        String info = subject.toString();
+        if (info.startsWith(SnmpDeviceProvider.SCHEME)) {
+            //+1 is due to length of colon separator
+            String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
+            int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
+            return Pair.of(ip, port);
+        }
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
index 2a36950..fb77c70 100644
--- a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
+++ b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.provider.snmp.device.impl;
 
+import com.google.common.collect.ImmutableList;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -36,6 +37,7 @@
 import org.onosproject.net.config.NetworkConfigEvent;
 import org.onosproject.net.config.NetworkConfigListener;
 import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.basics.SubjectFactories;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DeviceDescription;
 import org.onosproject.net.device.DeviceDescriptionDiscovery;
@@ -52,6 +54,8 @@
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 
+import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -72,7 +76,7 @@
 
     private static final String UNKNOWN = "unknown";
     private static final String APP_NAME = "org.onosproject.snmp";
-    private static final String SCHEME = "snmp";
+    protected static final String SCHEME = "snmp";
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected SnmpController controller;
@@ -102,7 +106,7 @@
     protected final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
 
 
-    protected final ConfigFactory factory =
+    protected final List<ConfigFactory> factories = ImmutableList.of(
             new ConfigFactory<ApplicationId, SnmpProviderConfig>(APP_SUBJECT_FACTORY,
                                                                  SnmpProviderConfig.class,
                                                                  "devices",
@@ -111,7 +115,15 @@
                 public SnmpProviderConfig createConfig() {
                     return new SnmpProviderConfig();
                 }
-            };
+            },
+            new ConfigFactory<DeviceId, SnmpDeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY,
+                                                          SnmpDeviceConfig.class,
+                                                          SCHEME) {
+                @Override
+                public SnmpDeviceConfig createConfig() {
+                    return new SnmpDeviceConfig();
+                }
+            });
 
 
     /**
@@ -127,8 +139,10 @@
 
         providerService = providerRegistry.register(this);
         appId = coreService.registerApplication(APP_NAME);
-        netCfgService.registerConfigFactory(factory);
+        factories.forEach(netCfgService::registerConfigFactory);
         netCfgService.addListener(cfgLister);
+        connectDevices();
+        addOrRemoveDevicesConfig();
         modified(context);
         log.info("Started");
     }
@@ -145,7 +159,7 @@
             log.error("Device builder did not terminate");
         }
         deviceBuilderExecutor.shutdownNow();
-        netCfgService.unregisterConfigFactory(factory);
+        factories.forEach(netCfgService::unregisterConfigFactory);
         netCfgService.removeListener(cfgLister);
         providerRegistry.unregister(this);
         providerService = null;
@@ -157,14 +171,16 @@
         log.info("Modified");
     }
 
+    //Old method to register devices provided via net-cfg under apps/snmp/ tree
     private void addOrRemoveDevicesConfig() {
         SnmpProviderConfig cfg = netCfgService.getConfig(appId, SnmpProviderConfig.class);
         if (cfg != null) {
             try {
                 cfg.getDevicesInfo().forEach(info -> {
-                    SnmpDevice device = new DefaultSnmpDevice(info.ip().toString(),
-                                                              info.port(), info.username(), info.password());
-                    buildDevice(device);
+                    buildDevice(new DefaultSnmpDevice(info.ip().toString(),
+                                                      info.port(), info.username(),
+                                                      info.password()));
+
                 });
             } catch (ConfigException e) {
                 log.error("Cannot read config error " + e);
@@ -172,6 +188,18 @@
         }
     }
 
+    //Method to register devices provided via net-cfg under devices/ tree
+    private void connectDevices() {
+        Set<DeviceId> deviceSubjects =
+                netCfgService.getSubjects(DeviceId.class, SnmpDeviceConfig.class);
+        deviceSubjects.forEach(deviceId -> {
+            SnmpDeviceConfig config =
+                    netCfgService.getConfig(deviceId, SnmpDeviceConfig.class);
+            buildDevice(new DefaultSnmpDevice(config.ip().toString(),
+                                              config.port(), config.username(), config.password()));
+        });
+    }
+
     private void buildDevice(SnmpDevice device) {
         if (device != null) {
             log.debug("Device Detail:host={}, port={}, state={}",
@@ -310,12 +338,19 @@
 
         @Override
         public void event(NetworkConfigEvent event) {
-            addOrRemoveDevicesConfig();
+            if (event.configClass().equals(SnmpDeviceConfig.class)) {
+                connectDevices();
+            } else {
+                log.warn("Injecting device via this Json is deprecated, " +
+                                 "please put configuration under devices/");
+                addOrRemoveDevicesConfig();
+            }
         }
 
         @Override
         public boolean isRelevant(NetworkConfigEvent event) {
-            return event.configClass().equals(SnmpProviderConfig.class) &&
+            return (event.configClass().equals(SnmpDeviceConfig.class) ||
+                    event.configClass().equals(SnmpProviderConfig.class)) &&
                     (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
                             event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
         }
diff --git a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpProviderConfig.java b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpProviderConfig.java
index 0c3486a..9fecad3 100644
--- a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpProviderConfig.java
+++ b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpProviderConfig.java
@@ -27,7 +27,9 @@
 
 /**
  * Configuration decoder for SNMP provider.
+ * @deprecated 1.10.0 Kingfisher
  */
+@Deprecated
 @Beta
 public class SnmpProviderConfig extends Config<ApplicationId> {