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.protocol.bmp.impl.app;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bmp.impl.app.KeyConstructorUtil.constructKeys;
14 import com.google.common.base.Preconditions;
15 import com.google.common.net.InetAddresses;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelFutureListener;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.util.List;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
28 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
29 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
30 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
31 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
32 import org.opendaylight.protocol.bmp.impl.config.BmpDeployerDependencies;
33 import org.opendaylight.protocol.bmp.impl.spi.BmpMonitoringStation;
34 import org.opendaylight.protocol.concepts.KeyMapping;
35 import org.opendaylight.protocol.util.Ipv4Util;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev180329.odl.bmp.monitors.bmp.monitor.config.MonitoredRouter;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.BmpMonitor;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.MonitorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.bmp.monitor.Monitor;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.routers.Router;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public final class BmpMonitoringStationImpl implements BmpMonitoringStation, ClusterSingletonService {
50 private static final Logger LOG = LoggerFactory.getLogger(BmpMonitoringStationImpl.class);
52 private static final QName MONITOR_ID_QNAME = QName.create(Monitor.QNAME, "monitor-id").intern();
53 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
54 ServiceGroupIdentifier.create("bmp-monitors-service-group");
56 private final DOMDataBroker domDataBroker;
57 private final InetSocketAddress address;
58 private final MonitorId monitorId;
59 private final List<MonitoredRouter> monitoredRouters;
60 private final BmpDispatcher dispatcher;
61 private final RouterSessionManager sessionManager;
62 private final YangInstanceIdentifier yangMonitorId;
63 private Channel channel;
64 private ClusterSingletonServiceRegistration singletonServiceRegistration;
66 public BmpMonitoringStationImpl(final BmpDeployerDependencies bmpDeployerDependencies,
67 final BmpDispatcher dispatcher, final MonitorId monitorId, final InetSocketAddress address,
68 final List<MonitoredRouter> mrs) {
69 this.domDataBroker = requireNonNull(bmpDeployerDependencies.getDomDataBroker());
70 this.dispatcher = requireNonNull(dispatcher);
71 this.monitorId = monitorId;
72 this.monitoredRouters = mrs;
73 this.address = requireNonNull(address);
75 this.yangMonitorId = YangInstanceIdentifier.builder()
76 .node(BmpMonitor.QNAME).node(Monitor.QNAME)
77 .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, monitorId.getValue()).build();
79 this.sessionManager = new RouterSessionManager(this.yangMonitorId, this.domDataBroker,
80 bmpDeployerDependencies.getExtensions(), bmpDeployerDependencies.getTree());
82 LOG.info("BMP Monitor Singleton Service {} registered, Monitor Id {}",
83 getIdentifier().getValue(), this.monitorId.getValue());
84 this.singletonServiceRegistration = bmpDeployerDependencies.getClusterSingletonProvider()
85 .registerClusterSingletonService(this);
89 public synchronized void instantiateServiceInstance() {
90 LOG.info("BMP Monitor Singleton Service {} instantiated, Monitor Id {}",
91 getIdentifier().getValue(), this.monitorId.getValue());
93 final ChannelFuture channelFuture = this.dispatcher.createServer(this.address, this.sessionManager,
94 constructKeys(this.monitoredRouters));
96 this.channel = channelFuture.sync().channel();
98 LOG.info("BMP Monitoring station {} started", this.monitorId.getValue());
100 connectMonitoredRouters(this.dispatcher);
101 LOG.info("Connecting to monitored routers completed.");
102 } catch (final InterruptedException e) {
103 LOG.error("Failed to instantiate BMP Monitor Singleton {}", this.monitorId.getValue(), e);
109 public synchronized ListenableFuture<Void> closeServiceInstance() {
110 LOG.info("BMP Monitor Singleton Service {} instance closed, Monitor Id {}",
111 getIdentifier().getValue(), this.monitorId.getValue());
112 if (this.channel != null) {
113 this.channel.close().addListener((ChannelFutureListener) future -> {
114 Preconditions.checkArgument(future.isSuccess(),
115 "Channel failed to close: %s", future.cause());
116 BmpMonitoringStationImpl.this.sessionManager.close();
120 final DOMDataWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
121 wTx.delete(LogicalDatastoreType.OPERATIONAL, this.yangMonitorId);
122 LOG.info("BMP monitoring station {} closed.", this.monitorId.getValue());
128 public ServiceGroupIdentifier getIdentifier() {
129 return SERVICE_GROUP_IDENTIFIER;
132 private void connectMonitoredRouters(final BmpDispatcher pdispatcher) {
133 if (this.monitoredRouters != null) {
134 for (final MonitoredRouter mr : this.monitoredRouters) {
136 requireNonNull(mr.getAddress());
137 requireNonNull(mr.getPort());
138 final String s = mr.getAddress().getIpv4Address().getValue();
139 final InetAddress addr = InetAddresses.forString(s);
140 final KeyMapping ret;
141 final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
142 ret = KeyMapping.getKeyMapping(addr, rfc2385KeyPassword.getValue());
143 pdispatcher.createClient(Ipv4Util.toInetSocketAddress(mr.getAddress(), mr.getPort()),
144 this.sessionManager, ret);
150 private synchronized void createEmptyMonitor() {
151 final DOMDataWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
152 wTx.put(LogicalDatastoreType.OPERATIONAL,
153 YangInstanceIdentifier.builder().node(BmpMonitor.QNAME).node(Monitor.QNAME)
154 .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, this.monitorId.getValue()).build(),
155 ImmutableNodes.mapEntryBuilder(Monitor.QNAME, MONITOR_ID_QNAME, this.monitorId.getValue())
156 .addChild(ImmutableNodes.leafNode(MONITOR_ID_QNAME, this.monitorId.getValue()))
157 .addChild(ImmutableNodes.mapNodeBuilder(Router.QNAME).build())
160 wTx.submit().checkedGet();
161 } catch (final TransactionCommitFailedException e) {
162 LOG.error("Failed to initiate BMP Monitor {}.", this.monitorId.getValue(), e);
167 public synchronized void close() throws Exception {
168 if (this.singletonServiceRegistration != null) {
169 this.singletonServiceRegistration.close();
170 this.singletonServiceRegistration = null;