Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 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.net.intent.impl; |
| 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. |
| 28 | */ |
| 29 | public class InstallerRegistry { |
| 30 | private final Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> installers; |
| 31 | |
| 32 | public InstallerRegistry() { |
| 33 | installers = Maps.newConcurrentMap(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Registers the specific installer for the given intent class. |
| 38 | * |
| 39 | * @param cls intent class |
| 40 | * @param installer intent installer |
| 41 | * @param <T> the type of intent |
| 42 | */ |
| 43 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { |
| 44 | installers.put(cls, installer); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Unregisters the installer for the specific intent class. |
| 49 | * |
| 50 | * @param cls intent class |
| 51 | * @param <T> the type of intent |
| 52 | */ |
| 53 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
| 54 | installers.remove(cls); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns immutable set of binding of currently registered intent installers. |
| 59 | * |
| 60 | * @return the set of installer bindings |
| 61 | */ |
| 62 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
| 63 | return ImmutableMap.copyOf(installers); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get an Intent installer by given Intent type. |
| 68 | * |
| 69 | * @param cls the Intent type |
| 70 | * @param <T> the Intent type |
| 71 | * @return the Intent installer of the Intent type if exists; null otherwise |
| 72 | */ |
| 73 | public <T extends Intent> IntentInstaller<T> getInstaller(Class<T> cls) { |
| 74 | return (IntentInstaller<T>) installers.get(cls); |
| 75 | } |
| 76 | } |