summaryrefslogtreecommitdiffstats
path: root/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/VppPollOperDataImpl.java
blob: 71d4bea4b4be2e16992cf9f3083e042ce9fe949a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.v3po.impl;

import java.util.HashMap;
import java.util.concurrent.Future;

import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.V3poService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppPollOperDataOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppPollOperDataOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppState;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.openvpp.vppjapi.vppBridgeDomainDetails;
import org.openvpp.vppjapi.vppBridgeDomainInterfaceDetails;
import org.openvpp.vppjapi.vppL2Fib;
import org.openvpp.vppjapi.vppVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VppPollOperDataImpl implements V3poService {
    private static final Logger LOG = LoggerFactory.getLogger(VppPollOperDataImpl.class);
    private vppVersion version;
    private DataBroker db;
    private int[] bdIds;
    private HashMap<Integer, vppL2Fib[]> l2fibByBdId = new HashMap<Integer, vppL2Fib[]>();

    /**
     * TODO-ADD-JAVADOC.
     */
    public VppPollOperDataImpl(DataBroker dataBroker) {
        db = dataBroker;
    }

    /**
     * TODO-ADD-JAVADOC.
     */
    public DataBroker getDataBroker() {
        return db;
    }

    /**
     * TODO-ADD-JAVADOC.
     * Update operational data and return string of space separated
     * interfaces names
     */
    public String updateOperational() {
        V3poApiRequest api = new V3poApiRequest(this);
        version = api.getVppVersion();
        
        bdIds = api.bridgeDomainDump(-1);
        
        // TODO: we don't need to cache BDs now that we got rid of callbacks
        l2fibByBdId.clear();
        if (bdIds != null) {
            for (int idx = 0; idx < bdIds.length; idx++) {
                l2fibByBdId.put(bdIds[idx], api.l2FibTableDump(bdIds[idx]));
            }
        }
        api.swInterfaceDumpAll();
        
        // build vpp-state
        VppStateCustomBuilder stateBuilder = new VppStateCustomBuilder();
        
        // bridge domains
        for (int i = 0; i < bdIds.length; i++) {
            vppBridgeDomainDetails bd = api.getBridgeDomainDetails(bdIds[i]);
            VppStateBridgeDomainBuilder bdBuilder = 
                    new VppStateBridgeDomainBuilder(
                            bd.name, bd.flood, bd.uuFlood, 
                            bd.arpTerm, bd.forward, bd.learn);
            
            for (int ifIdx = 0; ifIdx < bd.interfaces.length; ifIdx++) {
                vppBridgeDomainInterfaceDetails bdIf = bd.interfaces[ifIdx];
                bdBuilder.addInterface(bdIf.interfaceName, 
                        bd.bviInterfaceName == bdIf.interfaceName, 
                        bdIf.splitHorizonGroup);
            }
            
            vppL2Fib[] bdFibs = l2fibByBdId.get(bdIds[i]);

            for (int fibIdx = 0; fibIdx < bdFibs.length; fibIdx++) {
                vppL2Fib fib = bdFibs[fibIdx];
                bdBuilder.addL2Fib(fib.filter, fib.bridgedVirtualInterface,
                                   fib.outgoingInterface, fib.physAddress,
                                   fib.staticConfig);
            }
            
            stateBuilder.addBridgeDomain(bdBuilder.build());
        }
        
        stateBuilder.setVersion(version);
        
        // write to oper
        writeVppState(getVppStateIid(), stateBuilder.build());
        
        return api.ifNames;
    }
    
    @Override
    public Future<RpcResult<VppPollOperDataOutput>> vppPollOperData() {
        String ifNames = updateOperational();
        

        VppPollOperDataOutput output = new VppPollOperDataOutputBuilder()
            .setStatus(new Long(1)).build();

        return RpcResultBuilder.success(output).buildFuture();
    }
    
    private InstanceIdentifier<VppState> getVppStateIid() {
        return (InstanceIdentifier.create(VppState.class));
    }
    
    private void writeVppState(InstanceIdentifier<VppState> iid, VppState vppState) {
        WriteTransaction transaction = db.newWriteOnlyTransaction();

        //LOG.info("VPPOPER-INFO: Writing vpp-state to oper DataStore.");
        transaction.put(LogicalDatastoreType.OPERATIONAL, iid, vppState);
        
        CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
        Futures.addCallback(future, new LoggingFuturesCallBack<Void>(
                "VPPOPER-WARNING: Failed to write vpp-state to oper datastore", LOG));
    }
}