0d3dd916da752034c751fc4bbdc71fd9c3bfb46c
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / write / TerminalDeviceChangeListener.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.transportpce.device.write;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.concurrent.ExecutionException;
21
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.DataObjectModification;
25 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
26 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.mdsal.binding.api.WriteTransaction;
29 import org.opendaylight.mdsal.common.api.CommitInfo;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.terminal.device.rev170708.terminal.device.top.TerminalDevice;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.base.Preconditions;
39 import com.google.common.util.concurrent.FluentFuture;
40
41 /**
42  * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
43  */
44 final class TerminalDeviceChangeListener implements DataTreeChangeListener<TerminalDevice> {
45
46     private static final Logger LOG = LoggerFactory.getLogger(TerminalDeviceChangeListener.class);
47     private final DataBroker dataBroker;
48
49     public TerminalDeviceChangeListener(DataBroker deviceDataBroker) {
50         this.dataBroker = deviceDataBroker;
51         Preconditions.checkArgument(this.dataBroker != null, "terminal-device datastore is null");
52     }
53
54
55     /**
56      * Delete change from terminal-device
57      * oper datastore.
58      *
59      * @param id container identifier
60      */
61     private void deleteContainer(DataTreeIdentifier<TerminalDevice> rootPath,
62             DataObjectModification<? extends DataObject> modified) {
63         Class<? extends DataObject> type = modified.getDataType();
64         PathArgument path = modified.getIdentifier();
65         LOG.info("deleting container type '{}' with id '{}' ...", type.toString(), path);
66     }
67
68
69     /**
70      * Merge change to Honeycomb
71      * config datastore and device
72      * config datastore.
73      *
74      * @param id TerminalDevice identifier
75      * @param dataAfter TerminalDevice to be merged
76      */
77     private void processChange(final InstanceIdentifier<TerminalDevice> id,
78             DataObjectModification<? extends DataObject> modified, final TerminalDevice dataAfter) {
79         LOG.info("processing change ...");
80         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
81         if (writeTx != null) {
82             LOG.info("WriteTransactions are ok, merge terminal-device data to datastores");
83             if(dataAfter != null) {
84                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, id, dataAfter);
85                 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
86                 try {
87                     future.get();
88                     LOG.info("terminal-device merged to terminal-device oper datastore");
89                 } catch (InterruptedException | ExecutionException e) {
90                     LOG.error("Failed to merge terminal-device to datastores");
91                 }
92             } else {
93                 LOG.error("terminal-device is null");
94             }
95         } else {
96             LOG.error("WriteTransaction object is null");
97         }
98     }
99
100 @Override
101 public void onDataTreeChanged(@NonNull Collection<DataTreeModification<TerminalDevice>> changes) {
102   LOG.info("onDataTreeChanged for terminal-device");
103         for (DataTreeModification<TerminalDevice> change : changes) {
104             final DataObjectModification<TerminalDevice> rootNode = change.getRootNode();
105             final DataTreeIdentifier<TerminalDevice> rootPath = change.getRootPath();
106             if (rootNode != null ) {
107                 final TerminalDevice dataBefore = rootNode.getDataBefore();
108                 final TerminalDevice dataAfter = rootNode.getDataAfter();
109                 LOG.info("Received terminal-device change({}):\n before={} \n after={}", rootNode.getModificationType(), dataBefore,
110                         dataAfter);
111                 Collection<? extends DataObjectModification<? extends DataObject>> modifiedChildren = rootNode.getModifiedChildren();
112                 switch (rootNode.getModificationType()) {
113                     case SUBTREE_MODIFIED:
114                         if (!modifiedChildren.isEmpty()) {
115                             Iterator<? extends DataObjectModification<? extends DataObject>> iterator = modifiedChildren.iterator();
116                             while (iterator.hasNext()) {
117                                 DataObjectModification<? extends DataObject> modified = iterator.next();
118                                 LOG.info("modified = \ndataType : {}\nid : {}\nmodifiedType : {}\noldData : {}\nnewData : {} \n",
119                                         modified.getDataType(), modified.getIdentifier(),modified.getModificationType(),
120                                         modified.getDataBefore(), modified.getDataAfter());
121                                 switch (modified.getModificationType()) {
122                                   case SUBTREE_MODIFIED:
123                                   case WRITE :
124                                       processChange(rootPath.getRootIdentifier(), modified, dataAfter);
125                                       break;
126                                   case DELETE:
127                                       deleteContainer(rootPath, modified);
128                                       break;
129                                   default:
130                                       break;
131                                }
132                             }
133                         }
134                         //processChange(rootPath.getRootIdentifier(), dataAfter);
135                         break;
136                     case WRITE :
137                         processChange(rootPath.getRootIdentifier(), null, dataAfter);
138                         break;
139                     case DELETE:
140                         LOG.info("terminal-device config datastore is deleted !");
141                         break;
142                     default:
143                         break;
144                 }
145             } else {
146                 LOG.error("rootNode is null !");
147             }
148         }
149
150 }
151
152
153 }