Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cluster.impl; |
| 17 | |
| 18 | import static java.net.NetworkInterface.getNetworkInterfaces; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.net.Inet4Address; |
| 22 | import java.net.InetAddress; |
| 23 | import java.net.NetworkInterface; |
| 24 | import java.util.Collections; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.atomic.AtomicReference; |
| 27 | import java.util.function.Function; |
| 28 | |
| 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.onlab.packet.IpAddress; |
| 35 | import org.onosproject.cluster.ClusterMetadata; |
| 36 | import org.onosproject.cluster.ClusterMetadataProvider; |
| 37 | import org.onosproject.cluster.ClusterMetadataProviderRegistry; |
| 38 | import org.onosproject.cluster.ControllerNode; |
| 39 | import org.onosproject.cluster.DefaultControllerNode; |
| 40 | import org.onosproject.cluster.DefaultPartition; |
| 41 | import org.onosproject.cluster.NodeId; |
| 42 | import org.onosproject.cluster.Partition; |
| 43 | import org.onosproject.cluster.PartitionId; |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 44 | import org.onosproject.core.VersionService; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 45 | import org.onosproject.net.provider.ProviderId; |
| 46 | import org.onosproject.store.service.Versioned; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Provider of default {@link ClusterMetadata cluster metadata}. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | public 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 Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected VersionService versionService; |
| 64 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 65 | 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 Yuta | 4ad9df0 | 2016-05-20 14:29:15 -0700 | [diff] [blame] | 71 | private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>(); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 72 | |
| 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 Jampani | 597282a | 2016-06-04 09:23:17 -0700 | [diff] [blame] | 78 | // partition 1 |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 79 | Partition partition = new DefaultPartition( |
| 80 | PartitionId.from(1), |
| 81 | versionService.version(), |
| 82 | ImmutableSet.of(localNode.id())); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 83 | ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID, |
| 84 | "default", |
| 85 | ImmutableSet.of(localNode), |
Madan Jampani | 597282a | 2016-06-04 09:23:17 -0700 | [diff] [blame] | 86 | ImmutableSet.of(partition)); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 87 | 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 | } |