2 * Copyright (c) 2016 CableLabs 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
8 package org.opendaylight.unimgr.impl;
10 import java.util.Collection;
12 import org.opendaylight.controller.md.sal.binding.api.*;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriverRepoService;
15 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.ForwardingConstructs;
16 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.forwarding.constructs.ForwardingConstruct;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * NRP top level change model listener
24 * @author bartosz.michalik@amartus.com
26 public class ForwardingConstructChangeListener implements DataTreeChangeListener<ForwardingConstruct>, AutoCloseable {
27 private static final Logger LOG = LoggerFactory.getLogger(ForwardingConstructChangeListener.class);
29 private final ListenerRegistration<ForwardingConstructChangeListener> listener;
31 private final ForwardingConstructActivatorService routeActivator;
33 private final ActivationDriverRepoService activationRepoService;
35 private final DataBroker dataBroker;
37 public ForwardingConstructChangeListener(DataBroker dataBroker, ActivationDriverRepoService activationRepoService) {
38 this.dataBroker = dataBroker;
39 this.activationRepoService = activationRepoService;
40 routeActivator = new ForwardingConstructActivatorService(activationRepoService);
42 final InstanceIdentifier<ForwardingConstruct> fwPath = getFcPath();
43 final DataTreeIdentifier<ForwardingConstruct> dataTreeIid =
44 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fwPath);
45 listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
47 LOG.info("ForwardingConstructChangeListener created and registered");
51 * Basic Implementation of DataTreeChange Listener to execute add, update or remove command
52 * based on the data object modification type.
55 public void onDataTreeChanged(Collection<DataTreeModification<ForwardingConstruct>> collection) {
56 //TODO add lock for concurrency support
57 if (activationRepoService == null) {
58 LOG.warn("ActivationDriverRepoService is not ready yet - ignoring request");
61 for (final DataTreeModification<ForwardingConstruct> change : collection) {
62 final DataObjectModification<ForwardingConstruct> root = change.getRootNode();
64 switch (root.getModificationType()) {
65 case SUBTREE_MODIFIED:
69 //TO overcome whole subtree change event
70 boolean update = change.getRootNode().getDataBefore() != null;
88 protected void add(DataTreeModification<ForwardingConstruct> newDataObject) {
89 //TODO: Refine the logged addition
90 LOG.debug("FcRoute add event received {}", newDataObject);
91 routeActivator.activate(
92 getFcForActivation(newDataObject),
93 getFcActivationStateTracker(newDataObject)
97 protected void remove(DataTreeModification<ForwardingConstruct> removedDataObject) {
98 //TODO: Refine the logged removal
99 LOG.debug("FcRoute remove event received {}", removedDataObject);
100 routeActivator.deactivate(
101 getFcForDeactivation(removedDataObject),
102 getFcActivationStateTracker(removedDataObject)
107 protected void update(DataTreeModification<ForwardingConstruct> modifiedDataObject) {
108 //TODO: Refine the logged modification
109 LOG.debug("FcRoute update event received {}", modifiedDataObject);
111 //TODO for the moment transactional nature of this action is ignored :P
112 ForwardingConstructActivationStateTracker stateTracker = getFcActivationStateTracker(modifiedDataObject);
113 routeActivator.deactivate(getFcForDeactivation(modifiedDataObject), stateTracker);
114 routeActivator.activate(getFcForActivation(modifiedDataObject), stateTracker);
118 public void close() {
122 private InstanceIdentifier<ForwardingConstruct> getFcPath() {
123 return InstanceIdentifier
124 .builder(ForwardingConstructs.class).child(ForwardingConstruct.class).build();
127 private InstanceIdentifier<ForwardingConstruct> getFcIid(DataTreeModification<ForwardingConstruct> fcModification) {
128 return fcModification.getRootPath().getRootIdentifier();
131 private ForwardingConstruct getFcForActivation(DataTreeModification<ForwardingConstruct> fcModification) {
132 return fcModification.getRootNode().getDataAfter();
135 private ForwardingConstruct getFcForDeactivation(DataTreeModification<ForwardingConstruct> fcModification) {
136 return fcModification.getRootNode().getDataBefore();
140 private ForwardingConstructActivationStateTracker getFcActivationStateTracker(
141 DataTreeModification<ForwardingConstruct> fcModification) {
142 return new ForwardingConstructActivationStateTracker(dataBroker, getFcIid(fcModification));