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 | |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 18 | import java.util.Map; |
| 19 | import java.util.Set; |
| 20 | import java.util.concurrent.ConcurrentHashMap; |
| 21 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.codec.CodecService; |
| 28 | import org.onosproject.codec.JsonCodec; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import org.onosproject.core.Application; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Annotations; |
| 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.Device; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.HostLocation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Link; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Path; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowEntry; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.criteria.Criterion; |
| 42 | import org.onosproject.net.flow.instructions.Instruction; |
| 43 | import org.onosproject.net.intent.ConnectivityIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.Constraint; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.Intent; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 48 | import org.onosproject.net.topology.Topology; |
| 49 | import org.onosproject.net.topology.TopologyCluster; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 53 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Implementation of the JSON codec brokering service. |
| 57 | */ |
| 58 | @Component(immediate = true) |
| 59 | @Service |
| 60 | public class CodecManager implements CodecService { |
| 61 | |
| 62 | private static Logger log = LoggerFactory.getLogger(CodecManager.class); |
| 63 | |
| 64 | private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>(); |
| 65 | |
| 66 | @Activate |
| 67 | public void activate() { |
| 68 | codecs.clear(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 69 | registerCodec(Application.class, new ApplicationCodec()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 70 | registerCodec(Annotations.class, new AnnotationsCodec()); |
| 71 | registerCodec(Device.class, new DeviceCodec()); |
| 72 | registerCodec(Port.class, new PortCodec()); |
| 73 | registerCodec(ConnectPoint.class, new ConnectPointCodec()); |
| 74 | registerCodec(Link.class, new LinkCodec()); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 75 | registerCodec(Host.class, new HostCodec()); |
| 76 | registerCodec(HostLocation.class, new HostLocationCodec()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 77 | registerCodec(HostToHostIntent.class, new HostToHostIntentCodec()); |
| 78 | registerCodec(PointToPointIntent.class, new PointToPointIntentCodec()); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 79 | registerCodec(Intent.class, new IntentCodec()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 80 | registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 81 | registerCodec(FlowEntry.class, new FlowEntryCodec()); |
| 82 | registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec()); |
| 83 | registerCodec(TrafficSelector.class, new TrafficSelectorCodec()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 84 | registerCodec(Instruction.class, new InstructionCodec()); |
| 85 | registerCodec(Criterion.class, new CriterionCodec()); |
| 86 | registerCodec(Ethernet.class, new EthernetCodec()); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 87 | registerCodec(Constraint.class, new ConstraintCodec()); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 88 | registerCodec(Topology.class, new TopologyCodec()); |
| 89 | registerCodec(TopologyCluster.class, new TopologyClusterCodec()); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 90 | registerCodec(Path.class, new PathCodec()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 91 | log.info("Started"); |
| 92 | } |
| 93 | |
| 94 | @Deactivate |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 95 | public void deactivate() { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 96 | codecs.clear(); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Set<Class<?>> getCodecs() { |
| 102 | return ImmutableSet.copyOf(codecs.keySet()); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | @SuppressWarnings("unchecked") |
| 107 | public <T> JsonCodec<T> getCodec(Class<T> entityClass) { |
| 108 | return codecs.get(entityClass); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { |
| 113 | codecs.putIfAbsent(entityClass, codec); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void unregisterCodec(Class<?> entityClass) { |
| 118 | codecs.remove(entityClass); |
| 119 | } |
| 120 | |
| 121 | } |