TapiLink creation refactoring
[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.HashMap;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.mdsal.binding.api.NotificationService;
15 import org.opendaylight.mdsal.binding.api.RpcProviderService;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.transportpce.common.InstanceIdentifiers;
18 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
19 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
20 import org.opendaylight.transportpce.tapi.connectivity.ConnectivityUtils;
21 import org.opendaylight.transportpce.tapi.connectivity.TapiConnectivityImpl;
22 import org.opendaylight.transportpce.tapi.listeners.TapiPceListenerImpl;
23 import org.opendaylight.transportpce.tapi.listeners.TapiRendererListenerImpl;
24 import org.opendaylight.transportpce.tapi.listeners.TapiServiceHandlerListenerImpl;
25 import org.opendaylight.transportpce.tapi.topology.TapiNetconfTopologyListener;
26 import org.opendaylight.transportpce.tapi.topology.TapiPortMappingListener;
27 import org.opendaylight.transportpce.tapi.topology.TapiTopologyImpl;
28 import org.opendaylight.transportpce.tapi.topology.TopologyUtils;
29 import org.opendaylight.transportpce.tapi.utils.TapiContext;
30 import org.opendaylight.transportpce.tapi.utils.TapiInitialORMapping;
31 import org.opendaylight.transportpce.tapi.utils.TapiLink;
32 import org.opendaylight.transportpce.tapi.utils.TapiListener;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.TransportpcePceListener;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.Network;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.Nodes;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.TransportpceRendererListener;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.TransportpceServicehandlerListener;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev210408.TransportpceTapinetworkutilsService;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.OrgOpenroadmServiceService;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.TapiCommonService;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.TapiConnectivityService;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.TapiTopologyService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.ServiceInterfacePoints;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.concepts.ObjectRegistration;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * Class to register TAPI interface Service and Notification.
53  *
54  * @author Gilles Thouenon (gilles.thouenon@orange.com) on behalf of Orange
55  *
56  */
57 public class TapiProvider {
58
59     private static final Logger LOG = LoggerFactory.getLogger(TapiProvider.class);
60
61     private static final InstanceIdentifier<Nodes> MAPPING_II = InstanceIdentifier.create(Network.class)
62         .child(org.opendaylight.yang.gen.v1.http
63             .org.opendaylight.transportpce.portmapping.rev210426.network.Nodes.class);
64     private final DataBroker dataBroker;
65     private final RpcProviderService rpcProviderService;
66     private ObjectRegistration<TapiConnectivityService> rpcRegistration;
67     private ObjectRegistration<TransportpceTapinetworkutilsService> tapiNetworkutilsServiceRpcRegistration;
68     private ListenerRegistration<TapiNetconfTopologyListener> dataTreeChangeListenerRegistration;
69     private ListenerRegistration<TapiPortMappingListener> mappingListenerListenerRegistration;
70     private ListenerRegistration<TransportpcePceListener> pcelistenerRegistration;
71     private ListenerRegistration<TransportpceRendererListener> rendererlistenerRegistration;
72     private ListenerRegistration<TransportpceServicehandlerListener> servicehandlerlistenerRegistration;
73     private final OrgOpenroadmServiceService serviceHandler;
74     private final ServiceDataStoreOperations serviceDataStoreOperations;
75     private final TapiListener tapiListener;
76     private final TapiNetconfTopologyListener topologyListener;
77     private TapiPortMappingListener tapiPortMappingListener;
78     private final NetworkTransactionService networkTransactionService;
79     private final TransportpceTapinetworkutilsService tapiNetworkUtils;
80     private TapiPceListenerImpl pceListenerImpl;
81     private TapiRendererListenerImpl rendererListenerImpl;
82     private TapiServiceHandlerListenerImpl serviceHandlerListenerImpl;
83     private final NotificationService notificationService;
84
85     public TapiProvider(DataBroker dataBroker, RpcProviderService rpcProviderService,
86             OrgOpenroadmServiceService serviceHandler, ServiceDataStoreOperations serviceDataStoreOperations,
87             TapiListener tapiListener, NetworkTransactionService networkTransactionService,
88             TapiNetconfTopologyListener topologyListener, TapiPortMappingListener tapiPortMappingListener,
89             TransportpceTapinetworkutilsService tapiNetworkUtils, TapiPceListenerImpl pceListenerImpl,
90             TapiRendererListenerImpl rendererListenerImpl, TapiServiceHandlerListenerImpl serviceHandlerListenerImpl,
91             NotificationService notificationService) {
92         this.dataBroker = dataBroker;
93         this.rpcProviderService = rpcProviderService;
94         this.serviceHandler = serviceHandler;
95         this.serviceDataStoreOperations = serviceDataStoreOperations;
96         this.tapiListener = tapiListener;
97         this.networkTransactionService = networkTransactionService;
98         this.topologyListener = topologyListener;
99         this.tapiPortMappingListener = tapiPortMappingListener;
100         this.tapiNetworkUtils = tapiNetworkUtils;
101         this.pceListenerImpl = pceListenerImpl;
102         this.rendererListenerImpl = rendererListenerImpl;
103         this.serviceHandlerListenerImpl = serviceHandlerListenerImpl;
104         this.notificationService = notificationService;
105     }
106
107     /**
108      * Method called when the blueprint container is created.
109      */
110     public void init() {
111         LOG.info("TapiProvider Session Initiated");
112         TapiContext tapiContext = new TapiContext(this.networkTransactionService);
113         LOG.info("Empty TAPI context created: {}", tapiContext.getTapiContext());
114         TapiLink tapiLink = new TapiLink(this.networkTransactionService);
115         TopologyUtils topologyUtils = new TopologyUtils(this.networkTransactionService, this.dataBroker, tapiLink);
116         ConnectivityUtils connectivityUtils = new ConnectivityUtils(this.serviceDataStoreOperations, new HashMap<>(),
117                 tapiContext);
118         TapiInitialORMapping tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
119                 tapiContext, this.serviceDataStoreOperations);
120         tapiInitialORMapping.performTopoInitialMapping();
121         tapiInitialORMapping.performServInitialMapping();
122
123         TapiConnectivityImpl tapi = new TapiConnectivityImpl(this.serviceHandler, tapiContext, connectivityUtils,
124                 pceListenerImpl, rendererListenerImpl, serviceHandlerListenerImpl);
125         TapiTopologyImpl topo = new TapiTopologyImpl(this.dataBroker, tapiContext, topologyUtils, tapiLink);
126         rpcRegistration = rpcProviderService.registerRpcImplementation(TapiConnectivityService.class, tapi);
127         rpcProviderService.registerRpcImplementation(TapiTopologyService.class, topo);
128         rpcProviderService.registerRpcImplementation(TapiCommonService.class, topo);
129         dataTreeChangeListenerRegistration =
130             dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
131                 InstanceIdentifiers.NETCONF_TOPOLOGY_II.child(Node.class)), topologyListener);
132         mappingListenerListenerRegistration =
133             dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
134                 MAPPING_II), tapiPortMappingListener);
135         tapiNetworkutilsServiceRpcRegistration =
136                 rpcProviderService.registerRpcImplementation(TransportpceTapinetworkutilsService.class,
137                         this.tapiNetworkUtils);
138         @NonNull
139         InstanceIdentifier<ServiceInterfacePoints> sipIID = InstanceIdentifier.create(ServiceInterfacePoints.class);
140         dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(
141             LogicalDatastoreType.CONFIGURATION, sipIID), tapiListener);
142         // Notification Listener
143         pcelistenerRegistration = notificationService.registerNotificationListener(pceListenerImpl);
144         rendererlistenerRegistration = notificationService.registerNotificationListener(rendererListenerImpl);
145         servicehandlerlistenerRegistration =
146                 notificationService.registerNotificationListener(serviceHandlerListenerImpl);
147     }
148
149     /**
150      * Method called when the blueprint container is destroyed.
151      */
152     public void close() {
153         LOG.info("TapiProvider Session Closed");
154         if (dataTreeChangeListenerRegistration != null) {
155             dataTreeChangeListenerRegistration.close();
156         }
157         if (mappingListenerListenerRegistration != null) {
158             mappingListenerListenerRegistration.close();
159         }
160         if (tapiNetworkutilsServiceRpcRegistration != null) {
161             tapiNetworkutilsServiceRpcRegistration.close();
162         }
163         pcelistenerRegistration.close();
164         rendererlistenerRegistration.close();
165         servicehandlerlistenerRegistration.close();
166         rpcRegistration.close();
167     }
168 }