2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.ofswitch.config;
10 import static java.util.Objects.requireNonNull;
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.openflowplugin.common.wait.SimpleTaskRetryLooper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class DefaultConfigPusher implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
41 private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigPusher.class);
42 private static final long STARTUP_LOOP_TICK = 500L;
43 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
44 private final NodeConfigService nodeConfigService;
45 private final DataBroker dataBroker;
46 private final DeviceOwnershipService deviceOwnershipService;
47 private ListenerRegistration<?> listenerRegistration;
50 public DefaultConfigPusher(final NodeConfigService nodeConfigService, final DataBroker dataBroker,
51 final DeviceOwnershipService deviceOwnershipService) {
52 this.nodeConfigService = nodeConfigService;
53 this.dataBroker = dataBroker;
54 this.deviceOwnershipService = requireNonNull(deviceOwnershipService, "DeviceOwnershipService can not be null");
57 @SuppressWarnings("checkstyle:IllegalCatch")
61 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
62 .augmentation(FlowCapableNode.class);
63 final DataTreeIdentifier<FlowCapableNode> identifier = DataTreeIdentifier.create(
64 LogicalDatastoreType.OPERATIONAL, path);
65 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
66 listenerRegistration = looper.loopUntilNoException(
67 () -> dataBroker.registerDataTreeChangeListener(identifier, DefaultConfigPusher.this));
68 } catch (Exception e) {
69 LOG.error("DataTreeChangeListener registration failed", e);
70 throw new IllegalStateException("DefaultConfigPusher startup failed!", e);
72 LOG.info("DefaultConfigPusher has started.");
78 if (listenerRegistration != null) {
79 listenerRegistration.close();
84 public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
85 for (DataTreeModification<FlowCapableNode> modification : modifications) {
86 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
87 String nodeId = modification.getRootPath().getRootIdentifier()
88 .firstKeyOf(Node.class).getId().getValue();
89 if (deviceOwnershipService.isEntityOwned(nodeId)) {
90 SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
91 setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
92 setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
93 setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
94 .getRootIdentifier().firstIdentifierOf(Node.class)));
95 LoggingFutures.addErrorLogging(nodeConfigService.setConfig(setConfigInputBuilder.build()),
98 LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);