Refactor InterfaceManagerCommonUtils to non-static
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / confighelpers / OvsVlanMemberConfigAddHelper.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.genius.interfacemanager.renderer.ovs.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class OvsVlanMemberConfigAddHelper {
25     private static final Logger LOG = LoggerFactory.getLogger(OvsVlanMemberConfigAddHelper.class);
26
27     private final DataBroker dataBroker;
28     private final IdManagerService idManager;
29     private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
30
31     public OvsVlanMemberConfigAddHelper(DataBroker dataBroker, IdManagerService idManager,
32             InterfaceManagerCommonUtils interfaceManagerCommonUtils) {
33         this.dataBroker = dataBroker;
34         this.idManager = idManager;
35         this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
36     }
37
38     public DataBroker getDataBroker() {
39         return dataBroker;
40     }
41
42     public IdManagerService getIdManager() {
43         return idManager;
44     }
45
46     public InterfaceManagerCommonUtils getInterfaceManagerCommonUtils() {
47         return interfaceManagerCommonUtils;
48     }
49
50     public List<ListenableFuture<Void>> addConfiguration(ParentRefs parentRefs, Interface interfaceNew) {
51         LOG.info("adding vlan member configuration for interface {}", interfaceNew.getName());
52         List<ListenableFuture<Void>> futures = new ArrayList<>();
53         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
54         interfaceManagerCommonUtils.createInterfaceChildEntry(parentRefs.getParentInterface(), interfaceNew.getName(),
55                 writeTransaction);
56         futures.add(writeTransaction.submit());
57
58         InterfaceKey interfaceKey = new InterfaceKey(parentRefs.getParentInterface());
59         Interface ifaceParent = interfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey);
60         if (ifaceParent == null) {
61             LOG.info("Parent Interface: {} not found when adding child interface: {}", parentRefs.getParentInterface(),
62                     interfaceNew.getName());
63             return futures;
64         }
65
66         IfL2vlan parentIfL2Vlan = ifaceParent.getAugmentation(IfL2vlan.class);
67         if (parentIfL2Vlan == null || parentIfL2Vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
68             LOG.error("Parent Interface: {} not of trunk Type when adding trunk-member: {}", ifaceParent, interfaceNew);
69             return futures;
70         }
71
72         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
73             .ietf.interfaces.rev140508.interfaces.state.Interface ifState = interfaceManagerCommonUtils
74                 .getInterfaceState(parentRefs.getParentInterface());
75         interfaceManagerCommonUtils.addStateEntry(interfaceNew.getName(), futures, ifState);
76         return futures;
77     }
78 }