blob: 83fe78b98382927ab0c1224cab3212255849e71d [file] [log] [blame]
Madan Jampaniad3c5262016-01-20 00:50:17 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Jordan Halterman07f052b2017-10-08 14:22:41 -070044import org.onosproject.core.VersionService;
Madan Jampaniad3c5262016-01-20 00:50:17 -080045import org.onosproject.net.provider.ProviderId;
46import org.onosproject.store.service.Versioned;
47import org.slf4j.Logger;
48
49import com.google.common.collect.ImmutableSet;
Madan Jampaniad3c5262016-01-20 00:50:17 -080050
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
Jordan Halterman07f052b2017-10-08 14:22:41 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected VersionService versionService;
64
Madan Jampaniad3c5262016-01-20 00:50:17 -080065 private static final String ONOS_IP = "ONOS_IP";
66 private static final String ONOS_INTERFACE = "ONOS_INTERFACE";
67 private static final String ONOS_ALLOW_IPV6 = "ONOS_ALLOW_IPV6";
68 private static final String DEFAULT_ONOS_INTERFACE = "eth0";
69 private static final int DEFAULT_ONOS_PORT = 9876;
70 private static final ProviderId PROVIDER_ID = new ProviderId("default", "none");
HIGUCHI Yuta4ad9df02016-05-20 14:29:15 -070071 private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>();
Madan Jampaniad3c5262016-01-20 00:50:17 -080072
73 @Activate
74 public void activate() {
75 String localIp = getSiteLocalAddress();
76 ControllerNode localNode =
77 new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
Madan Jampani597282a2016-06-04 09:23:17 -070078 // partition 1
Jordan Halterman07f052b2017-10-08 14:22:41 -070079 Partition partition = new DefaultPartition(
80 PartitionId.from(1),
81 versionService.version(),
82 ImmutableSet.of(localNode.id()));
Madan Jampaniad3c5262016-01-20 00:50:17 -080083 ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID,
84 "default",
85 ImmutableSet.of(localNode),
Madan Jampani597282a2016-06-04 09:23:17 -070086 ImmutableSet.of(partition));
Madan Jampaniad3c5262016-01-20 00:50:17 -080087 long version = System.currentTimeMillis();
88 cachedMetadata.set(new Versioned<>(metadata, version));
89 providerRegistry.register(this);
90 log.info("Started");
91 }
92
93 @Deactivate
94 public void deactivate() {
95 providerRegistry.unregister(this);
96 log.info("Stopped");
97 }
98
99 @Override
100 public ProviderId id() {
101 return PROVIDER_ID;
102 }
103
104 @Override
105 public Versioned<ClusterMetadata> getClusterMetadata() {
106 return cachedMetadata.get();
107 }
108
109 @Override
110 public void setClusterMetadata(ClusterMetadata metadata) {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) {
126 throw new UnsupportedOperationException();
127 }
128
129 @Override
130 public boolean isAvailable() {
131 return true;
132 }
133
134 private static String getSiteLocalAddress() {
135
136 /*
137 * If the IP ONOS should use is set via the environment variable we will assume it is valid and should be used.
138 * Setting the IP address takes presidence over setting the interface via the environment.
139 */
140 String useOnosIp = System.getenv(ONOS_IP);
141 if (useOnosIp != null) {
142 return useOnosIp;
143 }
144
145 // Read environment variables for IP interface information or set to default
146 String useOnosInterface = System.getenv(ONOS_INTERFACE);
147 if (useOnosInterface == null) {
148 useOnosInterface = DEFAULT_ONOS_INTERFACE;
149 }
150
151 // Capture if they want to limit IP address selection to only IPv4 (default).
152 boolean allowIPv6 = (System.getenv(ONOS_ALLOW_IPV6) != null);
153
154 Function<NetworkInterface, IpAddress> ipLookup = nif -> {
155 IpAddress fallback = null;
156
157 // nif can be null if the interface name specified doesn't exist on the node's host
158 if (nif != null) {
159 for (InetAddress address : Collections.list(nif.getInetAddresses())) {
160 if (address.isSiteLocalAddress() && (allowIPv6 || address instanceof Inet4Address)) {
161 return IpAddress.valueOf(address);
162 }
163 if (fallback == null && !address.isLoopbackAddress() && !address.isMulticastAddress()
164 && (allowIPv6 || address instanceof Inet4Address)) {
165 fallback = IpAddress.valueOf(address);
166 }
167 }
168 }
169 return fallback;
170 };
171 try {
172 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface));
173 if (ip != null) {
174 return ip.toString();
175 }
176 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) {
177 if (!nif.getName().equals(useOnosInterface)) {
178 ip = ipLookup.apply(nif);
179 if (ip != null) {
180 return ip.toString();
181 }
182 }
183 }
184 } catch (Exception e) {
185 throw new IllegalStateException("Unable to get network interfaces", e);
186 }
187
188 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString();
189 }
190}