Do not use blueprint-maven-plugin in of-switch-config-pusher
[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 package org.opendaylight.openflowplugin.openflow.ofswitch.config;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Collection;
13 import javax.annotation.PostConstruct;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
18 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
21 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.mdsal.binding.api.DataTreeModification;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
26 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInputBuilder;
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.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 @Singleton
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     @Inject
50     public DefaultConfigPusher(final NodeConfigService nodeConfigService, final DataBroker dataBroker,
51             final DeviceOwnershipService deviceOwnershipService) {
52         this.nodeConfigService = nodeConfigService;
53         this.dataBroker = dataBroker;
54         this.deviceOwnershipService = requireNonNull(deviceOwnershipService, "DeviceOwnershipService can not be null");
55     }
56
57     @SuppressWarnings("checkstyle:IllegalCatch")
58     @PostConstruct
59     public void start() {
60         try {
61             final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
62                     .augmentation(FlowCapableNode.class);
63             final DataTreeIdentifier<FlowCapableNode> identifier = DataTreeIdentifier.create(
64                     LogicalDatastoreType.OPERATIONAL, path);
65             final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
66             listenerRegistration = looper.loopUntilNoException(
67                 () -> dataBroker.registerDataTreeChangeListener(identifier, DefaultConfigPusher.this));
68         } catch (Exception e) {
69             LOG.error("DataTreeChangeListener registration failed", e);
70             throw new IllegalStateException("DefaultConfigPusher startup failed!", e);
71         }
72         LOG.info("DefaultConfigPusher has started.");
73     }
74
75     @Override
76     @PreDestroy
77     public void close() {
78         if (listenerRegistration != null) {
79             listenerRegistration.close();
80         }
81     }
82
83     @Override
84     public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
85         for (DataTreeModification<FlowCapableNode> modification : modifications) {
86             if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
87                 String nodeId = modification.getRootPath().getRootIdentifier()
88                         .firstKeyOf(Node.class).getId().getValue();
89                 if (deviceOwnershipService.isEntityOwned(nodeId)) {
90                     SetConfigInputBuilder setConfigInputBuilder = new SetConfigInputBuilder();
91                     setConfigInputBuilder.setFlag(SwitchConfigFlag.FRAGNORMAL.toString());
92                     setConfigInputBuilder.setMissSearchLength(OFConstants.OFPCML_NO_BUFFER);
93                     setConfigInputBuilder.setNode(new NodeRef(modification.getRootPath()
94                             .getRootIdentifier().firstIdentifierOf(Node.class)));
95                     LoggingFutures.addErrorLogging(nodeConfigService.setConfig(setConfigInputBuilder.build()),
96                             LOG, "addFlow");
97                 } else {
98                     LOG.debug("Node {} is not owned by this controller, so skip setting config", nodeId);
99                 }
100             }
101         }
102     }
103
104 }