blob: 030fa896b1ddade65f64c07c61d1b6793c289aab [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;
49import com.google.common.collect.Sets;
50
51/**
52 * Provider of default {@link ClusterMetadata cluster metadata}.
53 */
54@Component(immediate = true)
55public class DefaultClusterMetadataProvider implements ClusterMetadataProvider {
56
57 private final Logger log = getLogger(getClass());
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected ClusterMetadataProviderRegistry providerRegistry;
61
62 private static final String ONOS_IP = "ONOS_IP";
63 private static final String ONOS_INTERFACE = "ONOS_INTERFACE";
64 private static final String ONOS_ALLOW_IPV6 = "ONOS_ALLOW_IPV6";
65 private static final String DEFAULT_ONOS_INTERFACE = "eth0";
66 private static final int DEFAULT_ONOS_PORT = 9876;
67 private static final ProviderId PROVIDER_ID = new ProviderId("default", "none");
68 private AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>();
69
70 @Activate
71 public void activate() {
72 String localIp = getSiteLocalAddress();
73 ControllerNode localNode =
74 new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
75 // p0 partition
76 Partition basePartition = new DefaultPartition(PartitionId.from(0), Sets.newHashSet(localNode.id()));
77 // p1 partition
78 Partition extendedPartition = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(localNode.id()));
79 ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID,
80 "default",
81 ImmutableSet.of(localNode),
82 ImmutableSet.of(basePartition, extendedPartition));
83 long version = System.currentTimeMillis();
84 cachedMetadata.set(new Versioned<>(metadata, version));
85 providerRegistry.register(this);
86 log.info("Started");
87 }
88
89 @Deactivate
90 public void deactivate() {
91 providerRegistry.unregister(this);
92 log.info("Stopped");
93 }
94
95 @Override
96 public ProviderId id() {
97 return PROVIDER_ID;
98 }
99
100 @Override
101 public Versioned<ClusterMetadata> getClusterMetadata() {
102 return cachedMetadata.get();
103 }
104
105 @Override
106 public void setClusterMetadata(ClusterMetadata metadata) {
107 throw new UnsupportedOperationException();
108 }
109
110 @Override
111 public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
112 throw new UnsupportedOperationException();
113 }
114
115 @Override
116 public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
117 throw new UnsupportedOperationException();
118 }
119
120 @Override
121 public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) {
122 throw new UnsupportedOperationException();
123 }
124
125 @Override
126 public boolean isAvailable() {
127 return true;
128 }
129
130 private static String getSiteLocalAddress() {
131
132 /*
133 * If the IP ONOS should use is set via the environment variable we will assume it is valid and should be used.
134 * Setting the IP address takes presidence over setting the interface via the environment.
135 */
136 String useOnosIp = System.getenv(ONOS_IP);
137 if (useOnosIp != null) {
138 return useOnosIp;
139 }
140
141 // Read environment variables for IP interface information or set to default
142 String useOnosInterface = System.getenv(ONOS_INTERFACE);
143 if (useOnosInterface == null) {
144 useOnosInterface = DEFAULT_ONOS_INTERFACE;
145 }
146
147 // Capture if they want to limit IP address selection to only IPv4 (default).
148 boolean allowIPv6 = (System.getenv(ONOS_ALLOW_IPV6) != null);
149
150 Function<NetworkInterface, IpAddress> ipLookup = nif -> {
151 IpAddress fallback = null;
152
153 // nif can be null if the interface name specified doesn't exist on the node's host
154 if (nif != null) {
155 for (InetAddress address : Collections.list(nif.getInetAddresses())) {
156 if (address.isSiteLocalAddress() && (allowIPv6 || address instanceof Inet4Address)) {
157 return IpAddress.valueOf(address);
158 }
159 if (fallback == null && !address.isLoopbackAddress() && !address.isMulticastAddress()
160 && (allowIPv6 || address instanceof Inet4Address)) {
161 fallback = IpAddress.valueOf(address);
162 }
163 }
164 }
165 return fallback;
166 };
167 try {
168 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface));
169 if (ip != null) {
170 return ip.toString();
171 }
172 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) {
173 if (!nif.getName().equals(useOnosInterface)) {
174 ip = ipLookup.apply(nif);
175 if (ip != null) {
176 return ip.toString();
177 }
178 }
179 }
180 } catch (Exception e) {
181 throw new IllegalStateException("Unable to get network interfaces", e);
182 }
183
184 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString();
185 }
186}