blob: 2afb99f2542122461501eb15400188da7d09d55f [file] [log] [blame]
Carmelo Cascone17fc9e42016-05-31 11:29:21 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.bmv2.ctl;
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.onlab.util.KryoNamespace;
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070026import org.onosproject.bmv2.api.context.Bmv2FlowRuleTranslator;
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070027import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
28import org.onosproject.bmv2.api.runtime.Bmv2FlowRuleWrapper;
29import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam;
30import org.onosproject.bmv2.api.runtime.Bmv2MatchKey;
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070031import org.onosproject.bmv2.api.runtime.Bmv2TableEntryReference;
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070032import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam;
33import org.onosproject.bmv2.api.runtime.Bmv2ValidMatchParam;
Carmelo Cascone25f18882016-06-14 19:16:50 -070034import org.onosproject.bmv2.api.service.Bmv2Controller;
35import org.onosproject.bmv2.api.service.Bmv2DeviceContextService;
36import org.onosproject.bmv2.api.service.Bmv2TableEntryService;
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070037import org.onosproject.store.serializers.KryoNamespaces;
38import org.onosproject.store.service.EventuallyConsistentMap;
39import org.onosproject.store.service.StorageService;
40import org.onosproject.store.service.WallClockTimestamp;
41import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070044import static com.google.common.base.Preconditions.checkNotNull;
45
46/**
47 * Implementation of the Bmv2TableEntryService.
48 */
49@Component(immediate = true)
50@Service
51public class Bmv2TableEntryServiceImpl implements Bmv2TableEntryService {
52
53 private final Logger log = LoggerFactory.getLogger(this.getClass());
54
55 private final Bmv2FlowRuleTranslator translator = new Bmv2FlowRuleTranslatorImpl();
56
57 private EventuallyConsistentMap<Bmv2TableEntryReference, Bmv2FlowRuleWrapper> flowRules;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected Bmv2Controller controller;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected Bmv2DeviceContextService contextService;
67
68
69 @Activate
70 public void activate() {
71 KryoNamespace kryo = new KryoNamespace.Builder()
72 .register(KryoNamespaces.API)
73 .register(Bmv2TableEntryReference.class)
74 .register(Bmv2MatchKey.class)
75 .register(Bmv2ExactMatchParam.class)
76 .register(Bmv2TernaryMatchParam.class)
77 .register(Bmv2LpmMatchParam.class)
78 .register(Bmv2ValidMatchParam.class)
79 .register(Bmv2FlowRuleWrapper.class)
80 .build();
81
82 flowRules = storageService.<Bmv2TableEntryReference, Bmv2FlowRuleWrapper>eventuallyConsistentMapBuilder()
83 .withSerializer(kryo)
84 .withTimestampProvider((k, v) -> new WallClockTimestamp())
85 .withName("onos-bmv2-flowrules")
86 .build();
87
88 log.info("Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
93 log.info("Stopped");
94 }
95
96 @Override
97 public Bmv2FlowRuleTranslator getFlowRuleTranslator() {
98 return translator;
99 }
100
101 @Override
Carmelo Cascone17fc9e42016-05-31 11:29:21 -0700102 public Bmv2FlowRuleWrapper lookupEntryReference(Bmv2TableEntryReference entryRef) {
103 checkNotNull(entryRef, "table entry reference cannot be null");
104 return flowRules.get(entryRef);
105 }
106
107 @Override
108 public void bindEntryReference(Bmv2TableEntryReference entryRef, Bmv2FlowRuleWrapper rule) {
109 checkNotNull(entryRef, "table entry reference cannot be null");
110 checkNotNull(rule, "bmv2 flow rule cannot be null");
111 flowRules.put(entryRef, rule);
112 }
113
114 @Override
115 public void unbindEntryReference(Bmv2TableEntryReference entryRef) {
116 checkNotNull(entryRef, "table entry reference cannot be null");
117 flowRules.remove(entryRef);
118 }
119}