blob: de07093d922c23c36d1c1445827fb01da392f818 [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);
Madan Jampani597282a2016-06-04 09:23:17 -070074 // partition 1
75 Partition partition = new DefaultPartition(PartitionId.from(1), ImmutableSet.of(localNode.id()));
Madan Jampaniad3c5262016-01-20 00:50:17 -080076 ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID,
77 "default",
78 ImmutableSet.of(localNode),
Madan Jampani597282a2016-06-04 09:23:17 -070079 ImmutableSet.of(partition));
Madan Jampaniad3c5262016-01-20 00:50:17 -080080 long version = System.currentTimeMillis();
81 cachedMetadata.set(new Versioned<>(metadata, version));
82 providerRegistry.register(this);
83 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
88 providerRegistry.unregister(this);
89 log.info("Stopped");
90 }
91
92 @Override
93 public ProviderId id() {
94 return PROVIDER_ID;
95 }
96
97 @Override
98 public Versioned<ClusterMetadata> getClusterMetadata() {
99 return cachedMetadata.get();
100 }
101
102 @Override
103 public void setClusterMetadata(ClusterMetadata metadata) {
104 throw new UnsupportedOperationException();
105 }
106
107 @Override
108 public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
109 throw new UnsupportedOperationException();
110 }
111
112 @Override
113 public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
114 throw new UnsupportedOperationException();
115 }
116
117 @Override
118 public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) {
119 throw new UnsupportedOperationException();
120 }
121
122 @Override
123 public boolean isAvailable() {
124 return true;
125 }
126
127 private static String getSiteLocalAddress() {
128
129 /*
130 * If the IP ONOS should use is set via the environment variable we will assume it is valid and should be used.
131 * Setting the IP address takes presidence over setting the interface via the environment.
132 */
133 String useOnosIp = System.getenv(ONOS_IP);
134 if (useOnosIp != null) {
135 return useOnosIp;
136 }
137
138 // Read environment variables for IP interface information or set to default
139 String useOnosInterface = System.getenv(ONOS_INTERFACE);
140 if (useOnosInterface == null) {
141 useOnosInterface = DEFAULT_ONOS_INTERFACE;
142 }
143
144 // Capture if they want to limit IP address selection to only IPv4 (default).
145 boolean allowIPv6 = (System.getenv(ONOS_ALLOW_IPV6) != null);
146
147 Function<NetworkInterface, IpAddress> ipLookup = nif -> {
148 IpAddress fallback = null;
149
150 // nif can be null if the interface name specified doesn't exist on the node's host
151 if (nif != null) {
152 for (InetAddress address : Collections.list(nif.getInetAddresses())) {
153 if (address.isSiteLocalAddress() && (allowIPv6 || address instanceof Inet4Address)) {
154 return IpAddress.valueOf(address);
155 }
156 if (fallback == null && !address.isLoopbackAddress() && !address.isMulticastAddress()
157 && (allowIPv6 || address instanceof Inet4Address)) {
158 fallback = IpAddress.valueOf(address);
159 }
160 }
161 }
162 return fallback;
163 };
164 try {
165 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface));
166 if (ip != null) {
167 return ip.toString();
168 }
169 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) {
170 if (!nif.getName().equals(useOnosInterface)) {
171 ip = ipLookup.apply(nif);
172 if (ip != null) {
173 return ip.toString();
174 }
175 }
176 }
177 } catch (Exception e) {
178 throw new IllegalStateException("Unable to get network interfaces", e);
179 }
180
181 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString();
182 }
183}