cleaning up more cord apps

Change-Id: Ie48245b61926232ca9b2fa583492cb6e88f10990
diff --git a/api/src/main/java/org/onosproject/olt/AccessDeviceService.java b/api/src/main/java/org/onosproject/olt/AccessDeviceService.java
index 831784c..74d8a28 100644
--- a/api/src/main/java/org/onosproject/olt/AccessDeviceService.java
+++ b/api/src/main/java/org/onosproject/olt/AccessDeviceService.java
@@ -19,6 +19,9 @@
 import org.onlab.packet.VlanId;
 import org.onosproject.event.ListenerService;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+
+import java.util.Map;
 
 /**
  * Service for interacting with an access device (OLT).
@@ -41,4 +44,11 @@
      */
     void removeSubscriber(ConnectPoint port);
 
+    /**
+     * Returns the map of configured OLTs.
+     *
+     * @return a map
+     */
+    Map<DeviceId, AccessDeviceData> fetchOlts();
+
 }
diff --git a/app/src/main/java/org/onosproject/olt/cli/ShowOltCommand.java b/app/src/main/java/org/onosproject/olt/cli/ShowOltCommand.java
new file mode 100644
index 0000000..63b3af1
--- /dev/null
+++ b/app/src/main/java/org/onosproject/olt/cli/ShowOltCommand.java
@@ -0,0 +1,59 @@
+/*
+ * 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.olt.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.olt.AccessDeviceData;
+import org.onosproject.olt.AccessDeviceService;
+
+import java.util.Map;
+
+/**
+ * Adds a subscriber to an access device.
+ */
+@Command(scope = "onos", name = "show-olts",
+        description = "Shows configured OLTs")
+public class ShowOltCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "deviceId", description = "Access device ID",
+            required = false, multiValued = false)
+    private String strDeviceId = null;
+
+    @Override
+    protected void execute() {
+        AccessDeviceService service = AbstractShellCommand.get(AccessDeviceService.class);
+        Map<DeviceId, AccessDeviceData> data = service.fetchOlts();
+        if (strDeviceId != null) {
+            DeviceId deviceId = DeviceId.deviceId(strDeviceId);
+            print("OLT %s:", deviceId);
+            display(data.get(deviceId));
+        } else {
+            data.keySet().forEach(did -> {
+                print("OLT %s:", did);
+                display(data.get(did));
+            });
+        }
+    }
+
+    private void display(AccessDeviceData accessDeviceData) {
+        print("\tvlan : %s", accessDeviceData.vlan());
+        print("\tuplink : %s", accessDeviceData.uplink());
+    }
+}
diff --git a/app/src/main/java/org/onosproject/olt/impl/Olt.java b/app/src/main/java/org/onosproject/olt/impl/Olt.java
index fa9c2a4..ab03c0b 100644
--- a/app/src/main/java/org/onosproject/olt/impl/Olt.java
+++ b/app/src/main/java/org/onosproject/olt/impl/Olt.java
@@ -20,11 +20,14 @@
 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.Modified;
+import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.packet.EthType;
 import org.onlab.packet.VlanId;
+import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.event.AbstractListenerManager;
@@ -58,17 +61,22 @@
 import org.onosproject.olt.AccessDeviceEvent;
 import org.onosproject.olt.AccessDeviceListener;
 import org.onosproject.olt.AccessDeviceService;
+import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 
+import java.util.Dictionary;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Properties;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.onlab.util.Tools.get;
 import static org.onlab.util.Tools.groupedThreads;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -80,6 +88,9 @@
 public class Olt
         extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
         implements AccessDeviceService {
+
+    private static final short DEFAULT_VLAN = 0;
+
     private final Logger log = getLogger(getClass());
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -94,12 +105,18 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigRegistry networkConfig;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService componentConfigService;
+
+
+    @Property(name = "defaultVlan", intValue = DEFAULT_VLAN,
+            label = "Default VLAN RG<->ONU traffic")
+    private int defaultVlan = DEFAULT_VLAN;
+
     private final DeviceListener deviceListener = new InternalDeviceListener();
 
     private ApplicationId appId;
 
-    private static final VlanId DEFAULT_VLAN = VlanId.vlanId((short) 0);
-
     private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
                                                                          groupedThreads("onos/olt-service",
                                                                                         "olt-installer-%d"));
@@ -127,8 +144,10 @@
 
 
     @Activate
-    public void activate() {
+    public void activate(ComponentContext context) {
+        modified(context);
         appId = coreService.registerApplication("org.onosproject.olt");
+        componentConfigService.registerProperties(getClass());
 
         eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
 
@@ -160,13 +179,26 @@
 
     @Deactivate
     public void deactivate() {
+        componentConfigService.unregisterProperties(getClass(), false);
         deviceService.removeListener(deviceListener);
         networkConfig.removeListener(configListener);
         networkConfig.unregisterConfigFactory(configFactory);
         log.info("Stopped");
     }
 
-    @Override
+    @Modified
+    public void modified(ComponentContext context) {
+        Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
+
+        try {
+            String s = get(properties, "defaultVlan");
+            defaultVlan = isNullOrEmpty(s) ? DEFAULT_VLAN : Integer.parseInt(s.trim());
+        } catch (Exception e) {
+            defaultVlan = DEFAULT_VLAN;
+        }
+    }
+
+        @Override
     public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
         AccessDeviceData olt = oltData.get(port.deviceId());
 
@@ -192,6 +224,11 @@
 
     }
 
+    @Override
+    public Map<DeviceId, AccessDeviceData> fetchOlts() {
+        return Maps.newHashMap(oltData);
+    }
+
     private void unprovisionSubscriber(DeviceId deviceId, PortNumber uplink,
                                        PortNumber subscriberPort, VlanId deviceVlan) {
 
@@ -255,7 +292,7 @@
         CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
 
         TrafficSelector upstream = DefaultTrafficSelector.builder()
-                .matchVlanId(defaultVlan.orElse(DEFAULT_VLAN))
+                .matchVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
                 .matchInPort(subscriberPort)
                 .build();
 
@@ -275,7 +312,7 @@
 
         TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
                 .popVlan()
-                .setVlanId(defaultVlan.orElse(DEFAULT_VLAN))
+                .setVlanId(defaultVlan.orElse(VlanId.vlanId((short) this.defaultVlan)))
                 .setOutput(subscriberPort)
                 .build();
 
@@ -417,6 +454,7 @@
                     post(new AccessDeviceEvent(
                             AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
                             null, null));
+                    provisionDefaultFlows(devId);
                     break;
                 case DEVICE_REMOVED:
                     post(new AccessDeviceEvent(
@@ -450,21 +488,29 @@
 
                 case CONFIG_ADDED:
                 case CONFIG_UPDATED:
-                    if (event.configClass().equals(CONFIG_CLASS)) {
-                        AccessDeviceConfig config =
-                                networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
-                        if (config != null) {
-                            oltData.put(config.getOlt().deviceId(), config.getOlt());
-                            provisionDefaultFlows((DeviceId) event.subject());
-                        }
+
+                    AccessDeviceConfig config =
+                            networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
+                    if (config != null) {
+                        oltData.put(config.getOlt().deviceId(), config.getOlt());
+                        provisionDefaultFlows((DeviceId) event.subject());
                     }
+
                     break;
+                case CONFIG_REGISTERED:
                 case CONFIG_UNREGISTERED:
+                    break;
                 case CONFIG_REMOVED:
+                    oltData.remove(event.subject());
                 default:
                     break;
             }
         }
+
+        @Override
+        public boolean isRelevant(NetworkConfigEvent event) {
+            return event.configClass().equals(CONFIG_CLASS);
+        }
     }
 
     private void provisionDefaultFlows(DeviceId deviceId) {
diff --git a/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 1818c02..5d114f9 100644
--- a/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -30,6 +30,13 @@
                 <null/>
             </completers>
         </command>
+        <command>
+            <action class="org.onosproject.olt.cli.ShowOltCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+                <null/>
+            </completers>
+        </command>
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>