9ae9e180fac55690dc89843201a35557afb9bb58
[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 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.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
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;
43
44 @Singleton
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;
53
54     @Inject
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");
61     }
62
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     @PostConstruct
65     public void start() {
66         try {
67             final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
68                     .augmentation(FlowCapableNode.class);
69             final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
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);
77         }
78         LOG.info("DefaultConfigPusher has started.");
79     }
80
81     @Override
82     @PreDestroy
83     public void close() {
84         if (listenerRegistration != null) {
85             listenerRegistration.close();
86         }
87     }
88
89     @Override
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");
104                 } else {
105                     LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);
106                 }
107             }
108         }
109     }
110
111 }