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 com.google.common.base.Optional;
19 import com.google.common.base.Preconditions;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Future;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
30 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
31 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
32 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
33 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDeviceBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.InterfacesBuilder;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
57 final class DeviceChangeListener implements DataTreeChangeListener<OrgOpenroadmDevice> {
59 private static final Logger LOG = LoggerFactory.getLogger(DeviceChangeListener.class);
60 private final DataBroker dataBroker;
62 public DeviceChangeListener(DataBroker deviceDataBroker) {
63 this.dataBroker = deviceDataBroker;
64 Preconditions.checkArgument(this.dataBroker != null, "Device datastore is null");
68 public void onDataTreeChanged(Collection<DataTreeModification<OrgOpenroadmDevice>> changes) {
69 LOG.info("onDataTreeChanged");
70 for (DataTreeModification<OrgOpenroadmDevice> change : changes) {
71 final DataObjectModification<OrgOpenroadmDevice> rootNode = change.getRootNode();
72 final DataTreeIdentifier<OrgOpenroadmDevice> rootPath = change.getRootPath();
73 if (rootNode != null ) {
74 final OrgOpenroadmDevice dataBefore = rootNode.getDataBefore();
75 final OrgOpenroadmDevice dataAfter = rootNode.getDataAfter();
76 LOG.info("Received Device change({}):\n before={} \n after={}", rootNode.getModificationType(), dataBefore,
78 Collection<DataObjectModification<? extends DataObject>> modifiedChildren = rootNode.getModifiedChildren();
79 switch (rootNode.getModificationType()) {
80 case SUBTREE_MODIFIED:
81 if (!modifiedChildren.isEmpty()) {
82 Iterator<DataObjectModification<? extends DataObject>> iterator = modifiedChildren.iterator();
83 while (iterator.hasNext()) {
84 DataObjectModification<? extends DataObject> modified = iterator.next();
85 LOG.info("modified = \ndataType : {}\nid : {}\nmodifiedType : {}\noldData : {}\nnewData : {} \n",
86 modified.getDataType(), modified.getIdentifier(),modified.getModificationType(),
87 modified.getDataBefore(), modified.getDataAfter());
88 switch (modified.getModificationType()) {
89 case SUBTREE_MODIFIED:
91 processChange(rootPath.getRootIdentifier(), modified, dataAfter);
92 updateCircuitPackInterface(rootPath.getRootIdentifier(), modified, false);
95 updateCircuitPackInterface(rootPath.getRootIdentifier(), modified, true);
96 deleteContainer(rootPath, modified);
103 //processChange(rootPath.getRootIdentifier(), dataAfter);
106 processChange(rootPath.getRootIdentifier(), null, dataAfter);
109 LOG.info("device config datastore is deleted !");
115 LOG.error("rootNode is null !");
121 * Delete change from device
124 * @param id container identifier
126 private void deleteContainer(DataTreeIdentifier<OrgOpenroadmDevice> rootPath,
127 DataObjectModification<? extends DataObject> modified) {
128 final String ROADM_CONNECTIONS = "interface org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019"
129 + ".org.openroadm.device.container.org.openroadm.device.RoadmConnections";
130 final String INTERFACE_GRP = "interface org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019"
131 + ".interfaces.grp.Interface";
132 Class<? extends DataObject> type = modified.getDataType();
133 PathArgument path = modified.getIdentifier();
134 LOG.info("deleting container type '{}' with id '{}' ...", type.toString(), path);
135 String key = extractKey(path.toString());
137 InstanceIdentifier<?> iid = null;
138 switch (type.toString()) {
139 case ROADM_CONNECTIONS:
140 LOG.info("roadm-connections ...");
141 iid = rootPath.getRootIdentifier().child(RoadmConnections.class,
142 new RoadmConnectionsKey(key));
145 LOG.info("interface ....");
146 iid = rootPath.getRootIdentifier().child(Interface.class,
147 new InterfaceKey(key));
151 LOG.info("iid : {}", iid);
152 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
153 if (writeTx != null) {
154 LOG.info("WriteTransaction is ok, delete container device from device oper datastore");
156 LOG.info("deleting container element from device oper DS ...");
157 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
158 Future<Void> future = writeTx.submit();
160 LOG.info("container element '{}' deleted from device oper datastore", iid);
161 } catch (InterruptedException | ExecutionException e) {
162 LOG.error("Failed to process WriteTransactions",e);
165 LOG.error("WriteTransaction object is null");
168 LOG.error("extract key is null");
174 private String extractKey(String path) {
175 LOG.info("getting key from pathArgument ...");
176 String result = null;
177 if (path != null && path.length() > 2) {
178 result = path.substring(path.lastIndexOf("=") + 1, path.length()-2);
179 LOG.info("result : {}", result);
181 LOG.error("String pathArgument is not compliant !!");
188 * Merge change to Honeycomb
189 * config datastore and device
192 * @param id OrgOpenroadmDevice identifier
193 * @param dataAfter OrgOpenroadmDevice to be merged
195 private void processChange(final InstanceIdentifier<OrgOpenroadmDevice> id,
196 DataObjectModification<? extends DataObject> modified, final OrgOpenroadmDevice dataAfter) {
197 LOG.info("processing change ...");
198 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
199 if (writeTx != null) {
200 LOG.info("WriteTransactions are ok, merge device info to datastores");
201 if(dataAfter != null) {
202 String deviceId = dataAfter.getInfo().getNodeId().getValue();
203 writeTx.merge(LogicalDatastoreType.OPERATIONAL, id, dataAfter);
204 Future<Void> future = writeTx.submit();
207 LOG.info("device '{}' merged to device oper datastore", deviceId);
208 } catch (InterruptedException | ExecutionException e) {
209 LOG.error("Failed to merge Element '{}' to datastores", deviceId);
212 LOG.error("device is null");
215 LOG.error("WriteTransaction object is null");
220 *Update Interface info on
221 *Ports list in CircuitPacks.
223 * @param id device InstanceIdentifier
224 * @param modified DataObjectModification
225 * @param deviceData OrgOpenroadmDevice
226 * @param delete if yes delete interface from circuitpacks else adding interface
228 private void updateCircuitPackInterface(final InstanceIdentifier<OrgOpenroadmDevice> id,
229 DataObjectModification<? extends DataObject> modified, boolean delete) {
230 LOG.info("Updating circuit packs interface ...");
231 final String INTERFACE_GRP = "interface org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019"
232 + ".interfaces.grp.Interface";
233 InstanceIdentifier<CircuitPacks> iid = null;
234 if (modified != null) {
235 Class<? extends DataObject> type = modified.getDataType();
236 LOG.info("getting container type '{}' ...", type.toString());
238 if (type.toString().compareTo(INTERFACE_GRP) == 0) {
239 LOG.warn("interface update ! ");
240 Interface data = null;
242 data = (Interface) modified.getDataBefore();
244 data = (Interface) modified.getDataAfter();
246 LOG.info("Interface data gets : {}", data.toString());
248 String circuitPackName = data.getSupportingCircuitPackName();
249 String port = data.getSupportingPort().toString();
250 String interfaceName = data.getName();
251 if (circuitPackName != null && port != null && interfaceName != null) {
252 iid = id.child(CircuitPacks.class,
253 new CircuitPacksKey(circuitPackName));
254 OrgOpenroadmDevice operDevice = readDeviceOperData(id);
255 if (operDevice != null) {
256 OrgOpenroadmDeviceBuilder builder = new OrgOpenroadmDeviceBuilder(operDevice);
257 List<CircuitPacks> list = builder.getCircuitPacks();
258 LOG.info("Getting circuit packs ...");
259 for (CircuitPacks circuitPacks : list) {
260 if (circuitPacks.getCircuitPackName().compareTo(circuitPackName) == 0) {
261 LOG.info("circuitpack found");
262 List<Ports> portList = circuitPacks.getPorts();
263 LOG.info("Getting ports for circuit pack '{}' ...", circuitPackName);
264 for (Ports ports : portList) {
265 if (ports.getPortName().compareTo(port) == 0) {
266 LOG.info("port found");
267 PortsBuilder newPorts = new PortsBuilder(ports);
268 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
269 .port.Interfaces> value = newPorts.getInterfaces();
271 LOG.info("adding interface info to port '{}'", port);
272 if (value == null || value.isEmpty()) {
273 LOG.info("Interfaces List is empty !");
274 value = new ArrayList<org.opendaylight.yang.gen.v1.http.org
275 .openroadm.device.rev181019.port.Interfaces>();
276 value.add(new InterfacesBuilder().setInterfaceName(interfaceName)
278 } else { // value is not empty
279 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
280 .port.Interfaces tmp = getInterfaces(value, interfaceName);
282 LOG.warn("Interfaces with name '{}' already exists !",interfaceName);
284 } else { // no interface found
285 value.add(new InterfacesBuilder().setInterfaceName(interfaceName)
290 LOG.info("removing interface info from port '{}'", port);
291 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
292 .port.Interfaces tmp = getInterfaces(value, interfaceName);
294 LOG.info("Interfaces with name '{}' gets",interfaceName);
297 LOG.error("Interfaces list is null");
298 value = new ArrayList<org.opendaylight.yang.gen.v1.http.org.openroadm
299 .device.rev181019.port.Interfaces>();
302 newPorts.setInterfaces(value);
303 portList.remove(ports);
304 portList.add(newPorts.build());
305 LOG.info("port list updated !");
306 update(circuitPacks, iid);
314 LOG.error("reading operational device data failed !");
318 LOG.error("SupportingCircuitPackName / SupportingPort / Interface Name are/is null !");
322 LOG.warn("not an interface update ! ");
326 LOG.error("DataObjectModification is null");
331 * Search in {@link Interfaces} list
332 * if an interfaces already exists.
334 * @param value Interfaces List
335 * @param interfaceName Interface Name
338 private org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces getInterfaces(
339 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces> value,
340 String interfaceName) {
341 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces result = null;
342 if (value != null && !value.isEmpty()) {
343 LOG.info("Getting interfaces list ...");
344 for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
345 .port.Interfaces tmp : value) {
347 && tmp.getInterfaceName().compareTo(interfaceName) == 0) {
348 LOG.info("Interfaces with name '{}' gets",interfaceName);
354 LOG.error("Interfaces list is empty");
360 * Update circuitPacks information
361 * in device oper datastore.
363 * @param circuitPacks CircuitPacks data
364 * @param iid CircuitPacks InstanceIdentifier
365 * @param id OrgOpenroadmDevice InstanceIdentifier
366 * @param deviceData OrgOpenroadmDevice data
368 private void update(CircuitPacks circuitPacks, final InstanceIdentifier<CircuitPacks> iid) {
369 LOG.info("updating oper Datastore with iid : {}", iid);
370 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
371 if (writeTx != null) {
372 LOG.info("WriteTransaction is ok, update container circuitpacks from device oper datastore");
374 LOG.info("updating container circuitpacks from device oper DS ...");
375 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid,circuitPacks);
376 Future<Void> future = writeTx.submit();
378 LOG.info("container circuitpacks '{}' merged to device oper datastore", iid);
379 } catch (InterruptedException | ExecutionException e) {
380 LOG.error("Failed to process WriteTransactions",e);
383 LOG.error("WriteTransaction object is null");
388 * Get {@link OrgOpenroadmDevice} data
389 * from operational datastore
391 * @param id OrgOpenroadmDevice InstanceIdentifier
392 * @return OrgOpenroadmDevice result
394 private OrgOpenroadmDevice readDeviceOperData(final InstanceIdentifier<OrgOpenroadmDevice> id) {
395 OrgOpenroadmDevice result = null;
396 LOG.info("reading device operational datastore ...");
397 ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
398 if (readTx != null) {
399 LOG.info("ReadTransaction is ok");
401 LOG.info("reading device from device oper DS ...");
402 Optional<OrgOpenroadmDevice> device = readTx.read(LogicalDatastoreType.OPERATIONAL, id).get();
403 if (device.isPresent()) {
404 result = device.get();
405 LOG.info("device gets from device oper datastore");
407 } catch (InterruptedException | ExecutionException e) {
408 LOG.error("Failed to process ReadTransaction",e);
411 LOG.error("ReadTransaction object is null");