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 javax.annotation.PostConstruct;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.apache.aries.blueprint.annotation.service.Reference;
20 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
21 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
24 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.mdsal.binding.api.DataTreeModification;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
29 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 public class DefaultConfigPusher implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
46 private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigPusher.class);
47 private static final long STARTUP_LOOP_TICK = 500L;
48 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
49 private final NodeConfigService nodeConfigService;
50 private final DataBroker dataBroker;
51 private final DeviceOwnershipService deviceOwnershipService;
52 private ListenerRegistration<?> listenerRegistration;
55 public DefaultConfigPusher(NodeConfigService nodeConfigService, @Reference DataBroker dataBroker,
56 @Reference DeviceOwnershipService deviceOwnershipService) {
57 this.nodeConfigService = nodeConfigService;
58 this.dataBroker = dataBroker;
59 this.deviceOwnershipService = Preconditions.checkNotNull(deviceOwnershipService,
60 "DeviceOwnershipService can not be null");
63 @SuppressWarnings("checkstyle:IllegalCatch")
67 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
68 .augmentation(FlowCapableNode.class);
69 final DataTreeIdentifier<FlowCapableNode> identifier = DataTreeIdentifier.create(
70 LogicalDatastoreType.OPERATIONAL, path);
71 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
72 listenerRegistration = looper.loopUntilNoException(
73 () -> dataBroker.registerDataTreeChangeListener(identifier, DefaultConfigPusher.this));
74 } catch (Exception e) {
75 LOG.error("DataTreeChangeListener registration failed: {}", e);
76 throw new IllegalStateException("DefaultConfigPusher startup failed!", e);
78 LOG.info("DefaultConfigPusher has started.");
84 if (listenerRegistration != null) {
85 listenerRegistration.close();
90 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
91 for (DataTreeModification<FlowCapableNode> modification : modifications) {
92 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
93 String nodeId = modification.getRootPath().getRootIdentifier()
94 .firstKeyOf(Node.class).getId().getValue();
95 if (deviceOwnershipService.isEntityOwned(nodeId)) {
96 SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
97 setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
98 setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
99 setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
100 .getRootIdentifier().firstIdentifierOf(Node.class)));
101 final Future<RpcResult<SetConfigOutput>> resultFuture =
102 nodeConfigService.setConfig(setConfigInputBuilder.build());
103 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
105 LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);