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 java.util.Collection;
12 import java.util.concurrent.Future;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class DefaultConfigPusher implements AutoCloseable, DataTreeChangeListener<FlowCapableNode> {
38 private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigPusher.class);
39 private static final long STARTUP_LOOP_TICK = 500L;
40 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
41 private final NodeConfigService nodeConfigService;
42 private final DataBroker dataBroker;
43 private ListenerRegistration<?> listenerRegistration;
45 public DefaultConfigPusher(NodeConfigService nodeConfigService, DataBroker dataBroker) {
46 this.nodeConfigService = nodeConfigService;
47 this.dataBroker = dataBroker;
50 @SuppressWarnings("checkstyle:IllegalCatch")
53 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
54 .augmentation(FlowCapableNode.class);
55 final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
56 LogicalDatastoreType.OPERATIONAL, path);
57 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
58 listenerRegistration = looper.loopUntilNoException(
59 () -> dataBroker.registerDataTreeChangeListener(identifier, DefaultConfigPusher.this));
60 } catch (Exception e) {
61 LOG.error("DataTreeChangeListener registration failed: {}", e);
62 throw new IllegalStateException("DefaultConfigPusher startup failed!", e);
64 LOG.info("DefaultConfigPusher has started.");
69 if (listenerRegistration != null) {
70 listenerRegistration.close();
75 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
76 for (DataTreeModification<FlowCapableNode> modification : modifications) {
77 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
78 SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
79 setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
80 setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
81 setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
82 .getRootIdentifier().firstIdentifierOf(Node.class)));
83 final Future<RpcResult<SetConfigOutput>> resultFuture =
84 nodeConfigService.setConfig(setConfigInputBuilder.build());
85 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");