More AAA refactoring

- parameterize the UDP port of the RADIUS server
- clean up some unused constants
- refactor config code to reduce repetitive code

Change-Id: I797dabd12c8ccd522f1ab0812c2b03da7264f2c0
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java b/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java
index 59042fe..479ec7e 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java
+++ b/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java
@@ -112,6 +112,9 @@
     // RADIUS port number
     protected long radiusPort;
 
+    // RADIUS server TCP port number
+    protected short radiusServerPort;
+
     // our application-specific event handler
     private ReactivePacketProcessor processor = new ReactivePacketProcessor();
 
@@ -206,8 +209,8 @@
         TrafficSelector radSelector = DefaultTrafficSelector.builder()
                 .matchEthType(EthType.EtherType.IPV4.ethType().toShort())
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
-                .matchUdpDst(TpPort.tpPort(1812))
-                .matchUdpSrc(TpPort.tpPort(1812))
+                .matchUdpDst(TpPort.tpPort(radiusServerPort))
+                .matchUdpSrc(TpPort.tpPort(radiusServerPort))
                 .build();
         packetService.requestPackets(radSelector, CONTROL, appId);
     }
@@ -223,8 +226,8 @@
         TrafficSelector radSelector = DefaultTrafficSelector.builder()
                 .matchEthType(EthType.EtherType.IPV4.ethType().toShort())
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
-                .matchUdpDst(TpPort.tpPort(1812))
-                .matchUdpSrc(TpPort.tpPort(1812))
+                .matchUdpDst(TpPort.tpPort(radiusServerPort))
+                .matchUdpSrc(TpPort.tpPort(radiusServerPort))
                 .build();
         packetService.cancelPackets(radSelector, CONTROL, appId);
     }
@@ -452,8 +455,8 @@
             IPv4 ip4Packet = new IPv4();
             Ethernet ethPkt = new Ethernet();
             radiusMessage.setParent(udp);
-            udp.setDestinationPort((short) 1812);
-            udp.setSourcePort((short) 1812); // TODO: make this configurable
+            udp.setDestinationPort(radiusServerPort);
+            udp.setSourcePort(radiusServerPort);
             udp.setPayload(radiusMessage);
             udp.setParent(ip4Packet);
             ip4Packet.setSourceAddress(AAA.this.nasIpAddress.getHostAddress());
@@ -524,15 +527,9 @@
             if (newCfg.radiusPort() != -1) {
                 radiusPort = newCfg.radiusPort();
             }
-
-            log.info("AAA app configuration:");
-            log.info("NAS IP is {}", nasIpAddress);
-            log.info("RADIUS IP is {}", radiusIpAddress);
-            log.info("NAS MAC is {}", nasMacAddress);
-            log.info("RADIUS MAC is {}", radiusMacAddress);
-            log.info("RADIUS secret is {}", radiusSecret);
-            log.info("RADIUS switch is {}", radiusSwitch);
-            log.info("RADIUS port is {}", radiusPort);
+            if (newCfg.radiusServerUDPPort() != -1) {
+                radiusServerPort = newCfg.radiusServerUDPPort();
+            }
         }
 
         @Override
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/AAAConfig.java b/apps/aaa/src/main/java/org/onosproject/aaa/AAAConfig.java
index 565ce9f..c18d2bf 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/AAAConfig.java
+++ b/apps/aaa/src/main/java/org/onosproject/aaa/AAAConfig.java
@@ -28,6 +28,7 @@
 public class AAAConfig extends Config<ApplicationId> {
 
     private static final String RADIUS_IP = "radiusIp";
+    private static final String RADIUS_SERVER_PORT = "1812";
     private static final String RADIUS_MAC = "radiusMac";
     private static final String NAS_IP = "nasIp";
     private static final String NAS_MAC = "nasMac";
@@ -47,9 +48,6 @@
     // NAS MAC address
     protected static final String DEFAULT_NAS_MAC = "00:00:00:00:10:01";
 
-    // RADIUS uplink port
-    protected static final int DEFAULT_RADIUS_UPLINK = 2;
-
     // RADIUS server shared secret
     protected static final String DEFAULT_RADIUS_SECRET = "ONOSecret";
 
@@ -59,6 +57,24 @@
     // Radius Port Number
     protected static final String DEFAULT_RADIUS_PORT = "129";
 
+    // Radius Server UDP Port Number
+    protected static final String DEFAULT_RADIUS_SERVER_PORT = "1812";
+
+    /**
+     * Gets the value of a string property, protecting for an empty
+     * JSON object.
+     *
+     * @param name name of the property
+     * @param defaultValue default value if none has been specified
+     * @return String value if one os found, default value otherwise
+     */
+    private String getStringProperty(String name, String defaultValue) {
+        if (object == null) {
+            return defaultValue;
+        }
+        return get(name, defaultValue);
+    }
+
     /**
      * Returns the NAS ip.
      *
@@ -66,10 +82,7 @@
      */
     public InetAddress nasIp() {
         try {
-            if (object == null) {
-                return InetAddress.getByName(DEFAULT_NAS_IP);
-            }
-            return InetAddress.getByName(get(NAS_IP, DEFAULT_NAS_IP));
+            return InetAddress.getByName(getStringProperty(NAS_IP, DEFAULT_NAS_IP));
         } catch (UnknownHostException e) {
             return null;
         }
@@ -92,10 +105,7 @@
      */
     public InetAddress radiusIp() {
         try {
-            if (object == null) {
-                return InetAddress.getByName(DEFAULT_RADIUS_IP);
-            }
-            return InetAddress.getByName(get(RADIUS_IP, DEFAULT_RADIUS_IP));
+            return InetAddress.getByName(getStringProperty(RADIUS_IP, DEFAULT_RADIUS_IP));
         } catch (UnknownHostException e) {
             return null;
         }
@@ -117,10 +127,7 @@
      * @return mac address or null if not set
      */
     public String radiusMac() {
-        if (object == null) {
-            return DEFAULT_RADIUS_MAC;
-        }
-        return get(RADIUS_MAC, DEFAULT_RADIUS_MAC);
+        return getStringProperty(RADIUS_MAC, DEFAULT_RADIUS_MAC);
     }
 
     /**
@@ -139,10 +146,7 @@
      * @return mac address or null if not set
      */
     public String nasMac() {
-        if (object == null) {
-            return DEFAULT_NAS_MAC;
-        }
-        return get(NAS_MAC, DEFAULT_NAS_MAC);
+        return getStringProperty(NAS_MAC, DEFAULT_NAS_MAC);
     }
 
     /**
@@ -161,10 +165,7 @@
      * @return radius secret or null if not set
      */
     public String radiusSecret() {
-        if (object == null) {
-            return DEFAULT_RADIUS_SECRET;
-        }
-        return get(RADIUS_SECRET, DEFAULT_RADIUS_SECRET);
+        return getStringProperty(RADIUS_SECRET, DEFAULT_RADIUS_SECRET);
     }
 
     /**
@@ -183,10 +184,7 @@
      * @return radius switch ID or null if not set
      */
     public String radiusSwitch() {
-        if (object == null) {
-            return DEFAULT_RADIUS_SWITCH;
-        }
-        return get(RADIUS_SWITCH, DEFAULT_RADIUS_SWITCH);
+        return getStringProperty(RADIUS_SWITCH, DEFAULT_RADIUS_SWITCH);
     }
 
     /**
@@ -205,10 +203,7 @@
      * @return radius port or null if not set
      */
     public long radiusPort() {
-        if (object == null) {
-            return Integer.parseInt(DEFAULT_RADIUS_PORT);
-        }
-        return Integer.parseInt(get(RADIUS_PORT, "-1"));
+        return Integer.parseInt(getStringProperty(RADIUS_PORT, DEFAULT_RADIUS_PORT));
     }
 
     /**
@@ -221,4 +216,24 @@
         return (BasicElementConfig) setOrClear(RADIUS_PORT, port);
     }
 
+    /**
+     * Returns the RADIUS server UDP port.
+     *
+     * @return radius server UDP port.
+     */
+    public short radiusServerUDPPort() {
+        return Short.parseShort(getStringProperty(RADIUS_SERVER_PORT,
+                                                  DEFAULT_RADIUS_SERVER_PORT));
+    }
+
+    /**
+     * Sets the RADIUS port.
+     *
+     * @param port new RADIUS UDP port; -1 to clear
+     * @return self
+     */
+    public BasicElementConfig radiusServerUDPPort(short port) {
+        return (BasicElementConfig) setOrClear(RADIUS_SERVER_PORT, (long) port);
+    }
+
 }
diff --git a/apps/aaa/src/test/java/org/onosproject/aaa/AAATest.java b/apps/aaa/src/test/java/org/onosproject/aaa/AAATest.java
index 214a83a..75a6033 100644
--- a/apps/aaa/src/test/java/org/onosproject/aaa/AAATest.java
+++ b/apps/aaa/src/test/java/org/onosproject/aaa/AAATest.java
@@ -495,11 +495,6 @@
         assertThat(stateMachine.state(), is(StateMachine.STATE_AUTHORIZED));
     }
 
-
-    private static final String RADIUS_SECRET = "radiusSecret";
-    private static final String RADIUS_SWITCH = "radiusSwitch";
-    private static final String RADIUS_PORT = "radiusPort";
-
     /**
      * Tests the default configuration.
      */