blob: 090727fe597b40d62b96666001d4789c161e9d3e [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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
Jian Li72315152015-12-10 17:20:43 -080018import com.codahale.metrics.Metric;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070019import com.google.common.collect.ImmutableSet;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Service;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080024import org.onlab.packet.Ethernet;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070025import org.onosproject.cluster.ControllerNode;
Jian Lib68a2b02016-05-02 11:23:32 -070026import org.onosproject.cluster.RoleInfo;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.codec.CodecService;
28import org.onosproject.codec.JsonCodec;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import org.onosproject.core.Application;
Jian Lie1c1c8d2016-05-09 16:24:40 -070030import org.onosproject.core.ApplicationId;
Claudine Chiufb8b8162016-04-01 23:50:51 +000031import org.onosproject.incubator.net.virtual.TenantId;
32import org.onosproject.incubator.net.virtual.VirtualDevice;
Claudine Chiu25f07be2016-06-27 16:21:21 +000033import org.onosproject.incubator.net.virtual.VirtualHost;
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000034import org.onosproject.incubator.net.virtual.VirtualLink;
Claudine Chiufb8b8162016-04-01 23:50:51 +000035import org.onosproject.incubator.net.virtual.VirtualNetwork;
36import org.onosproject.incubator.net.virtual.VirtualPort;
Jian Lib68a2b02016-05-02 11:23:32 -070037import org.onosproject.mastership.MastershipTerm;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.Annotations;
39import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.Device;
Ray Milkey1f95bd32014-12-10 11:11:00 -080041import org.onosproject.net.Host;
42import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.Link;
Jian Lib68a2b02016-05-02 11:23:32 -070044import org.onosproject.net.MastershipRole;
Ray Milkey19ffea32015-01-28 10:03:06 -080045import org.onosproject.net.Path;
Jayasree Ghoshd3ff5402016-08-17 18:41:19 +053046import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.Port;
Jian Liecb3c0f2015-12-15 10:07:49 -080048import org.onosproject.net.device.PortStatistics;
Ray Milkey39616f32015-05-14 15:43:00 -070049import org.onosproject.net.driver.Driver;
Ray Milkey4f5de002014-12-17 19:26:11 -080050import org.onosproject.net.flow.FlowEntry;
Ray Milkeyd43fe452015-05-29 09:35:12 -070051import org.onosproject.net.flow.FlowRule;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070052import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkey4f5de002014-12-17 19:26:11 -080053import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080055import org.onosproject.net.flow.criteria.Criterion;
56import org.onosproject.net.flow.instructions.Instruction;
Jian Li8bcef8b2016-01-06 11:35:53 -080057import org.onosproject.net.flowobjective.FilteringObjective;
58import org.onosproject.net.flowobjective.ForwardingObjective;
59import org.onosproject.net.flowobjective.NextObjective;
Ray Milkey39616f32015-05-14 15:43:00 -070060import org.onosproject.net.group.Group;
61import org.onosproject.net.group.GroupBucket;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080062import org.onosproject.net.intent.ConnectivityIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080063import org.onosproject.net.intent.Constraint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080064import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080065import org.onosproject.net.intent.Intent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080066import org.onosproject.net.intent.PointToPointIntent;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090067import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Brian Stankeb9170d92016-02-19 14:18:42 -050068import org.onosproject.net.key.DeviceKey;
Jonathan Hart07eb0412016-02-08 16:42:29 -080069import org.onosproject.net.mcast.McastRoute;
Jian Li5c411232015-12-16 15:29:16 -080070import org.onosproject.net.meter.Band;
71import org.onosproject.net.meter.Meter;
Jian Li95678962016-01-26 17:17:52 -080072import org.onosproject.net.meter.MeterRequest;
Jian Lib68b9a82016-02-23 10:25:54 +090073import org.onosproject.net.region.Region;
Ray Milkeyb9af9462015-07-17 11:12:09 -070074import org.onosproject.net.statistic.Load;
Ray Milkey82e50312015-01-22 17:01:42 -080075import org.onosproject.net.topology.Topology;
76import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080077import org.slf4j.Logger;
78import org.slf4j.LoggerFactory;
79
Thomas Vachuskade563cf2015-04-01 00:28:50 -070080import java.util.Map;
81import java.util.Set;
82import java.util.concurrent.ConcurrentHashMap;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080083
Heedo Kang4a47a302016-02-29 17:40:23 +090084import static org.onosproject.security.AppGuard.checkPermission;
85import static org.onosproject.security.AppPermission.Type.*;
86
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080087/**
88 * Implementation of the JSON codec brokering service.
89 */
90@Component(immediate = true)
91@Service
92public class CodecManager implements CodecService {
93
94 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
95
96 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
97
98 @Activate
99 public void activate() {
100 codecs.clear();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101 registerCodec(Application.class, new ApplicationCodec());
Jian Lie1c1c8d2016-05-09 16:24:40 -0700102 registerCodec(ApplicationId.class, new ApplicationIdCodec());
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700103 registerCodec(ControllerNode.class, new ControllerNodeCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800104 registerCodec(Annotations.class, new AnnotationsCodec());
105 registerCodec(Device.class, new DeviceCodec());
106 registerCodec(Port.class, new PortCodec());
107 registerCodec(ConnectPoint.class, new ConnectPointCodec());
108 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -0800109 registerCodec(Host.class, new HostCodec());
110 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800111 registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
112 registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
Chiara Contolia8f69ff2016-07-28 01:06:07 +0900113 registerCodec(SinglePointToMultiPointIntent.class, new SinglePointToMultiPointIntentCodec());
Ray Milkey2b217142014-12-15 09:24:24 -0800114 registerCodec(Intent.class, new IntentCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800115 registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -0800116 registerCodec(FlowEntry.class, new FlowEntryCodec());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700117 registerCodec(FlowRule.class, new FlowRuleCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -0800118 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
119 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800120 registerCodec(Instruction.class, new InstructionCodec());
121 registerCodec(Criterion.class, new CriterionCodec());
122 registerCodec(Ethernet.class, new EthernetCodec());
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800123 registerCodec(Constraint.class, new ConstraintCodec());
Ray Milkey82e50312015-01-22 17:01:42 -0800124 registerCodec(Topology.class, new TopologyCodec());
125 registerCodec(TopologyCluster.class, new TopologyClusterCodec());
Ray Milkey19ffea32015-01-28 10:03:06 -0800126 registerCodec(Path.class, new PathCodec());
Jayasree Ghoshd3ff5402016-08-17 18:41:19 +0530127 registerCodec(DisjointPath.class, new DisjointPathCodec());
Ray Milkey39616f32015-05-14 15:43:00 -0700128 registerCodec(Group.class, new GroupCodec());
129 registerCodec(Driver.class, new DriverCodec());
130 registerCodec(GroupBucket.class, new GroupBucketCodec());
Ray Milkeyb9af9462015-07-17 11:12:09 -0700131 registerCodec(Load.class, new LoadCodec());
Jian Li95678962016-01-26 17:17:52 -0800132 registerCodec(MeterRequest.class, new MeterRequestCodec());
Jian Li5c411232015-12-16 15:29:16 -0800133 registerCodec(Meter.class, new MeterCodec());
134 registerCodec(Band.class, new MeterBandCodec());
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700135 registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
Srikanth Vavilapallid120f5c2015-11-24 14:15:01 -0800136 registerCodec(PortStatistics.class, new PortStatisticsCodec());
Jian Li72315152015-12-10 17:20:43 -0800137 registerCodec(Metric.class, new MetricCodec());
Jian Li8bcef8b2016-01-06 11:35:53 -0800138 registerCodec(FilteringObjective.class, new FilteringObjectiveCodec());
139 registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
140 registerCodec(NextObjective.class, new NextObjectiveCodec());
Jonathan Hart07eb0412016-02-08 16:42:29 -0800141 registerCodec(McastRoute.class, new McastRouteCodec());
Brian Stankeb9170d92016-02-19 14:18:42 -0500142 registerCodec(DeviceKey.class, new DeviceKeyCodec());
Jian Lib68b9a82016-02-23 10:25:54 +0900143 registerCodec(Region.class, new RegionCodec());
Claudine Chiufb8b8162016-04-01 23:50:51 +0000144 registerCodec(TenantId.class, new TenantIdCodec());
145 registerCodec(VirtualNetwork.class, new VirtualNetworkCodec());
146 registerCodec(VirtualDevice.class, new VirtualDeviceCodec());
147 registerCodec(VirtualPort.class, new VirtualPortCodec());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000148 registerCodec(VirtualLink.class, new VirtualLinkCodec());
Claudine Chiu25f07be2016-06-27 16:21:21 +0000149 registerCodec(VirtualHost.class, new VirtualHostCodec());
Jian Lib68a2b02016-05-02 11:23:32 -0700150 registerCodec(MastershipTerm.class, new MastershipTermCodec());
151 registerCodec(MastershipRole.class, new MastershipRoleCodec());
152 registerCodec(RoleInfo.class, new RoleInfoCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800153 log.info("Started");
154 }
155
156 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -0800157 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800158 codecs.clear();
159 log.info("Stopped");
160 }
161
162 @Override
163 public Set<Class<?>> getCodecs() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900164 checkPermission(CODEC_READ);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800165 return ImmutableSet.copyOf(codecs.keySet());
166 }
167
168 @Override
169 @SuppressWarnings("unchecked")
170 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900171 checkPermission(CODEC_READ);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800172 return codecs.get(entityClass);
173 }
174
175 @Override
176 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900177 checkPermission(CODEC_WRITE);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800178 codecs.putIfAbsent(entityClass, codec);
179 }
180
181 @Override
182 public void unregisterCodec(Class<?> entityClass) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900183 checkPermission(CODEC_WRITE);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800184 codecs.remove(entityClass);
185 }
186
187}