Bump upstreams
[openflowplugin.git] / applications / of-switch-config-pusher / src / main / java / org / opendaylight / openflowplugin / openflow / ofswitch / config / DefaultConfigPusher.java
index 40952c6ac779bc0b4f6d49fa98cf2be1aa4592c0..5ae80a8e3c15f914cafed84dcde5a61ede2e474e 100644 (file)
@@ -5,69 +5,83 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.openflow.ofswitch.config;
 
-import java.util.Set;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import static java.util.Objects.requireNonNull;
+
+import java.util.List;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.api.RpcService;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-/**
- * Created by Martin Bobak mbobak@cisco.com on 10/14/14.
- */
-public class DefaultConfigPusher implements AutoCloseable, DataChangeListener {
-
-    private final NodeConfigService nodeConfigService;
-    private final DataBroker dataBroker;
-    private ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
+@Singleton
+@Component(service = { })
+public final class DefaultConfigPusher implements AutoCloseable, DataTreeChangeListener<FlowCapableNode> {
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigPusher.class);
 
-    public DefaultConfigPusher(NodeConfigService nodeConfigService, DataBroker dataBroker) {
-        this.nodeConfigService = nodeConfigService;
-        this.dataBroker = dataBroker;
-    }
+    private final DeviceOwnershipService deviceOwnershipService;
+    private final SetConfig setConfig;
+    private final Registration reg;
 
-    public void start() {
-        InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class).
-                augmentation(FlowCapableNode.class);
-        dataChangeListenerRegistration = dataBroker.registerDataChangeListener(
-                LogicalDatastoreType.OPERATIONAL,
-                path, this, AsyncDataBroker.DataChangeScope.BASE);
+    @Inject
+    @Activate
+    public DefaultConfigPusher(@Reference final DataBroker dataBroker, @Reference final RpcService rpcService,
+            @Reference final DeviceOwnershipService deviceOwnershipService) {
+        this.deviceOwnershipService = requireNonNull(deviceOwnershipService);
+        setConfig = rpcService.getRpc(SetConfig.class);
+        reg = dataBroker.registerTreeChangeListener(
+            DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,
+                InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)), this);
+        LOG.info("DefaultConfigPusher has started.");
     }
 
+    @PreDestroy
+    @Deactivate
     @Override
     public void close() {
-        if(dataChangeListenerRegistration != null) {
-            dataChangeListenerRegistration.close();
-        }
+        reg.close();
     }
 
     @Override
-    public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
-        final Set<InstanceIdentifier<?>> changedDataKeys = change.getCreatedData().keySet();
-
-        if (changedDataKeys != null) {
-            for (InstanceIdentifier<?> key : changedDataKeys) {
-                SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
-                setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
-                setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
-                setConfigInputBuilder.setNode(new NodeRef(key.firstIdentifierOf(Node.class)));
-                nodeConfigService.setConfig(setConfigInputBuilder.build());
+    public void onDataTreeChanged(final List<DataTreeModification<FlowCapableNode>> modifications) {
+        for (var modification : modifications) {
+            if (modification.getRootNode().modificationType() == ModificationType.WRITE) {
+                final var nodeId = modification.getRootPath().path().firstKeyOf(Node.class).getId().getValue();
+                if (deviceOwnershipService.isEntityOwned(nodeId)) {
+                    LoggingFutures.addErrorLogging(setConfig.invoke(new SetConfigInputBuilder()
+                        .setFlag(SwitchConfigFlag.FRAGNORMAL.toString())
+                        .setMissSearchLength(OFConstants.OFPCML_NO_BUFFER)
+                        .setNode(new NodeRef(modification.getRootPath().path().firstIdentifierOf(Node.class)))
+                        .build()), LOG, "addFlow");
+                } else {
+                    LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);
+                }
             }
         }
-
     }
 }