2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.yang.bmp.impl;
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;
32 public class BmpMonitorImplModule extends org.opendaylight.controller.config.yang.bmp.impl.AbstractBmpMonitorImplModule {
34 private static final Logger LOG = LoggerFactory.getLogger(BmpMonitorImplModule.class);
36 private static final int PRIVILEGED_PORTS = 1024;
38 private BundleContext bundleContext;
40 public BmpMonitorImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
41 super(identifier, dependencyResolver);
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);
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();
53 return address.getIpv6Address().getValue();
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);
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));
72 return ret.isEmpty() ? Optional.<KeyMapping>absent() : Optional.<KeyMapping>of(ret);
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.");
85 public java.lang.AutoCloseable createInstance() {
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);
96 private SchemaContext getSchemaProvider() {
97 if (getDomDataProviderDependency() instanceof SchemaContextProvider) {
98 return ((SchemaContextProvider) getDomDataProviderDependency()).getSchemaContext();
100 final ServiceReference<SchemaService> serviceRef = this.bundleContext.getServiceReference(SchemaService.class);
101 return this.bundleContext.getService(serviceRef).getGlobalContext();
104 public void setBundleContext(final BundleContext bundleContext) {
105 this.bundleContext = bundleContext;