2 * Copyright © 2021 Nokia. All rights reserved.
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
8 package org.opendaylight.transportpce.tapi.topology;
10 import java.util.List;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.Mapping;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.MappingKey;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.network.Nodes;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class TapiPortMappingListener implements DataTreeChangeListener<Nodes> {
23 private static final Logger LOG = LoggerFactory.getLogger(TapiPortMappingListener.class);
25 private final TapiNetworkModelService tapiNetworkModelService;
27 public TapiPortMappingListener(TapiNetworkModelService tapiNetworkModelService) {
28 this.tapiNetworkModelService = tapiNetworkModelService;
32 public void onDataTreeChanged(@NonNull List<DataTreeModification<Nodes>> changes) {
33 for (DataTreeModification<Nodes> change : changes) {
34 var rootNode = change.getRootNode();
35 LOG.debug("TAPI module: Change in Node = {}", rootNode);
36 // Data before needs to be not null
37 Nodes nodesAft = rootNode.dataAfter();
38 if (nodesAft == null) {
41 Nodes nodesBef = rootNode.dataBefore();
42 if (nodesBef == null) {
43 this.tapiNetworkModelService.createTapiNode(
44 nodesAft.getNodeId(), nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
47 // TODO -> need to filter out the ones that are not after creation.
48 // (Mapping before = null & Mapping after != null) is the rule for a first time connected device
49 String nodeId = nodesAft.getNodeId();
50 Map<MappingKey, Mapping> mappingAft = nodesAft.getMapping();
51 Map<MappingKey, Mapping> mappingBef = nodesBef.getMapping();
52 LOG.info("Change in node {} with OR version = {}",
53 nodeId, nodesAft.getNodeInfo().getOpenroadmVersion().getName());
54 //TODO avoid long message and concatenation in following LOG messages
55 if (mappingAft == null) {
56 LOG.warn("Mapping already existed in the datastore, which means that node {} already existed "
57 + "in TAPI topology. The action to take will be different", nodeId);
60 if (mappingBef == null) {
61 LOG.info("New mapping for node {} = {}", nodeId, mappingAft);
62 LOG.info("As the mapping is now created for the first time, "
63 + "we can proceed with the creation of the node {} in the TAPI topology", nodeId);
64 this.tapiNetworkModelService.createTapiNode(
65 nodeId, nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
68 for (Map.Entry<MappingKey, Mapping> entry : mappingAft.entrySet()) {
69 Mapping oldMapping = mappingBef.get(entry.getKey());
70 Mapping newMapping = mappingAft.get(entry.getKey());
71 if (oldMapping == null || newMapping == null) {
74 if (!oldMapping.getPortAdminState().equals(newMapping.getPortAdminState())
75 || !oldMapping.getPortOperState().equals(newMapping.getPortOperState())) {
76 this.tapiNetworkModelService.updateTapiTopology(nodeId, entry.getValue());