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