673147fa585a4b89e5f1191d7f5f269c37dd703d
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / write / ComponentsChangeListener.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.platform.rev180130.platform.component.top.Components;
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 ComponentsChangeListener implements DataTreeChangeListener<Components> {
45
46     private static final Logger LOG = LoggerFactory.getLogger(ComponentsChangeListener.class);
47     private final DataBroker dataBroker;
48
49     public ComponentsChangeListener(DataBroker deviceDataBroker) {
50         this.dataBroker = deviceDataBroker;
51         Preconditions.checkArgument(this.dataBroker != null, "Device datastore is null");
52     }
53
54
55     /**
56      * Delete change from device
57      * oper datastore.
58      *
59      * @param id container identifier
60      */
61     private void deleteContainer(DataTreeIdentifier<Components> 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 OrgOpenroadmDevice identifier
75      * @param dataAfter OrgOpenroadmDevice to be merged
76      */
77     private void processChange(final InstanceIdentifier<Components> id,
78             DataObjectModification<? extends DataObject> modified, final Components dataAfter) {
79         LOG.info("processing change ...");
80         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
81         if (writeTx != null) {
82             LOG.info("WriteTransactions are ok, merge device info 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("components merged to components oper datastore");
89                 } catch (InterruptedException | ExecutionException e) {
90                     LOG.error("Failed to merge components to datastores");
91                 }
92             } else {
93                 LOG.error("components is null");
94             }
95         } else {
96             LOG.error("WriteTransaction object is null");
97         }
98     }
99
100     @Override
101     public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Components>> changes) {
102          LOG.info("onDataTreeChanged for Components");
103             for (DataTreeModification<Components> change : changes) {
104                 final DataObjectModification<Components> rootNode = change.getRootNode();
105                 final DataTreeIdentifier<Components> rootPath = change.getRootPath();
106                 if (rootNode != null ) {
107                     final Components dataBefore = rootNode.getDataBefore();
108                     final Components dataAfter = rootNode.getDataAfter();
109                     LOG.info("Received Components 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("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 }