blob: dacef68e39efbc75bbaa4c021ddbe316cfb36d25 [file] [log] [blame]
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -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 */
Thomas Vachuskad24aa7f2015-05-14 18:37:54 -070016package org.onosproject.incubator.net.config;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070017
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuskae2b7e7e2015-05-20 11:11:31 -070020import com.google.common.annotations.Beta;
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070021
22import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
25 * Base abstraction of a configuration facade for a specific subject. Derived
26 * classes should keep all state in the specified JSON tree.
27 *
28 * @param <S> type of subject
29 */
Thomas Vachuskae2b7e7e2015-05-20 11:11:31 -070030@Beta
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070031public abstract class Config<S> {
32
33 protected ObjectMapper mapper;
34 protected ObjectNode node;
35 private S subject;
36 private ConfigApplyDelegate<S> delegate;
37
38 /**
39 * Returns the specific subject to which this configuration pertains.
40 *
41 * @return configuration subject
42 */
43 S subject() {
44 return subject;
45 }
46
47 /**
48 * Initializes the configuration behaviour with necessary context.
49 *
Thomas Vachuska266b4432015-04-30 18:13:25 -070050 * @param subject configuration subject
51 * @param node JSON object node where configuration data is stored
52 * @param mapper JSON object mapper
53 * @param delegate delegate context
Thomas Vachuskaf0e1fae2015-04-24 00:51:51 -070054 */
55 public void init(S subject, ObjectNode node, ObjectMapper mapper,
56 ConfigApplyDelegate<S> delegate) {
57 this.subject = checkNotNull(subject);
58 this.node = checkNotNull(node);
59 this.mapper = checkNotNull(mapper);
60 this.delegate = checkNotNull(delegate);
61 }
62
63 /**
64 * Applies any configuration changes made via this configuration.
65 */
66 public void apply() {
67 delegate.onApply(this);
68 }
69
70}