]> git.basschouten.com Git - openhab-addons.git/blob
ef9360b28fd53f0c93f3367a958128b5d29d4ab6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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         if (transport != null) {
104             transport.close();
105             transport = null;
106         }
107         if (snmp != null) {
108             snmp.close();
109             snmp = null;
110         }
111     }
112
113     @Override
114     public void addCommandResponder(CommandResponder listener) {
115         if (snmp != null) {
116             snmp.addCommandResponder(listener);
117         }
118         listeners.add(listener);
119     }
120
121     @Override
122     public void removeCommandResponder(CommandResponder listener) {
123         if (snmp != null) {
124             snmp.removeCommandResponder(listener);
125         }
126         listeners.remove(listener);
127     }
128
129     @Override
130     public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseListener listener)
131             throws IOException {
132         if (snmp != null) {
133             snmp.send(pdu, target, userHandle, listener);
134             logger.trace("send {} to {}", pdu, target);
135         } else {
136             logger.warn("SNMP service not initialized, can't send {} to {}", pdu, target);
137         }
138     }
139 }