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.FluentFuture;
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 java.util.concurrent.ExecutionException;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.common.api.CommitInfo;
29 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
31 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
32 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
33 import org.opendaylight.protocol.bmp.impl.config.BmpDeployerDependencies;
34 import org.opendaylight.protocol.bmp.impl.spi.BmpMonitoringStation;
35 import org.opendaylight.protocol.concepts.KeyMapping;
36 import org.opendaylight.protocol.util.Ipv4Util;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev180329.odl.bmp.monitors.bmp.monitor.config.MonitoredRouter;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.BmpMonitor;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.MonitorId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.bmp.monitor.Monitor;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev180329.routers.Router;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public final class BmpMonitoringStationImpl implements BmpMonitoringStation, ClusterSingletonService {
51 private static final Logger LOG = LoggerFactory.getLogger(BmpMonitoringStationImpl.class);
53 private static final QName MONITOR_ID_QNAME = QName.create(Monitor.QNAME, "monitor-id").intern();
54 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
55 ServiceGroupIdentifier.create("bmp-monitors-service-group");
57 private final DOMDataBroker domDataBroker;
58 private final InetSocketAddress address;
59 private final MonitorId monitorId;
60 private final List<MonitoredRouter> monitoredRouters;
61 private final BmpDispatcher dispatcher;
62 private final RouterSessionManager sessionManager;
63 private final YangInstanceIdentifier yangMonitorId;
64 private Channel channel;
65 private ClusterSingletonServiceRegistration singletonServiceRegistration;
67 public BmpMonitoringStationImpl(final BmpDeployerDependencies bmpDeployerDependencies,
68 final BmpDispatcher dispatcher, final MonitorId monitorId, final InetSocketAddress address,
69 final List<MonitoredRouter> mrs) {
70 this.domDataBroker = requireNonNull(bmpDeployerDependencies.getDomDataBroker());
71 this.dispatcher = requireNonNull(dispatcher);
72 this.monitorId = monitorId;
73 this.monitoredRouters = mrs;
74 this.address = requireNonNull(address);
76 this.yangMonitorId = YangInstanceIdentifier.builder()
77 .node(BmpMonitor.QNAME).node(Monitor.QNAME)
78 .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, monitorId.getValue()).build();
80 this.sessionManager = new RouterSessionManager(this.yangMonitorId, this.domDataBroker,
81 bmpDeployerDependencies.getExtensions(), bmpDeployerDependencies.getTree());
83 LOG.info("BMP Monitor Singleton Service {} registered, Monitor Id {}",
84 getIdentifier().getValue(), this.monitorId.getValue());
85 this.singletonServiceRegistration = bmpDeployerDependencies.getClusterSingletonProvider()
86 .registerClusterSingletonService(this);
90 public synchronized void instantiateServiceInstance() {
91 LOG.info("BMP Monitor Singleton Service {} instantiated, Monitor Id {}",
92 getIdentifier().getValue(), this.monitorId.getValue());
94 final ChannelFuture channelFuture = this.dispatcher.createServer(this.address, this.sessionManager,
95 constructKeys(this.monitoredRouters));
97 this.channel = channelFuture.sync().channel();
99 LOG.info("BMP Monitoring station {} started", this.monitorId.getValue());
101 connectMonitoredRouters(this.dispatcher);
102 LOG.info("Connecting to monitored routers completed.");
103 } catch (final InterruptedException e) {
104 LOG.error("Failed to instantiate BMP Monitor Singleton {}", this.monitorId.getValue(), e);
110 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
111 LOG.info("BMP Monitor Singleton Service {} instance closed, Monitor Id {}",
112 getIdentifier().getValue(), this.monitorId.getValue());
113 if (this.channel != null) {
114 this.channel.close().addListener((ChannelFutureListener) future -> {
115 Preconditions.checkArgument(future.isSuccess(),
116 "Channel failed to close: %s", future.cause());
117 BmpMonitoringStationImpl.this.sessionManager.close();
121 final DOMDataWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
122 wTx.delete(LogicalDatastoreType.OPERATIONAL, this.yangMonitorId);
123 LOG.info("BMP monitoring station {} closed.", this.monitorId.getValue());
129 public ServiceGroupIdentifier getIdentifier() {
130 return SERVICE_GROUP_IDENTIFIER;
133 private void connectMonitoredRouters(final BmpDispatcher pdispatcher) {
134 if (this.monitoredRouters != null) {
135 for (final MonitoredRouter mr : this.monitoredRouters) {
137 requireNonNull(mr.getAddress());
138 requireNonNull(mr.getPort());
139 final String s = mr.getAddress().getIpv4Address().getValue();
140 final InetAddress addr = InetAddresses.forString(s);
141 final KeyMapping ret;
142 final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
143 ret = KeyMapping.getKeyMapping(addr, rfc2385KeyPassword.getValue());
144 pdispatcher.createClient(Ipv4Util.toInetSocketAddress(mr.getAddress(), mr.getPort()),
145 this.sessionManager, ret);
151 private synchronized void createEmptyMonitor() {
152 final DOMDataWriteTransaction wTx = this.domDataBroker.newWriteOnlyTransaction();
153 wTx.put(LogicalDatastoreType.OPERATIONAL,
154 YangInstanceIdentifier.builder().node(BmpMonitor.QNAME).node(Monitor.QNAME)
155 .nodeWithKey(Monitor.QNAME, MONITOR_ID_QNAME, this.monitorId.getValue()).build(),
156 ImmutableNodes.mapEntryBuilder(Monitor.QNAME, MONITOR_ID_QNAME, this.monitorId.getValue())
157 .addChild(ImmutableNodes.leafNode(MONITOR_ID_QNAME, this.monitorId.getValue()))
158 .addChild(ImmutableNodes.mapNodeBuilder(Router.QNAME).build())
162 } catch (final ExecutionException | InterruptedException e) {
163 LOG.error("Failed to initiate BMP Monitor {}.", this.monitorId.getValue(), e);
168 public synchronized void close() throws Exception {
169 if (this.singletonServiceRegistration != null) {
170 this.singletonServiceRegistration.close();
171 this.singletonServiceRegistration = null;