blob: c51cc8486c9829ea58a2aaf57080eb86cdc5e7bb [file] [log] [blame]
Simon Hunt41b943e2015-05-21 13:52:01 -07001/*
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
18package org.onosproject.cord.gui.model;
19
Simon Hunt6c2555b2015-05-21 18:17:56 -070020import com.google.common.collect.ImmutableSet;
21
Simon Hunt87b157c2015-05-22 12:09:59 -070022import java.util.HashMap;
23import java.util.Map;
Simon Hunt6c2555b2015-05-21 18:17:56 -070024import java.util.Set;
25
Simon Hunt41b943e2015-05-21 13:52:01 -070026/**
27 * Encapsulates a bundle, including current state.
28 */
29public class Bundle {
30 private final BundleDescriptor bundleDescriptor;
Simon Hunt87b157c2015-05-22 12:09:59 -070031 private final Map<XosFunctionDescriptor, XosFunction> functionMap =
32 new HashMap<XosFunctionDescriptor, XosFunction>();
Simon Hunt41b943e2015-05-21 13:52:01 -070033
34 /**
35 * Constructs a new bundle instance.
36 *
37 * @param bundleDescriptor the descriptor
38 */
39 public Bundle(BundleDescriptor bundleDescriptor) {
40 this.bundleDescriptor = bundleDescriptor;
Simon Hunt87b157c2015-05-22 12:09:59 -070041 initFunctions();
Simon Hunt41b943e2015-05-21 13:52:01 -070042 }
43
44 /**
45 * Returns the bundle descriptor.
46 *
47 * @return the descriptor
48 */
49 public BundleDescriptor descriptor() {
50 return bundleDescriptor;
51 }
52
Simon Hunt6c2555b2015-05-21 18:17:56 -070053 /**
54 * Returns the set of function instances for this bundle.
55 *
56 * @return the functions
57 */
58 public Set<XosFunction> functions() {
Simon Hunt87b157c2015-05-22 12:09:59 -070059 return ImmutableSet.copyOf(functionMap.values());
Simon Hunt6c2555b2015-05-21 18:17:56 -070060 }
61
62 /**
63 * Creates an initial set of function instances.
Simon Hunt6c2555b2015-05-21 18:17:56 -070064 */
Simon Hunt87b157c2015-05-22 12:09:59 -070065 private void initFunctions() {
Simon Hunt6c2555b2015-05-21 18:17:56 -070066 for (XosFunctionDescriptor xfd: bundleDescriptor.functions()) {
Simon Hunt87b157c2015-05-22 12:09:59 -070067 functionMap.put(xfd, createFunction(xfd));
Simon Hunt6c2555b2015-05-21 18:17:56 -070068 }
Simon Hunt6c2555b2015-05-21 18:17:56 -070069 }
70
71 private XosFunction createFunction(XosFunctionDescriptor xfd) {
72 XosFunction func;
73 switch (xfd) {
74 case URL_FILTER:
Simon Huntab5232e2015-05-26 16:59:46 -070075 func = new UrlFilterFunction();
Simon Hunt6c2555b2015-05-21 18:17:56 -070076 break;
77
78 default:
79 func = new DefaultXosFunction(xfd);
80 break;
81 }
82 return func;
83 }
Simon Hunt87b157c2015-05-22 12:09:59 -070084
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 Hunt41b943e2015-05-21 13:52:01 -070095}