Don't use NotificationListener (TapiProvider)
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / impl / TapiProvider.java
1 /*
2  * Copyright © 2018 Orange, Inc. and others.  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.impl;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.mdsal.binding.api.NotificationService;
17 import org.opendaylight.mdsal.binding.api.RpcProviderService;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.transportpce.common.InstanceIdentifiers;
20 import org.opendaylight.transportpce.common.NetworkUtils;
21 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
22 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
23 import org.opendaylight.transportpce.tapi.connectivity.ConnectivityUtils;
24 import org.opendaylight.transportpce.tapi.connectivity.TapiConnectivityImpl;
25 import org.opendaylight.transportpce.tapi.listeners.TapiNetworkModelNotificationHandler;
26 import org.opendaylight.transportpce.tapi.listeners.TapiPceNotificationHandler;
27 import org.opendaylight.transportpce.tapi.listeners.TapiRendererNotificationHandler;
28 import org.opendaylight.transportpce.tapi.listeners.TapiServiceNotificationHandler;
29 import org.opendaylight.transportpce.tapi.topology.TapiNetconfTopologyListener;
30 import org.opendaylight.transportpce.tapi.topology.TapiNetworkModelService;
31 import org.opendaylight.transportpce.tapi.topology.TapiOrLinkListener;
32 import org.opendaylight.transportpce.tapi.topology.TapiPortMappingListener;
33 import org.opendaylight.transportpce.tapi.topology.TapiTopologyImpl;
34 import org.opendaylight.transportpce.tapi.topology.TopologyUtils;
35 import org.opendaylight.transportpce.tapi.utils.TapiContext;
36 import org.opendaylight.transportpce.tapi.utils.TapiInitialORMapping;
37 import org.opendaylight.transportpce.tapi.utils.TapiLink;
38 import org.opendaylight.transportpce.tapi.utils.TapiLinkImpl;
39 import org.opendaylight.transportpce.tapi.utils.TapiListener;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.Network;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.network.Nodes;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev210408.TransportpceTapinetworkutilsService;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.OrgOpenroadmServiceService;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.ServiceInterfacePoints;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yangtools.concepts.ObjectRegistration;
52 import org.opendaylight.yangtools.concepts.Registration;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.osgi.service.component.annotations.Activate;
55 import org.osgi.service.component.annotations.Component;
56 import org.osgi.service.component.annotations.Deactivate;
57 import org.osgi.service.component.annotations.Reference;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * Class to register TAPI interface Service and Notification.
63  *
64  * @author Gilles Thouenon (gilles.thouenon@orange.com) on behalf of Orange
65  *
66  */
67 @Component
68 public class TapiProvider {
69
70     private static final Logger LOG = LoggerFactory.getLogger(TapiProvider.class);
71
72     private static final InstanceIdentifier<Nodes> MAPPING_II = InstanceIdentifier.create(Network.class)
73         .child(org.opendaylight.yang.gen.v1.http
74             .org.opendaylight.transportpce.portmapping.rev220922.network.Nodes.class);
75     private static final InstanceIdentifier<Link> LINK_II = InstanceIdentifier.create(Networks.class).child(
76         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network.class,
77             new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).augmentation(Network1.class)
78         .child(Link.class);
79     private final DataBroker dataBroker;
80     private final NetworkTransactionService networkTransactionService;
81     private final OrgOpenroadmServiceService serviceHandler;
82     private final ServiceDataStoreOperations serviceDataStoreOperations;
83     private ObjectRegistration<TransportpceTapinetworkutilsService> tapiNetworkutilsServiceRpcRegistration;
84     private List<Registration> listeners;
85     private List<Registration> rpcRegistrations = new ArrayList<>();
86     private Registration pcelistenerRegistration;
87     private Registration rendererlistenerRegistration;
88     private Registration servicehandlerlistenerRegistration;
89     private Registration tapinetworkmodellistenerRegistration;
90
91     @Activate
92     public TapiProvider(@Reference DataBroker dataBroker,
93             @Reference RpcProviderService rpcProviderService,
94             @Reference NotificationService notificationService,
95             @Reference NotificationPublishService notificationPublishService,
96             @Reference NetworkTransactionService networkTransactionService,
97             @Reference OrgOpenroadmServiceService serviceHandler,
98             @Reference ServiceDataStoreOperations serviceDataStoreOperations,
99             @Reference TapiNetworkModelNotificationHandler tapiNetworkModelNotificationHandler,
100             @Reference TapiNetworkModelService tapiNetworkModelServiceImpl) {
101         this.dataBroker = dataBroker;
102         this.networkTransactionService = networkTransactionService;
103         this.serviceHandler = serviceHandler;
104         this.serviceDataStoreOperations = serviceDataStoreOperations;
105         LOG.info("TapiProvider Session Initiated");
106         TapiContext tapiContext = new TapiContext(this.networkTransactionService);
107         LOG.info("Empty TAPI context created: {}", tapiContext.getTapiContext());
108         TapiLink tapiLink = new TapiLinkImpl(this.networkTransactionService);
109         TopologyUtils topologyUtils = new TopologyUtils(this.networkTransactionService, this.dataBroker, tapiLink);
110         ConnectivityUtils connectivityUtils = new ConnectivityUtils(this.serviceDataStoreOperations, new HashMap<>(),
111                 tapiContext, this.networkTransactionService);
112         TapiInitialORMapping tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
113                 tapiContext, this.serviceDataStoreOperations);
114         tapiInitialORMapping.performTopoInitialMapping();
115         tapiInitialORMapping.performServInitialMapping();
116         TapiPceNotificationHandler pceListenerImpl = new TapiPceNotificationHandler(dataBroker);
117         TapiRendererNotificationHandler rendererListenerImpl = new TapiRendererNotificationHandler(dataBroker,
118                 notificationPublishService);
119
120         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(this.serviceHandler, tapiContext,
121                 connectivityUtils, pceListenerImpl, rendererListenerImpl);
122         rpcRegistrations.add(rpcProviderService.registerRpcImplementations(tapiConnectivity.registerRPCs()));
123         TapiTopologyImpl topo = new TapiTopologyImpl(this.dataBroker, tapiContext, topologyUtils, tapiLink);
124         rpcRegistrations.add(rpcProviderService.registerRpcImplementations(topo.registerRPCs()));
125
126         this.listeners = new ArrayList<>();
127         TapiNetconfTopologyListener topologyListener = new TapiNetconfTopologyListener(tapiNetworkModelServiceImpl);
128         TapiOrLinkListener orLinkListener = new TapiOrLinkListener(tapiLink, networkTransactionService);
129         TapiPortMappingListener tapiPortMappingListener = new TapiPortMappingListener(tapiNetworkModelServiceImpl);
130         listeners.add(dataBroker.registerDataTreeChangeListener(
131                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, LINK_II), orLinkListener));
132         listeners.add(dataBroker.registerDataTreeChangeListener(
133                 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifiers.NETCONF_TOPOLOGY_II
134                     .child(Node.class)),
135                 topologyListener));
136         listeners.add(dataBroker.registerDataTreeChangeListener(
137                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, MAPPING_II), tapiPortMappingListener));
138         TapiListener tapiListener = new TapiListener();
139         listeners.add(dataBroker.registerDataTreeChangeListener(
140                 DataTreeIdentifier.create(
141                         LogicalDatastoreType.CONFIGURATION,
142                         InstanceIdentifier.create(ServiceInterfacePoints.class)),
143                 tapiListener));
144         // Notification Listener
145         pcelistenerRegistration = notificationService.registerCompositeListener(pceListenerImpl.getCompositeListener());
146         rendererlistenerRegistration = notificationService
147             .registerCompositeListener(rendererListenerImpl.getCompositeListener());
148         TapiServiceNotificationHandler serviceHandlerListenerImpl = new TapiServiceNotificationHandler(dataBroker);
149         servicehandlerlistenerRegistration = notificationService
150             .registerCompositeListener(serviceHandlerListenerImpl.getCompositeListener());
151         tapinetworkmodellistenerRegistration = notificationService
152             .registerCompositeListener(tapiNetworkModelNotificationHandler.getCompositeListener());
153     }
154
155     /**
156      * Method called when the blueprint container is destroyed.
157      */
158     @Deactivate
159     public void close() {
160         listeners.forEach(lis -> lis.close());
161         listeners.clear();
162         pcelistenerRegistration.close();
163         rendererlistenerRegistration.close();
164         servicehandlerlistenerRegistration.close();
165         tapinetworkmodellistenerRegistration.close();
166         for (Registration reg : rpcRegistrations) {
167             reg.close();
168         }
169         LOG.info("TapiProvider Session Closed");
170     }
171
172     public List<Registration> getRegisteredRpcs() {
173         return rpcRegistrations;
174     }
175
176 }