blob: 2aa6f7ccedd16b5a04ad97419fcd2c593cc762bd [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,
Simon Hunt30abc9a2015-06-09 17:13:51 -070056 XosFunctionDescriptor.FIREWALL,
57 XosFunctionDescriptor.CDN);
Simon Huntf844f632015-05-20 19:06:35 -070058
Simon Hunt41b943e2015-05-21 13:52:01 -070059 /**
60 * Designates the FAMILY bundle.
61 */
62 public static final BundleDescriptor FAMILY_BUNDLE =
Simon Huntf844f632015-05-20 19:06:35 -070063 new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME,
Simon Hunt09a32db2015-05-21 15:00:42 -070064 FAMILY_DESCRIPTION,
Simon Huntf844f632015-05-20 19:06:35 -070065 XosFunctionDescriptor.INTERNET,
66 XosFunctionDescriptor.FIREWALL,
Simon Hunt30abc9a2015-06-09 17:13:51 -070067 XosFunctionDescriptor.CDN,
Simon Hunta29c87b2015-05-21 09:56:19 -070068 XosFunctionDescriptor.URL_FILTER);
Simon Huntf844f632015-05-20 19:06:35 -070069
Simon Hunt41b943e2015-05-21 13:52:01 -070070 // all bundles, in the order they should be listed in the GUI
71 private static final List<BundleDescriptor> ALL_BUNDLES = ImmutableList.of(
72 BASIC_BUNDLE,
73 FAMILY_BUNDLE
74 );
75
Simon Huntf844f632015-05-20 19:06:35 -070076 /**
77 * Returns the list of available bundles.
78 *
79 * @return available bundles
80 */
81 public static List<BundleDescriptor> availableBundles() {
Simon Hunt41b943e2015-05-21 13:52:01 -070082 return ALL_BUNDLES;
83 }
84
85 /**
86 * Returns the bundle descriptor for the given identifier.
87 *
88 * @param bundleId bundle identifier
89 * @return bundle descriptor
90 * @throws IllegalArgumentException if bundle ID is unknown
91 */
92 public static BundleDescriptor bundleFromId(String bundleId) {
93 for (BundleDescriptor bd : ALL_BUNDLES) {
94 if (bd.id().equals(bundleId)) {
95 return bd;
96 }
97 }
98 throw new IllegalArgumentException("unknown bundle: " + bundleId);
99 }
100
101 /**
Simon Huntee6a7372015-05-28 14:04:24 -0700102 * Returns an object node representation of the given bundle.
Simon Hunt30abc9a2015-06-09 17:13:51 -0700103 * Note that some functions (such as CDN) are not added to the output
104 * as we don't want them to appear in the GUI.
Simon Hunt41b943e2015-05-21 13:52:01 -0700105 *
106 * @param bundle the bundle
Simon Huntee6a7372015-05-28 14:04:24 -0700107 * @return object node
Simon Hunt41b943e2015-05-21 13:52:01 -0700108 */
Simon Huntee6a7372015-05-28 14:04:24 -0700109 public static ObjectNode toObjectNode(Bundle bundle) {
Simon Hunt41b943e2015-05-21 13:52:01 -0700110 ObjectNode root = objectNode();
Simon Hunt09a32db2015-05-21 15:00:42 -0700111 BundleDescriptor descriptor = bundle.descriptor();
Simon Hunt41b943e2015-05-21 13:52:01 -0700112
113 ObjectNode bnode = objectNode()
Simon Hunt09a32db2015-05-21 15:00:42 -0700114 .put(ID, descriptor.id())
115 .put(NAME, descriptor.displayName())
116 .put(DESC, descriptor.description());
Simon Hunt41b943e2015-05-21 13:52:01 -0700117
118 ArrayNode funcs = arrayNode();
119 for (XosFunctionDescriptor xfd: bundle.descriptor().functions()) {
Simon Hunt30abc9a2015-06-09 17:13:51 -0700120 if (xfd.visible()) {
121 funcs.add(XosFunctionFactory.toObjectNode(xfd));
122 }
Simon Hunt41b943e2015-05-21 13:52:01 -0700123 }
124 bnode.set(FUNCTIONS, funcs);
125 root.set(BUNDLE, bnode);
126
127 ArrayNode bundles = arrayNode();
128 for (BundleDescriptor bd: BundleFactory.availableBundles()) {
129 ObjectNode bdnode = objectNode()
130 .put(ID, bd.id())
Simon Hunt09a32db2015-05-21 15:00:42 -0700131 .put(NAME, bd.displayName())
132 .put(DESC, bd.description());
Simon Hunt41b943e2015-05-21 13:52:01 -0700133 bundles.add(bdnode);
134 }
135 root.set(BUNDLES, bundles);
Simon Huntee6a7372015-05-28 14:04:24 -0700136 return root;
Simon Huntf844f632015-05-20 19:06:35 -0700137 }
138}