Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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 | |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 18 | import com.codahale.metrics.Metric; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 25 | import org.onosproject.cluster.ControllerNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.codec.CodecService; |
| 27 | import org.onosproject.codec.JsonCodec; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 28 | import org.onosproject.core.Application; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Annotations; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.Device; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Host; |
| 33 | import org.onosproject.net.HostLocation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Link; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Path; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Port; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 37 | import org.onosproject.net.device.PortStatistics; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 38 | import org.onosproject.net.driver.Driver; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowEntry; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRule; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.Criterion; |
| 45 | import org.onosproject.net.flow.instructions.Instruction; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 47 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.NextObjective; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 49 | import org.onosproject.net.group.Group; |
| 50 | import org.onosproject.net.group.GroupBucket; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 51 | import org.onosproject.net.intent.ConnectivityIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 52 | import org.onosproject.net.intent.Constraint; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 53 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 54 | import org.onosproject.net.intent.Intent; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 55 | import org.onosproject.net.intent.PointToPointIntent; |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 56 | import org.onosproject.net.key.DeviceKey; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 57 | import org.onosproject.net.mcast.McastRoute; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 58 | import org.onosproject.net.meter.Band; |
| 59 | import org.onosproject.net.meter.Meter; |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 60 | import org.onosproject.net.meter.MeterRequest; |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 61 | import org.onosproject.net.region.Region; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 62 | import org.onosproject.net.statistic.Load; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 63 | import org.onosproject.net.topology.Topology; |
| 64 | import org.onosproject.net.topology.TopologyCluster; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 65 | import org.slf4j.Logger; |
| 66 | import org.slf4j.LoggerFactory; |
| 67 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 68 | import java.util.Map; |
| 69 | import java.util.Set; |
| 70 | import java.util.concurrent.ConcurrentHashMap; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 71 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 72 | import static org.onosproject.security.AppGuard.checkPermission; |
| 73 | import static org.onosproject.security.AppPermission.Type.*; |
| 74 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Implementation of the JSON codec brokering service. |
| 77 | */ |
| 78 | @Component(immediate = true) |
| 79 | @Service |
| 80 | public class CodecManager implements CodecService { |
| 81 | |
| 82 | private static Logger log = LoggerFactory.getLogger(CodecManager.class); |
| 83 | |
| 84 | private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>(); |
| 85 | |
| 86 | @Activate |
| 87 | public void activate() { |
| 88 | codecs.clear(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 89 | registerCodec(Application.class, new ApplicationCodec()); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 90 | registerCodec(ControllerNode.class, new ControllerNodeCodec()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 91 | registerCodec(Annotations.class, new AnnotationsCodec()); |
| 92 | registerCodec(Device.class, new DeviceCodec()); |
| 93 | registerCodec(Port.class, new PortCodec()); |
| 94 | registerCodec(ConnectPoint.class, new ConnectPointCodec()); |
| 95 | registerCodec(Link.class, new LinkCodec()); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 96 | registerCodec(Host.class, new HostCodec()); |
| 97 | registerCodec(HostLocation.class, new HostLocationCodec()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 98 | registerCodec(HostToHostIntent.class, new HostToHostIntentCodec()); |
| 99 | registerCodec(PointToPointIntent.class, new PointToPointIntentCodec()); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 100 | registerCodec(Intent.class, new IntentCodec()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 101 | registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 102 | registerCodec(FlowEntry.class, new FlowEntryCodec()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 103 | registerCodec(FlowRule.class, new FlowRuleCodec()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 104 | registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec()); |
| 105 | registerCodec(TrafficSelector.class, new TrafficSelectorCodec()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 106 | registerCodec(Instruction.class, new InstructionCodec()); |
| 107 | registerCodec(Criterion.class, new CriterionCodec()); |
| 108 | registerCodec(Ethernet.class, new EthernetCodec()); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 109 | registerCodec(Constraint.class, new ConstraintCodec()); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 110 | registerCodec(Topology.class, new TopologyCodec()); |
| 111 | registerCodec(TopologyCluster.class, new TopologyClusterCodec()); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 112 | registerCodec(Path.class, new PathCodec()); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 113 | registerCodec(Group.class, new GroupCodec()); |
| 114 | registerCodec(Driver.class, new DriverCodec()); |
| 115 | registerCodec(GroupBucket.class, new GroupBucketCodec()); |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 116 | registerCodec(Load.class, new LoadCodec()); |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 117 | registerCodec(MeterRequest.class, new MeterRequestCodec()); |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 118 | registerCodec(Meter.class, new MeterCodec()); |
| 119 | registerCodec(Band.class, new MeterBandCodec()); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 120 | registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec()); |
Srikanth Vavilapalli | d120f5c | 2015-11-24 14:15:01 -0800 | [diff] [blame] | 121 | registerCodec(PortStatistics.class, new PortStatisticsCodec()); |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 122 | registerCodec(Metric.class, new MetricCodec()); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 123 | registerCodec(FilteringObjective.class, new FilteringObjectiveCodec()); |
| 124 | registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec()); |
| 125 | registerCodec(NextObjective.class, new NextObjectiveCodec()); |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 126 | registerCodec(McastRoute.class, new McastRouteCodec()); |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 127 | registerCodec(DeviceKey.class, new DeviceKeyCodec()); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 128 | registerCodec(Region.class, new RegionCodec()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 129 | log.info("Started"); |
| 130 | } |
| 131 | |
| 132 | @Deactivate |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 133 | public void deactivate() { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 134 | codecs.clear(); |
| 135 | log.info("Stopped"); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Set<Class<?>> getCodecs() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 140 | checkPermission(CODEC_READ); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 141 | return ImmutableSet.copyOf(codecs.keySet()); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | @SuppressWarnings("unchecked") |
| 146 | public <T> JsonCodec<T> getCodec(Class<T> entityClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 147 | checkPermission(CODEC_READ); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 148 | return codecs.get(entityClass); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 153 | checkPermission(CODEC_WRITE); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 154 | codecs.putIfAbsent(entityClass, codec); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void unregisterCodec(Class<?> entityClass) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 159 | checkPermission(CODEC_WRITE); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 160 | codecs.remove(entityClass); |
| 161 | } |
| 162 | |
| 163 | } |