Refactor (un)registration of device listeners
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / dto / NodeRegistration.java
1 /*
2  * Copyright © 2016 AT&T 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.networkmodel.dto;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.transportpce.common.StringConstants;
15 import org.opendaylight.transportpce.common.mapping.PortMapping;
16 import org.opendaylight.transportpce.networkmodel.listeners.AlarmNotificationListener;
17 import org.opendaylight.transportpce.networkmodel.listeners.AlarmNotificationListener221;
18 import org.opendaylight.transportpce.networkmodel.listeners.DeOperationsListener;
19 import org.opendaylight.transportpce.networkmodel.listeners.DeOperationsListener221;
20 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener121;
21 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener221;
22 import org.opendaylight.transportpce.networkmodel.listeners.DeviceListener710;
23 import org.opendaylight.transportpce.networkmodel.listeners.TcaListener;
24 import org.opendaylight.transportpce.networkmodel.listeners.TcaListener221;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev161014.OrgOpenroadmDeOperationsListener;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceListener;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class NodeRegistration {
33     private static final Logger LOG = LoggerFactory.getLogger(NodeRegistration.class);
34     private final String nodeId;
35     private final String nodeVersion;
36     private final NotificationService notificationService;
37     private final DataBroker dataBroker;
38     private final PortMapping portMapping;
39     private final List<ListenerRegistration<?>> listeners;
40
41     public NodeRegistration(String nodeId, String nodeVersion, NotificationService notificationService,
42             DataBroker dataBroker, PortMapping portMapping) {
43         this.nodeId = nodeId;
44         this.nodeVersion = nodeVersion;
45         this.notificationService = notificationService;
46         this.dataBroker = dataBroker;
47         this.portMapping = portMapping;
48         listeners = new ArrayList<ListenerRegistration<?>>();
49     }
50
51     public void registerListeners() {
52         switch (this.nodeVersion) {
53             case StringConstants.OPENROADM_DEVICE_VERSION_1_2_1:
54                 registerListeners121();
55                 break;
56             case StringConstants.OPENROADM_DEVICE_VERSION_2_2_1:
57                 registerListeners221();
58                 break;
59             case StringConstants.OPENROADM_DEVICE_VERSION_7_1_0:
60                 registerListeners710();
61                 break;
62             default:
63                 LOG.debug("Unable to register listeners - unknown device version for {}", this.nodeId);
64                 break;
65         }
66     }
67
68     public void unregisterListeners() {
69         LOG.info("Unregistering notification listeners for node: {}", this.nodeId);
70         for (ListenerRegistration<?> listenerRegistration : listeners) {
71             listenerRegistration.close();
72         }
73     }
74
75     private void registerListeners121() {
76         OrgOpenroadmAlarmListener alarmListener = new AlarmNotificationListener(this.dataBroker);
77         LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
78         listeners.add(notificationService.registerNotificationListener(alarmListener));
79
80         OrgOpenroadmDeOperationsListener deOperationsListener = new DeOperationsListener();
81         LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
82         listeners.add(notificationService.registerNotificationListener(deOperationsListener));
83
84         OrgOpenroadmDeviceListener deviceListener = new DeviceListener121(nodeId, this.portMapping);
85         LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
86         listeners.add(notificationService.registerNotificationListener(deviceListener));
87
88         TcaListener tcaListener = new TcaListener();
89         LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
90         listeners.add(notificationService.registerNotificationListener(tcaListener));
91     }
92
93     private void registerListeners221() {
94         AlarmNotificationListener221 alarmListener = new AlarmNotificationListener221(dataBroker);
95         LOG.info("Registering notification listener on OrgOpenroadmAlarmListener for node: {}", nodeId);
96         listeners.add(notificationService.registerNotificationListener(alarmListener));
97
98         DeOperationsListener221 deOperationsListener = new DeOperationsListener221();
99         LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
100         listeners.add(notificationService.registerNotificationListener(deOperationsListener));
101
102         DeviceListener221 deviceListener = new DeviceListener221(nodeId, this.portMapping);
103         LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
104         listeners.add(notificationService.registerNotificationListener(deviceListener));
105
106         TcaListener221 tcaListener = new TcaListener221();
107         LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
108         listeners.add(notificationService.registerNotificationListener(tcaListener));
109     }
110
111     private void registerListeners710() {
112         DeviceListener710 deviceListener = new DeviceListener710(nodeId, this.portMapping);
113         LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
114         listeners.add(notificationService.registerNotificationListener(deviceListener));
115     }
116 }