blob: 8258334195288757b917bab9ee3df1145c655b91 [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;
26import org.onosproject.bmv2.api.context.Bmv2DeviceContext;
27import org.onosproject.bmv2.api.context.Bmv2FlowRuleTranslator;
28import org.onosproject.bmv2.api.service.Bmv2DeviceContextService;
29import org.onosproject.bmv2.api.service.Bmv2Controller;
30import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent;
31import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
32import org.onosproject.bmv2.api.runtime.Bmv2FlowRuleWrapper;
33import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam;
34import org.onosproject.bmv2.api.runtime.Bmv2MatchKey;
35import org.onosproject.bmv2.api.runtime.Bmv2ParsedTableEntry;
36import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
37import org.onosproject.bmv2.api.runtime.Bmv2TableEntryReference;
38import org.onosproject.bmv2.api.service.Bmv2TableEntryService;
39import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam;
40import org.onosproject.bmv2.api.runtime.Bmv2ValidMatchParam;
41import org.onosproject.net.DeviceId;
42import org.onosproject.store.serializers.KryoNamespaces;
43import org.onosproject.store.service.EventuallyConsistentMap;
44import org.onosproject.store.service.StorageService;
45import org.onosproject.store.service.WallClockTimestamp;
46import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.Collections;
50import java.util.List;
51
52import static com.google.common.base.Preconditions.checkNotNull;
53
54/**
55 * Implementation of the Bmv2TableEntryService.
56 */
57@Component(immediate = true)
58@Service
59public class Bmv2TableEntryServiceImpl implements Bmv2TableEntryService {
60
61 private final Logger log = LoggerFactory.getLogger(this.getClass());
62
63 private final Bmv2FlowRuleTranslator translator = new Bmv2FlowRuleTranslatorImpl();
64
65 private EventuallyConsistentMap<Bmv2TableEntryReference, Bmv2FlowRuleWrapper> flowRules;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected StorageService storageService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected Bmv2Controller controller;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected Bmv2DeviceContextService contextService;
75
76
77 @Activate
78 public void activate() {
79 KryoNamespace kryo = new KryoNamespace.Builder()
80 .register(KryoNamespaces.API)
81 .register(Bmv2TableEntryReference.class)
82 .register(Bmv2MatchKey.class)
83 .register(Bmv2ExactMatchParam.class)
84 .register(Bmv2TernaryMatchParam.class)
85 .register(Bmv2LpmMatchParam.class)
86 .register(Bmv2ValidMatchParam.class)
87 .register(Bmv2FlowRuleWrapper.class)
88 .build();
89
90 flowRules = storageService.<Bmv2TableEntryReference, Bmv2FlowRuleWrapper>eventuallyConsistentMapBuilder()
91 .withSerializer(kryo)
92 .withTimestampProvider((k, v) -> new WallClockTimestamp())
93 .withName("onos-bmv2-flowrules")
94 .build();
95
96 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
101 log.info("Stopped");
102 }
103
104 @Override
105 public Bmv2FlowRuleTranslator getFlowRuleTranslator() {
106 return translator;
107 }
108
109 @Override
110 public List<Bmv2ParsedTableEntry> getTableEntries(DeviceId deviceId, String tableName) {
111 try {
112 Bmv2DeviceContext context = contextService.getContext(deviceId);
113 if (context == null) {
114 log.warn("Unable to get table entries, found null context for {}", deviceId);
115 return Collections.emptyList();
116 }
117 Bmv2DeviceAgent agent = controller.getAgent(deviceId);
118 String tableDump = agent.dumpTable(tableName);
119 return Bmv2TableDumpParser.parse(tableDump, context.configuration());
120 } catch (Bmv2RuntimeException e) {
121 log.warn("Unable to get table entries for {}: {}", deviceId, e.explain());
122 return Collections.emptyList();
123 }
124 }
125
126 @Override
127 public Bmv2FlowRuleWrapper lookupEntryReference(Bmv2TableEntryReference entryRef) {
128 checkNotNull(entryRef, "table entry reference cannot be null");
129 return flowRules.get(entryRef);
130 }
131
132 @Override
133 public void bindEntryReference(Bmv2TableEntryReference entryRef, Bmv2FlowRuleWrapper rule) {
134 checkNotNull(entryRef, "table entry reference cannot be null");
135 checkNotNull(rule, "bmv2 flow rule cannot be null");
136 flowRules.put(entryRef, rule);
137 }
138
139 @Override
140 public void unbindEntryReference(Bmv2TableEntryReference entryRef) {
141 checkNotNull(entryRef, "table entry reference cannot be null");
142 flowRules.remove(entryRef);
143 }
144}