08331f135a5452b89df23f8290c69202eb9e0783
[openflowplugin.git] / applications / of-switch-config-pusher / src / main / java / org / opendaylight / openflowplugin / openflow / ofswitch / config / DefaultConfigPusher.java
1 /**
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.ofswitch.config;
10
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.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
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.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
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;
48
49     public DefaultConfigPusher(NodeConfigService nodeConfigService, DataBroker dataBroker,
50             DeviceOwnershipService deviceOwnershipService) {
51         this.nodeConfigService = nodeConfigService;
52         this.dataBroker = dataBroker;
53         this.deviceOwnershipService = Preconditions.checkNotNull(deviceOwnershipService,
54                 "DeviceOwnershipService can not be null");
55     }
56
57     @SuppressWarnings("checkstyle:IllegalCatch")
58     public void start() {
59         try {
60             final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
61                     .augmentation(FlowCapableNode.class);
62             final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
63                     LogicalDatastoreType.OPERATIONAL, path);
64             final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
65             listenerRegistration = looper.loopUntilNoException(
66                 () -> dataBroker.registerDataTreeChangeListener(identifier, DefaultConfigPusher.this));
67         } catch (Exception e) {
68             LOG.error("DataTreeChangeListener registration failed: {}", e);
69             throw new IllegalStateException("DefaultConfigPusher startup failed!", e);
70         }
71         LOG.info("DefaultConfigPusher has started.");
72     }
73
74     @Override
75     public void close() {
76         if (listenerRegistration != null) {
77             listenerRegistration.close();
78         }
79     }
80
81     @Override
82     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
83         for (DataTreeModification<FlowCapableNode> modification : modifications) {
84             if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
85                 String nodeId = modification.getRootPath().getRootIdentifier()
86                         .firstKeyOf(Node.class, NodeKey.class).getId().getValue();
87                 if (deviceOwnershipService.isEntityOwned(nodeId)) {
88                     SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
89                     setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
90                     setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
91                     setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
92                             .getRootIdentifier().firstIdentifierOf(Node.class)));
93                     final Future<RpcResult<SetConfigOutput>> resultFuture =
94                             nodeConfigService.setConfig(setConfigInputBuilder.build());
95                     JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
96                 } else {
97                     LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);
98                 }
99             }
100         }
101     }
102
103 }