Refactoring of TAPI Step1
[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.openroadm.service.rev230526.OrgOpenroadmServiceService;
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.rev220922.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 OrgOpenroadmServiceService serviceHandler;
80     private final ServiceDataStoreOperations serviceDataStoreOperations;
81     private List<Registration> listeners;
82     private List<Registration> rpcRegistrations = new ArrayList<>();
83     private Registration pcelistenerRegistration;
84     private Registration rendererlistenerRegistration;
85     private Registration servicehandlerlistenerRegistration;
86     private Registration tapinetworkmodellistenerRegistration;
87
88     @Activate
89     public TapiProvider(@Reference DataBroker dataBroker,
90             @Reference RpcProviderService rpcProviderService,
91             @Reference NotificationService notificationService,
92             @Reference NotificationPublishService notificationPublishService,
93             @Reference NetworkTransactionService networkTransactionService,
94             @Reference OrgOpenroadmServiceService serviceHandler,
95             @Reference ServiceDataStoreOperations serviceDataStoreOperations,
96             @Reference TapiNetworkModelNotificationHandler tapiNetworkModelNotificationHandler,
97             @Reference TapiNetworkModelService tapiNetworkModelServiceImpl) {
98         this.dataBroker = dataBroker;
99         this.networkTransactionService = networkTransactionService;
100         this.serviceHandler = serviceHandler;
101         this.serviceDataStoreOperations = serviceDataStoreOperations;
102         LOG.info("TapiProvider Session Initiated");
103         TapiContext tapiContext = new TapiContext(this.networkTransactionService);
104         LOG.info("Empty TAPI context created: {}", tapiContext.getTapiContext());
105         TapiLink tapiLink = new TapiLinkImpl(this.networkTransactionService);
106         TopologyUtils topologyUtils = new TopologyUtils(this.networkTransactionService, this.dataBroker, tapiLink);
107         ConnectivityUtils connectivityUtils = new ConnectivityUtils(this.serviceDataStoreOperations, new HashMap<>(),
108                 tapiContext, this.networkTransactionService);
109         TapiInitialORMapping tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
110                 tapiContext, this.serviceDataStoreOperations);
111         tapiInitialORMapping.performTopoInitialMapping();
112         tapiInitialORMapping.performServInitialMapping();
113         TapiPceNotificationHandler pceListenerImpl = new TapiPceNotificationHandler(dataBroker, connectivityUtils);
114         TapiRendererNotificationHandler rendererListenerImpl = new TapiRendererNotificationHandler(dataBroker,
115                 notificationPublishService);
116
117         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(this.serviceHandler, tapiContext,
118             connectivityUtils, pceListenerImpl, rendererListenerImpl, networkTransactionService);
119         rpcRegistrations.add(rpcProviderService.registerRpcImplementations(tapiConnectivity.registerRPCs()));
120         TapiTopologyImpl topo = new TapiTopologyImpl(this.dataBroker, tapiContext, topologyUtils, tapiLink);
121         rpcRegistrations.add(rpcProviderService.registerRpcImplementations(topo.registerRPCs()));
122
123         this.listeners = new ArrayList<>();
124         TapiNetconfTopologyListener topologyListener = new TapiNetconfTopologyListener(tapiNetworkModelServiceImpl);
125         TapiOrLinkListener orLinkListener = new TapiOrLinkListener(tapiLink, networkTransactionService);
126         TapiPortMappingListener tapiPortMappingListener = new TapiPortMappingListener(tapiNetworkModelServiceImpl);
127         listeners.add(dataBroker.registerDataTreeChangeListener(
128                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, LINK_II), orLinkListener));
129         listeners.add(dataBroker.registerDataTreeChangeListener(
130                 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifiers.NETCONF_TOPOLOGY_II
131                     .child(Node.class)),
132                 topologyListener));
133         listeners.add(dataBroker.registerDataTreeChangeListener(
134                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, MAPPING_II), tapiPortMappingListener));
135         TapiListener tapiListener = new TapiListener();
136         listeners.add(dataBroker.registerDataTreeChangeListener(
137                 DataTreeIdentifier.create(
138                         LogicalDatastoreType.CONFIGURATION,
139                         InstanceIdentifier.create(ServiceInterfacePoints.class)),
140                 tapiListener));
141         // Notification Listener
142         pcelistenerRegistration = notificationService.registerCompositeListener(pceListenerImpl.getCompositeListener());
143         rendererlistenerRegistration = notificationService
144             .registerCompositeListener(rendererListenerImpl.getCompositeListener());
145         TapiServiceNotificationHandler serviceHandlerListenerImpl = new TapiServiceNotificationHandler(dataBroker);
146         servicehandlerlistenerRegistration = notificationService
147             .registerCompositeListener(serviceHandlerListenerImpl.getCompositeListener());
148         tapinetworkmodellistenerRegistration = notificationService
149             .registerCompositeListener(tapiNetworkModelNotificationHandler.getCompositeListener());
150     }
151
152     /**
153      * Method called when the blueprint container is destroyed.
154      */
155     @Deactivate
156     public void close() {
157         listeners.forEach(lis -> lis.close());
158         listeners.clear();
159         pcelistenerRegistration.close();
160         rendererlistenerRegistration.close();
161         servicehandlerlistenerRegistration.close();
162         tapinetworkmodellistenerRegistration.close();
163         for (Registration reg : rpcRegistrations) {
164             reg.close();
165         }
166         LOG.info("TapiProvider Session Closed");
167     }
168
169     public List<Registration> getRegisteredRpcs() {
170         return rpcRegistrations;
171     }
172
173 }