Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bmv2.ctl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.bmv2.api.context.Bmv2DeviceContext; |
| 27 | import org.onosproject.bmv2.api.context.Bmv2FlowRuleTranslator; |
| 28 | import org.onosproject.bmv2.api.service.Bmv2DeviceContextService; |
| 29 | import org.onosproject.bmv2.api.service.Bmv2Controller; |
| 30 | import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent; |
| 31 | import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam; |
| 32 | import org.onosproject.bmv2.api.runtime.Bmv2FlowRuleWrapper; |
| 33 | import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam; |
| 34 | import org.onosproject.bmv2.api.runtime.Bmv2MatchKey; |
| 35 | import org.onosproject.bmv2.api.runtime.Bmv2ParsedTableEntry; |
| 36 | import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException; |
| 37 | import org.onosproject.bmv2.api.runtime.Bmv2TableEntryReference; |
| 38 | import org.onosproject.bmv2.api.service.Bmv2TableEntryService; |
| 39 | import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam; |
| 40 | import org.onosproject.bmv2.api.runtime.Bmv2ValidMatchParam; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.store.serializers.KryoNamespaces; |
| 43 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 44 | import org.onosproject.store.service.StorageService; |
| 45 | import org.onosproject.store.service.WallClockTimestamp; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| 49 | import java.util.Collections; |
| 50 | import java.util.List; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | |
| 54 | /** |
| 55 | * Implementation of the Bmv2TableEntryService. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | @Service |
| 59 | public 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 | } |