blob: b6d90f0d4665cd42645970ce63b908dfeba3cdf3 [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 Cascone1a7e4f92017-11-20 23:04:02 -080031import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020032import org.onosproject.net.pi.runtime.PiActionGroup;
Carmelo Cascone00a59962017-06-16 17:51:49 +090033import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080034import org.onosproject.net.pi.service.PiTranslatable;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080035import org.onosproject.net.pi.service.PiTranslationService;
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080036import org.onosproject.net.pi.service.PiTranslationStore;
Carmelo Cascone00a59962017-06-16 17:51:49 +090037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080040import java.util.Optional;
41
Carmelo Cascone00a59962017-06-16 17:51:49 +090042/**
Carmelo Cascone87b9b392017-10-02 18:33:20 +020043 * Implementation of the protocol-independent translation service.
Carmelo Cascone00a59962017-06-16 17:51:49 +090044 */
45@Component(immediate = true)
46@Service
Carmelo Cascone87b9b392017-10-02 18:33:20 +020047public class PiTranslationServiceImpl implements PiTranslationService {
Carmelo Cascone00a59962017-06-16 17:51:49 +090048
49 private final Logger log = LoggerFactory.getLogger(this.getClass());
50
Carmelo Cascone87b9b392017-10-02 18:33:20 +020051 // TODO: implement cache to speed up translation.
Carmelo Cascone00a59962017-06-16 17:51:49 +090052
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected DeviceService deviceService;
55
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 private PiTranslationStore translationStore;
58
Carmelo Cascone00a59962017-06-16 17:51:49 +090059 @Activate
60 public void activate() {
61 log.info("Started");
62 }
63
64 @Deactivate
65 public void deactivate() {
66 log.info("Stopped");
67 }
68
69 @Override
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080070 public PiTableEntry translate(FlowRule rule, PiPipeconf pipeconf)
71 throws PiTranslationException {
72 final PiTableEntry piTableEntry = PiFlowRuleTranslator
73 .translate(rule, pipeconf, getDevice(rule.deviceId()));
74 translationStore.addOrUpdate(rule, piTableEntry, pipeconf.id());
75 return piTableEntry;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020076 }
Carmelo Cascone00a59962017-06-16 17:51:49 +090077
Carmelo Cascone87b9b392017-10-02 18:33:20 +020078 @Override
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080079 public Optional<FlowRule> lookup(PiTableEntry piTableEntry,
80 PiPipeconfId pipeconfId) {
81 final PiTranslatable original = translationStore
82 .lookup(piTableEntry, pipeconfId);
83 return original == null
84 ? Optional.empty()
85 : Optional.of((FlowRule) original);
86 }
87
88 @Override
89 public PiActionGroup translate(Group group, PiPipeconf pipeconf)
90 throws PiTranslationException {
91 return PiGroupTranslator.translate(group, pipeconf,
92 getDevice(group.deviceId()));
93 }
94
95 @Override
96 public Optional<Group> lookup(PiActionGroup piActionGroup,
97 PiPipeconfId pipeconfId) {
98 // TODO: implement learning and lookup of groups
99 return Optional.empty();
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200100 }
101
102 private Device getDevice(DeviceId deviceId) throws PiTranslationException {
103 final Device device = deviceService.getDevice(deviceId);
Carmelo Cascone00a59962017-06-16 17:51:49 +0900104 if (device == null) {
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -0800105 throw new PiTranslationException(
106 "Unable to get device " + deviceId);
Carmelo Cascone00a59962017-06-16 17:51:49 +0900107 }
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200108 return device;
Carmelo Cascone00a59962017-06-16 17:51:49 +0900109 }
110}
111