Jian Li | 7eb2078 | 2021-02-27 01:10:50 +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 | import com.google.common.base.Strings; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.event.ListenerRegistry; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterAdminService; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterListener; |
| 29 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterService; |
| 30 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterStore; |
| 31 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterStoreDelegate; |
| 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.util.Set; |
| 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 router. |
| 48 | */ |
| 49 | @Component( |
| 50 | immediate = true, |
| 51 | service = {KubevirtRouterAdminService.class, KubevirtRouterService.class } |
| 52 | ) |
| 53 | public class KubevirtRouterManager |
| 54 | extends ListenerRegistry<KubevirtRouterEvent, KubevirtRouterListener> |
| 55 | implements KubevirtRouterAdminService, KubevirtRouterService { |
| 56 | |
| 57 | protected final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | private static final String MSG_ROUTER = "Kubevirt router %s %s"; |
| 60 | private static final String MSG_CREATED = "created"; |
| 61 | private static final String MSG_UPDATED = "updated"; |
| 62 | private static final String MSG_REMOVED = "removed"; |
| 63 | |
| 64 | private static final String ERR_NULL_ROUTER = "Kubevirt router cannot be null"; |
| 65 | private static final String ERR_NULL_ROUTER_NAME = "Kubevirt router name cannot be null"; |
| 66 | private static final String ERR_IN_USE = " still in use"; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 69 | protected CoreService coreService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 72 | protected KubevirtRouterStore kubevirtRouterStore; |
| 73 | |
| 74 | private final InternalRouterStorageDelegate delegate = new InternalRouterStorageDelegate(); |
| 75 | |
| 76 | private ApplicationId appId; |
| 77 | |
| 78 | @Activate |
| 79 | protected void activate() { |
| 80 | appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID); |
| 81 | |
| 82 | kubevirtRouterStore.setDelegate(delegate); |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | protected void deactivate() { |
| 88 | kubevirtRouterStore.unsetDelegate(delegate); |
| 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void createRouter(KubevirtRouter router) { |
| 94 | checkNotNull(router, ERR_NULL_ROUTER); |
| 95 | checkArgument(!Strings.isNullOrEmpty(router.name()), ERR_NULL_ROUTER_NAME); |
| 96 | |
| 97 | kubevirtRouterStore.createRouter(router); |
| 98 | log.info(String.format(MSG_ROUTER, router.name(), MSG_CREATED)); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void updateRouter(KubevirtRouter router) { |
| 103 | checkNotNull(router, ERR_NULL_ROUTER); |
| 104 | checkArgument(!Strings.isNullOrEmpty(router.name()), ERR_NULL_ROUTER_NAME); |
| 105 | |
| 106 | kubevirtRouterStore.updateRouter(router); |
| 107 | log.info(String.format(MSG_ROUTER, router.name(), MSG_UPDATED)); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void removeRouter(String name) { |
| 112 | checkArgument(name != null, ERR_NULL_ROUTER_NAME); |
| 113 | synchronized (this) { |
| 114 | if (isRouterInUse(name)) { |
| 115 | final String error = String.format(MSG_ROUTER, name, ERR_IN_USE); |
| 116 | throw new IllegalStateException(error); |
| 117 | } |
| 118 | |
| 119 | KubevirtRouter router = kubevirtRouterStore.removeRouter(name); |
| 120 | if (router != null) { |
| 121 | log.info(String.format(MSG_ROUTER, router.name(), MSG_REMOVED)); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void updatePeerRouterMac(String name, MacAddress mac) { |
| 128 | KubevirtRouter router = kubevirtRouterStore.router(name); |
| 129 | if (router == null) { |
| 130 | log.warn("The router is not found with the given name {}", name); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | KubevirtPeerRouter existing = router.peerRouter(); |
| 135 | if (existing == null) { |
| 136 | log.warn("The peer router is not found with the given name {}", name); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | KubevirtPeerRouter updated = new KubevirtPeerRouter(existing.ipAddress(), mac); |
| 141 | kubevirtRouterStore.updateRouter(router.updatePeerRouter(updated)); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void clear() { |
| 146 | kubevirtRouterStore.clear(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public KubevirtRouter router(String name) { |
| 151 | checkArgument(name != null, ERR_NULL_ROUTER_NAME); |
| 152 | return kubevirtRouterStore.router(name); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Set<KubevirtRouter> routers() { |
| 157 | return ImmutableSet.copyOf(kubevirtRouterStore.routers()); |
| 158 | } |
| 159 | |
| 160 | private boolean isRouterInUse(String name) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | private class InternalRouterStorageDelegate implements KubevirtRouterStoreDelegate { |
| 165 | |
| 166 | @Override |
| 167 | public void notify(KubevirtRouterEvent event) { |
| 168 | log.trace("send kubevirt router event {}", event); |
| 169 | process(event); |
| 170 | } |
| 171 | } |
| 172 | } |