blob: 562cdc0df4be8c826340019416accff9d3f5f9d5 [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;
Ray Milkey2b217142014-12-15 09:24:24 -080032import org.onosproject.net.intent.Intent;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Map;
37import java.util.Set;
38import java.util.concurrent.ConcurrentHashMap;
39
40/**
41 * Implementation of the JSON codec brokering service.
42 */
43@Component(immediate = true)
44@Service
45public class CodecManager implements CodecService {
46
47 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
48
49 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
50
51 @Activate
52 public void activate() {
53 codecs.clear();
54 registerCodec(Annotations.class, new AnnotationsCodec());
55 registerCodec(Device.class, new DeviceCodec());
56 registerCodec(Port.class, new PortCodec());
57 registerCodec(ConnectPoint.class, new ConnectPointCodec());
58 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -080059 registerCodec(Host.class, new HostCodec());
60 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkey2b217142014-12-15 09:24:24 -080061 registerCodec(Intent.class, new IntentCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080062 log.info("Started");
63 }
64
65 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -080066 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080067 codecs.clear();
68 log.info("Stopped");
69 }
70
71 @Override
72 public Set<Class<?>> getCodecs() {
73 return ImmutableSet.copyOf(codecs.keySet());
74 }
75
76 @Override
77 @SuppressWarnings("unchecked")
78 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
79 return codecs.get(entityClass);
80 }
81
82 @Override
83 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
84 codecs.putIfAbsent(entityClass, codec);
85 }
86
87 @Override
88 public void unregisterCodec(Class<?> entityClass) {
89 codecs.remove(entityClass);
90 }
91
92}