blob: 68a2d208ecfbf51d372c4ccf0c5ad3b537c1bed2 [file] [log] [blame]
Carmelo Cascone00a59962017-06-16 17:51:49 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone00a59962017-06-16 17:51:49 +09003 *
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 */
16
17package org.onosproject.net.pi.impl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.net.Device;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020026import org.onosproject.net.DeviceId;
Carmelo Cascone00a59962017-06-16 17:51:49 +090027import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.flow.FlowRule;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020029import org.onosproject.net.group.Group;
Carmelo Cascone00a59962017-06-16 17:51:49 +090030import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020031import org.onosproject.net.pi.runtime.PiActionGroup;
Carmelo Cascone00a59962017-06-16 17:51:49 +090032import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020033import org.onosproject.net.pi.runtime.PiTranslationService;
Carmelo Cascone00a59962017-06-16 17:51:49 +090034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Carmelo Cascone00a59962017-06-16 17:51:49 +090037/**
Carmelo Cascone87b9b392017-10-02 18:33:20 +020038 * Implementation of the protocol-independent translation service.
Carmelo Cascone00a59962017-06-16 17:51:49 +090039 */
40@Component(immediate = true)
41@Service
Carmelo Cascone87b9b392017-10-02 18:33:20 +020042public class PiTranslationServiceImpl implements PiTranslationService {
Carmelo Cascone00a59962017-06-16 17:51:49 +090043
44 private final Logger log = LoggerFactory.getLogger(this.getClass());
45
Carmelo Cascone87b9b392017-10-02 18:33:20 +020046 // TODO: implement cache to speed up translation.
Carmelo Cascone00a59962017-06-16 17:51:49 +090047
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected DeviceService deviceService;
50
51 @Activate
52 public void activate() {
53 log.info("Started");
54 }
55
56 @Deactivate
57 public void deactivate() {
58 log.info("Stopped");
59 }
60
61 @Override
Carmelo Cascone87b9b392017-10-02 18:33:20 +020062 public PiTableEntry translateFlowRule(FlowRule rule, PiPipeconf pipeconf) throws PiTranslationException {
63 return PiFlowRuleTranslator.translate(rule, pipeconf, getDevice(rule.deviceId()));
64 }
Carmelo Cascone00a59962017-06-16 17:51:49 +090065
Carmelo Cascone87b9b392017-10-02 18:33:20 +020066 @Override
67 public PiActionGroup translateGroup(Group group, PiPipeconf pipeconf) throws PiTranslationException {
68 return PiGroupTranslator.translate(group, pipeconf, getDevice(group.deviceId()));
69 }
70
71 private Device getDevice(DeviceId deviceId) throws PiTranslationException {
72 final Device device = deviceService.getDevice(deviceId);
Carmelo Cascone00a59962017-06-16 17:51:49 +090073 if (device == null) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +020074 throw new PiTranslationException("Unable to get device " + deviceId);
Carmelo Cascone00a59962017-06-16 17:51:49 +090075 }
Carmelo Cascone87b9b392017-10-02 18:33:20 +020076 return device;
Carmelo Cascone00a59962017-06-16 17:51:49 +090077 }
78}
79