Fix CI Cent OS 8 configuration issue (workaround)
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TapiPortMappingListener.java
1 /*
2  * Copyright © 2021 Nokia.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.tapi.topology;
9
10 import java.util.List;
11 import java.util.Map;
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;
20
21 public class TapiPortMappingListener implements DataTreeChangeListener<Nodes> {
22
23     private static final Logger LOG = LoggerFactory.getLogger(TapiPortMappingListener.class);
24
25     private final TapiNetworkModelService tapiNetworkModelService;
26
27     public TapiPortMappingListener(TapiNetworkModelService tapiNetworkModelService) {
28         this.tapiNetworkModelService = tapiNetworkModelService;
29     }
30
31     @Override
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) {
39                 continue;
40             }
41             Nodes nodesBef = rootNode.dataBefore();
42             if (nodesBef == null) {
43                 this.tapiNetworkModelService.createTapiNode(
44                     nodesAft.getNodeId(), nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
45                 continue;
46             }
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);
58                 continue;
59             }
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);
66                 continue;
67             }
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) {
72                     continue;
73                 }
74                 if (!oldMapping.getPortAdminState().equals(newMapping.getPortAdminState())
75                         || !oldMapping.getPortOperState().equals(newMapping.getPortOperState())) {
76                     this.tapiNetworkModelService.updateTapiTopology(nodeId, entry.getValue());
77                 }
78             }
79         }
80     }
81 }