* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.protocol.pcep;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
import java.net.InetAddress;
-import java.util.HashMap;
+import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.Immutable;
+
+public final class SpeakerIdMapping implements Immutable {
+ private static final @NonNull SpeakerIdMapping EMPTY = new SpeakerIdMapping(ImmutableMap.of());
+
+ private final ImmutableMap<InetAddress, byte[]> map;
-public final class SpeakerIdMapping extends HashMap<InetAddress, byte[]> {
- private static final long serialVersionUID = 1L;
+ private SpeakerIdMapping(final Map<InetAddress, byte[]> map) {
+ this.map = ImmutableMap.copyOf(map);
+ }
+
+ public static @NonNull SpeakerIdMapping of() {
+ return EMPTY;
+ }
- private SpeakerIdMapping() {
- super();
+ public static @NonNull SpeakerIdMapping copyOf(final Map<InetAddress, byte[]> map) {
+ return map.isEmpty() ? of()
+ // Defensive: disconnect byte[]s from caller
+ : new SpeakerIdMapping(Maps.transformValues(map, byte[]::clone));
}
- public static SpeakerIdMapping getSpeakerIdMap() {
- return new SpeakerIdMapping();
+ public byte @Nullable [] speakerIdForAddress(final InetAddress address) {
+ final byte[] found = map.get(address);
+ // Defensive: do not leak byte[]
+ return found == null ? null : found.clone();
}
}
\ No newline at end of file
private static final long SLEEP_FOR = 50;
private final int port = InetSocketAddressUtil.getRandomPort();
final InetSocketAddress remoteAddress = InetSocketAddressUtil
- .getRandomLoopbackInetSocketAddress(this.port);
+ .getRandomLoopbackInetSocketAddress(port);
final InetSocketAddress localAddress = InetSocketAddressUtil
- .getRandomLoopbackInetSocketAddress(this.port);
+ .getRandomLoopbackInetSocketAddress(port);
PCCSessionListener pccSessionListener;
private PCEPDispatcher pceDispatcher;
private final PCEPExtensionProviderContext extensionProvider = new SimplePCEPExtensionProviderContext();
ServiceLoader.load(PCEPExtensionProviderActivator.class).forEach(act -> act.start(extensionProvider));
- this.messageRegistry = this.extensionProvider.getMessageHandlerRegistry();
- this.pceDispatcher = new PCEPDispatcherImpl(this.messageRegistry, nf, new NioEventLoopGroup(),
+ messageRegistry = extensionProvider.getMessageHandlerRegistry();
+ pceDispatcher = new PCEPDispatcherImpl(messageRegistry, nf, new NioEventLoopGroup(),
new NioEventLoopGroup());
}
serverAddress2, final PCEPPeerProposal peerProposal) {
final StatefulActivator activator07 = new StatefulActivator();
final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
- activator07.start(this.extensionProvider);
- optimizationsActivator.start(this.extensionProvider);
+ activator07.start(extensionProvider);
+ optimizationsActivator.start(extensionProvider);
- final ChannelFuture future = this.pceDispatcher
+ final ChannelFuture future = pceDispatcher
.createServer(new DispatcherDependencies(serverAddress2, factory, peerProposal));
waitFutureSuccess(future);
return future.channel();
}
Future<PCEPSession> createPCCSession(final Uint64 dbVersion) {
- final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(this.messageRegistry);
+ final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(messageRegistry);
final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
- final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, this.localAddress.getAddress(),
+ final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, localAddress.getAddress(),
0, -1, new HashedWheelTimer(), Optional.empty());
- return pccDispatcher.createClient(this.remoteAddress, -1, () -> {
- this.pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
- return this.pccSessionListener;
- }, snf, KeyMapping.getKeyMapping(), this.localAddress, dbVersion);
+ return pccDispatcher.createClient(remoteAddress, -1, () -> {
+ pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
+ return pccSessionListener;
+ }, snf, KeyMapping.getKeyMapping(), localAddress, dbVersion);
}
private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
}
TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
- return checkSessionListenerNotNull(factory, this.localAddress.getHostString());
+ return checkSessionListenerNotNull(factory, localAddress.getHostString());
}
- private class DispatcherDependencies implements PCEPDispatcherDependencies {
+ private static class DispatcherDependencies implements PCEPDispatcherDependencies {
final KeyMapping keys = KeyMapping.getKeyMapping();
- final SpeakerIdMapping ids = SpeakerIdMapping.getSpeakerIdMap();
private final InetSocketAddress address;
private final TestingSessionListenerFactory listenerFactory;
private final PCEPPeerProposal peerProposal;
@Override
public InetSocketAddress getAddress() {
- return this.address;
+ return address;
}
@Override
@Override
public SpeakerIdMapping getSpeakerIdMapping() {
- return ids;
+ return SpeakerIdMapping.of();
}
@Override
public PCEPSessionListenerFactory getListenerFactory() {
- return this.listenerFactory;
+ return listenerFactory;
}
@Override
public PCEPPeerProposal getPeerProposal() {
- return this.peerProposal;
+ return peerProposal;
}
}
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.protocol.pcep.testtool;
import java.net.InetSocketAddress;
private final PCEPSessionListenerFactory listenerFactory = new TestingSessionListenerFactory();
private final InetSocketAddress address;
private final KeyMapping keys = KeyMapping.getKeyMapping();
- private final SpeakerIdMapping speakerIds = SpeakerIdMapping.getSpeakerIdMap();
TestToolPCEPDispatcherDependencies(final InetSocketAddress address) {
this.address = address;
@Override
public InetSocketAddress getAddress() {
- return this.address;
+ return address;
}
@Override
public KeyMapping getKeys() {
- return this.keys;
+ return keys;
}
@Override
public SpeakerIdMapping getSpeakerIdMapping() {
- return this.speakerIds;
+ return SpeakerIdMapping.of();
}
@Override
public PCEPSessionListenerFactory getListenerFactory() {
- return this.listenerFactory;
+ return listenerFactory;
}
}
@Override
public void setPeerSpecificProposal(final InetSocketAddress address, final TlvsBuilder openBuilder) {
requireNonNull(address);
- final byte[] speakerId = pcepDispatcherDependencies.getSpeakerIdMapping().get(address.getAddress());
- peerProposal.setPeerProposal(createNodeId(address.getAddress()), openBuilder, speakerId);
+ peerProposal.setPeerProposal(createNodeId(address.getAddress()), openBuilder,
+ pcepDispatcherDependencies.getSpeakerIdMapping().speakerIdForAddress(address.getAddress()));
}
short getRpcTimeout() {
import static java.util.Objects.requireNonNull;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.net.InetAddresses;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
+import java.util.Map;
import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.protocol.concepts.KeyMapping;
import org.opendaylight.protocol.pcep.SpeakerIdMapping;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.config.rev200120.pcep.config.SessionConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.config.rev181109.PcepNodeConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.sync.optimizations.config.rev181109.PcepNodeSyncConfig;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
public final class PCEPTopologyConfiguration {
private final KeyMapping keys;
private final TopologyId topologyId;
private final short rpcTimeout;
- private final SpeakerIdMapping speakerIds;
+ private final @NonNull SpeakerIdMapping speakerIds;
private final InstanceIdentifier<Topology> topology;
public PCEPTopologyConfiguration(final @NonNull SessionConfig config, final @NonNull Topology topology) {
requireNonNull(topology);
- this.address = PCEPTopologyProviderUtil.getInetSocketAddress(requireNonNull(config.getListenAddress()),
+ address = PCEPTopologyProviderUtil.getInetSocketAddress(requireNonNull(config.getListenAddress()),
requireNonNull(config.getListenPort()));
- this.keys = requireNonNull(PCEPTopologyProviderUtil.contructKeys(topology));
- this.speakerIds = requireNonNull(PCEPTopologyProviderUtil.contructSpeakersId(topology));
- this.topologyId = requireNonNull(topology.getTopologyId());
- this.rpcTimeout = config.getRpcTimeout();
+ keys = requireNonNull(PCEPTopologyProviderUtil.contructKeys(topology));
+ speakerIds = contructSpeakersId(topology.getNode());
+ topologyId = requireNonNull(topology.getTopologyId());
+ rpcTimeout = config.getRpcTimeout();
this.topology = InstanceIdentifier.builder(NetworkTopology.class)
- .child(Topology.class, new TopologyKey(this.topologyId)).build();
+ .child(Topology.class, new TopologyKey(topologyId)).build();
}
public @NonNull TopologyId getTopologyId() {
- return this.topologyId;
+ return topologyId;
}
public @NonNull InstanceIdentifier<Topology> getTopology() {
- return this.topology;
+ return topology;
}
public short getRpcTimeout() {
- return this.rpcTimeout;
+ return rpcTimeout;
}
public @NonNull InetSocketAddress getAddress() {
- return this.address;
+ return address;
}
public @NonNull KeyMapping getKeys() {
- return this.keys;
+ return keys;
}
public @NonNull SpeakerIdMapping getSpeakerIds() {
- return this.speakerIds;
+ return speakerIds;
+ }
+
+ private static @NonNull SpeakerIdMapping contructSpeakersId(final @Nullable Map<NodeKey, Node> nodes) {
+ if (nodes == null) {
+ return SpeakerIdMapping.of();
+ }
+
+ final var builder = ImmutableMap.<InetAddress, byte[]>builder();
+ for (var node : nodes.values()) {
+ if (node != null) {
+ final var nodeConfig = node.augmentation(PcepNodeConfig.class);
+ if (nodeConfig != null) {
+ final var sessionConfig = nodeConfig.getSessionConfig();
+ if (sessionConfig != null) {
+ final var nodeSyncConfig = sessionConfig.augmentation(PcepNodeSyncConfig.class);
+ if (nodeSyncConfig != null) {
+ final var speakerEntityId = nodeSyncConfig.getSpeakerEntityIdValue();
+ if (speakerEntityId != null) {
+ builder.put(InetAddresses.forString(node.getNodeId().getValue()), speakerEntityId);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return SpeakerIdMapping.copyOf(builder.build());
}
}
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.protocol.concepts.KeyMapping;
-import org.opendaylight.protocol.pcep.SpeakerIdMapping;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.config.rev181109.PcepNodeConfig;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.sync.optimizations.config.rev181109.PcepNodeSyncConfig;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
final class PCEPTopologyProviderUtil {
final @NonNull PortNumber port) {
return new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressForNoZone(address), port.getValue().toJava());
}
-
- static SpeakerIdMapping contructSpeakersId(final Topology topology) {
- final SpeakerIdMapping ret = SpeakerIdMapping.getSpeakerIdMap();
- if (topology.getNode() == null) {
- return ret;
- }
- topology.nonnullNode().values().stream()
- .filter(Objects::nonNull)
- .filter(node -> node.augmentation(PcepNodeConfig.class) != null)
- .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig() != null)
- .filter(node -> node.augmentation(PcepNodeConfig.class).getSessionConfig()
- .augmentation(PcepNodeSyncConfig.class) != null)
- .forEach(node -> {
- final PcepNodeConfig config = node.augmentation(PcepNodeConfig.class);
- final PcepNodeSyncConfig nodeSyncConfig = config.getSessionConfig()
- .augmentation(PcepNodeSyncConfig.class);
- final InetAddress address = InetAddresses.forString(node.getNodeId().getValue());
- ret.put(address, nodeSyncConfig.getSpeakerEntityIdValue());
- });
-
- return ret;
- }
}