Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.impl; |
| 17 | |
| 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.onosproject.event.ListenerRegistry; |
| 22 | import org.onosproject.k8snetworking.api.DefaultK8sIpam; |
| 23 | import org.onosproject.k8snetworking.api.K8sIpam; |
| 24 | import org.onosproject.k8snetworking.api.K8sIpamAdminService; |
| 25 | import org.onosproject.k8snetworking.api.K8sIpamEvent; |
| 26 | import org.onosproject.k8snetworking.api.K8sIpamListener; |
| 27 | import org.onosproject.k8snetworking.api.K8sIpamService; |
| 28 | import org.onosproject.k8snetworking.api.K8sIpamStore; |
| 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.Set; |
| 37 | import java.util.stream.Collectors; |
| 38 | |
| 39 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Provides implementation of administering and interfacing kubernetes IPAM. |
| 44 | */ |
| 45 | @Component( |
| 46 | immediate = true, |
| 47 | service = {K8sIpamAdminService.class, K8sIpamService.class} |
| 48 | ) |
| 49 | public class K8sIpamManager |
| 50 | extends ListenerRegistry<K8sIpamEvent, K8sIpamListener> |
| 51 | implements K8sIpamAdminService, K8sIpamService { |
| 52 | |
| 53 | protected final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 56 | protected CoreService coreService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 59 | protected K8sIpamStore k8sIpamStore; |
| 60 | |
| 61 | private ApplicationId appId; |
| 62 | |
| 63 | @Activate |
| 64 | protected void activate() { |
| 65 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 66 | |
| 67 | log.info("Started"); |
| 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | protected void deactivate() { |
| 72 | log.info("Stopped"); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public IpAddress allocateIp(String networkId) { |
| 77 | IpAddress availableIp = availableIps(networkId).stream() |
| 78 | .findFirst().orElse(null); |
| 79 | if (availableIp != null) { |
| 80 | String ipamId = networkId + "-" + availableIp.toString(); |
| 81 | k8sIpamStore.removeAvailableIp(ipamId); |
| 82 | k8sIpamStore.createAllocatedIp( |
| 83 | new DefaultK8sIpam(ipamId, availableIp, networkId)); |
| 84 | |
| 85 | log.info("Allocate a new IP {}", availableIp.toString()); |
| 86 | |
| 87 | return availableIp; |
| 88 | } else { |
| 89 | log.warn("No IPs are available for allocating."); |
| 90 | } |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | @Override |
Jian Li | 7970b71 | 2019-05-03 20:58:21 +0900 | [diff] [blame] | 95 | public void reserveIp(String networkId, IpAddress ipAddress) { |
| 96 | if (!allocatedIps(networkId).contains(ipAddress)) { |
| 97 | String ipamId = networkId + "-" + ipAddress.toString(); |
| 98 | k8sIpamStore.removeAvailableIp(ipamId); |
| 99 | k8sIpamStore.createAllocatedIp( |
| 100 | new DefaultK8sIpam(ipamId, ipAddress, networkId)); |
| 101 | |
| 102 | log.info("Reserved the IP {}", ipAddress.toString()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 107 | public boolean releaseIp(String networkId, IpAddress ipAddress) { |
| 108 | IpAddress releasedIp = allocatedIps(networkId).stream() |
| 109 | .filter(ip -> ip.equals(ipAddress)) |
| 110 | .findFirst().orElse(null); |
| 111 | if (releasedIp != null) { |
| 112 | String ipamId = networkId + "-" + releasedIp.toString(); |
| 113 | k8sIpamStore.removeAllocatedIp(ipamId); |
| 114 | k8sIpamStore.createAvailableIp( |
| 115 | new DefaultK8sIpam(ipamId, releasedIp, networkId)); |
| 116 | |
| 117 | log.info("Release the IP {}", releasedIp.toString()); |
| 118 | |
| 119 | return true; |
| 120 | } else { |
| 121 | log.warn("Failed to find requested IP {} for releasing...", ipAddress.toString()); |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void initializeIpPool(String networkId, Set<IpAddress> ipAddresses) { |
| 129 | ipAddresses.forEach(ip -> { |
| 130 | String ipamId = networkId + "-" + ip; |
| 131 | K8sIpam ipam = new DefaultK8sIpam(ipamId, ip, networkId); |
| 132 | k8sIpamStore.createAvailableIp(ipam); |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void purgeIpPool(String networkId) { |
| 138 | k8sIpamStore.clear(networkId); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Set<IpAddress> allocatedIps(String networkId) { |
| 143 | return k8sIpamStore.allocatedIps().stream() |
| 144 | .filter(i -> i.networkId().equals(networkId)) |
| 145 | .map(K8sIpam::ipAddress).collect(Collectors.toSet()); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Set<IpAddress> availableIps(String networkId) { |
| 150 | return k8sIpamStore.availableIps().stream() |
| 151 | .filter(i -> i.networkId().equals(networkId)) |
| 152 | .map(K8sIpam::ipAddress).collect(Collectors.toSet()); |
| 153 | } |
| 154 | } |