Fixup Augmentable and Identifiable methods changing
[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 package org.opendaylight.neutron.transcriber;
9
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;
39
40 @Singleton
41 @OsgiServiceProvider(classes = INeutronTapFlowCRUD.class)
42 public final class NeutronTapFlowInterface
43         extends AbstractTranscriberInterface<TapFlow, TapFlows, TapFlowKey, NeutronTapFlow, TapServiceAttributes>
44         implements INeutronTapFlowCRUD {
45
46     private static final Logger LOG = LoggerFactory.getLogger(NeutronTapFlowInterface.class);
47
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();
53
54     @Inject
55     public NeutronTapFlowInterface(DataBroker db) {
56         super(TapFlowBuilder.class, db);
57     }
58
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());
64     }
65
66     @Override
67     protected List<TapFlow> getDataObjectList(TapFlows flows) {
68         return flows.getTapFlow();
69     }
70
71     @Override
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());
77         }
78         if (flow.getSourcePort() != null) {
79             answer.setTapFlowSourcePort(flow.getSourcePort().getValue());
80         }
81         if (flow.getDirection() != null) {
82             answer.setTapFlowDirection(DIRECTION_MAP.get(flow.getDirection()));
83         }
84
85         return answer;
86     }
87
88     @Override
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()));
94         }
95         if (flow.getTapFlowSourcePort() != null) {
96             flowBuilder.setSourcePort(toUuid(flow.getTapFlowSourcePort()));
97         }
98         if (flow.getTapFlowDirection() != null) {
99             final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
100             flowBuilder.setDirection(mapper.get(flow.getTapFlowDirection()));
101         }
102
103         return flowBuilder.build();
104     }
105
106     @Override
107     public boolean tapFlowExists(String tapServiceUUID, String tapFlowUUID) {
108         final TapFlow dataObject = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
109         return dataObject != null;
110     }
111
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;
116     }
117
118
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();
125         try {
126             future.get();
127         } catch (InterruptedException | ExecutionException e) {
128             LOG.warn("Transaction Failed ", e);
129             return false;
130         }
131         return true;
132     }
133
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();
139         try {
140             future.get();
141         } catch (InterruptedException | ExecutionException e) {
142             LOG.warn("Transation failed ", e);
143             return false;
144         }
145         return true;
146     }
147
148     private boolean addTapFlowMd(NeutronTapFlow tapFlow) {
149         return updateTapFlowMd(tapFlow);
150     }
151
152     @Override
153     public boolean updateTapFlow(NeutronTapFlow tapFlow) {
154         return updateTapFlowMd(tapFlow);
155     }
156
157     @Override
158     public boolean addTapFlow(NeutronTapFlow tapFlow) {
159         if (!tapServiceExists(tapFlow.getTapFlowServiceID())) {
160             return false;
161         }
162         if (tapFlowExists(tapFlow.getTapFlowServiceID(), tapFlow.getID())) {
163             return false;
164         }
165         addTapFlowMd(tapFlow);
166         return true;
167     }
168
169     @Override
170     public NeutronTapFlow getTapFlow(String tapServiceUUID, String tapFlowUUID) {
171         final TapFlow tapFlow = readMd(createTapFlowInstanceIdentifier(tapServiceUUID, toMd(tapFlowUUID)));
172         if (tapFlow == null) {
173             return null;
174         }
175         return fromMd(tapFlow);
176     }
177
178     @Override
179     public boolean deleteTapFlow(String tapServiceUUID, String tapFlowUUID) {
180         return removeTapFlowMd(tapServiceUUID, tapFlowUUID);
181     }
182 }