tapaas: tapaas support
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronTapFlowInterface.java
1 /*
2  * Copyright (c) 2017 Intel Corporation and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.neutron.transcriber;
10
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;
37
38 public final class NeutronTapFlowInterface
39         extends AbstractTranscriberInterface<TapFlow, TapFlows, TapFlowKey, NeutronTapFlow, TapServiceAttributes>
40         implements INeutronTapFlowCRUD {
41
42     private static final Logger LOG = LoggerFactory.getLogger(NeutronTapFlowInterface.class);
43
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();
49
50     NeutronTapFlowInterface(DataBroker db) {
51         super(TapFlowBuilder.class, db);
52     }
53
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());
59     }
60
61     @Override
62     protected List<TapFlow> getDataObjectList(TapFlows flows) {
63         return flows.getTapFlow();
64     }
65
66     @Override
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());
72         }
73         if (flow.getSourcePort() != null) {
74             answer.setTapFlowSourcePort(flow.getSourcePort().getValue());
75         }
76         if (flow.getDirection() != null) {
77             answer.setTapFlowDirection(DIRECTION_MAP.get(flow.getDirection()));
78         }
79
80         return answer;
81     }
82
83     @Override
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()));
89         }
90         if (flow.getTapFlowSourcePort() != null) {
91             flowBuilder.setSourcePort(toUuid(flow.getTapFlowSourcePort()));
92         }
93         if (flow.getTapFlowDirection() != null) {
94             final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
95             flowBuilder.setDirection(mapper.get(flow.getTapFlowDirection()));
96         }
97
98         return flowBuilder.build();
99     }
100
101     public boolean tapFlowExists(String tapServiceUUID, String tapFlowUUID) {
102         final TapFlow dataObject = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
103         return dataObject != null;
104     }
105
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;
110     }
111
112
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();
119         try {
120             future.get();
121         } catch (InterruptedException | ExecutionException e) {
122             LOG.warn("Transaction Failed ", e);
123             return false;
124         }
125         return true;
126     }
127
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();
133         try {
134             future.get();
135         } catch (InterruptedException | ExecutionException e) {
136             LOG.warn("Transation failed ", e);
137             return false;
138         }
139         return true;
140     }
141
142     private boolean addTapFlowMd(NeutronTapFlow tapFlow) {
143         return updateTapFlowMd(tapFlow);
144     }
145
146     @Override
147     public boolean updateTapFlow(NeutronTapFlow tapFlow) {
148         return updateTapFlowMd(tapFlow);
149     }
150
151     @Override
152     public boolean addTapFlow(NeutronTapFlow tapFlow) {
153         if (!tapServiceExists(tapFlow.getTapFlowServiceID())) {
154             return false;
155         }
156         if (tapFlowExists(tapFlow.getTapFlowServiceID(), tapFlow.getID())) {
157             return false;
158         }
159         addTapFlowMd(tapFlow);
160         return true;
161     }
162
163     @Override
164     public NeutronTapFlow getTapFlow(String tapServiceUUID, String tapFlowUUID) {
165         final TapFlow tapFlow = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
166         if (tapFlow == null) {
167             return null;
168         }
169         return fromMd(tapFlow);
170     }
171
172     @Override
173     public boolean deleteTapFlow(String tapServiceUUID, String tapFlowUUID) {
174         return removeTapFlowMd(tapServiceUUID, tapFlowUUID);
175     }
176 }