Jian Li | 3e81b18 | 2021-01-11 02:35:06 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.impl; |
| 17 | |
| 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.event.ListenerRegistry; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkEvent; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkListener; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService; |
| 29 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkStore; |
| 30 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkStoreDelegate; |
| 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.Set; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkArgument; |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Provides implementation of administering and interfacing kubevirt network. |
| 48 | */ |
| 49 | @Component( |
| 50 | immediate = true, |
| 51 | service = {KubevirtNetworkAdminService.class, KubevirtNetworkService.class} |
| 52 | ) |
| 53 | public class KubevirtNetworkManager |
| 54 | extends ListenerRegistry<KubevirtNetworkEvent, KubevirtNetworkListener> |
| 55 | implements KubevirtNetworkAdminService, KubevirtNetworkService { |
| 56 | |
| 57 | protected final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | private static final String MSG_NETWORK = "Kubernetes network %s %s"; |
| 60 | |
| 61 | private static final String MSG_CREATED = "created"; |
| 62 | private static final String MSG_UPDATED = "updated"; |
| 63 | private static final String MSG_REMOVED = "removed"; |
| 64 | |
| 65 | private static final String ERR_NULL_NETWORK = "Kubernetes network cannot be null"; |
| 66 | private static final String ERR_NULL_NETWORK_ID = "Kubernetes network ID cannot be null"; |
| 67 | |
| 68 | private static final String ERR_IN_USE = " still in use"; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 71 | protected CoreService coreService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 74 | protected KubevirtNetworkStore networkStore; |
| 75 | |
| 76 | private final KubevirtNetworkStoreDelegate delegate = new InternalNetworkStorageDelegate(); |
| 77 | |
| 78 | private ApplicationId appId; |
| 79 | |
| 80 | @Activate |
| 81 | protected void activate() { |
| 82 | appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID); |
| 83 | |
| 84 | networkStore.setDelegate(delegate); |
| 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | protected void deactivate() { |
| 90 | networkStore.unsetDelegate(delegate); |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void createNetwork(KubevirtNetwork network) { |
| 96 | checkNotNull(network, ERR_NULL_NETWORK); |
| 97 | checkArgument(!Strings.isNullOrEmpty(network.networkId()), ERR_NULL_NETWORK_ID); |
| 98 | |
| 99 | networkStore.createNetwork(network); |
| 100 | |
| 101 | log.info(String.format(MSG_NETWORK, network.name(), MSG_CREATED)); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void updateNetwork(KubevirtNetwork network) { |
| 106 | checkNotNull(network, ERR_NULL_NETWORK); |
| 107 | checkArgument(!Strings.isNullOrEmpty(network.networkId()), ERR_NULL_NETWORK_ID); |
| 108 | |
| 109 | networkStore.updateNetwork(network); |
| 110 | |
| 111 | log.info(String.format(MSG_NETWORK, network.networkId(), MSG_UPDATED)); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void removeNetwork(String networkId) { |
| 116 | checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_NETWORK_ID); |
| 117 | |
| 118 | synchronized (this) { |
| 119 | // if (isNetworkInUse(networkId)) { |
| 120 | // final String error = String.format(MSG_NETWORK, networkId, ERR_IN_USE); |
| 121 | // throw new IllegalStateException(error); |
| 122 | // } |
| 123 | KubevirtNetwork network = networkStore.removeNetwork(networkId); |
| 124 | |
| 125 | if (network != null) { |
| 126 | log.info(String.format(MSG_NETWORK, network.name(), MSG_REMOVED)); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void clear() { |
| 133 | networkStore.clear(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public KubevirtNetwork network(String networkId) { |
| 138 | checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_NETWORK_ID); |
| 139 | return networkStore.network(networkId); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Set<KubevirtNetwork> networks() { |
| 144 | return ImmutableSet.copyOf(networkStore.networks()); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Set<KubevirtNetwork> networks(KubevirtNetwork.Type type) { |
| 149 | return ImmutableSet.copyOf(networkStore.networks().stream() |
| 150 | .filter(n -> n.type() == type).collect(Collectors.toSet())); |
| 151 | } |
| 152 | |
| 153 | private class InternalNetworkStorageDelegate implements KubevirtNetworkStoreDelegate { |
| 154 | |
| 155 | @Override |
| 156 | public void notify(KubevirtNetworkEvent event) { |
| 157 | if (event != null) { |
| 158 | log.trace("send kubevirt networking event {}", event); |
| 159 | process(event); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |