Migrate RpcService usage in TapiNetworkUtilsImpl
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / impl / TapiProvider.java
index e58b26422fd28f779427d53e6817aa8c710abedc..065a840968c5283a01d312b3cf3d3be2bc3395ab 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.transportpce.tapi.impl;
 
+import java.util.ArrayList;
 import java.util.HashMap;
-import org.eclipse.jdt.annotation.NonNull;
+import java.util.List;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.binding.api.NotificationService;
 import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
@@ -20,11 +22,11 @@ import org.opendaylight.transportpce.common.network.NetworkTransactionService;
 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
 import org.opendaylight.transportpce.tapi.connectivity.ConnectivityUtils;
 import org.opendaylight.transportpce.tapi.connectivity.TapiConnectivityImpl;
-import org.opendaylight.transportpce.tapi.listeners.TapiNetworkModelListenerImpl;
 import org.opendaylight.transportpce.tapi.listeners.TapiPceListenerImpl;
 import org.opendaylight.transportpce.tapi.listeners.TapiRendererListenerImpl;
 import org.opendaylight.transportpce.tapi.listeners.TapiServiceHandlerListenerImpl;
 import org.opendaylight.transportpce.tapi.topology.TapiNetconfTopologyListener;
+import org.opendaylight.transportpce.tapi.topology.TapiNetworkModelService;
 import org.opendaylight.transportpce.tapi.topology.TapiOrLinkListener;
 import org.opendaylight.transportpce.tapi.topology.TapiPortMappingListener;
 import org.opendaylight.transportpce.tapi.topology.TapiTopologyImpl;
@@ -39,8 +41,7 @@ import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmappi
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.network.Nodes;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.TransportpceRendererListener;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.TransportpceServicehandlerListener;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev210408.TransportpceTapinetworkutilsService;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.OrgOpenroadmServiceService;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.OrgOpenroadmServiceService;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
@@ -48,12 +49,18 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.top
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.TapiCommonService;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.TapiConnectivityService;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.TapiNotificationListener;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.TapiTopologyService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.ServiceInterfacePoints;
 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;
 
@@ -63,6 +70,7 @@ import org.slf4j.LoggerFactory;
  * @author Gilles Thouenon (gilles.thouenon@orange.com) on behalf of Orange
  *
  */
+@Component
 public class TapiProvider {
 
     private static final Logger LOG = LoggerFactory.getLogger(TapiProvider.class);
@@ -75,60 +83,30 @@ public class TapiProvider {
             new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).augmentation(Network1.class)
         .child(Link.class);
     private final DataBroker dataBroker;
-    private final RpcProviderService rpcProviderService;
+    private final NetworkTransactionService networkTransactionService;
+    private final OrgOpenroadmServiceService serviceHandler;
+    private final ServiceDataStoreOperations serviceDataStoreOperations;
     private ObjectRegistration<TapiConnectivityService> rpcRegistration;
-    private ObjectRegistration<TransportpceTapinetworkutilsService> tapiNetworkutilsServiceRpcRegistration;
-    private ListenerRegistration<TapiNetconfTopologyListener> dataTreeChangeListenerRegistration;
-    private ListenerRegistration<TapiOrLinkListener> dataTreeChangeListenerRegistration1;
-    private ListenerRegistration<TapiPortMappingListener> mappingListenerListenerRegistration;
+    private List<Registration> listeners;
     private ListenerRegistration<TransportpcePceListener> pcelistenerRegistration;
     private ListenerRegistration<TransportpceRendererListener> rendererlistenerRegistration;
     private ListenerRegistration<TransportpceServicehandlerListener> servicehandlerlistenerRegistration;
-    private ListenerRegistration<TapiNetworkModelListenerImpl> tapinetworkmodellistenerRegistration;
-    private final OrgOpenroadmServiceService serviceHandler;
-    private final ServiceDataStoreOperations serviceDataStoreOperations;
-    private final TapiListener tapiListener;
-    private final TapiNetconfTopologyListener topologyListener;
-    private final TapiOrLinkListener orLinkListener;
-    private TapiPortMappingListener tapiPortMappingListener;
-    private final NetworkTransactionService networkTransactionService;
-    private final TransportpceTapinetworkutilsService tapiNetworkUtils;
-    private TapiPceListenerImpl pceListenerImpl;
-    private TapiRendererListenerImpl rendererListenerImpl;
-    private TapiServiceHandlerListenerImpl serviceHandlerListenerImpl;
-    private final NotificationService notificationService;
-    private TapiNetworkModelListenerImpl tapiNetworkModelListenerImpl;
+    private ListenerRegistration<TapiNotificationListener> tapinetworkmodellistenerRegistration;
 
-    public TapiProvider(DataBroker dataBroker, RpcProviderService rpcProviderService,
-            OrgOpenroadmServiceService serviceHandler,ServiceDataStoreOperations serviceDataStoreOperations,
-            TapiListener tapiListener, NetworkTransactionService networkTransactionService,
-            TapiNetconfTopologyListener topologyListener, TapiPortMappingListener tapiPortMappingListener,
-            TransportpceTapinetworkutilsService tapiNetworkUtils, TapiPceListenerImpl pceListenerImpl,
-            TapiRendererListenerImpl rendererListenerImpl, TapiServiceHandlerListenerImpl serviceHandlerListenerImpl,
-            NotificationService notificationService, TapiOrLinkListener orLinkListener,
-            TapiNetworkModelListenerImpl tapiNetworkModelListenerImpl) {
+    @Activate
+    public TapiProvider(@Reference DataBroker dataBroker,
+            @Reference RpcProviderService rpcProviderService,
+            @Reference NotificationService notificationService,
+            @Reference NotificationPublishService notificationPublishService,
+            @Reference NetworkTransactionService networkTransactionService,
+            @Reference OrgOpenroadmServiceService serviceHandler,
+            @Reference ServiceDataStoreOperations serviceDataStoreOperations,
+            @Reference TapiNotificationListener tapiNetworkModelListenerImpl,
+            @Reference TapiNetworkModelService tapiNetworkModelServiceImpl) {
         this.dataBroker = dataBroker;
-        this.rpcProviderService = rpcProviderService;
+        this.networkTransactionService = networkTransactionService;
         this.serviceHandler = serviceHandler;
         this.serviceDataStoreOperations = serviceDataStoreOperations;
-        this.tapiListener = tapiListener;
-        this.networkTransactionService = networkTransactionService;
-        this.topologyListener = topologyListener;
-        this.tapiPortMappingListener = tapiPortMappingListener;
-        this.tapiNetworkUtils = tapiNetworkUtils;
-        this.pceListenerImpl = pceListenerImpl;
-        this.rendererListenerImpl = rendererListenerImpl;
-        this.serviceHandlerListenerImpl = serviceHandlerListenerImpl;
-        this.notificationService = notificationService;
-        this.orLinkListener = orLinkListener;
-        this.tapiNetworkModelListenerImpl = tapiNetworkModelListenerImpl;
-        //this.notificationPublishService = notificationPublishService;
-    }
-
-    /**
-     * Method called when the blueprint container is created.
-     */
-    public void init() {
         LOG.info("TapiProvider Session Initiated");
         TapiContext tapiContext = new TapiContext(this.networkTransactionService);
         LOG.info("Empty TAPI context created: {}", tapiContext.getTapiContext());
@@ -140,6 +118,9 @@ public class TapiProvider {
                 tapiContext, this.serviceDataStoreOperations);
         tapiInitialORMapping.performTopoInitialMapping();
         tapiInitialORMapping.performServInitialMapping();
+        TapiPceListenerImpl pceListenerImpl = new TapiPceListenerImpl(dataBroker);
+        TapiRendererListenerImpl rendererListenerImpl = new TapiRendererListenerImpl(dataBroker,
+                notificationPublishService);
 
         TapiConnectivityImpl tapi = new TapiConnectivityImpl(this.serviceHandler, tapiContext, connectivityUtils,
                 pceListenerImpl, rendererListenerImpl);
@@ -148,52 +129,47 @@ public class TapiProvider {
         rpcRegistration = rpcProviderService.registerRpcImplementation(TapiConnectivityService.class, tapi);
         rpcProviderService.registerRpcImplementation(TapiTopologyService.class, topo);
         rpcProviderService.registerRpcImplementation(TapiCommonService.class, topo);
-        dataTreeChangeListenerRegistration1 =
-            dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
-                LINK_II), orLinkListener);
-        dataTreeChangeListenerRegistration =
-            dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
-                InstanceIdentifiers.NETCONF_TOPOLOGY_II.child(Node.class)), topologyListener);
-        mappingListenerListenerRegistration =
-            dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
-                MAPPING_II), tapiPortMappingListener);
-        tapiNetworkutilsServiceRpcRegistration =
-                rpcProviderService.registerRpcImplementation(TransportpceTapinetworkutilsService.class,
-                        this.tapiNetworkUtils);
-        @NonNull
-        InstanceIdentifier<ServiceInterfacePoints> sipIID = InstanceIdentifier.create(ServiceInterfacePoints.class);
-        dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(
-            LogicalDatastoreType.CONFIGURATION, sipIID), tapiListener);
+
+        this.listeners = new ArrayList<>();
+        TapiNetconfTopologyListener topologyListener = new TapiNetconfTopologyListener(tapiNetworkModelServiceImpl);
+        TapiOrLinkListener orLinkListener = new TapiOrLinkListener(tapiLink, networkTransactionService);
+        TapiPortMappingListener tapiPortMappingListener = new TapiPortMappingListener(tapiNetworkModelServiceImpl);
+        listeners.add(dataBroker.registerDataTreeChangeListener(
+                DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, LINK_II), orLinkListener));
+        listeners.add(dataBroker.registerDataTreeChangeListener(
+                DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifiers.NETCONF_TOPOLOGY_II
+                    .child(Node.class)),
+                topologyListener));
+        listeners.add(dataBroker.registerDataTreeChangeListener(
+                DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, MAPPING_II), tapiPortMappingListener));
+        TapiListener tapiListener = new TapiListener();
+        listeners.add(dataBroker.registerDataTreeChangeListener(
+                DataTreeIdentifier.create(
+                        LogicalDatastoreType.CONFIGURATION,
+                        InstanceIdentifier.create(ServiceInterfacePoints.class)),
+                tapiListener));
         // Notification Listener
         pcelistenerRegistration = notificationService.registerNotificationListener(pceListenerImpl);
         rendererlistenerRegistration = notificationService.registerNotificationListener(rendererListenerImpl);
-        servicehandlerlistenerRegistration =
-                notificationService.registerNotificationListener(serviceHandlerListenerImpl);
-        tapinetworkmodellistenerRegistration =
-                notificationService.registerNotificationListener(tapiNetworkModelListenerImpl);
+        TapiServiceHandlerListenerImpl serviceHandlerListenerImpl = new TapiServiceHandlerListenerImpl(dataBroker);
+        servicehandlerlistenerRegistration = notificationService
+            .registerNotificationListener(serviceHandlerListenerImpl);
+        tapinetworkmodellistenerRegistration = notificationService
+            .registerNotificationListener(tapiNetworkModelListenerImpl);
     }
 
     /**
      * Method called when the blueprint container is destroyed.
      */
+    @Deactivate
     public void close() {
-        LOG.info("TapiProvider Session Closed");
-        if (dataTreeChangeListenerRegistration != null) {
-            dataTreeChangeListenerRegistration.close();
-        }
-        if (mappingListenerListenerRegistration != null) {
-            mappingListenerListenerRegistration.close();
-        }
-        if (dataTreeChangeListenerRegistration1 != null) {
-            dataTreeChangeListenerRegistration1.close();
-        }
-        if (tapiNetworkutilsServiceRpcRegistration != null) {
-            tapiNetworkutilsServiceRpcRegistration.close();
-        }
+        listeners.forEach(lis -> lis.close());
+        listeners.clear();
         pcelistenerRegistration.close();
         rendererlistenerRegistration.close();
         servicehandlerlistenerRegistration.close();
         rpcRegistration.close();
         tapinetworkmodellistenerRegistration.close();
+        LOG.info("TapiProvider Session Closed");
     }
 }