66c144c1b85b9b8da490f860a7e5219d92b17c90
[bgpcep.git] / bgp / bmp-impl / src / main / java / org / opendaylight / controller / config / yang / bmp / impl / BmpMonitorImplModule.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.controller.config.yang.bmp.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.net.InetAddresses;
14 import io.netty.util.internal.PlatformDependent;
15 import java.nio.charset.StandardCharsets;
16 import java.security.AccessControlException;
17 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.protocol.bmp.impl.app.BmpMonitoringStationImpl;
20 import org.opendaylight.protocol.concepts.KeyMapping;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.MonitorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class BmpMonitorImplModule extends org.opendaylight.controller.config.yang.bmp.impl.AbstractBmpMonitorImplModule {
33
34     private static final Logger LOG = LoggerFactory.getLogger(BmpMonitorImplModule.class);
35
36     private static final int PRIVILEGED_PORTS = 1024;
37
38     private BundleContext bundleContext;
39
40     public BmpMonitorImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
41         super(identifier, dependencyResolver);
42     }
43
44     public BmpMonitorImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final org.opendaylight.controller.config.yang.bmp.impl.BmpMonitorImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
45         super(identifier, dependencyResolver, oldModule, oldInstance);
46     }
47
48     private String getAddressString(final IpAddress address) {
49         Preconditions.checkArgument(address.getIpv4Address() != null || address.getIpv6Address() != null, "Address %s is invalid", address);
50         if (address.getIpv4Address() != null) {
51             return address.getIpv4Address().getValue();
52         }
53         return address.getIpv6Address().getValue();
54     }
55
56     private Optional<KeyMapping> constructKeys() {
57         final KeyMapping ret = KeyMapping.getKeyMapping();
58         if (getMonitoredRouter() != null) {
59             for (final MonitoredRouter mr : getMonitoredRouter()) {
60                 if (mr.getAddress() == null) {
61                     LOG.warn("Monitored router {} does not have an address skipping it", mr);
62                     continue;
63                 }
64                 final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
65                 if (rfc2385KeyPassword != null && !rfc2385KeyPassword.getValue().isEmpty()) {
66                     final String s = getAddressString(mr.getAddress());
67                     ret.put(InetAddresses.forString(s), rfc2385KeyPassword.getValue().getBytes(StandardCharsets.US_ASCII));
68                 }
69             }
70         }
71
72         return ret.isEmpty() ? Optional.<KeyMapping>absent() : Optional.<KeyMapping>of(ret);
73     }
74
75     @Override
76     public void customValidation() {
77         JmxAttributeValidationException.checkNotNull(getBindingPort(), bindingPortJmxAttribute);
78         // check if unix root user
79         if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && getBindingPort().getValue() < PRIVILEGED_PORTS) {
80             throw new AccessControlException("Unable to bind port " + getBindingPort().getValue() + " while running as non-root user.");
81         }
82     }
83
84     @Override
85     public java.lang.AutoCloseable createInstance() {
86         try {
87             return BmpMonitoringStationImpl.createBmpMonitorInstance(getExtensionsDependency(), getBmpDispatcherDependency(),
88                     getDomDataProviderDependency(), new MonitorId(getIdentifier().getInstanceName()),
89                     Ipv4Util.toInetSocketAddress(getBindingAddress(), getBindingPort()),
90                     constructKeys(), getCodecTreeFactoryDependency(), getSchemaProvider(), getMonitoredRouter());
91         } catch(final InterruptedException e) {
92             throw new IllegalStateException("Failed to istantiate BMP application.", e);
93         }
94     }
95
96     private SchemaContext getSchemaProvider() {
97         if (getDomDataProviderDependency() instanceof SchemaContextProvider) {
98             return ((SchemaContextProvider) getDomDataProviderDependency()).getSchemaContext();
99         }
100         final ServiceReference<SchemaService> serviceRef = this.bundleContext.getServiceReference(SchemaService.class);
101         return this.bundleContext.getService(serviceRef).getGlobalContext();
102     }
103
104     public void setBundleContext(final BundleContext bundleContext) {
105         this.bundleContext = bundleContext;
106     }
107
108 }