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.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 22 | import org.onosproject.incubator.net.virtual.intent.VirtualIntentCompiler; |
| 23 | import org.onosproject.net.intent.Intent; |
| 24 | import org.onosproject.net.intent.IntentException; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.LinkedList; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Queue; |
| 31 | import java.util.concurrent.ConcurrentHashMap; |
| 32 | import java.util.concurrent.ConcurrentMap; |
| 33 | |
| 34 | public final class VirtualIntentCompilerRegistry { |
| 35 | private final ConcurrentMap<Class<? extends Intent>, |
| 36 | VirtualIntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
| 37 | |
| 38 | // non-instantiable (except for our Singleton) |
| 39 | private VirtualIntentCompilerRegistry() { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | public static VirtualIntentCompilerRegistry getInstance() { |
| 44 | return SingletonHelper.INSTANCE; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Registers the specified compiler for the given intent class. |
| 49 | * |
| 50 | * @param cls intent class |
| 51 | * @param compiler intent compiler |
| 52 | * @param <T> the type of intent |
| 53 | */ |
| 54 | public <T extends Intent> void registerCompiler(Class<T> cls, |
| 55 | VirtualIntentCompiler<T> compiler) { |
| 56 | compilers.put(cls, compiler); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Unregisters the compiler for the specified intent class. |
| 61 | * |
| 62 | * @param cls intent class |
| 63 | * @param <T> the type of intent |
| 64 | */ |
| 65 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 66 | compilers.remove(cls); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns immutable set of bindings of currently registered intent compilers. |
| 71 | * |
| 72 | * @return the set of compiler bindings |
| 73 | */ |
| 74 | public Map<Class<? extends Intent>, VirtualIntentCompiler<? extends Intent>> getCompilers() { |
| 75 | return ImmutableMap.copyOf(compilers); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Compiles an intent recursively. |
| 80 | * |
| 81 | * @param networkId network identifier |
| 82 | * @param intent intent |
| 83 | * @param previousInstallables previous intent installables |
| 84 | * @return result of compilation |
| 85 | */ |
| 86 | public List<Intent> compile(NetworkId networkId, |
| 87 | Intent intent, List<Intent> previousInstallables) { |
| 88 | if (intent.isInstallable()) { |
| 89 | return ImmutableList.of(intent); |
| 90 | } |
| 91 | |
| 92 | // FIXME: get previous resources |
| 93 | List<Intent> installables = new ArrayList<>(); |
| 94 | Queue<Intent> compileQueue = new LinkedList<>(); |
| 95 | compileQueue.add(intent); |
| 96 | |
| 97 | Intent compiling; |
| 98 | while ((compiling = compileQueue.poll()) != null) { |
| 99 | registerSubclassCompilerIfNeeded(compiling); |
| 100 | |
| 101 | List<Intent> compiled = getCompiler(compiling) |
| 102 | .compile(networkId, compiling, previousInstallables); |
| 103 | |
| 104 | compiled.forEach(i -> { |
| 105 | if (i.isInstallable()) { |
| 106 | installables.add(i); |
| 107 | } else { |
| 108 | compileQueue.add(i); |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | return installables; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns the corresponding intent compiler to the specified intent. |
| 117 | * |
| 118 | * @param intent intent |
| 119 | * @param <T> the type of intent |
| 120 | * @return intent compiler corresponding to the specified intent |
| 121 | */ |
| 122 | private <T extends Intent> VirtualIntentCompiler<T> getCompiler(T intent) { |
| 123 | @SuppressWarnings("unchecked") |
| 124 | VirtualIntentCompiler<T> compiler = |
| 125 | (VirtualIntentCompiler<T>) compilers.get(intent.getClass()); |
| 126 | if (compiler == null) { |
| 127 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 128 | } |
| 129 | return compiler; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Registers an intent compiler of the specified intent if an intent compiler |
| 134 | * for the intent is not registered. This method traverses the class hierarchy of |
| 135 | * the intent. Once an intent compiler for a parent type is found, this method |
| 136 | * registers the found intent compiler. |
| 137 | * |
| 138 | * @param intent intent |
| 139 | */ |
| 140 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 141 | if (!compilers.containsKey(intent.getClass())) { |
| 142 | Class<?> cls = intent.getClass(); |
| 143 | while (cls != Object.class) { |
| 144 | // As long as we're within the Intent class descendants |
| 145 | if (Intent.class.isAssignableFrom(cls)) { |
| 146 | VirtualIntentCompiler<?> compiler = compilers.get(cls); |
| 147 | if (compiler != null) { |
| 148 | compilers.put(intent.getClass(), compiler); |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | cls = cls.getSuperclass(); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Prevents object instantiation from external. |
| 159 | */ |
| 160 | private static final class SingletonHelper { |
| 161 | private static final String ILLEGAL_ACCESS_MSG = |
| 162 | "Should not instantiate this class."; |
| 163 | private static final VirtualIntentCompilerRegistry INSTANCE = |
| 164 | new VirtualIntentCompilerRegistry(); |
| 165 | |
| 166 | private SingletonHelper() { |
| 167 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 168 | } |
| 169 | } |
| 170 | } |