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