Refactor SupportedIfCapability usage
[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 && mappingBef == null) {
47                     LOG.info("New mapping for node {} = {}", nodeId, mappingAft);
48                     LOG.info("As the mapping is now created for the first time, "
49                         + "we can proceed with the creation of the node {} in the TAPI topology", nodeId);
50                     this.tapiNetworkModelService.createTapiNode(nodeId,
51                         nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
52                 } else {
53                     LOG.warn("Mapping already existed in the datastore, which means that node {} already existed "
54                         + "in TAPI topology. The action to take will be different", nodeId);
55                 }
56             }
57         }
58     }
59 }