blob: 73aba4a6c25a28ed597e6493c869d7bff608c344 [file] [log] [blame]
Madan Jampaniad3c5262016-01-20 00:50:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampaniad3c5262016-01-20 00:50:17 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cluster.impl;
17
18import static java.net.NetworkInterface.getNetworkInterfaces;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.net.Inet4Address;
22import java.net.InetAddress;
23import java.net.NetworkInterface;
24import java.util.Collections;
25import java.util.Set;
26import java.util.concurrent.atomic.AtomicReference;
27import java.util.function.Function;
28
29import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
32import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
34import org.onlab.packet.IpAddress;
35import org.onosproject.cluster.ClusterMetadata;
36import org.onosproject.cluster.ClusterMetadataProvider;
37import org.onosproject.cluster.ClusterMetadataProviderRegistry;
38import org.onosproject.cluster.ControllerNode;
39import org.onosproject.cluster.DefaultControllerNode;
40import org.onosproject.cluster.DefaultPartition;
41import org.onosproject.cluster.NodeId;
42import org.onosproject.cluster.Partition;
43import org.onosproject.cluster.PartitionId;
44import org.onosproject.net.provider.ProviderId;
45import org.onosproject.store.service.Versioned;
46import org.slf4j.Logger;
47
48import com.google.common.collect.ImmutableSet;
Madan Jampaniad3c5262016-01-20 00:50:17 -080049
50/**
51 * Provider of default {@link ClusterMetadata cluster metadata}.
52 */
53@Component(immediate = true)
54public class DefaultClusterMetadataProvider implements ClusterMetadataProvider {
55
56 private final Logger log = getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ClusterMetadataProviderRegistry providerRegistry;
60
61 private static final String ONOS_IP = "ONOS_IP";
62 private static final String ONOS_INTERFACE = "ONOS_INTERFACE";
63 private static final String ONOS_ALLOW_IPV6 = "ONOS_ALLOW_IPV6";
64 private static final String DEFAULT_ONOS_INTERFACE = "eth0";
65 private static final int DEFAULT_ONOS_PORT = 9876;
66 private static final ProviderId PROVIDER_ID = new ProviderId("default", "none");
HIGUCHI Yuta4ad9df02016-05-20 14:29:15 -070067 private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>();
Madan Jampaniad3c5262016-01-20 00:50:17 -080068
69 @Activate
70 public void activate() {
71 String localIp = getSiteLocalAddress();
72 ControllerNode localNode =
73 new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
74 // p0 partition
HIGUCHI Yuta4ad9df02016-05-20 14:29:15 -070075 Partition basePartition = new DefaultPartition(PartitionId.from(0), ImmutableSet.of(localNode.id()));
Madan Jampaniad3c5262016-01-20 00:50:17 -080076 // p1 partition
HIGUCHI Yuta4ad9df02016-05-20 14:29:15 -070077 Partition extendedPartition = new DefaultPartition(PartitionId.from(1), ImmutableSet.of(localNode.id()));
Madan Jampaniad3c5262016-01-20 00:50:17 -080078 ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID,
79 "default",
80 ImmutableSet.of(localNode),
81 ImmutableSet.of(basePartition, extendedPartition));
82 long version = System.currentTimeMillis();
83 cachedMetadata.set(new Versioned<>(metadata, version));
84 providerRegistry.register(this);
85 log.info("Started");
86 }
87
88 @Deactivate
89 public void deactivate() {
90 providerRegistry.unregister(this);
91 log.info("Stopped");
92 }
93
94 @Override
95 public ProviderId id() {
96 return PROVIDER_ID;
97 }
98
99 @Override
100 public Versioned<ClusterMetadata> getClusterMetadata() {
101 return cachedMetadata.get();
102 }
103
104 @Override
105 public void setClusterMetadata(ClusterMetadata metadata) {
106 throw new UnsupportedOperationException();
107 }
108
109 @Override
110 public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public boolean isAvailable() {
126 return true;
127 }
128
129 private static String getSiteLocalAddress() {
130
131 /*
132 * If the IP ONOS should use is set via the environment variable we will assume it is valid and should be used.
133 * Setting the IP address takes presidence over setting the interface via the environment.
134 */
135 String useOnosIp = System.getenv(ONOS_IP);
136 if (useOnosIp != null) {
137 return useOnosIp;
138 }
139
140 // Read environment variables for IP interface information or set to default
141 String useOnosInterface = System.getenv(ONOS_INTERFACE);
142 if (useOnosInterface == null) {
143 useOnosInterface = DEFAULT_ONOS_INTERFACE;
144 }
145
146 // Capture if they want to limit IP address selection to only IPv4 (default).
147 boolean allowIPv6 = (System.getenv(ONOS_ALLOW_IPV6) != null);
148
149 Function<NetworkInterface, IpAddress> ipLookup = nif -> {
150 IpAddress fallback = null;
151
152 // nif can be null if the interface name specified doesn't exist on the node's host
153 if (nif != null) {
154 for (InetAddress address : Collections.list(nif.getInetAddresses())) {
155 if (address.isSiteLocalAddress() && (allowIPv6 || address instanceof Inet4Address)) {
156 return IpAddress.valueOf(address);
157 }
158 if (fallback == null && !address.isLoopbackAddress() && !address.isMulticastAddress()
159 && (allowIPv6 || address instanceof Inet4Address)) {
160 fallback = IpAddress.valueOf(address);
161 }
162 }
163 }
164 return fallback;
165 };
166 try {
167 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface));
168 if (ip != null) {
169 return ip.toString();
170 }
171 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) {
172 if (!nif.getName().equals(useOnosInterface)) {
173 ip = ipLookup.apply(nif);
174 if (ip != null) {
175 return ip.toString();
176 }
177 }
178 }
179 } catch (Exception e) {
180 throw new IllegalStateException("Unable to get network interfaces", e);
181 }
182
183 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString();
184 }
185}