Turn NetworkUtilsImpl into a Component
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / NetworkModelProvider.java
index 796125db72fc91d7eb5f63899ce8ab563655f47f..0eafd00f110bdccfff67998b72a647e4278a1ac1 100644 (file)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.transportpce.networkmodel;
 
+import java.util.ArrayList;
+import java.util.List;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.NotificationService;
@@ -14,27 +17,35 @@ import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.InstanceIdentifiers;
 import org.opendaylight.transportpce.common.NetworkUtils;
+import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
+import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
 import org.opendaylight.transportpce.networkmodel.listeners.PortMappingListener;
 import org.opendaylight.transportpce.networkmodel.listeners.ServiceHandlerListener;
 import org.opendaylight.transportpce.networkmodel.service.FrequenciesService;
+import org.opendaylight.transportpce.networkmodel.service.NetworkModelService;
 import org.opendaylight.transportpce.networkmodel.util.TpceNetwork;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev170818.TransportpceNetworkutilsService;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.Network;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev220630.TransportpceNetworkutilsService;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.Network;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.mapping.Mapping;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.TransportpceServicehandlerListener;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.concepts.ObjectRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Component
 public class NetworkModelProvider {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetworkModelProvider.class);
     private static final InstanceIdentifier<Mapping> MAPPING_II = InstanceIdentifier.create(Network.class)
-        .child(org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network
+        .child(org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.network
                 .Nodes.class)
         .child(Mapping.class);
 
@@ -42,61 +53,65 @@ public class NetworkModelProvider {
     private final RpcProviderService rpcProviderService;
     private final TransportpceNetworkutilsService networkutilsService;
     private final NetConfTopologyListener topologyListener;
-    private ListenerRegistration<NetConfTopologyListener> dataTreeChangeListenerRegistration;
-    private ListenerRegistration<PortMappingListener> mappingListenerRegistration;
-    private ObjectRegistration<TransportpceNetworkutilsService> networkutilsServiceRpcRegistration;
+    private List<Registration> listeners;
+    private @NonNull Registration networkutilsServiceRpcRegistration;
     private TpceNetwork tpceNetwork;
     private ListenerRegistration<TransportpceServicehandlerListener> serviceHandlerListenerRegistration;
     private NotificationService notificationService;
     private FrequenciesService frequenciesService;
     private PortMappingListener portMappingListener;
 
-    public NetworkModelProvider(NetworkTransactionService networkTransactionService, final DataBroker dataBroker,
-        final RpcProviderService rpcProviderService, final TransportpceNetworkutilsService networkutilsService,
-        final NetConfTopologyListener topologyListener, NotificationService notificationService,
-        FrequenciesService frequenciesService, PortMappingListener portMappingListener) {
+    @Activate
+    public NetworkModelProvider(@Reference NetworkTransactionService networkTransactionService,
+            @Reference final DataBroker dataBroker,
+            @Reference final RpcProviderService rpcProviderService,
+            @Reference final NetworkModelService networkModelService,
+            @Reference DeviceTransactionManager deviceTransactionManager,
+            @Reference PortMapping portMapping,
+            @Reference NotificationService notificationService,
+            @Reference FrequenciesService frequenciesService,
+            @Reference TransportpceNetworkutilsService networkUtils) {
         this.dataBroker = dataBroker;
         this.rpcProviderService = rpcProviderService;
-        this.networkutilsService = networkutilsService;
-        this.topologyListener = topologyListener;
-        this.tpceNetwork = new TpceNetwork(networkTransactionService);
         this.notificationService = notificationService;
         this.frequenciesService = frequenciesService;
-        this.portMappingListener = portMappingListener;
+        this.listeners = new ArrayList<>();
+        this.networkutilsService = networkUtils;
+        this.topologyListener = new NetConfTopologyListener(networkModelService, dataBroker, deviceTransactionManager,
+            portMapping);
+        this.tpceNetwork = new TpceNetwork(networkTransactionService);
+        this.portMappingListener = new PortMappingListener(networkModelService);
+        this.init();
     }
 
     /**
      * Method called when the blueprint container is created.
      */
-    public void init() {
+    private void init() {
         LOG.info("NetworkModelProvider Session Initiated");
         tpceNetwork.createLayer(NetworkUtils.CLLI_NETWORK_ID);
         tpceNetwork.createLayer(NetworkUtils.UNDERLAY_NETWORK_ID);
         tpceNetwork.createLayer(NetworkUtils.OVERLAY_NETWORK_ID);
         tpceNetwork.createLayer(NetworkUtils.OTN_NETWORK_ID);
-        dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
+        listeners.add(dataBroker.registerDataTreeChangeListener(
                 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
-                InstanceIdentifiers.NETCONF_TOPOLOGY_II.child(Node.class)), topologyListener);
-        mappingListenerRegistration = dataBroker.registerDataTreeChangeListener(
-                DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, MAPPING_II), portMappingListener);
-        networkutilsServiceRpcRegistration =
-            rpcProviderService.registerRpcImplementation(TransportpceNetworkutilsService.class, networkutilsService);
-        TransportpceServicehandlerListener serviceHandlerListner =
-                new ServiceHandlerListener(frequenciesService);
+                InstanceIdentifiers.NETCONF_TOPOLOGY_II.child(Node.class)), topologyListener));
+        listeners.add(dataBroker.registerDataTreeChangeListener(
+                DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, MAPPING_II), portMappingListener));
+        networkutilsServiceRpcRegistration = rpcProviderService
+            .registerRpcImplementation(TransportpceNetworkutilsService.class, networkutilsService);
+        TransportpceServicehandlerListener serviceHandlerListner = new ServiceHandlerListener(frequenciesService);
         serviceHandlerListenerRegistration = notificationService.registerNotificationListener(serviceHandlerListner);
     }
 
         /**
          * Method called when the blueprint container is destroyed.
          */
+    @Deactivate
     public void close() {
         LOG.info("NetworkModelProvider Closed");
-        if (dataTreeChangeListenerRegistration != null) {
-            dataTreeChangeListenerRegistration.close();
-        }
-        if (mappingListenerRegistration != null) {
-            mappingListenerRegistration.close();
-        }
+        listeners.forEach(lis -> lis.close());
+        listeners.clear();
         if (networkutilsServiceRpcRegistration != null) {
             networkutilsServiceRpcRegistration.close();
         }