Convert TapiLinkImpl into a Component
[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.NetworkUtils;
19 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
20 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
21 import org.opendaylight.transportpce.tapi.connectivity.ConnectivityUtils;
22 import org.opendaylight.transportpce.tapi.connectivity.TapiConnectivityImpl;
23 import org.opendaylight.transportpce.tapi.listeners.TapiNetworkModelListenerImpl;
24 import org.opendaylight.transportpce.tapi.listeners.TapiPceListenerImpl;
25 import org.opendaylight.transportpce.tapi.listeners.TapiRendererListenerImpl;
26 import org.opendaylight.transportpce.tapi.listeners.TapiServiceHandlerListenerImpl;
27 import org.opendaylight.transportpce.tapi.topology.TapiNetconfTopologyListener;
28 import org.opendaylight.transportpce.tapi.topology.TapiOrLinkListener;
29 import org.opendaylight.transportpce.tapi.topology.TapiPortMappingListener;
30 import org.opendaylight.transportpce.tapi.topology.TapiTopologyImpl;
31 import org.opendaylight.transportpce.tapi.topology.TopologyUtils;
32 import org.opendaylight.transportpce.tapi.utils.TapiContext;
33 import org.opendaylight.transportpce.tapi.utils.TapiInitialORMapping;
34 import org.opendaylight.transportpce.tapi.utils.TapiLink;
35 import org.opendaylight.transportpce.tapi.utils.TapiLinkImpl;
36 import org.opendaylight.transportpce.tapi.utils.TapiListener;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.TransportpcePceListener;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.Network;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.network.Nodes;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.TransportpceRendererListener;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.TransportpceServicehandlerListener;
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.rev211210.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.onf.otcc.yang.tapi.common.rev181210.TapiCommonService;
50 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.TapiConnectivityService;
51 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.TapiTopologyService;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.ServiceInterfacePoints;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
54 import org.opendaylight.yangtools.concepts.ListenerRegistration;
55 import org.opendaylight.yangtools.concepts.ObjectRegistration;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * Class to register TAPI interface Service and Notification.
62  *
63  * @author Gilles Thouenon (gilles.thouenon@orange.com) on behalf of Orange
64  *
65  */
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 RpcProviderService rpcProviderService;
79     private ObjectRegistration<TapiConnectivityService> rpcRegistration;
80     private ObjectRegistration<TransportpceTapinetworkutilsService> tapiNetworkutilsServiceRpcRegistration;
81     private ListenerRegistration<TapiNetconfTopologyListener> dataTreeChangeListenerRegistration;
82     private ListenerRegistration<TapiOrLinkListener> dataTreeChangeListenerRegistration1;
83     private ListenerRegistration<TapiPortMappingListener> mappingListenerListenerRegistration;
84     private ListenerRegistration<TransportpcePceListener> pcelistenerRegistration;
85     private ListenerRegistration<TransportpceRendererListener> rendererlistenerRegistration;
86     private ListenerRegistration<TransportpceServicehandlerListener> servicehandlerlistenerRegistration;
87     private ListenerRegistration<TapiNetworkModelListenerImpl> tapinetworkmodellistenerRegistration;
88     private final OrgOpenroadmServiceService serviceHandler;
89     private final ServiceDataStoreOperations serviceDataStoreOperations;
90     private final TapiListener tapiListener;
91     private final TapiNetconfTopologyListener topologyListener;
92     private final TapiOrLinkListener orLinkListener;
93     private TapiPortMappingListener tapiPortMappingListener;
94     private final NetworkTransactionService networkTransactionService;
95     private final TransportpceTapinetworkutilsService tapiNetworkUtils;
96     private TapiPceListenerImpl pceListenerImpl;
97     private TapiRendererListenerImpl rendererListenerImpl;
98     private TapiServiceHandlerListenerImpl serviceHandlerListenerImpl;
99     private final NotificationService notificationService;
100     private TapiNetworkModelListenerImpl tapiNetworkModelListenerImpl;
101
102     public TapiProvider(DataBroker dataBroker, RpcProviderService rpcProviderService,
103             OrgOpenroadmServiceService serviceHandler,ServiceDataStoreOperations serviceDataStoreOperations,
104             TapiListener tapiListener, NetworkTransactionService networkTransactionService,
105             TapiNetconfTopologyListener topologyListener, TapiPortMappingListener tapiPortMappingListener,
106             TransportpceTapinetworkutilsService tapiNetworkUtils, TapiPceListenerImpl pceListenerImpl,
107             TapiRendererListenerImpl rendererListenerImpl, TapiServiceHandlerListenerImpl serviceHandlerListenerImpl,
108             NotificationService notificationService, TapiOrLinkListener orLinkListener,
109             TapiNetworkModelListenerImpl tapiNetworkModelListenerImpl) {
110         this.dataBroker = dataBroker;
111         this.rpcProviderService = rpcProviderService;
112         this.serviceHandler = serviceHandler;
113         this.serviceDataStoreOperations = serviceDataStoreOperations;
114         this.tapiListener = tapiListener;
115         this.networkTransactionService = networkTransactionService;
116         this.topologyListener = topologyListener;
117         this.tapiPortMappingListener = tapiPortMappingListener;
118         this.tapiNetworkUtils = tapiNetworkUtils;
119         this.pceListenerImpl = pceListenerImpl;
120         this.rendererListenerImpl = rendererListenerImpl;
121         this.serviceHandlerListenerImpl = serviceHandlerListenerImpl;
122         this.notificationService = notificationService;
123         this.orLinkListener = orLinkListener;
124         this.tapiNetworkModelListenerImpl = tapiNetworkModelListenerImpl;
125         //this.notificationPublishService = notificationPublishService;
126     }
127
128     /**
129      * Method called when the blueprint container is created.
130      */
131     public void init() {
132         LOG.info("TapiProvider Session Initiated");
133         TapiContext tapiContext = new TapiContext(this.networkTransactionService);
134         LOG.info("Empty TAPI context created: {}", tapiContext.getTapiContext());
135         TapiLink tapiLink = new TapiLinkImpl(this.networkTransactionService);
136         TopologyUtils topologyUtils = new TopologyUtils(this.networkTransactionService, this.dataBroker, tapiLink);
137         ConnectivityUtils connectivityUtils = new ConnectivityUtils(this.serviceDataStoreOperations, new HashMap<>(),
138                 tapiContext, this.networkTransactionService);
139         TapiInitialORMapping tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
140                 tapiContext, this.serviceDataStoreOperations);
141         tapiInitialORMapping.performTopoInitialMapping();
142         tapiInitialORMapping.performServInitialMapping();
143
144         TapiConnectivityImpl tapi = new TapiConnectivityImpl(this.serviceHandler, tapiContext, connectivityUtils,
145                 pceListenerImpl, rendererListenerImpl);
146         TapiTopologyImpl topo = new TapiTopologyImpl(this.dataBroker, tapiContext, topologyUtils, tapiLink);
147
148         rpcRegistration = rpcProviderService.registerRpcImplementation(TapiConnectivityService.class, tapi);
149         rpcProviderService.registerRpcImplementation(TapiTopologyService.class, topo);
150         rpcProviderService.registerRpcImplementation(TapiCommonService.class, topo);
151         dataTreeChangeListenerRegistration1 =
152             dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
153                 LINK_II), orLinkListener);
154         dataTreeChangeListenerRegistration =
155             dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
156                 InstanceIdentifiers.NETCONF_TOPOLOGY_II.child(Node.class)), topologyListener);
157         mappingListenerListenerRegistration =
158             dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
159                 MAPPING_II), tapiPortMappingListener);
160         tapiNetworkutilsServiceRpcRegistration =
161                 rpcProviderService.registerRpcImplementation(TransportpceTapinetworkutilsService.class,
162                         this.tapiNetworkUtils);
163         @NonNull
164         InstanceIdentifier<ServiceInterfacePoints> sipIID = InstanceIdentifier.create(ServiceInterfacePoints.class);
165         dataBroker.registerDataTreeChangeListener(DataTreeIdentifier.create(
166             LogicalDatastoreType.CONFIGURATION, sipIID), tapiListener);
167         // Notification Listener
168         pcelistenerRegistration = notificationService.registerNotificationListener(pceListenerImpl);
169         rendererlistenerRegistration = notificationService.registerNotificationListener(rendererListenerImpl);
170         servicehandlerlistenerRegistration =
171                 notificationService.registerNotificationListener(serviceHandlerListenerImpl);
172         tapinetworkmodellistenerRegistration =
173                 notificationService.registerNotificationListener(tapiNetworkModelListenerImpl);
174     }
175
176     /**
177      * Method called when the blueprint container is destroyed.
178      */
179     public void close() {
180         LOG.info("TapiProvider Session Closed");
181         if (dataTreeChangeListenerRegistration != null) {
182             dataTreeChangeListenerRegistration.close();
183         }
184         if (mappingListenerListenerRegistration != null) {
185             mappingListenerListenerRegistration.close();
186         }
187         if (dataTreeChangeListenerRegistration1 != null) {
188             dataTreeChangeListenerRegistration1.close();
189         }
190         if (tapiNetworkutilsServiceRpcRegistration != null) {
191             tapiNetworkutilsServiceRpcRegistration.close();
192         }
193         pcelistenerRegistration.close();
194         rendererlistenerRegistration.close();
195         servicehandlerlistenerRegistration.close();
196         rpcRegistration.close();
197         tapinetworkmodellistenerRegistration.close();
198     }
199 }