Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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.k8snode.impl; |
| 17 | |
| 18 | import org.apache.commons.net.util.SubnetUtils; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.util.KryoNamespace; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.k8snode.api.ExternalNetworkService; |
| 25 | import org.onosproject.store.serializers.KryoNamespaces; |
| 26 | import org.onosproject.store.service.ConsistentMap; |
| 27 | import org.onosproject.store.service.Serializer; |
| 28 | import org.onosproject.store.service.StorageService; |
| 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Arrays; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ExecutorService; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 42 | import static org.onlab.util.Tools.groupedThreads; |
| 43 | import static org.onosproject.k8snode.api.K8sNodeService.APP_ID; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * External network service implementation. |
| 48 | */ |
| 49 | @Component( |
| 50 | immediate = true, |
| 51 | service = { ExternalNetworkService.class } |
| 52 | ) |
| 53 | public class ExternalNetworkManager implements ExternalNetworkService { |
| 54 | |
| 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | private static final KryoNamespace |
| 58 | SERIALIZER_EXTERNAL_NETWORK = KryoNamespace.newBuilder() |
| 59 | .register(KryoNamespaces.API) |
| 60 | .build(); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 63 | protected CoreService coreService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 66 | protected StorageService storageService; |
| 67 | |
| 68 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 69 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 70 | |
| 71 | private ConsistentMap<String, Set<String>> networkIpPool; |
| 72 | |
| 73 | @Activate |
| 74 | protected void activate() { |
| 75 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 76 | networkIpPool = storageService.<String, Set<String>>consistentMapBuilder() |
| 77 | .withSerializer(Serializer.using(SERIALIZER_EXTERNAL_NETWORK)) |
| 78 | .withName("external-network-ip-pool") |
| 79 | .withApplicationId(appId) |
| 80 | .build(); |
| 81 | log.info("Started"); |
| 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | protected void deactivate() { |
| 86 | eventExecutor.shutdown(); |
| 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void registerNetwork(IpPrefix cidr) { |
| 92 | if (!networkIpPool.containsKey(cidr.toString())) { |
| 93 | SubnetUtils utils = new SubnetUtils(cidr.toString()); |
| 94 | utils.setInclusiveHostCount(false); |
| 95 | SubnetUtils.SubnetInfo info = utils.getInfo(); |
| 96 | |
| 97 | Set<String> all = Arrays.stream(info.getAllAddresses()) |
| 98 | .collect(Collectors.toSet()); |
| 99 | all.remove(info.getNetworkAddress()); |
| 100 | all.remove(info.getHighAddress()); |
| 101 | all.remove(info.getLowAddress()); |
| 102 | all.remove(info.getBroadcastAddress()); |
| 103 | |
| 104 | networkIpPool.put(cidr.toString(), all); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void unregisterNetwork(IpPrefix cidr) { |
| 110 | if (!networkIpPool.containsKey(cidr.toString())) { |
| 111 | log.warn("The given network {} is not found!", cidr.toString()); |
| 112 | } else { |
| 113 | networkIpPool.remove(cidr.toString()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public IpAddress getGatewayIp(IpPrefix cidr) { |
| 119 | SubnetUtils utils = new SubnetUtils(cidr.toString()); |
| 120 | utils.setInclusiveHostCount(false); |
| 121 | SubnetUtils.SubnetInfo info = utils.getInfo(); |
| 122 | |
| 123 | return IpAddress.valueOf(info.getLowAddress()); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public IpAddress allocateIp(IpPrefix cidr) { |
| 128 | if (!networkIpPool.containsKey(cidr.toString())) { |
| 129 | log.error("The given network {} is not found", cidr.toString()); |
| 130 | return null; |
| 131 | } else { |
| 132 | Set<String> pool = networkIpPool.get(cidr.toString()).value(); |
| 133 | String ipStr = pool.stream().findFirst().orElse(null); |
| 134 | if (ipStr == null) { |
| 135 | log.error("No IPs are found in the given network {}", cidr.toString()); |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | pool.remove(ipStr); |
| 140 | networkIpPool.put(cidr.toString(), pool); |
| 141 | return IpAddress.valueOf(ipStr); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void releaseIp(IpPrefix cidr, IpAddress ip) { |
| 147 | if (!networkIpPool.containsKey(cidr.toString())) { |
| 148 | log.error("The given network {} is not found", cidr.toString()); |
| 149 | } else { |
| 150 | Set<String> pool = networkIpPool.get(cidr.toString()).value(); |
| 151 | pool.add(ip.toString()); |
| 152 | networkIpPool.put(cidr.toString(), pool); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Set<String> getAllIps(IpPrefix cidr) { |
| 158 | return networkIpPool.get(cidr.toString()).value(); |
| 159 | } |
| 160 | } |