Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
| 24 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Encapsulates a bundle, including current state. |
| 27 | */ |
| 28 | public class Bundle { |
| 29 | private final BundleDescriptor bundleDescriptor; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 30 | private final Set<XosFunction> functions; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Constructs a new bundle instance. |
| 34 | * |
| 35 | * @param bundleDescriptor the descriptor |
| 36 | */ |
| 37 | public Bundle(BundleDescriptor bundleDescriptor) { |
| 38 | this.bundleDescriptor = bundleDescriptor; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 39 | this.functions = initFunctions(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns the bundle descriptor. |
| 44 | * |
| 45 | * @return the descriptor |
| 46 | */ |
| 47 | public BundleDescriptor descriptor() { |
| 48 | return bundleDescriptor; |
| 49 | } |
| 50 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Returns the set of function instances for this bundle. |
| 53 | * |
| 54 | * @return the functions |
| 55 | */ |
| 56 | public Set<XosFunction> functions() { |
| 57 | return ImmutableSet.copyOf(functions); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Creates an initial set of function instances. |
| 62 | * |
| 63 | * @return initial function instances |
| 64 | */ |
| 65 | private Set<XosFunction> initFunctions() { |
| 66 | Set<XosFunction> funcs = new HashSet<XosFunction>(); |
| 67 | for (XosFunctionDescriptor xfd: bundleDescriptor.functions()) { |
| 68 | funcs.add(createFunction(xfd)); |
| 69 | } |
| 70 | return funcs; |
| 71 | } |
| 72 | |
| 73 | private XosFunction createFunction(XosFunctionDescriptor xfd) { |
| 74 | XosFunction func; |
| 75 | switch (xfd) { |
| 76 | case URL_FILTER: |
| 77 | func = new UrlFilterFunction(xfd); |
| 78 | break; |
| 79 | |
| 80 | default: |
| 81 | func = new DefaultXosFunction(xfd); |
| 82 | break; |
| 83 | } |
| 84 | return func; |
| 85 | } |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 86 | } |