2 * Copyright (c) 2017 Intel Corporation and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.neutron.transcriber;
10 import com.google.common.collect.ImmutableBiMap;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.neutron.spi.INeutronTapFlowCRUD;
21 import org.opendaylight.neutron.spi.NeutronTapFlow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionBoth;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionIn;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionOut;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.TapServiceAttributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.TapFlows;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.tap.flows.TapFlow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.tap.flows.TapFlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.tap.flows.TapFlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.services.attributes.TapServices;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.services.attributes.tap.services.TapService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.services.attributes.tap.services.TapServiceKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 @OsgiServiceProvider(classes = INeutronTapFlowCRUD.class)
42 public final class NeutronTapFlowInterface
43 extends AbstractTranscriberInterface<TapFlow, TapFlows, TapFlowKey, NeutronTapFlow, TapServiceAttributes>
44 implements INeutronTapFlowCRUD {
46 private static final Logger LOG = LoggerFactory.getLogger(NeutronTapFlowInterface.class);
48 private static final ImmutableBiMap<Class<? extends DirectionBase>,
49 String> DIRECTION_MAP = new ImmutableBiMap.Builder<Class<? extends DirectionBase>, String>()
50 .put(DirectionOut.class, "OUT")
51 .put(DirectionIn.class, "IN")
52 .put(DirectionBoth.class, "BOTH").build();
55 public NeutronTapFlowInterface(DataBroker db) {
56 super(TapFlowBuilder.class, db);
59 protected InstanceIdentifier<TapFlow> createTapFlowInstanceIdentifier(String tapServiceUUID, TapFlow item) {
60 return InstanceIdentifier.create(Neutron.class)
61 .child(TapServices.class)
62 .child(TapService.class, new TapServiceKey(toUuid(tapServiceUUID)))
63 .child(TapFlows.class).child(TapFlow.class, item.key());
67 protected List<TapFlow> getDataObjectList(TapFlows flows) {
68 return flows.getTapFlow();
72 protected NeutronTapFlow fromMd(TapFlow flow) {
73 final NeutronTapFlow answer = new NeutronTapFlow();
74 fromMdBaseAttributes(flow, answer);
75 if (flow.getTapServiceId() != null) {
76 answer.setTapFlowServiceID(flow.getTapServiceId().getValue());
78 if (flow.getSourcePort() != null) {
79 answer.setTapFlowSourcePort(flow.getSourcePort().getValue());
81 if (flow.getDirection() != null) {
82 answer.setTapFlowDirection(DIRECTION_MAP.get(flow.getDirection()));
89 protected TapFlow toMd(NeutronTapFlow flow) {
90 final TapFlowBuilder flowBuilder = new TapFlowBuilder();
91 toMdBaseAttributes(flow, flowBuilder);
92 if (flow.getTapFlowServiceID() != null) {
93 flowBuilder.setTapServiceId(toUuid(flow.getTapFlowServiceID()));
95 if (flow.getTapFlowSourcePort() != null) {
96 flowBuilder.setSourcePort(toUuid(flow.getTapFlowSourcePort()));
98 if (flow.getTapFlowDirection() != null) {
99 final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
100 flowBuilder.setDirection(mapper.get(flow.getTapFlowDirection()));
103 return flowBuilder.build();
107 public boolean tapFlowExists(String tapServiceUUID, String tapFlowUUID) {
108 final TapFlow dataObject = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
109 return dataObject != null;
112 private boolean tapServiceExists(String tapServiceUUID) {
113 final TapService tapService = readMd(InstanceIdentifier.create(Neutron.class).child(TapServices.class)
114 .child(TapService.class, new TapServiceKey(toUuid(tapServiceUUID))));
115 return tapService != null;
119 private boolean updateTapFlowMd(NeutronTapFlow tapFlow) {
120 final WriteTransaction transaction = getDataBroker().newWriteOnlyTransaction();
121 final TapFlow item = toMd(tapFlow);
122 final InstanceIdentifier<TapFlow> iid = createTapFlowInstanceIdentifier(tapFlow.getTapFlowServiceID(), item);
123 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, item, true);
124 final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
127 } catch (InterruptedException | ExecutionException e) {
128 LOG.warn("Transaction Failed ", e);
134 private boolean removeTapFlowMd(String tapServiceUUID, String tapFlowUUID) {
135 final WriteTransaction transaction = getDataBroker().newWriteOnlyTransaction();
136 final InstanceIdentifier<TapFlow> iid = createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID));
137 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
138 final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
141 } catch (InterruptedException | ExecutionException e) {
142 LOG.warn("Transation failed ", e);
148 private boolean addTapFlowMd(NeutronTapFlow tapFlow) {
149 return updateTapFlowMd(tapFlow);
153 public boolean updateTapFlow(NeutronTapFlow tapFlow) {
154 return updateTapFlowMd(tapFlow);
158 public boolean addTapFlow(NeutronTapFlow tapFlow) {
159 if (!tapServiceExists(tapFlow.getTapFlowServiceID())) {
162 if (tapFlowExists(tapFlow.getTapFlowServiceID(), tapFlow.getID())) {
165 addTapFlowMd(tapFlow);
170 public NeutronTapFlow getTapFlow(String tapServiceUUID, String tapFlowUUID) {
171 final TapFlow tapFlow = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
172 if (tapFlow == null) {
175 return fromMd(tapFlow);
179 public boolean deleteTapFlow(String tapServiceUUID, String tapFlowUUID) {
180 return removeTapFlowMd(tapServiceUUID, tapFlowUUID);