2 * Copyright (c) 2018 Orange and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.fd.honeycomb.transportpce.device.write;
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.concurrent.ExecutionException;
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;
38 import com.google.common.base.Preconditions;
39 import com.google.common.util.concurrent.FluentFuture;
42 * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
44 final class TerminalDeviceChangeListener implements DataTreeChangeListener<TerminalDevice> {
46 private static final Logger LOG = LoggerFactory.getLogger(TerminalDeviceChangeListener.class);
47 private final DataBroker dataBroker;
49 public TerminalDeviceChangeListener(DataBroker deviceDataBroker) {
50 this.dataBroker = deviceDataBroker;
51 Preconditions.checkArgument(this.dataBroker != null, "terminal-device datastore is null");
56 * Delete change from terminal-device
59 * @param id container identifier
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);
70 * Merge change to Honeycomb
71 * config datastore and device
74 * @param id TerminalDevice identifier
75 * @param dataAfter TerminalDevice to be merged
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();
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");
93 LOG.error("terminal-device is null");
96 LOG.error("WriteTransaction object is null");
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,
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:
124 processChange(rootPath.getRootIdentifier(), modified, dataAfter);
127 deleteContainer(rootPath, modified);
134 //processChange(rootPath.getRootIdentifier(), dataAfter);
137 processChange(rootPath.getRootIdentifier(), null, dataAfter);
140 LOG.info("terminal-device config datastore is deleted !");
146 LOG.error("rootNode is null !");