blob: 4c9e6212f1a4662ffe187cceb8857eee34ce0518 [file] [log] [blame]
Simon Huntf844f632015-05-20 19:06:35 -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 Hunt41b943e2015-05-21 13:52:01 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntf844f632015-05-20 19:06:35 -070022import com.google.common.collect.ImmutableList;
23
24import java.util.List;
25
26/**
Simon Hunt41b943e2015-05-21 13:52:01 -070027 * Utility factory for creating and/or operating on bundles.
Simon Huntf844f632015-05-20 19:06:35 -070028 */
Simon Hunt41b943e2015-05-21 13:52:01 -070029public class BundleFactory extends JsonFactory {
30
31 private static final String BUNDLE = "bundle";
32 private static final String BUNDLES = "bundles";
33 private static final String FUNCTIONS = "functions";
Simon Huntf844f632015-05-20 19:06:35 -070034
35 private static final String BASIC_ID = "basic";
36 private static final String BASIC_DISPLAY_NAME = "Basic Bundle";
Simon Hunt09a32db2015-05-21 15:00:42 -070037 private static final String BASIC_DESCRIPTION =
38 "Provides basic internet and firewall functions.";
Simon Huntf844f632015-05-20 19:06:35 -070039
40 private static final String FAMILY_ID = "family";
41 private static final String FAMILY_DISPLAY_NAME = "Family Bundle";
Simon Hunt09a32db2015-05-21 15:00:42 -070042 private static final String FAMILY_DESCRIPTION =
43 "Provides internet, firewall and parental control functions.";
44
Simon Huntf844f632015-05-20 19:06:35 -070045
46 // no instantiation
47 private BundleFactory() {}
48
Simon Hunt41b943e2015-05-21 13:52:01 -070049 /**
50 * Designates the BASIC bundle.
51 */
52 public static final BundleDescriptor BASIC_BUNDLE =
Simon Huntf844f632015-05-20 19:06:35 -070053 new DefaultBundleDescriptor(BASIC_ID, BASIC_DISPLAY_NAME,
Simon Hunt09a32db2015-05-21 15:00:42 -070054 BASIC_DESCRIPTION,
Simon Huntf844f632015-05-20 19:06:35 -070055 XosFunctionDescriptor.INTERNET,
56 XosFunctionDescriptor.FIREWALL);
57
Simon Hunt41b943e2015-05-21 13:52:01 -070058 /**
59 * Designates the FAMILY bundle.
60 */
61 public static final BundleDescriptor FAMILY_BUNDLE =
Simon Huntf844f632015-05-20 19:06:35 -070062 new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME,
Simon Hunt09a32db2015-05-21 15:00:42 -070063 FAMILY_DESCRIPTION,
Simon Huntf844f632015-05-20 19:06:35 -070064 XosFunctionDescriptor.INTERNET,
65 XosFunctionDescriptor.FIREWALL,
Simon Hunta29c87b2015-05-21 09:56:19 -070066 XosFunctionDescriptor.URL_FILTER);
Simon Huntf844f632015-05-20 19:06:35 -070067
Simon Hunt41b943e2015-05-21 13:52:01 -070068 // all bundles, in the order they should be listed in the GUI
69 private static final List<BundleDescriptor> ALL_BUNDLES = ImmutableList.of(
70 BASIC_BUNDLE,
71 FAMILY_BUNDLE
72 );
73
Simon Huntf844f632015-05-20 19:06:35 -070074 /**
75 * Returns the list of available bundles.
76 *
77 * @return available bundles
78 */
79 public static List<BundleDescriptor> availableBundles() {
Simon Hunt41b943e2015-05-21 13:52:01 -070080 return ALL_BUNDLES;
81 }
82
83 /**
84 * Returns the bundle descriptor for the given identifier.
85 *
86 * @param bundleId bundle identifier
87 * @return bundle descriptor
88 * @throws IllegalArgumentException if bundle ID is unknown
89 */
90 public static BundleDescriptor bundleFromId(String bundleId) {
91 for (BundleDescriptor bd : ALL_BUNDLES) {
92 if (bd.id().equals(bundleId)) {
93 return bd;
94 }
95 }
96 throw new IllegalArgumentException("unknown bundle: " + bundleId);
97 }
98
99 /**
Simon Huntee6a7372015-05-28 14:04:24 -0700100 * Returns an object node representation of the given bundle.
Simon Hunt41b943e2015-05-21 13:52:01 -0700101 *
102 * @param bundle the bundle
Simon Huntee6a7372015-05-28 14:04:24 -0700103 * @return object node
Simon Hunt41b943e2015-05-21 13:52:01 -0700104 */
Simon Huntee6a7372015-05-28 14:04:24 -0700105 public static ObjectNode toObjectNode(Bundle bundle) {
Simon Hunt41b943e2015-05-21 13:52:01 -0700106 ObjectNode root = objectNode();
Simon Hunt09a32db2015-05-21 15:00:42 -0700107 BundleDescriptor descriptor = bundle.descriptor();
Simon Hunt41b943e2015-05-21 13:52:01 -0700108
109 ObjectNode bnode = objectNode()
Simon Hunt09a32db2015-05-21 15:00:42 -0700110 .put(ID, descriptor.id())
111 .put(NAME, descriptor.displayName())
112 .put(DESC, descriptor.description());
Simon Hunt41b943e2015-05-21 13:52:01 -0700113
114 ArrayNode funcs = arrayNode();
115 for (XosFunctionDescriptor xfd: bundle.descriptor().functions()) {
116 funcs.add(XosFunctionFactory.toObjectNode(xfd));
117 }
118 bnode.set(FUNCTIONS, funcs);
119 root.set(BUNDLE, bnode);
120
121 ArrayNode bundles = arrayNode();
122 for (BundleDescriptor bd: BundleFactory.availableBundles()) {
123 ObjectNode bdnode = objectNode()
124 .put(ID, bd.id())
Simon Hunt09a32db2015-05-21 15:00:42 -0700125 .put(NAME, bd.displayName())
126 .put(DESC, bd.description());
Simon Hunt41b943e2015-05-21 13:52:01 -0700127 bundles.add(bdnode);
128 }
129 root.set(BUNDLES, bundles);
Simon Huntee6a7372015-05-28 14:04:24 -0700130 return root;
Simon Huntf844f632015-05-20 19:06:35 -0700131 }
132}