Yoonseon Han | 096cea0 | 2017-05-15 15:10:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl.intent; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.net.intent.Intent; |
| 22 | import org.onosproject.net.intent.IntentInstaller; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * The local registry for Intent installer for virtual networks. |
| 28 | */ |
| 29 | public final class VirtualIntentInstallerRegistry { |
| 30 | private final Map<Class<? extends Intent>, |
| 31 | IntentInstaller<? extends Intent>> installers; |
| 32 | |
| 33 | // non-instantiable (except for our Singleton) |
| 34 | private VirtualIntentInstallerRegistry() { |
| 35 | installers = Maps.newConcurrentMap(); |
| 36 | } |
| 37 | |
| 38 | public static VirtualIntentInstallerRegistry getInstance() { |
| 39 | return SingletonHelper.INSTANCE; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Registers the specific installer for the given intent class. |
| 44 | * |
| 45 | * @param cls intent class |
| 46 | * @param installer intent installer |
| 47 | * @param <T> the type of intent |
| 48 | */ |
| 49 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { |
| 50 | installers.put(cls, installer); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Unregisters the installer for the specific intent class. |
| 55 | * |
| 56 | * @param cls intent class |
| 57 | * @param <T> the type of intent |
| 58 | */ |
| 59 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
| 60 | installers.remove(cls); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns immutable set of binding of currently registered intent installers. |
| 65 | * |
| 66 | * @return the set of installer bindings |
| 67 | */ |
| 68 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
| 69 | return ImmutableMap.copyOf(installers); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get an Intent installer by given Intent type. |
| 74 | * |
| 75 | * @param cls the Intent type |
| 76 | * @param <T> the Intent type |
| 77 | * @return the Intent installer of the Intent type if exists; null otherwise |
| 78 | */ |
| 79 | public <T extends Intent> IntentInstaller<T> getInstaller(Class<T> cls) { |
| 80 | return (IntentInstaller<T>) installers.get(cls); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Prevents object instantiation from external. |
| 85 | */ |
| 86 | private static final class SingletonHelper { |
| 87 | private static final String ILLEGAL_ACCESS_MSG = |
| 88 | "Should not instantiate this class."; |
| 89 | private static final VirtualIntentInstallerRegistry INSTANCE = |
| 90 | new VirtualIntentInstallerRegistry(); |
| 91 | |
| 92 | private SingletonHelper() { |
| 93 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 94 | } |
| 95 | } |
| 96 | } |