Moved ProxyArp, SDN-IP and BgpRouter to use new config format.

The new config format is based on the new network configuration subsystem.

Includes a few config fixes to NetworkConfigLoader and InterfaceManager.

Change-Id: Id7f766736decb7afb6b63c2731d3baba9fc7c764
diff --git a/apps/routing/src/main/java/org/onosproject/routing/cli/BgpPeersListCommand.java b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpPeersListCommand.java
deleted file mode 100644
index ff6cf26..0000000
--- a/apps/routing/src/main/java/org/onosproject/routing/cli/BgpPeersListCommand.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright 2015 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.routing.cli;
-
-import org.apache.karaf.shell.commands.Command;
-import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.core.ApplicationId;
-import org.onosproject.core.CoreService;
-import org.onosproject.net.config.NetworkConfigService;
-import org.onosproject.routing.RoutingService;
-import org.onosproject.routing.config.impl.BgpConfig;
-
-/**
- * Lists the BGP peers configured in the system.
- */
-@Command(scope = "onos", name = "bgp-peers",
-        description = "Lists all BGP peers")
-public class BgpPeersListCommand extends AbstractShellCommand {
-
-    private static final String FORMAT = "%s : %s";
-
-    @Override
-    protected void execute() {
-        NetworkConfigService configService = get(NetworkConfigService.class);
-        CoreService coreService = get(CoreService.class);
-        ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
-
-        print(appId.toString());
-
-        BgpConfig config = configService.getConfig(appId, BgpConfig.class);
-
-        if (config == null || config.bgpPeers().isEmpty()) {
-            print("No peers configured");
-        } else {
-            config.bgpPeers().forEach(
-                    p -> print(FORMAT, p.ipAddress(), p.connectPoint()));
-        }
-    }
-}
diff --git a/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java
index 42b5076..d37c6d3 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/cli/BgpSpeakersListCommand.java
@@ -22,7 +22,7 @@
 import org.onosproject.core.CoreService;
 import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.routing.RoutingService;
-import org.onosproject.routing.config.impl.BgpConfig;
+import org.onosproject.routing.config.BgpConfig;
 
 /**
  * Lists the BGP speakers configured in the system.
@@ -47,7 +47,7 @@
             print("No speakers configured");
         } else {
             config.bgpSpeakers().forEach(
-                    s -> print(FORMAT, s.connectPoint(), s.listenAddresses()));
+                    s -> print(FORMAT, s.connectPoint(), s.peers()));
         }
     }
 }
diff --git a/apps/routing/src/main/java/org/onosproject/routing/config/impl/BgpConfig.java b/apps/routing/src/main/java/org/onosproject/routing/config/impl/BgpConfig.java
deleted file mode 100644
index 24400e8..0000000
--- a/apps/routing/src/main/java/org/onosproject/routing/config/impl/BgpConfig.java
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright 2015 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.routing.config.impl;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.google.common.collect.Sets;
-import org.onlab.packet.IpAddress;
-import org.onosproject.core.ApplicationId;
-import org.onosproject.net.config.Config;
-import org.onosproject.net.ConnectPoint;
-
-import java.util.Set;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * Configuration object for BGP config.
- */
-public class BgpConfig extends Config<ApplicationId> {
-
-    public static final String PEERS = "bgpPeers";
-    public static final String SPEAKERS = "bgpSpeakers";
-    public static final String CONNECT_POINT = "connectPoint";
-    public static final String IP_ADDRESS = "ipAddress";
-    public static final String LISTEN_ADDRESSES = "listenAddresses";
-
-    /**
-     * Gets the set of configured BGP peers.
-     *
-     * @return BGP peers
-     */
-    public Set<BgpPeerConfig> bgpPeers() {
-        Set<BgpPeerConfig> peers = Sets.newHashSet();
-
-        JsonNode peersNode = node.get(PEERS);
-        peersNode.forEach(jsonNode -> peers.add(
-                new BgpPeerConfig(ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
-                        IpAddress.valueOf(jsonNode.path(IP_ADDRESS).asText()))));
-
-        return peers;
-    }
-
-    /**
-     * Gets the set of configured BGP speakers.
-     *
-     * @return BGP speakers
-     */
-    public Set<BgpSpeakerConfig> bgpSpeakers() {
-        Set<BgpSpeakerConfig> speakers = Sets.newHashSet();
-
-        JsonNode speakersNode = node.get(SPEAKERS);
-        speakersNode.forEach(jsonNode -> {
-            Set<IpAddress> listenAddresses = Sets.newHashSet();
-            jsonNode.path(LISTEN_ADDRESSES).forEach(addressNode ->
-                    listenAddresses.add(IpAddress.valueOf(addressNode.asText()))
-            );
-            speakers.add(new BgpSpeakerConfig(
-                    ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
-                    listenAddresses));
-        });
-
-        return speakers;
-    }
-
-    /**
-     * Configuration for a BGP peer.
-     */
-    public class BgpPeerConfig {
-        private ConnectPoint connectPoint;
-        private IpAddress ipAddress;
-
-        public BgpPeerConfig(ConnectPoint connectPoint, IpAddress ipAddress) {
-            this.connectPoint = connectPoint;
-            this.ipAddress = ipAddress;
-        }
-
-        public ConnectPoint connectPoint() {
-            return connectPoint;
-        }
-
-        public IpAddress ipAddress() {
-            return ipAddress;
-        }
-
-    }
-
-    /**
-     * Configuration for a BGP speaker.
-     */
-    public class BgpSpeakerConfig {
-
-        private ConnectPoint connectPoint;
-        private Set<IpAddress> listenAddresses;
-
-        public BgpSpeakerConfig(ConnectPoint connectPoint, Set<IpAddress> listenAddresses) {
-            this.connectPoint = checkNotNull(connectPoint);
-            this.listenAddresses = checkNotNull(listenAddresses);
-        }
-
-        public ConnectPoint connectPoint() {
-            return connectPoint;
-        }
-
-        public Set<IpAddress> listenAddresses() {
-            return listenAddresses;
-        }
-    }
-}
diff --git a/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java b/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
index ec0d901..1078319 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
@@ -30,16 +30,22 @@
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.config.ConfigFactory;
 import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.config.basics.SubjectFactories;
-import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.host.HostService;
+import org.onosproject.routing.config.BgpConfig;
 import org.onosproject.routing.config.BgpPeer;
 import org.onosproject.routing.config.BgpSpeaker;
 import org.onosproject.routing.config.Interface;
 import org.onosproject.routing.config.LocalIpPrefixEntry;
 import org.onosproject.routing.config.RoutingConfigurationService;
+import org.onosproject.routing.impl.Router;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,6 +57,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 import static org.onosproject.routing.RouteEntry.createBinaryString;
 
@@ -74,6 +81,15 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigRegistry registry;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigService configService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected InterfaceService interfaceService;
+
     private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>();
     private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>();
     private Set<IpAddress> gatewayIpAddresses = new HashSet<>();
@@ -178,7 +194,20 @@
 
     @Override
     public Set<ConnectPoint> getBgpPeerConnectPoints() {
-        return Collections.unmodifiableSet(bgpPeerConnectPoints);
+        // TODO perhaps cache this result in future
+        ApplicationId routerAppId = coreService.getAppId(Router.ROUTER_APP_ID);
+        if (routerAppId == null) {
+            return Collections.emptySet();
+        }
+
+        BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class);
+
+        return bgpConfig.bgpSpeakers().stream()
+                .flatMap(speaker -> speaker.peers().stream())
+                .map(peer -> interfaceService.getMatchingInterface(peer))
+                .filter(intf -> intf != null)
+                .map(intf -> intf.connectPoint())
+                .collect(Collectors.toSet());
     }
 
     @Override
diff --git a/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java b/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java
index c5c7741..c4f291ba 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/impl/Router.java
@@ -35,6 +35,8 @@
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
 import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Host;
 import org.onosproject.net.host.HostEvent;
@@ -49,7 +51,6 @@
 import org.onosproject.routing.RouteListener;
 import org.onosproject.routing.RouteUpdate;
 import org.onosproject.routing.RoutingService;
-import org.onosproject.routing.config.Interface;
 import org.onosproject.routing.config.RoutingConfigurationService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -60,6 +61,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ConcurrentHashMap;
@@ -110,6 +112,9 @@
     protected BgpService bgpService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected InterfaceService interfaceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected RoutingConfigurationService routingConfigurationService;
 
     private ExecutorService bgpUpdatesExecutor;
@@ -603,8 +608,7 @@
             RouteEntry routeEntry = getLongestMatchableRouteEntry(dstIpAddress);
             if (routeEntry != null) {
                 nextHopIpAddress = routeEntry.nextHop();
-                Interface it = routingConfigurationService
-                        .getMatchingInterface(nextHopIpAddress);
+                Interface it = interfaceService.getMatchingInterface(nextHopIpAddress);
                 if (it != null) {
                     return it.connectPoint();
                 } else {
@@ -700,18 +704,18 @@
     private TrafficType trafficTypeClassifier(ConnectPoint srcConnectPoint,
                                               IpAddress dstIp) {
         LocationType dstIpLocationType = getLocationType(dstIp);
-        Interface srcInterface =
-                routingConfigurationService.getInterface(srcConnectPoint);
+        Optional<Interface> srcInterface =
+                interfaceService.getInterfacesByPort(srcConnectPoint).stream().findFirst();
 
         switch (dstIpLocationType) {
         case INTERNET:
-            if (srcInterface == null) {
+            if (!srcInterface.isPresent()) {
                 return TrafficType.HOST_TO_INTERNET;
             } else {
                 return TrafficType.INTERNET_TO_INTERNET;
             }
         case LOCAL:
-            if (srcInterface == null) {
+            if (!srcInterface.isPresent()) {
                 return TrafficType.HOST_TO_HOST;
             } else {
                 // TODO Currently we only consider local public prefixes.