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; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame^] | 32 | import org.onosproject.net.flow.FlowEntry; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.Intent; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.Map; |
| 40 | import java.util.Set; |
| 41 | import java.util.concurrent.ConcurrentHashMap; |
| 42 | |
| 43 | /** |
| 44 | * Implementation of the JSON codec brokering service. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class CodecManager implements CodecService { |
| 49 | |
| 50 | private static Logger log = LoggerFactory.getLogger(CodecManager.class); |
| 51 | |
| 52 | private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>(); |
| 53 | |
| 54 | @Activate |
| 55 | public void activate() { |
| 56 | codecs.clear(); |
| 57 | registerCodec(Annotations.class, new AnnotationsCodec()); |
| 58 | registerCodec(Device.class, new DeviceCodec()); |
| 59 | registerCodec(Port.class, new PortCodec()); |
| 60 | registerCodec(ConnectPoint.class, new ConnectPointCodec()); |
| 61 | registerCodec(Link.class, new LinkCodec()); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 62 | registerCodec(Host.class, new HostCodec()); |
| 63 | registerCodec(HostLocation.class, new HostLocationCodec()); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 64 | registerCodec(Intent.class, new IntentCodec()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame^] | 65 | registerCodec(FlowEntry.class, new FlowEntryCodec()); |
| 66 | registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec()); |
| 67 | registerCodec(TrafficSelector.class, new TrafficSelectorCodec()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 68 | log.info("Started"); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 72 | public void deactivate() { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 73 | codecs.clear(); |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Set<Class<?>> getCodecs() { |
| 79 | return ImmutableSet.copyOf(codecs.keySet()); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | @SuppressWarnings("unchecked") |
| 84 | public <T> JsonCodec<T> getCodec(Class<T> entityClass) { |
| 85 | return codecs.get(entityClass); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { |
| 90 | codecs.putIfAbsent(entityClass, codec); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void unregisterCodec(Class<?> entityClass) { |
| 95 | codecs.remove(entityClass); |
| 96 | } |
| 97 | |
| 98 | } |