Simon Hunt | f844f63 | 2015-05-20 19:06:35 -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 | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 27 | * Utility factory for creating and/or operating on bundles. |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 28 | */ |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 29 | public 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 Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 34 | |
| 35 | private static final String BASIC_ID = "basic"; |
| 36 | private static final String BASIC_DISPLAY_NAME = "Basic Bundle"; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 37 | private static final String BASIC_DESCRIPTION = |
| 38 | "Provides basic internet and firewall functions."; |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 39 | |
| 40 | private static final String FAMILY_ID = "family"; |
| 41 | private static final String FAMILY_DISPLAY_NAME = "Family Bundle"; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 42 | private static final String FAMILY_DESCRIPTION = |
| 43 | "Provides internet, firewall and parental control functions."; |
| 44 | |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 45 | |
| 46 | // no instantiation |
| 47 | private BundleFactory() {} |
| 48 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Designates the BASIC bundle. |
| 51 | */ |
| 52 | public static final BundleDescriptor BASIC_BUNDLE = |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 53 | new DefaultBundleDescriptor(BASIC_ID, BASIC_DISPLAY_NAME, |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 54 | BASIC_DESCRIPTION, |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 55 | XosFunctionDescriptor.INTERNET, |
| 56 | XosFunctionDescriptor.FIREWALL); |
| 57 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Designates the FAMILY bundle. |
| 60 | */ |
| 61 | public static final BundleDescriptor FAMILY_BUNDLE = |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 62 | new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME, |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 63 | FAMILY_DESCRIPTION, |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 64 | XosFunctionDescriptor.INTERNET, |
| 65 | XosFunctionDescriptor.FIREWALL, |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 66 | XosFunctionDescriptor.URL_FILTER); |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 67 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 68 | // 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 Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Returns the list of available bundles. |
| 76 | * |
| 77 | * @return available bundles |
| 78 | */ |
| 79 | public static List<BundleDescriptor> availableBundles() { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 80 | 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 Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 100 | * Returns an object node representation of the given bundle. |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 101 | * |
| 102 | * @param bundle the bundle |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 103 | * @return object node |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 104 | */ |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 105 | public static ObjectNode toObjectNode(Bundle bundle) { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 106 | ObjectNode root = objectNode(); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 107 | BundleDescriptor descriptor = bundle.descriptor(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 108 | |
| 109 | ObjectNode bnode = objectNode() |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 110 | .put(ID, descriptor.id()) |
| 111 | .put(NAME, descriptor.displayName()) |
| 112 | .put(DESC, descriptor.description()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 113 | |
| 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 125 | .put(NAME, bd.displayName()) |
| 126 | .put(DESC, bd.description()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 127 | bundles.add(bdnode); |
| 128 | } |
| 129 | root.set(BUNDLES, bundles); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 130 | return root; |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 131 | } |
| 132 | } |