Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.codec.impl; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.codec.CodecService; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.net.Annotations; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.Device; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame^] | 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostLocation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.Port; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.util.Map; |
| 36 | import java.util.Set; |
| 37 | import java.util.concurrent.ConcurrentHashMap; |
| 38 | |
| 39 | /** |
| 40 | * Implementation of the JSON codec brokering service. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public 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 Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame^] | 58 | registerCodec(Host.class, new HostCodec()); |
| 59 | registerCodec(HostLocation.class, new HostLocationCodec()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 60 | log.info("Started"); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame^] | 64 | public void deactivate() { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 65 | 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 | } |