blob: ec1ff380707d9ddc580b8851ff507b046dad75c8 [file] [log] [blame]
Simon Hunt6c2555b2015-05-21 18:17:56 -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
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
Simon Hunt87b157c2015-05-22 12:09:59 -070022import static com.google.common.base.Preconditions.checkNotNull;
23
Simon Hunt6c2555b2015-05-21 18:17:56 -070024/**
25 * Specialization of XosFunction for URL filtering.
26 */
27public class UrlFilterFunction extends DefaultXosFunction {
28
29 private static final String LEVEL = "level";
Simon Huntb1246412015-06-01 13:37:26 -070030 private static final String URI_PATTERN = "%s/%s/";
Simon Hunt6c2555b2015-05-21 18:17:56 -070031
32 /**
Simon Huntab5232e2015-05-26 16:59:46 -070033 * Denotes the URL filtering levels available. From most restrictive
Simon Huntb13c8f12015-06-04 14:09:06 -070034 * to least restrictive. Note: <em>NONE</em> allows nothing;
35 * <em>ALL</em> allows everything.
Simon Hunt6c2555b2015-05-21 18:17:56 -070036 */
Simon Huntb13c8f12015-06-04 14:09:06 -070037 public enum Level { NONE, G, PG, PG_13, R, ALL }
Simon Hunt6c2555b2015-05-21 18:17:56 -070038
39 /**
40 * The default URL filtering level
41 */
Simon Huntab5232e2015-05-26 16:59:46 -070042 public static final Level DEFAULT_LEVEL = Level.G;
Simon Hunt6c2555b2015-05-21 18:17:56 -070043
Simon Huntab5232e2015-05-26 16:59:46 -070044 public UrlFilterFunction() {
45 super(XosFunctionDescriptor.URL_FILTER);
Simon Hunt6c2555b2015-05-21 18:17:56 -070046 }
47
48 @Override
Simon Hunt87b157c2015-05-22 12:09:59 -070049 public void applyParam(SubscriberUser user, String param, String value) {
50 Memento memo = user.getMemento(descriptor());
51 checkNotNull(memo, "missing memento for " + descriptor());
52 UrlFilterMemento ufMemo = (UrlFilterMemento) memo;
53
54 if (LEVEL.equals(param)) {
55 Level newLevel = Level.valueOf(value.toUpperCase());
56 ufMemo.setLevel(newLevel);
Simon Hunt7d02c082015-05-29 12:17:09 -070057
58 // Also store the (string version) of the level
59 // (not in the memento). Hackish, but that's how it is for now.
60 user.setUrlFilterLevel(value);
Simon Hunt87b157c2015-05-22 12:09:59 -070061 }
62 }
63
64 @Override
Simon Hunt6c2555b2015-05-21 18:17:56 -070065 public Memento createMemento() {
66 return new UrlFilterMemento();
67 }
68
69 class UrlFilterMemento implements Memento {
70 private Level level = DEFAULT_LEVEL;
71
72 public ObjectNode toObjectNode() {
73 ObjectNode node = MAPPER.createObjectNode();
74 node.put(LEVEL, level.name());
75 return node;
76 }
77
78 public void setLevel(Level level) {
79 this.level = level;
80 }
Simon Hunta00b0ce2015-05-22 15:57:11 -070081
82 public String level() {
83 return level.toString();
84 }
85 }
86
87 @Override
88 public String xosUrlApply(SubscriberUser user) {
89 XosFunctionDescriptor xfd = XosFunctionDescriptor.URL_FILTER;
90 UrlFilterMemento memo = (UrlFilterMemento) user.getMemento(xfd);
Simon Huntb1246412015-06-01 13:37:26 -070091 return String.format(URI_PATTERN, xfd.id(), memo.level());
Simon Hunt6c2555b2015-05-21 18:17:56 -070092 }
93}