Activation status handling mechanism for ForwardingConstruct provisioning
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / ForwardingConstructChangeListener.java
1 /*
2  * Copyright (c) 2016 CableLabs 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.unimgr.impl;
9
10 import java.util.Collection;
11
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;
21
22 /**
23  * NRP top level change model listener
24  * @author bartosz.michalik@amartus.com
25  */
26 public class ForwardingConstructChangeListener implements DataTreeChangeListener<ForwardingConstruct>, AutoCloseable {
27     private static final Logger LOG = LoggerFactory.getLogger(ForwardingConstructChangeListener.class);
28
29     private final ListenerRegistration<ForwardingConstructChangeListener> listener;
30
31     private final ForwardingConstructActivatorService routeActivator;
32
33     private final ActivationDriverRepoService activationRepoService;
34
35     private final DataBroker dataBroker;
36
37     public ForwardingConstructChangeListener(DataBroker dataBroker, ActivationDriverRepoService activationRepoService) {
38         this.dataBroker  = dataBroker;
39         this.activationRepoService = activationRepoService;
40         routeActivator = new ForwardingConstructActivatorService(activationRepoService);
41
42         final InstanceIdentifier<ForwardingConstruct> fwPath = getFcPath();
43         final DataTreeIdentifier<ForwardingConstruct> dataTreeIid =
44                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fwPath);
45         listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
46
47         LOG.info("ForwardingConstructChangeListener created and registered");
48     }
49
50     /**
51      * Basic Implementation of DataTreeChange Listener to execute add, update or remove command
52      * based on the data object modification type.
53      */
54     @Override
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");
59             return;
60         }
61         for (final DataTreeModification<ForwardingConstruct> change : collection) {
62             final DataObjectModification<ForwardingConstruct> root = change.getRootNode();
63
64             switch (root.getModificationType()) {
65                 case SUBTREE_MODIFIED:
66                     update(change);
67                     break;
68                 case WRITE:
69                     //TO overcome whole subtree change event
70                     boolean update = change.getRootNode().getDataBefore() != null;
71
72                     if (update) {
73                         update(change);
74                     } else {
75                         add(change);
76                     }
77
78                     break;
79                 case DELETE:
80                     remove(change);
81                     break;
82                 default:
83                     break;
84             }
85         }
86     }
87
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)
94         );
95     }
96
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)
103         );
104
105     }
106
107     protected void update(DataTreeModification<ForwardingConstruct> modifiedDataObject) {
108         //TODO: Refine the logged modification
109         LOG.debug("FcRoute update event received {}", modifiedDataObject);
110
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);
115     }
116
117     @Override
118     public void close() {
119         listener.close();
120     }
121
122     private InstanceIdentifier<ForwardingConstruct> getFcPath() {
123         return InstanceIdentifier
124                 .builder(ForwardingConstructs.class).child(ForwardingConstruct.class).build();
125     }
126
127     private InstanceIdentifier<ForwardingConstruct> getFcIid(DataTreeModification<ForwardingConstruct> fcModification) {
128         return fcModification.getRootPath().getRootIdentifier();
129     }
130
131     private ForwardingConstruct getFcForActivation(DataTreeModification<ForwardingConstruct> fcModification) {
132         return fcModification.getRootNode().getDataAfter();
133     }
134
135     private ForwardingConstruct getFcForDeactivation(DataTreeModification<ForwardingConstruct> fcModification) {
136         return fcModification.getRootNode().getDataBefore();
137     }
138
139
140     private ForwardingConstructActivationStateTracker getFcActivationStateTracker(
141             DataTreeModification<ForwardingConstruct> fcModification) {
142         return new ForwardingConstructActivationStateTracker(dataBroker, getFcIid(fcModification));
143     }
144 }