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
9 package org.opendaylight.openflowplugin.openflow.ofswitch.config;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.concurrent.Future;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
22 import org.opendaylight.openflowplugin.api.OFConstants;
23 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
24 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class DefaultConfigPusher implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
40 private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigPusher.class);
41 private static final long STARTUP_LOOP_TICK = 500L;
42 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
43 private final NodeConfigService nodeConfigService;
44 private final DataBroker dataBroker;
45 private final DeviceOwnershipService deviceOwnershipService;
46 private ListenerRegistration<?> listenerRegistration;
48 public DefaultConfigPusher(NodeConfigService nodeConfigService, DataBroker dataBroker,
49 DeviceOwnershipService deviceOwnershipService) {
50 this.nodeConfigService = nodeConfigService;
51 this.dataBroker = dataBroker;
52 this.deviceOwnershipService = Preconditions.checkNotNull(deviceOwnershipService,
53 "DeviceOwnershipService can not be null");
56 @SuppressWarnings("checkstyle:IllegalCatch")
59 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
60 .augmentation(FlowCapableNode.class);
61 final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
62 LogicalDatastoreType.OPERATIONAL, path);
63 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
64 listenerRegistration = looper.loopUntilNoException(
65 () -> dataBroker.registerDataTreeChangeListener(identifier, DefaultConfigPusher.this));
66 } catch (Exception e) {
67 LOG.error("DataTreeChangeListener registration failed: {}", e);
68 throw new IllegalStateException("DefaultConfigPusher startup failed!", e);
70 LOG.info("DefaultConfigPusher has started.");
75 if (listenerRegistration != null) {
76 listenerRegistration.close();
81 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
82 for (DataTreeModification<FlowCapableNode> modification : modifications) {
83 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
84 String nodeId = modification.getRootPath().getRootIdentifier()
85 .firstKeyOf(Node.class).getId().getValue();
86 if (deviceOwnershipService.isEntityOwned(nodeId)) {
87 SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
88 setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
89 setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
90 setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
91 .getRootIdentifier().firstIdentifierOf(Node.class)));
92 final Future<RpcResult<SetConfigOutput>> resultFuture =
93 nodeConfigService.setConfig(setConfigInputBuilder.build());
94 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
96 LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);