1e2e7ce4d57baef642421bc6c0479fe471894c3b
[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.Collection;
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.rev220316.mapping.Mapping;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.mapping.MappingKey;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.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 Collection<DataTreeModification<Nodes>> changes) {
33         for (DataTreeModification<Nodes> change : changes) {
34             LOG.debug("TAPI module: Change in Node = {}", change.getRootNode());
35             // Data before needs to be not null
36             if (change.getRootNode().getDataAfter() != null && change.getRootNode().getDataBefore() != null) {
37                 Nodes nodesAft = change.getRootNode().getDataAfter();
38                 Nodes nodesBef = change.getRootNode().getDataBefore();
39                 // TODO -> need to filter out the ones that are not after creation.
40                 //  (Mapping before = null & Mapping after != null) is the rule for a first time connected device
41                 String nodeId = nodesAft.getNodeId();
42                 Map<MappingKey, Mapping> mappingAft = nodesAft.getMapping();
43                 Map<MappingKey, Mapping> mappingBef = nodesBef.getMapping();
44                 LOG.info("Change in node {} with OR version = {}", nodeId,
45                     nodesAft.getNodeInfo().getOpenroadmVersion().getName());
46                 if (mappingAft == null) {
47                     LOG.warn("Mapping already existed in the datastore, which means that node {} already existed "
48                         + "in TAPI topology. The action to take will be different", nodeId);
49                     continue;
50                 }
51                 if (mappingBef == null) {
52                     LOG.info("New mapping for node {} = {}", nodeId, mappingAft);
53                     LOG.info("As the mapping is now created for the first time, "
54                         + "we can proceed with the creation of the node {} in the TAPI topology", nodeId);
55                     this.tapiNetworkModelService.createTapiNode(nodeId,
56                         nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
57                 } else {
58                     for (Map.Entry<MappingKey, Mapping> entry : mappingAft.entrySet()) {
59                         Mapping oldMapping = mappingBef.get(entry.getKey());
60                         Mapping newMapping = mappingAft.get(entry.getKey());
61                         if (!oldMapping.getPortAdminState().equals(newMapping.getPortAdminState())
62                                 || !oldMapping.getPortOperState().equals(newMapping.getPortOperState())) {
63                             this.tapiNetworkModelService.updateTapiTopology(nodeId, entry.getValue());
64                         }
65                     }
66                 }
67             }
68         }
69     }
70 }