]> git.basschouten.com Git - openhab-addons.git/blob
2aa9b55805e21780cae1ead3f6b7ff7cfbd8a005
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.snmp.internal;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.snmp.internal.config.SnmpServiceConfiguration;
23 import org.openhab.core.config.core.Configuration;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Deactivate;
27 import org.osgi.service.component.annotations.Modified;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.snmp4j.CommandResponder;
31 import org.snmp4j.PDU;
32 import org.snmp4j.Snmp;
33 import org.snmp4j.Target;
34 import org.snmp4j.event.ResponseListener;
35 import org.snmp4j.security.Priv3DES;
36 import org.snmp4j.security.SecurityProtocols;
37 import org.snmp4j.smi.UdpAddress;
38 import org.snmp4j.transport.DefaultUdpTransportMapping;
39
40 /**
41  * The {@link SnmpServiceImpl} implements SnmpService
42  * handlers.
43  *
44  * @author Jan N. Klug - Initial contribution
45  */
46
47 @NonNullByDefault
48 @Component(configurationPid = "binding.snmp", service = SnmpService.class)
49 public class SnmpServiceImpl implements SnmpService {
50     private final Logger logger = LoggerFactory.getLogger(SnmpServiceImpl.class);
51
52     private @NonNullByDefault({}) SnmpServiceConfiguration config;
53     private @Nullable Snmp snmp;
54     private @Nullable DefaultUdpTransportMapping transport;
55
56     private List<CommandResponder> listeners = new ArrayList<>();
57
58     @Activate
59     public SnmpServiceImpl(Map<String, Object> config) {
60         modified(config);
61     }
62
63     @Modified
64     protected void modified(Map<String, Object> config) {
65         this.config = new Configuration(config).as(SnmpServiceConfiguration.class);
66         try {
67             shutdownSnmp();
68
69             final DefaultUdpTransportMapping transport;
70
71             if (this.config.port > 0) {
72                 transport = new DefaultUdpTransportMapping(new UdpAddress(this.config.port), true);
73             } else {
74                 transport = new DefaultUdpTransportMapping();
75             }
76
77             SecurityProtocols.getInstance().addDefaultProtocols();
78             SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
79
80             final Snmp snmp = new Snmp(transport);
81             listeners.forEach(listener -> snmp.addCommandResponder(listener));
82             snmp.listen();
83
84             this.snmp = snmp;
85             this.transport = transport;
86
87             logger.debug("initialized SNMP at {}", transport.getAddress());
88         } catch (IOException e) {
89             logger.warn("could not open SNMP instance on port {}: {}", this.config.port, e.getMessage());
90         }
91     }
92
93     @Deactivate
94     public void deactivate() {
95         try {
96             shutdownSnmp();
97         } catch (IOException e) {
98             logger.info("could not end SNMP: {}", e.getMessage());
99         }
100     }
101
102     private void shutdownSnmp() throws IOException {
103         DefaultUdpTransportMapping transport = this.transport;
104         if (transport != null) {
105             transport.close();
106             this.transport = null;
107         }
108         Snmp snmp = this.snmp;
109         if (snmp != null) {
110             snmp.close();
111             this.snmp = null;
112         }
113     }
114
115     @Override
116     public void addCommandResponder(CommandResponder listener) {
117         Snmp snmp = this.snmp;
118         if (snmp != null) {
119             snmp.addCommandResponder(listener);
120         }
121         listeners.add(listener);
122     }
123
124     @Override
125     public void removeCommandResponder(CommandResponder listener) {
126         Snmp snmp = this.snmp;
127         if (snmp != null) {
128             snmp.removeCommandResponder(listener);
129         }
130         listeners.remove(listener);
131     }
132
133     @Override
134     public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseListener listener)
135             throws IOException {
136         Snmp snmp = this.snmp;
137         if (snmp != null) {
138             snmp.send(pdu, target, userHandle, listener);
139             logger.trace("send {} to {}", pdu, target);
140         } else {
141             logger.warn("SNMP service not initialized, can't send {} to {}", pdu, target);
142         }
143     }
144 }