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