Do not guard registration with retries
[openflowplugin.git] / applications / of-switch-config-pusher / src / main / java / org / opendaylight / openflowplugin / openflow / ofswitch / config / DefaultConfigPusher.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.openflow.ofswitch.config;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Collection;
13 import javax.annotation.PostConstruct;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
18 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
21 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.mdsal.binding.api.DataTreeModification;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
33 import org.opendaylight.yangtools.concepts.Registration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @Singleton
39 public class DefaultConfigPusher implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
40     private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigPusher.class);
41
42     private final NodeConfigService nodeConfigService;
43     private final DataBroker dataBroker;
44     private final DeviceOwnershipService deviceOwnershipService;
45
46     private Registration listenerRegistration;
47
48     @Inject
49     public DefaultConfigPusher(final NodeConfigService nodeConfigService, final DataBroker dataBroker,
50             final DeviceOwnershipService deviceOwnershipService) {
51         this.nodeConfigService = nodeConfigService;
52         this.dataBroker = dataBroker;
53         this.deviceOwnershipService = requireNonNull(deviceOwnershipService, "DeviceOwnershipService can not be null");
54     }
55
56     @PostConstruct
57     public void start() {
58         listenerRegistration = dataBroker.registerDataTreeChangeListener(
59             DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
60                 InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)),
61             this);
62         LOG.info("DefaultConfigPusher has started.");
63     }
64
65     @Override
66     @PreDestroy
67     public void close() {
68         if (listenerRegistration != null) {
69             listenerRegistration.close();
70         }
71     }
72
73     @Override
74     public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
75         for (DataTreeModification<FlowCapableNode> modification : modifications) {
76             if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
77                 String nodeId = modification.getRootPath().getRootIdentifier()
78                         .firstKeyOf(Node.class).getId().getValue();
79                 if (deviceOwnershipService.isEntityOwned(nodeId)) {
80                     SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
81                     setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
82                     setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
83                     setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
84                             .getRootIdentifier().firstIdentifierOf(Node.class)));
85                     LoggingFutures.addErrorLogging(nodeConfigService.setConfig(setConfigInputBuilder.build()),
86                             LOG, "addFlow");
87                 } else {
88                     LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);
89                 }
90             }
91         }
92     }
93
94 }