summaryrefslogtreecommitdiffstats
path: root/ioam/impl/src/main/java/io/fd/hc2vpp/vppioam/impl/config/IoamPotWriterCustomizer.java
blob: 2ea4d4f4620b4f82b072c0ffec2036cdb99debe6 (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
148
149
150
/*
 * Copyright (c) 2016 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.hc2vpp.vppioam.impl.config;


import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
import io.fd.hc2vpp.vppioam.impl.util.FutureJVppIoampotCustomizer;
import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.jvpp.ioampot.dto.PotProfileAdd;
import io.fd.jvpp.ioampot.dto.PotProfileAddReply;
import io.fd.jvpp.ioampot.dto.PotProfileDel;
import io.fd.jvpp.ioampot.dto.PotProfileDelReply;
import io.fd.jvpp.ioampot.future.FutureJVppIoampot;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.ioam.sb.pot.rev170112.pot.profile.PotProfileList;
import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.ioam.sb.pot.rev170112.pot.profiles.PotProfileSet;
import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.ioam.sb.pot.rev170112.pot.profiles.PotProfileSetKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class IoamPotWriterCustomizer extends FutureJVppIoampotCustomizer implements
        ListWriterCustomizer<PotProfileSet,PotProfileSetKey>, JvppReplyConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(IoamPotWriterCustomizer.class);

    public IoamPotWriterCustomizer(@Nonnull FutureJVppIoampot futureJVppIoampot) {
        super(futureJVppIoampot);
    }


    /**
     * Handle write operation. C from CRUD.
     *
     * @param id           Identifier(from root) of data being written
     * @param dataAfter    New data to be written
     * @param writeContext Write context can be used to store any useful information and then utilized by other customizers
     * @throws WriteFailedException if write was unsuccessful
     */
    @Override
    public void writeCurrentAttributes(@Nonnull InstanceIdentifier<PotProfileSet> id,
                                       @Nonnull PotProfileSet dataAfter, @Nonnull WriteContext writeContext)
            throws WriteFailedException {
        try {
            addPotProfile(dataAfter,id);
        } catch (WriteFailedException exCreate) {
            LOG.error("Add POT profile failed", exCreate);
            throw new WriteFailedException.CreateFailedException(id, dataAfter, exCreate);
        }

        LOG.info("POT profile added iid={}, added {}", id, dataAfter);
    }

    /**
     * Handle update operation. U from CRUD.
     *
     * @param id           Identifier(from root) of data being written
     * @param dataBefore   Old data
     * @param dataAfter    New, updated data
     * @param writeContext Write context can be used to store any useful information and then utilized by other customizers
     * @throws WriteFailedException if update was unsuccessful
     */
    @Override
    public void updateCurrentAttributes(@Nonnull InstanceIdentifier<PotProfileSet> id,
                                        @Nonnull PotProfileSet dataBefore, @Nonnull PotProfileSet dataAfter,
                                        @Nonnull WriteContext writeContext) throws WriteFailedException {
        try {
            delPotProfile(dataBefore,id);
            addPotProfile(dataAfter,id);
        } catch (WriteFailedException exUpdate) {
            LOG.error("Update POT Profile failed", exUpdate);
            throw new WriteFailedException.UpdateFailedException(id,dataBefore,dataAfter,exUpdate);
        }

        LOG.info("POT profile updated iid={}, added {}", id, dataAfter);
    }

    /**
     * Handle delete operation. D from CRUD.
     *
     * @param id           Identifier(from root) of data being written
     * @param dataBefore   Old data being deleted
     * @param writeContext Write context can be used to store any useful information and then utilized by other customizers
     * @throws WriteFailedException if delete was unsuccessful
     */
    @Override
    public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<PotProfileSet> id,
                                        @Nonnull PotProfileSet dataBefore, @Nonnull WriteContext writeContext)
            throws WriteFailedException {
        try {
            delPotProfile(dataBefore,id);
        } catch (WriteFailedException exDelete) {
            LOG.error("Del POT Profile failed", exDelete);
            throw new WriteFailedException.DeleteFailedException(id, exDelete);
        }

        LOG.info("POT profile deleted iid={}, added {}", id, dataBefore);
    }

    private void addPotProfile(PotProfileSet potProfileSet,
            InstanceIdentifier<PotProfileSet> id) throws WriteFailedException {
        for ( PotProfileList potProfileList : potProfileSet.getPotProfileList()) {
            writePotProfileList(potProfileList,potProfileSet.getName(),id);
        }
    }

    private PotProfileAddReply writePotProfileList(PotProfileList potProfileList, String name,
                                                   InstanceIdentifier<PotProfileSet> id) throws WriteFailedException{
        PotProfileAdd request = new PotProfileAdd();
        request.id = potProfileList.getIndex().getValue().byteValue();
        request.validator = (byte) (potProfileList.isValidator() ? 1 : 0);
        request.secretShare = potProfileList.getSecretShare().longValue();
        request.prime = potProfileList.getPrimeNumber().longValue();
        request.secretKey = potProfileList.isValidator() ? potProfileList.getValidatorKey().longValue() : 0;
        request.maxBits = potProfileList.getNumberOfBits().byteValue();
        request.lpc = potProfileList.getLpc().longValue();
        request.polynomialPublic = potProfileList.getPublicPolynomial().longValue();
        request.listNameLen = (byte) name.getBytes(StandardCharsets.UTF_8).length;
        request.listName = name.getBytes(StandardCharsets.UTF_8);

        return getReplyForWrite(getFutureJVppIoampot().potProfileAdd(request).toCompletableFuture(), id);
    }

    private PotProfileDelReply delPotProfile(PotProfileSet potProfileSet, InstanceIdentifier<PotProfileSet> id)
            throws WriteFailedException{
        PotProfileDel request = new PotProfileDel();
        request.listNameLen = (byte)potProfileSet.getName().getBytes(StandardCharsets.UTF_8).length;
        request.listName = potProfileSet.getName().getBytes(StandardCharsets.UTF_8);

        return getReplyForWrite(getFutureJVppIoampot().potProfileDel(request).toCompletableFuture(),id);
    }
}