blob: 66721c3dcc80f8e025a63187758e8158bd0d7b94 [file] [log] [blame]
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16package org.onosproject.d.config;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070020import static org.onosproject.d.config.ResourceIds.ROOT_ID;
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070021import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.Collections;
24import java.util.IdentityHashMap;
25import java.util.Map;
26import java.util.concurrent.CompletableFuture;
27
28import org.onosproject.config.DynamicConfigEvent;
29import org.onosproject.config.DynamicConfigListener;
30import org.onosproject.config.DynamicConfigService;
31import org.onosproject.config.Filter;
32import org.onosproject.config.ForwardingDynamicConfigService;
33import org.onosproject.net.DeviceId;
34import org.onosproject.yang.model.DataNode;
35import org.onosproject.yang.model.ResourceId;
36import org.onosproject.yang.model.RpcInput;
37import org.onosproject.yang.model.RpcOutput;
38import org.slf4j.Logger;
39
40import com.google.common.annotations.Beta;
41
42/**
43 * DynamicConfigService interface to provide a viewport
44 * under specified Device's tree.
45 */
46@Beta
47public class DynamicDeviceConfigServiceView
48 extends ForwardingDynamicConfigService
49 implements DynamicConfigService {
50
51 private static final Logger log = getLogger(DynamicDeviceConfigServiceView.class);
52
53 private final DeviceId deviceId;
54
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070055 // absolute ResourceId
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070056 private ResourceId deviceRoot;
57
58 /**
59 * original to wrapped listener.
60 */
61 private final Map<DynamicConfigListener, DynamicConfigListener> wrapped =
62 Collections.synchronizedMap(new IdentityHashMap<>());
63
64 protected DynamicDeviceConfigServiceView(DynamicConfigService delegate, DeviceId deviceId) {
65 super(delegate);
66 this.deviceId = checkNotNull(deviceId);
67 this.deviceRoot = DeviceResourceIds.toResourceId(this.deviceId);
68 }
69
70 /**
71 * Create a DynamicDeviceConfigServiceView for specified {@code deviceId}.
72 *
73 * @param service reference to actual DynamicConfigService.
74 * @param deviceId of the Device to provide view port about.
75 * @return DynamicDeviceConfigServiceView
76 */
77 public static DynamicDeviceConfigServiceView deviceView(DynamicConfigService service,
78 DeviceId deviceId) {
79 return new DynamicDeviceConfigServiceView(service, deviceId);
80 }
81
82 @Override
83 public void createNode(ResourceId path, DataNode node) {
84 super.createNode(toAbsoluteId(path), node);
85 }
86
87 @Override
88 public DataNode readNode(ResourceId path, Filter filter) {
89 // FIXME transform filter? is it possible?
90 return super.readNode(toAbsoluteId(path), filter);
91 }
92
93 @Override
94 public Boolean nodeExist(ResourceId path) {
95 return super.nodeExist(toAbsoluteId(path));
96 }
97
98 @Override
99 public void updateNode(ResourceId path, DataNode node) {
100 super.updateNode(toAbsoluteId(path), node);
101 }
102
103 @Override
104 public void replaceNode(ResourceId path, DataNode node) {
105 super.replaceNode(toAbsoluteId(path), node);
106 }
107
108 @Override
109 public void deleteNode(ResourceId path) {
110 super.deleteNode(toAbsoluteId(path));
111 }
112
113 @Override
Gaurav Agrawal142ceb02018-02-16 12:19:08 +0530114 public CompletableFuture<RpcOutput> invokeRpc(RpcInput input) {
115 return super.invokeRpc(new RpcInput(toAbsoluteId(input.id()), input.data()));
116 }
117
118 @Override
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700119 public void addListener(DynamicConfigListener listener) {
120 super.addListener(wrapped.computeIfAbsent(listener,
121 DynamicDeviceConfigListener::new));
122 }
123
124 @Override
125 public void removeListener(DynamicConfigListener listener) {
126 DynamicConfigListener w = wrapped.remove(listener);
127 if (w != null) {
128 super.removeListener(w);
129 }
130 }
131
132 private ResourceId toAbsoluteId(ResourceId path) {
133 checkArgument(!path.nodeKeys().contains(DeviceResourceIds.ROOT_NODE),
134 "%s was already absolute path", path);
135
136 return ResourceIds.concat(deviceRoot, path);
137 }
138
139 private ResourceId toDeviceRelativeId(ResourceId path) {
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -0700140 // case: absolute
141 if (ResourceIds.startsWithRootNode(path)) {
142 return ResourceIds.relativize(deviceRoot, path);
143 }
144 // case: root relative
145 if (DeviceResourceIds.isUnderDeviceRootNode(path)) {
146 // TODO not efficient
147 return ResourceIds.relativize(deviceRoot, ResourceIds.concat(ROOT_ID, path));
148 }
149 throw new IllegalArgumentException(path + " was not absolute device path");
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700150 }
151
152 class DynamicDeviceConfigListener implements DynamicConfigListener {
153
154 private final DynamicConfigListener lsnr;
155
156 DynamicDeviceConfigListener(DynamicConfigListener lsnr) {
157 this.lsnr = checkNotNull(lsnr);
158 }
159
160 private DynamicConfigEvent deviceEvent(DynamicConfigEvent event) {
161 return new DynamicConfigEvent(event.type(),
162 toDeviceRelativeId(event.subject()));
163 }
164
165 @Override
166 public boolean isRelevant(DynamicConfigEvent event) {
167 // FIXME ignore event irrelevant to current device
168 return lsnr.isRelevant(deviceEvent(event));
169 }
170
171 @Override
172 public void event(DynamicConfigEvent event) {
173 lsnr.event(deviceEvent(event));
174 }
175
176 }
177}