blob: 184998a14bc4e696cd9909dcea232c1630f4db93 [file] [log] [blame]
Jian Lie2a04ce2020-07-01 19:07:02 +09001/*
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 */
16package org.onosproject.k8snode.impl;
17
18import org.apache.commons.net.util.SubnetUtils;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21import org.onlab.util.KryoNamespace;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.k8snode.api.ExternalNetworkService;
25import org.onosproject.store.serializers.KryoNamespaces;
26import org.onosproject.store.service.ConsistentMap;
27import org.onosproject.store.service.Serializer;
28import org.onosproject.store.service.StorageService;
29import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
34import org.slf4j.Logger;
35
36import java.util.Arrays;
37import java.util.Set;
38import java.util.concurrent.ExecutorService;
39import java.util.stream.Collectors;
40
41import static java.util.concurrent.Executors.newSingleThreadExecutor;
42import static org.onlab.util.Tools.groupedThreads;
43import static org.onosproject.k8snode.api.K8sNodeService.APP_ID;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * External network service implementation.
48 */
49@Component(
50 immediate = true,
51 service = { ExternalNetworkService.class }
52)
53public 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}