Jian Li | 747e136 | 2019-02-19 22:59:46 +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 com.google.common.base.Strings; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import io.fabric8.kubernetes.api.model.Pod; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.event.ListenerRegistry; |
| 24 | import org.onosproject.k8snetworking.api.K8sPodAdminService; |
| 25 | import org.onosproject.k8snetworking.api.K8sPodEvent; |
| 26 | import org.onosproject.k8snetworking.api.K8sPodListener; |
| 27 | import org.onosproject.k8snetworking.api.K8sPodService; |
| 28 | import org.onosproject.k8snetworking.api.K8sPodStore; |
| 29 | import org.onosproject.k8snetworking.api.K8sPodStoreDelegate; |
| 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkArgument; |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Provides implementation of administering and interfacing kubernetes pod. |
| 46 | */ |
| 47 | @Component( |
| 48 | immediate = true, |
| 49 | service = {K8sPodAdminService.class, K8sPodService.class} |
| 50 | ) |
| 51 | public class K8sPodManager |
| 52 | extends ListenerRegistry<K8sPodEvent, K8sPodListener> |
| 53 | implements K8sPodAdminService, K8sPodService { |
| 54 | |
| 55 | protected final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | private static final String MSG_POD = "Kubernetes pod %s %s"; |
| 58 | private static final String MSG_CREATED = "created"; |
| 59 | private static final String MSG_UPDATED = "updated"; |
| 60 | private static final String MSG_REMOVED = "removed"; |
| 61 | |
| 62 | private static final String ERR_NULL_POD = "Kubernetes pod cannot be null"; |
| 63 | private static final String ERR_NULL_POD_UID = "Kubernetes pod UID cannot be null"; |
| 64 | |
| 65 | private static final String ERR_IN_USE = " still in use"; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 68 | protected CoreService coreService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 71 | protected K8sPodStore k8sPodStore; |
| 72 | |
| 73 | private final K8sPodStoreDelegate delegate = new InternalPodStorageDelegate(); |
| 74 | |
| 75 | private ApplicationId appId; |
| 76 | |
| 77 | @Activate |
| 78 | protected void activate() { |
| 79 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 80 | |
| 81 | k8sPodStore.setDelegate(delegate); |
| 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | protected void deactivate() { |
| 87 | k8sPodStore.unsetDelegate(delegate); |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void createPod(Pod pod) { |
| 93 | checkNotNull(pod, ERR_NULL_POD); |
| 94 | checkArgument(!Strings.isNullOrEmpty(pod.getMetadata().getUid()), |
| 95 | ERR_NULL_POD_UID); |
| 96 | |
| 97 | k8sPodStore.createPod(pod); |
| 98 | |
| 99 | log.info(String.format(MSG_POD, pod.getMetadata().getName(), MSG_CREATED)); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void updatePod(Pod pod) { |
| 104 | checkNotNull(pod, ERR_NULL_POD); |
| 105 | checkArgument(!Strings.isNullOrEmpty(pod.getMetadata().getUid()), |
| 106 | ERR_NULL_POD_UID); |
| 107 | |
| 108 | k8sPodStore.updatePod(pod); |
| 109 | |
| 110 | log.info(String.format(MSG_POD, pod.getMetadata().getName(), MSG_UPDATED)); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void removePod(String uid) { |
| 115 | checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_POD_UID); |
| 116 | |
| 117 | synchronized (this) { |
| 118 | if (isPodInUse(uid)) { |
| 119 | final String error = String.format(MSG_POD, uid, ERR_IN_USE); |
| 120 | throw new IllegalStateException(error); |
| 121 | } |
| 122 | |
| 123 | Pod pod = k8sPodStore.removePod(uid); |
| 124 | |
| 125 | if (pod != null) { |
| 126 | log.info(String.format(MSG_POD, |
| 127 | pod.getMetadata().getName(), MSG_REMOVED)); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void clear() { |
| 134 | k8sPodStore.clear(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Pod pod(String uid) { |
| 139 | checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_POD_UID); |
| 140 | return k8sPodStore.pod(uid); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Set<Pod> pods() { |
| 145 | return ImmutableSet.copyOf(k8sPodStore.pods()); |
| 146 | } |
| 147 | |
| 148 | private boolean isPodInUse(String uid) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | private class InternalPodStorageDelegate implements K8sPodStoreDelegate { |
| 153 | |
| 154 | @Override |
| 155 | public void notify(K8sPodEvent event) { |
| 156 | if (event != null) { |
| 157 | log.trace("send kubernetes pod event {}", event); |
| 158 | process(event); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |