Clean up code issues caused by transcriber refactoring
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronRouterInterface.java
1 /*
2  * Copyright IBM Corporation, 2013.  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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
23 import org.opendaylight.neutron.spi.NeutronRouter;
24 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
25 import org.opendaylight.neutron.spi.NeutronRouter_NetworkReference;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.Routers;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.Router;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.RouterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.ExternalGatewayInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.ExternalGatewayInfoBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, NeutronRouter> implements INeutronRouterCRUD {
41     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronRouterInterface.class);
42     private ConcurrentMap<String, NeutronRouter> routerDB  = new ConcurrentHashMap<String, NeutronRouter>();
43     // methods needed for creating caches
44
45
46     NeutronRouterInterface(ProviderContext providerContext) {
47         super(providerContext);
48     }
49
50
51     // IfNBRouterCRUD Interface methods
52
53     @Override
54     public boolean routerExists(String uuid) {
55         return routerDB.containsKey(uuid);
56     }
57
58     @Override
59     public NeutronRouter getRouter(String uuid) {
60         if (!routerExists(uuid)) {
61             return null;
62         }
63         return routerDB.get(uuid);
64     }
65
66     @Override
67     public List<NeutronRouter> getAllRouters() {
68         Set<NeutronRouter> allRouters = new HashSet<NeutronRouter>();
69         for (Entry<String, NeutronRouter> entry : routerDB.entrySet()) {
70             NeutronRouter router = entry.getValue();
71             allRouters.add(router);
72         }
73         LOGGER.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
74         List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
75         ans.addAll(allRouters);
76         return ans;
77     }
78
79     @Override
80     public boolean addRouter(NeutronRouter input) {
81         if (routerExists(input.getID())) {
82             return false;
83         }
84         routerDB.putIfAbsent(input.getID(), input);
85         addMd(input);
86         return true;
87     }
88
89     @Override
90     public boolean removeRouter(String uuid) {
91         if (!routerExists(uuid)) {
92             return false;
93         }
94         routerDB.remove(uuid);
95         removeMd(toMd(uuid));
96         return true;
97     }
98
99     @Override
100     public boolean updateRouter(String uuid, NeutronRouter delta) {
101         if (!routerExists(uuid)) {
102             return false;
103         }
104         NeutronRouter target = routerDB.get(uuid);
105         boolean rc = overwrite(target, delta);
106         if (rc) {
107             updateMd(routerDB.get(uuid));
108         }
109         return rc;
110     }
111
112     @Override
113     public boolean routerInUse(String routerUUID) {
114         if (!routerExists(routerUUID)) {
115             return true;
116         }
117         NeutronRouter target = routerDB.get(routerUUID);
118         return (target.getInterfaces().size() > 0);
119     }
120
121     @Override
122     protected Router toMd(NeutronRouter router) {
123
124         RouterBuilder routerBuilder = new RouterBuilder();
125
126         if (router.getRouterUUID() != null) {
127             routerBuilder.setUuid(toUuid(router.getRouterUUID()));
128         }
129         if (router.getName() != null) {
130             routerBuilder.setName(router.getName());
131         }
132         if (router.getTenantID() != null && !router.getTenantID().isEmpty()) {
133             routerBuilder.setTenantId(toUuid(router.getTenantID()));
134         }
135         if (router.getStatus() != null) {
136             routerBuilder.setStatus(router.getStatus());
137         }
138         if (router.getGatewayPortId() != null && !router.getGatewayPortId().isEmpty()) {
139             routerBuilder.setGatewayPortId(toUuid(router.getGatewayPortId()));
140         }
141         routerBuilder.setAdminStateUp(router.getAdminStateUp());
142         routerBuilder.setDistribted(router.getDistributed());
143         if (router.getRoutes() != null) {
144             List<String> routes = new ArrayList<String>();
145             for (String route : router.getRoutes()) {
146                 routes.add(route);
147             }
148             routerBuilder.setRoutes(routes);
149         }
150         if (router.getExternalGatewayInfo() != null) {
151             ExternalGatewayInfo externalGatewayInfo = null;
152             List<NeutronRouter_NetworkReference> neutronRouter_NetworkReferences = new ArrayList<NeutronRouter_NetworkReference>();
153             neutronRouter_NetworkReferences.add(router.getExternalGatewayInfo());
154             for (NeutronRouter_NetworkReference externalGatewayInfos : neutronRouter_NetworkReferences) {
155                 ExternalGatewayInfoBuilder builder = new ExternalGatewayInfoBuilder();
156                 builder.setEnableSnat(externalGatewayInfos.getEnableSNAT());
157                 builder.setExternalNetworkId(toUuid(externalGatewayInfos.getNetworkID()));
158                 List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
159                 for (int i = 0; i < externalFixedIps.size(); i++) {
160                     externalFixedIps.add((ExternalFixedIps) externalGatewayInfos.getExternalFixedIPs().get(i));
161                 }
162                 builder.setExternalFixedIps(externalFixedIps);
163                 externalGatewayInfo = builder.build();
164             }
165             routerBuilder.setExternalGatewayInfo(externalGatewayInfo);
166         }
167         if (router.getInterfaces() != null) {
168             Map<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
169             List<Interfaces> interfaces = new ArrayList<Interfaces>();
170             for (Entry<String, NeutronRouter_Interface> entry : mapInterfaces.entrySet()) {
171                 interfaces.add((Interfaces) entry.getValue());
172             }
173             routerBuilder.setInterfaces(interfaces);
174         }
175         if (router.getID() != null) {
176             routerBuilder.setUuid(toUuid(router.getID()));
177         } else {
178             LOGGER.warn("Attempting to write neutron router without UUID");
179         }
180         return routerBuilder.build();
181     }
182
183     @Override
184     protected InstanceIdentifier<Router> createInstanceIdentifier(Router router) {
185         return InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class, router.getKey());
186     }
187
188     @Override
189     protected Router toMd(String uuid) {
190         RouterBuilder routerBuilder = new RouterBuilder();
191         routerBuilder.setUuid(toUuid(uuid));
192         return routerBuilder.build();
193     }
194
195     public static void registerNewInterface(BundleContext context,
196                                             ProviderContext providerContext,
197                                             List<ServiceRegistration<?>> registrations) {
198         NeutronRouterInterface neutronRouterInterface = new NeutronRouterInterface(providerContext);
199         ServiceRegistration<INeutronRouterCRUD> neutronRouterInterfaceRegistration = context.registerService(INeutronRouterCRUD.class, neutronRouterInterface, null);
200         if(neutronRouterInterfaceRegistration != null) {
201             registrations.add(neutronRouterInterfaceRegistration);
202         }
203     }
204 }