b4a7b51d071fb39658e8e20ea8391f780884389f
[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.rev230728.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.rev230728.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.Registration;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.osgi.service.component.annotations.Activate;
54 import org.osgi.service.component.annotations.Component;
55 import org.osgi.service.component.annotations.Deactivate;
56 import org.osgi.service.component.annotations.Reference;
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 @Component
67 public class TapiProvider {
68
69     private static final Logger LOG = LoggerFactory.getLogger(TapiProvider.class);
70
71     private static final InstanceIdentifier<Nodes> MAPPING_II = InstanceIdentifier.create(Network.class)
72         .child(org.opendaylight.yang.gen.v1.http
73             .org.opendaylight.transportpce.portmapping.rev220922.network.Nodes.class);
74     private static final InstanceIdentifier<Link> LINK_II = InstanceIdentifier.create(Networks.class).child(
75         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network.class,
76             new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).augmentation(Network1.class)
77         .child(Link.class);
78     private final DataBroker dataBroker;
79     private final NetworkTransactionService networkTransactionService;
80     private final OrgOpenroadmServiceService serviceHandler;
81     private final ServiceDataStoreOperations serviceDataStoreOperations;
82     private List<Registration> listeners;
83     private List<Registration> rpcRegistrations = new ArrayList<>();
84     private Registration pcelistenerRegistration;
85     private Registration rendererlistenerRegistration;
86     private Registration servicehandlerlistenerRegistration;
87     private Registration tapinetworkmodellistenerRegistration;
88
89     @Activate
90     public TapiProvider(@Reference DataBroker dataBroker,
91             @Reference RpcProviderService rpcProviderService,
92             @Reference NotificationService notificationService,
93             @Reference NotificationPublishService notificationPublishService,
94             @Reference NetworkTransactionService networkTransactionService,
95             @Reference OrgOpenroadmServiceService serviceHandler,
96             @Reference ServiceDataStoreOperations serviceDataStoreOperations,
97             @Reference TapiNetworkModelNotificationHandler tapiNetworkModelNotificationHandler,
98             @Reference TapiNetworkModelService tapiNetworkModelServiceImpl) {
99         this.dataBroker = dataBroker;
100         this.networkTransactionService = networkTransactionService;
101         this.serviceHandler = serviceHandler;
102         this.serviceDataStoreOperations = serviceDataStoreOperations;
103         LOG.info("TapiProvider Session Initiated");
104         TapiContext tapiContext = new TapiContext(this.networkTransactionService);
105         LOG.info("Empty TAPI context created: {}", tapiContext.getTapiContext());
106         TapiLink tapiLink = new TapiLinkImpl(this.networkTransactionService);
107         TopologyUtils topologyUtils = new TopologyUtils(this.networkTransactionService, this.dataBroker, tapiLink);
108         ConnectivityUtils connectivityUtils = new ConnectivityUtils(this.serviceDataStoreOperations, new HashMap<>(),
109                 tapiContext, this.networkTransactionService);
110         TapiInitialORMapping tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
111                 tapiContext, this.serviceDataStoreOperations);
112         tapiInitialORMapping.performTopoInitialMapping();
113         tapiInitialORMapping.performServInitialMapping();
114         TapiPceNotificationHandler pceListenerImpl = new TapiPceNotificationHandler(dataBroker);
115         TapiRendererNotificationHandler rendererListenerImpl = new TapiRendererNotificationHandler(dataBroker,
116                 notificationPublishService);
117
118         TapiConnectivityImpl tapi = new TapiConnectivityImpl(this.serviceHandler, tapiContext, connectivityUtils,
119                 pceListenerImpl, rendererListenerImpl, networkTransactionService);
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 }