Merge "Add additional subnet E2E tests."
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronVPNServiceInterface.java
1 /*
2  * Copyright Tata Consultancy Services, 2015.  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 java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.neutron.spi.INeutronVPNServiceCRUD;
21 import org.opendaylight.neutron.spi.NeutronVPNService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.vpnaas.rev141002.vpnaas.attributes.VpnServices;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.vpnaas.rev141002.vpnaas.attributes.vpn.services.VpnService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.vpnaas.rev141002.vpnaas.attributes.vpn.services.VpnServiceBuilder;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.ServiceRegistration;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class NeutronVPNServiceInterface extends AbstractNeutronInterface<VpnService,NeutronVPNService> implements INeutronVPNServiceCRUD {
32     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronVPNServiceInterface.class);
33     private ConcurrentMap<String, NeutronVPNService> vpnServiceDB = new ConcurrentHashMap<String, NeutronVPNService>();
34
35
36     NeutronVPNServiceInterface(ProviderContext providerContext) {
37         super(providerContext);
38     }
39
40     @Override
41     public boolean neutronVPNServiceExists(String uuid) {
42         return vpnServiceDB.containsKey(uuid);
43     }
44
45     @Override
46     public NeutronVPNService getVPNService(String uuid) {
47         if (!neutronVPNServiceExists(uuid)) {
48             LOGGER.debug("No VPNService Have Been Defined");
49             return null;
50         }
51         return vpnServiceDB.get(uuid);
52     }
53
54     @Override
55     public List<NeutronVPNService> getAllVPNService() {
56         Set<NeutronVPNService> allVPNService = new HashSet<NeutronVPNService>();
57         for (Entry<String, NeutronVPNService> entry : vpnServiceDB.entrySet()) {
58             NeutronVPNService VPNService = entry.getValue();
59             allVPNService.add(VPNService);
60         }
61         LOGGER.debug("Exiting getVPNService, Found {} OpenStackVPNService", allVPNService.size());
62         List<NeutronVPNService> ans = new ArrayList<NeutronVPNService>();
63         ans.addAll(allVPNService);
64         return ans;
65     }
66
67     @Override
68     public boolean addVPNService(NeutronVPNService input) {
69         if (neutronVPNServiceExists(input.getID())) {
70             return false;
71         }
72         vpnServiceDB.putIfAbsent(input.getID(), input);
73         addMd(input);
74         return true;
75     }
76
77     @Override
78     public boolean removeVPNService(String uuid) {
79         if (!neutronVPNServiceExists(uuid)) {
80             return false;
81         }
82         vpnServiceDB.remove(uuid);
83         removeMd(toMd(uuid));
84         return true;
85     }
86
87     @Override
88     public boolean updateVPNService(String uuid, NeutronVPNService delta) {
89         if (!neutronVPNServiceExists(uuid)) {
90             return false;
91         }
92         NeutronVPNService target = vpnServiceDB.get(uuid);
93         boolean rc = overwrite(target, delta);
94         if (rc) {
95             updateMd(vpnServiceDB.get(uuid));
96         }
97         return rc;
98     }
99
100     @Override
101     public boolean neutronVPNServiceInUse(String uuid) {
102         return !neutronVPNServiceExists(uuid);
103     }
104
105     @Override
106     protected VpnService toMd(NeutronVPNService vpnService) {
107         VpnServiceBuilder vpnServiceBuilder = new VpnServiceBuilder();
108         if (vpnService.getName() != null) {
109             vpnServiceBuilder.setName(vpnService.getName());
110         }
111         if (vpnService.getTenantID() != null) {
112             vpnServiceBuilder.setTenantId(toUuid(vpnService.getTenantID()));
113         }
114         if (vpnService.getStatus() != null) {
115             vpnServiceBuilder.setStatus(vpnService.getStatus());
116         }
117         if (vpnService.getDescription() != null) {
118             vpnServiceBuilder.setDescr(vpnService.getDescription());
119         }
120         if (vpnService.getSubnetUUID() != null) {
121             vpnServiceBuilder.setSubnetId(toUuid(vpnService.getSubnetUUID()));
122         }
123         if (vpnService.getRouterUUID() != null) {
124             vpnServiceBuilder.setRouterId(toUuid(vpnService.getRouterUUID()));
125         }
126         vpnServiceBuilder.setAdminStateUp(vpnService.getAdminStateUp());
127         if (vpnService.getID() != null) {
128             vpnServiceBuilder.setUuid(toUuid(vpnService.getID()));
129         } else {
130             LOGGER.warn("Attempting to write neutron vpnService without UUID");
131         }
132         return vpnServiceBuilder.build();
133     }
134
135     @Override
136     protected InstanceIdentifier<VpnService> createInstanceIdentifier(VpnService vpnService) {
137         return InstanceIdentifier.create(VpnServices.class).child(VpnService.class, vpnService.getKey());
138     }
139
140     @Override
141     protected VpnService toMd(String uuid) {
142         VpnServiceBuilder vpnServiceBuilder = new VpnServiceBuilder();
143         vpnServiceBuilder.setUuid(toUuid(uuid));
144         return vpnServiceBuilder.build();
145     }
146
147     public static void registerNewInterface(BundleContext context,
148                                             ProviderContext providerContext,
149                                             List<ServiceRegistration<?>> registrations) throws Exception {
150         NeutronVPNServiceInterface neutronVPNServiceInterface = new NeutronVPNServiceInterface(providerContext);
151         ServiceRegistration<INeutronVPNServiceCRUD> neutronVPNServiceInterfaceRegistration = context.registerService(INeutronVPNServiceCRUD.class, neutronVPNServiceInterface, null);
152         if(neutronVPNServiceInterfaceRegistration != null) {
153             registrations.add(neutronVPNServiceInterfaceRegistration);
154         }
155     }
156 }