blob: a6f24ae67101386b206568e00b4d68fd6c283f8b [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
Madan Jampaniad3c5262016-01-20 00:50:17 -080018import java.net.Inet4Address;
19import java.net.InetAddress;
20import java.net.NetworkInterface;
21import java.util.Collections;
22import java.util.Set;
23import java.util.concurrent.atomic.AtomicReference;
24import java.util.function.Function;
25
Jordan Halterman00e92da2018-05-22 23:05:52 -070026import com.google.common.collect.ImmutableSet;
Madan Jampaniad3c5262016-01-20 00:50:17 -080027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.onlab.packet.IpAddress;
33import org.onosproject.cluster.ClusterMetadata;
34import org.onosproject.cluster.ClusterMetadataProvider;
35import org.onosproject.cluster.ClusterMetadataProviderRegistry;
36import org.onosproject.cluster.ControllerNode;
37import org.onosproject.cluster.DefaultControllerNode;
Madan Jampaniad3c5262016-01-20 00:50:17 -080038import org.onosproject.cluster.NodeId;
Madan Jampaniad3c5262016-01-20 00:50:17 -080039import org.onosproject.cluster.PartitionId;
Jordan Halterman07f052b2017-10-08 14:22:41 -070040import org.onosproject.core.VersionService;
Madan Jampaniad3c5262016-01-20 00:50:17 -080041import org.onosproject.net.provider.ProviderId;
42import org.onosproject.store.service.Versioned;
43import org.slf4j.Logger;
44
Jordan Halterman00e92da2018-05-22 23:05:52 -070045import static java.net.NetworkInterface.getNetworkInterfaces;
46import static org.slf4j.LoggerFactory.getLogger;
Madan Jampaniad3c5262016-01-20 00:50:17 -080047
48/**
49 * Provider of default {@link ClusterMetadata cluster metadata}.
50 */
51@Component(immediate = true)
52public class DefaultClusterMetadataProvider implements ClusterMetadataProvider {
53
54 private final Logger log = getLogger(getClass());
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected ClusterMetadataProviderRegistry providerRegistry;
58
Jordan Halterman07f052b2017-10-08 14:22:41 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected VersionService versionService;
61
Madan Jampaniad3c5262016-01-20 00:50:17 -080062 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");
HIGUCHI Yuta4ad9df02016-05-20 14:29:15 -070068 private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>();
Madan Jampaniad3c5262016-01-20 00:50:17 -080069
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);
Jordan Halterman00e92da2018-05-22 23:05:52 -070075 ClusterMetadata metadata = new ClusterMetadata(
Jordan Halterman19c123a2018-07-30 13:57:19 -070076 PROVIDER_ID, "default", localNode, ImmutableSet.of(), ImmutableSet.of());
Madan Jampaniad3c5262016-01-20 00:50:17 -080077 long version = System.currentTimeMillis();
78 cachedMetadata.set(new Versioned<>(metadata, version));
79 providerRegistry.register(this);
80 log.info("Started");
81 }
82
83 @Deactivate
84 public void deactivate() {
85 providerRegistry.unregister(this);
86 log.info("Stopped");
87 }
88
89 @Override
90 public ProviderId id() {
91 return PROVIDER_ID;
92 }
93
94 @Override
95 public Versioned<ClusterMetadata> getClusterMetadata() {
96 return cachedMetadata.get();
97 }
98
99 @Override
100 public void setClusterMetadata(ClusterMetadata metadata) {
101 throw new UnsupportedOperationException();
102 }
103
104 @Override
105 public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
106 throw new UnsupportedOperationException();
107 }
108
109 @Override
110 public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public boolean isAvailable() {
121 return true;
122 }
123
124 private static String getSiteLocalAddress() {
125
126 /*
127 * If the IP ONOS should use is set via the environment variable we will assume it is valid and should be used.
128 * Setting the IP address takes presidence over setting the interface via the environment.
129 */
130 String useOnosIp = System.getenv(ONOS_IP);
131 if (useOnosIp != null) {
132 return useOnosIp;
133 }
134
135 // Read environment variables for IP interface information or set to default
136 String useOnosInterface = System.getenv(ONOS_INTERFACE);
137 if (useOnosInterface == null) {
138 useOnosInterface = DEFAULT_ONOS_INTERFACE;
139 }
140
141 // Capture if they want to limit IP address selection to only IPv4 (default).
142 boolean allowIPv6 = (System.getenv(ONOS_ALLOW_IPV6) != null);
143
144 Function<NetworkInterface, IpAddress> ipLookup = nif -> {
145 IpAddress fallback = null;
146
147 // nif can be null if the interface name specified doesn't exist on the node's host
148 if (nif != null) {
149 for (InetAddress address : Collections.list(nif.getInetAddresses())) {
150 if (address.isSiteLocalAddress() && (allowIPv6 || address instanceof Inet4Address)) {
151 return IpAddress.valueOf(address);
152 }
153 if (fallback == null && !address.isLoopbackAddress() && !address.isMulticastAddress()
154 && (allowIPv6 || address instanceof Inet4Address)) {
155 fallback = IpAddress.valueOf(address);
156 }
157 }
158 }
159 return fallback;
160 };
161 try {
162 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface));
163 if (ip != null) {
164 return ip.toString();
165 }
166 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) {
167 if (!nif.getName().equals(useOnosInterface)) {
168 ip = ipLookup.apply(nif);
169 if (ip != null) {
170 return ip.toString();
171 }
172 }
173 }
174 } catch (Exception e) {
175 throw new IllegalStateException("Unable to get network interfaces", e);
176 }
177
178 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString();
179 }
180}