Jian Li | 49109b5 | 2019-01-22 00:17:28 +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.k8snode.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.util.Tools; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.LeadershipService; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.event.ListenerRegistry; |
| 26 | import org.onosproject.k8snode.api.K8sNode; |
| 27 | import org.onosproject.k8snode.api.K8sNode.Type; |
| 28 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
| 29 | import org.onosproject.k8snode.api.K8sNodeEvent; |
| 30 | import org.onosproject.k8snode.api.K8sNodeListener; |
| 31 | import org.onosproject.k8snode.api.K8sNodeService; |
| 32 | import org.onosproject.k8snode.api.K8sNodeStore; |
| 33 | import org.onosproject.k8snode.api.K8sNodeStoreDelegate; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.store.service.StorageService; |
| 37 | import org.osgi.service.component.ComponentContext; |
| 38 | import org.osgi.service.component.annotations.Activate; |
| 39 | import org.osgi.service.component.annotations.Component; |
| 40 | import org.osgi.service.component.annotations.Deactivate; |
| 41 | import org.osgi.service.component.annotations.Modified; |
| 42 | import org.osgi.service.component.annotations.Reference; |
| 43 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import java.util.Dictionary; |
| 47 | import java.util.Objects; |
| 48 | import java.util.Set; |
| 49 | import java.util.concurrent.ExecutorService; |
| 50 | import java.util.stream.Collectors; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkArgument; |
| 53 | import static com.google.common.base.Preconditions.checkNotNull; |
| 54 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 55 | import static org.onlab.util.Tools.groupedThreads; |
| 56 | import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE; |
| 57 | import static org.onosproject.k8snode.impl.OsgiPropertyConstants.OVSDB_PORT; |
| 58 | import static org.onosproject.k8snode.impl.OsgiPropertyConstants.OVSDB_PORT_NUM_DEFAULT; |
| 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
| 61 | /** |
| 62 | * Service administering the inventory of kubernetes nodes. |
| 63 | */ |
| 64 | @Component( |
| 65 | immediate = true, |
| 66 | service = { K8sNodeService.class, K8sNodeAdminService.class }, |
| 67 | property = { |
| 68 | OVSDB_PORT + ":Integer=" + OVSDB_PORT_NUM_DEFAULT |
| 69 | } |
| 70 | ) |
| 71 | public class K8sNodeManager |
| 72 | extends ListenerRegistry<K8sNodeEvent, K8sNodeListener> |
| 73 | implements K8sNodeService, K8sNodeAdminService { |
| 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | private static final String MSG_NODE = "Kubernetes node %s %s"; |
| 78 | private static final String MSG_CREATED = "created"; |
| 79 | private static final String MSG_UPDATED = "updated"; |
| 80 | private static final String MSG_REMOVED = "removed"; |
| 81 | |
| 82 | private static final String ERR_NULL_NODE = "Kubernetes node cannot be null"; |
| 83 | private static final String ERR_NULL_HOSTNAME = "Kubernetes node hostname cannot be null"; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 86 | protected K8sNodeStore nodeStore; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 89 | protected CoreService coreService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 92 | protected ClusterService clusterService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 95 | protected LeadershipService leadershipService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 98 | protected StorageService storageService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 101 | protected DeviceService deviceService; |
| 102 | |
| 103 | /** OVSDB server listen port. */ |
| 104 | private int ovsdbPortNum = OVSDB_PORT_NUM_DEFAULT; |
| 105 | |
| 106 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 107 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 108 | |
| 109 | private final K8sNodeStoreDelegate delegate = new K8sNodeManager.InternalNodeStoreDelegate(); |
| 110 | |
| 111 | private ApplicationId appId; |
| 112 | |
| 113 | @Activate |
| 114 | protected void activate() { |
| 115 | appId = coreService.registerApplication(APP_ID); |
| 116 | nodeStore.setDelegate(delegate); |
| 117 | |
| 118 | leadershipService.runForLeadership(appId.name()); |
| 119 | |
| 120 | log.info("Started"); |
| 121 | } |
| 122 | |
| 123 | @Deactivate |
| 124 | protected void deactivate() { |
| 125 | nodeStore.unsetDelegate(delegate); |
| 126 | |
| 127 | leadershipService.withdraw(appId.name()); |
| 128 | eventExecutor.shutdown(); |
| 129 | |
| 130 | log.info("Stopped"); |
| 131 | } |
| 132 | |
| 133 | @Modified |
| 134 | protected void modified(ComponentContext context) { |
| 135 | Dictionary<?, ?> properties = context.getProperties(); |
| 136 | int updatedOvsdbPort = Tools.getIntegerProperty(properties, OVSDB_PORT); |
| 137 | if (!Objects.equals(updatedOvsdbPort, ovsdbPortNum)) { |
| 138 | ovsdbPortNum = updatedOvsdbPort; |
| 139 | } |
| 140 | |
| 141 | log.info("Modified"); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void createNode(K8sNode node) { |
| 146 | checkNotNull(node, ERR_NULL_NODE); |
| 147 | nodeStore.createNode(node); |
| 148 | log.info(String.format(MSG_NODE, node.hostname(), MSG_CREATED)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void updateNode(K8sNode node) { |
| 153 | checkNotNull(node, ERR_NULL_NODE); |
| 154 | nodeStore.updateNode(node); |
| 155 | log.info(String.format(MSG_NODE, node.hostname(), MSG_UPDATED)); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public K8sNode removeNode(String hostname) { |
| 160 | checkArgument(!Strings.isNullOrEmpty(hostname), ERR_NULL_HOSTNAME); |
| 161 | K8sNode node = nodeStore.removeNode(hostname); |
| 162 | log.info(String.format(MSG_NODE, hostname, MSG_REMOVED)); |
| 163 | return node; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Set<K8sNode> nodes() { |
| 168 | return nodeStore.nodes(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Set<K8sNode> nodes(Type type) { |
| 173 | Set<K8sNode> nodes = nodeStore.nodes().stream() |
| 174 | .filter(node -> Objects.equals(node.type(), type)) |
| 175 | .collect(Collectors.toSet()); |
| 176 | return ImmutableSet.copyOf(nodes); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Set<K8sNode> completeNodes() { |
| 181 | Set<K8sNode> nodes = nodeStore.nodes().stream() |
| 182 | .filter(node -> node.state() == COMPLETE) |
| 183 | .collect(Collectors.toSet()); |
| 184 | return ImmutableSet.copyOf(nodes); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Set<K8sNode> completeNodes(Type type) { |
| 189 | Set<K8sNode> nodes = nodeStore.nodes().stream() |
| 190 | .filter(node -> node.type() == type && |
| 191 | node.state() == COMPLETE) |
| 192 | .collect(Collectors.toSet()); |
| 193 | return ImmutableSet.copyOf(nodes); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public K8sNode node(String hostname) { |
| 198 | return nodeStore.node(hostname); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public K8sNode node(DeviceId deviceId) { |
| 203 | return nodeStore.nodes().stream() |
| 204 | .filter(node -> Objects.equals(node.intgBridge(), deviceId) || |
| 205 | Objects.equals(node.ovsdb(), deviceId)) |
| 206 | .findFirst().orElse(null); |
| 207 | } |
| 208 | |
| 209 | private class InternalNodeStoreDelegate implements K8sNodeStoreDelegate { |
| 210 | |
| 211 | @Override |
| 212 | public void notify(K8sNodeEvent event) { |
| 213 | if (event != null) { |
| 214 | log.trace("send kubernetes node event {}", event); |
| 215 | process(event); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |