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
9 package org.opendaylight.neutron.transcriber;
11 import com.google.common.collect.ImmutableBiMap;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.neutron.spi.INeutronTapFlowCRUD;
20 import org.opendaylight.neutron.spi.NeutronTapFlow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionBoth;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionIn;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.DirectionOut;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.TapServiceAttributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.TapFlows;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.tap.flows.TapFlow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.tap.flows.TapFlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.service.attributes.tap.flows.TapFlowKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.services.attributes.TapServices;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.services.attributes.tap.services.TapService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.tapaas.rev171024.tap.services.attributes.tap.services.TapServiceKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public final class NeutronTapFlowInterface
39 extends AbstractTranscriberInterface<TapFlow, TapFlows, TapFlowKey, NeutronTapFlow, TapServiceAttributes>
40 implements INeutronTapFlowCRUD {
42 private static final Logger LOG = LoggerFactory.getLogger(NeutronTapFlowInterface.class);
44 private static final ImmutableBiMap<Class<? extends DirectionBase>,
45 String> DIRECTION_MAP = new ImmutableBiMap.Builder<Class<? extends DirectionBase>, String>()
46 .put(DirectionOut.class, "OUT")
47 .put(DirectionIn.class, "IN")
48 .put(DirectionBoth.class, "BOTH").build();
50 NeutronTapFlowInterface(DataBroker db) {
51 super(TapFlowBuilder.class, db);
54 protected InstanceIdentifier<TapFlow> createTapFlowInstanceIdentifier(String tapServiceUUID, TapFlow item) {
55 return InstanceIdentifier.create(Neutron.class)
56 .child(TapServices.class)
57 .child(TapService.class, new TapServiceKey(toUuid(tapServiceUUID)))
58 .child(TapFlows.class).child(TapFlow.class, item.getKey());
62 protected List<TapFlow> getDataObjectList(TapFlows flows) {
63 return flows.getTapFlow();
67 protected NeutronTapFlow fromMd(TapFlow flow) {
68 final NeutronTapFlow answer = new NeutronTapFlow();
69 fromMdBaseAttributes(flow, answer);
70 if (flow.getTapServiceId() != null) {
71 answer.setTapFlowServiceID(flow.getTapServiceId().getValue());
73 if (flow.getSourcePort() != null) {
74 answer.setTapFlowSourcePort(flow.getSourcePort().getValue());
76 if (flow.getDirection() != null) {
77 answer.setTapFlowDirection(DIRECTION_MAP.get(flow.getDirection()));
84 protected TapFlow toMd(NeutronTapFlow flow) {
85 final TapFlowBuilder flowBuilder = new TapFlowBuilder();
86 toMdBaseAttributes(flow, flowBuilder);
87 if (flow.getTapFlowServiceID() != null) {
88 flowBuilder.setTapServiceId(toUuid(flow.getTapFlowServiceID()));
90 if (flow.getTapFlowSourcePort() != null) {
91 flowBuilder.setSourcePort(toUuid(flow.getTapFlowSourcePort()));
93 if (flow.getTapFlowDirection() != null) {
94 final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
95 flowBuilder.setDirection(mapper.get(flow.getTapFlowDirection()));
98 return flowBuilder.build();
101 public boolean tapFlowExists(String tapServiceUUID, String tapFlowUUID) {
102 final TapFlow dataObject = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
103 return dataObject != null;
106 private boolean tapServiceExists(String tapServiceUUID) {
107 final TapService tapService = readMd(InstanceIdentifier.create(Neutron.class).child(TapServices.class)
108 .child(TapService.class, new TapServiceKey(toUuid(tapServiceUUID))));
109 return tapService != null;
113 private boolean updateTapFlowMd(NeutronTapFlow tapFlow) {
114 final WriteTransaction transaction = getDataBroker().newWriteOnlyTransaction();
115 final TapFlow item = toMd(tapFlow);
116 final InstanceIdentifier<TapFlow> iid = createTapFlowInstanceIdentifier(tapFlow.getTapFlowServiceID(), item);
117 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, item, true);
118 final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
121 } catch (InterruptedException | ExecutionException e) {
122 LOG.warn("Transaction Failed ", e);
128 private boolean removeTapFlowMd(String tapServiceUUID, String tapFlowUUID) {
129 final WriteTransaction transaction = getDataBroker().newWriteOnlyTransaction();
130 final InstanceIdentifier<TapFlow> iid = createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID));
131 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
132 final CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
135 } catch (InterruptedException | ExecutionException e) {
136 LOG.warn("Transation failed ", e);
142 private boolean addTapFlowMd(NeutronTapFlow tapFlow) {
143 return updateTapFlowMd(tapFlow);
147 public boolean updateTapFlow(NeutronTapFlow tapFlow) {
148 return updateTapFlowMd(tapFlow);
152 public boolean addTapFlow(NeutronTapFlow tapFlow) {
153 if (!tapServiceExists(tapFlow.getTapFlowServiceID())) {
156 if (tapFlowExists(tapFlow.getTapFlowServiceID(), tapFlow.getID())) {
159 addTapFlowMd(tapFlow);
164 public NeutronTapFlow getTapFlow(String tapServiceUUID, String tapFlowUUID) {
165 final TapFlow tapFlow = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
166 if (tapFlow == null) {
169 return fromMd(tapFlow);
173 public boolean deleteTapFlow(String tapServiceUUID, String tapFlowUUID) {
174 return removeTapFlowMd(tapServiceUUID, tapFlowUUID);