blob: 30be730423226c2ee943cb89f0a6dc86585d53de [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.codec.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
23import org.onlab.onos.codec.CodecService;
24import org.onlab.onos.codec.JsonCodec;
25import org.onlab.onos.net.Annotations;
26import org.onlab.onos.net.ConnectPoint;
27import org.onlab.onos.net.Device;
28import org.onlab.onos.net.Link;
29import org.onlab.onos.net.Port;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Map;
34import java.util.Set;
35import java.util.concurrent.ConcurrentHashMap;
36
37/**
38 * Implementation of the JSON codec brokering service.
39 */
40@Component(immediate = true)
41@Service
42public class CodecManager implements CodecService {
43
44 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
45
46 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
47
48 @Activate
49 public void activate() {
50 codecs.clear();
51 registerCodec(Annotations.class, new AnnotationsCodec());
52 registerCodec(Device.class, new DeviceCodec());
53 registerCodec(Port.class, new PortCodec());
54 registerCodec(ConnectPoint.class, new ConnectPointCodec());
55 registerCodec(Link.class, new LinkCodec());
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deativate() {
61 codecs.clear();
62 log.info("Stopped");
63 }
64
65 @Override
66 public Set<Class<?>> getCodecs() {
67 return ImmutableSet.copyOf(codecs.keySet());
68 }
69
70 @Override
71 @SuppressWarnings("unchecked")
72 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
73 return codecs.get(entityClass);
74 }
75
76 @Override
77 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
78 codecs.putIfAbsent(entityClass, codec);
79 }
80
81 @Override
82 public void unregisterCodec(Class<?> entityClass) {
83 codecs.remove(entityClass);
84 }
85
86}