blob: d0e871ae31889f450a4abd61b78c391fe4d54117 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec.impl;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.codec.CodecService;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.net.Annotations;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Device;
Ray Milkey1f95bd32014-12-10 11:11:00 -080028import org.onosproject.net.Host;
29import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Link;
31import org.onosproject.net.Port;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.Map;
36import java.util.Set;
37import java.util.concurrent.ConcurrentHashMap;
38
39/**
40 * Implementation of the JSON codec brokering service.
41 */
42@Component(immediate = true)
43@Service
44public class CodecManager implements CodecService {
45
46 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
47
48 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
49
50 @Activate
51 public void activate() {
52 codecs.clear();
53 registerCodec(Annotations.class, new AnnotationsCodec());
54 registerCodec(Device.class, new DeviceCodec());
55 registerCodec(Port.class, new PortCodec());
56 registerCodec(ConnectPoint.class, new ConnectPointCodec());
57 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -080058 registerCodec(Host.class, new HostCodec());
59 registerCodec(HostLocation.class, new HostLocationCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080060 log.info("Started");
61 }
62
63 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -080064 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080065 codecs.clear();
66 log.info("Stopped");
67 }
68
69 @Override
70 public Set<Class<?>> getCodecs() {
71 return ImmutableSet.copyOf(codecs.keySet());
72 }
73
74 @Override
75 @SuppressWarnings("unchecked")
76 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
77 return codecs.get(entityClass);
78 }
79
80 @Override
81 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
82 codecs.putIfAbsent(entityClass, codec);
83 }
84
85 @Override
86 public void unregisterCodec(Class<?> entityClass) {
87 codecs.remove(entityClass);
88 }
89
90}