Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -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 | |
| 18 | package org.onosproject.cord.gui.model; |
| 19 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Encapsulates a bundle, including current state. |
| 28 | */ |
| 29 | public class Bundle { |
| 30 | private final BundleDescriptor bundleDescriptor; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 31 | private final Map<XosFunctionDescriptor, XosFunction> functionMap = |
| 32 | new HashMap<XosFunctionDescriptor, XosFunction>(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Constructs a new bundle instance. |
| 36 | * |
| 37 | * @param bundleDescriptor the descriptor |
| 38 | */ |
| 39 | public Bundle(BundleDescriptor bundleDescriptor) { |
| 40 | this.bundleDescriptor = bundleDescriptor; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 41 | initFunctions(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns the bundle descriptor. |
| 46 | * |
| 47 | * @return the descriptor |
| 48 | */ |
| 49 | public BundleDescriptor descriptor() { |
| 50 | return bundleDescriptor; |
| 51 | } |
| 52 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Returns the set of function instances for this bundle. |
| 55 | * |
| 56 | * @return the functions |
| 57 | */ |
| 58 | public Set<XosFunction> functions() { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 59 | return ImmutableSet.copyOf(functionMap.values()); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates an initial set of function instances. |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 64 | */ |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 65 | private void initFunctions() { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 66 | for (XosFunctionDescriptor xfd: bundleDescriptor.functions()) { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 67 | functionMap.put(xfd, createFunction(xfd)); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 68 | } |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private XosFunction createFunction(XosFunctionDescriptor xfd) { |
| 72 | XosFunction func; |
| 73 | switch (xfd) { |
| 74 | case URL_FILTER: |
Simon Hunt | ab5232e | 2015-05-26 16:59:46 -0700 | [diff] [blame] | 75 | func = new UrlFilterFunction(); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 76 | break; |
| 77 | |
| 78 | default: |
| 79 | func = new DefaultXosFunction(xfd); |
| 80 | break; |
| 81 | } |
| 82 | return func; |
| 83 | } |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Returns the function instance for the specified descriptor, or returns |
| 87 | * null if function is not part of this bundle. |
| 88 | * |
| 89 | * @param xfd function descrriptor |
| 90 | * @return function instance |
| 91 | */ |
| 92 | public XosFunction findFunction(XosFunctionDescriptor xfd) { |
| 93 | return functionMap.get(xfd); |
| 94 | } |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 95 | } |