2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Modifications copyright (c) 2019 AT&T Intellectual Property
21 package io.fd.honeycomb.lcmapi.write;
23 import io.fd.honeycomb.lcmapi.CrudService;
24 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import java.io.IOException;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.stream.count.rev190118.stream.count.params.Streams;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Writer for {@link Element} list node from our YANG model.
41 public final class ElementCustomizer implements WriterCustomizer<Streams> {
43 private static final Logger LOG = LoggerFactory.getLogger(ElementCustomizer.class);
45 private final CrudService<Streams> crudService;
47 public ElementCustomizer(@Nonnull final CrudService<Streams> crudService) {
48 this.crudService = crudService;
52 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id, @Nonnull final Streams dataAfter,
53 @Nonnull final WriteContext writeContext) throws WriteFailedException {
54 //perform write of data,or throw exception
55 //invoked by PUT operation,if provided data doesn't exist in Config data
56 crudService.writeData(id, dataAfter);
58 runScript(dataAfter.getActiveStreams().getValue());
60 catch (IOException e) {
61 String message = "Write operation failed " + e;
67 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
68 @Nonnull final Streams dataBefore,
69 @Nonnull final Streams dataAfter, @Nonnull final WriteContext writeContext)
70 throws WriteFailedException {
71 //invoked by PUT operation,if provided data does exist in Config data
72 crudService.updateData(id, dataBefore, dataAfter);
74 runScript(dataAfter.getActiveStreams().getValue());
76 catch (IOException e) {
77 String message = "Write operation failed " + e;
83 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
84 @Nonnull final Streams dataBefore,
85 @Nonnull final WriteContext writeContext) throws WriteFailedException {
86 //perform delete of data,or throw exception
87 // Not supported, throw exception
88 throw new WriteFailedException.DeleteFailedException(id,
89 new UnsupportedOperationException("Delete operation not supported"));
92 //Update the number of running streams running a custom script that uses the old vPacketGen REST APIs
93 private void runScript(long streams) throws IOException {
95 String script = new String("bash /opt/update_running_streams.sh " + streams);
96 Runtime.getRuntime().exec(script);
97 String message = "Number of running streams updated to " + streams;