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