Skip to content

Commit

Permalink
add API endpoint, closes #285
Browse files Browse the repository at this point in the history
  • Loading branch information
haniotak authored and haniotak committed Mar 6, 2019
1 parent 062aa80 commit dcd44cd
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 16 deletions.
69 changes: 55 additions & 14 deletions backend/src/main/java/net/es/oscars/pss/svc/PSSAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import net.es.oscars.pss.ent.RouterCommands;
import net.es.oscars.resv.db.CommandHistoryRepository;
import net.es.oscars.resv.ent.Connection;
import net.es.oscars.resv.ent.Event;
import net.es.oscars.resv.ent.VlanJunction;
import net.es.oscars.resv.enums.EventType;
import net.es.oscars.resv.enums.State;
import net.es.oscars.resv.svc.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -40,16 +43,18 @@ public class PSSAdapter {
private PSSParamsAdapter paramsAdapter;
private CommandHistoryRepository historyRepo;
private NsiService nsiService;
private LogService logService;

@Autowired
public PSSAdapter(PSSProxy pssProxy, RouterCommandsRepository rcr,
CommandHistoryRepository historyRepo, NsiService nsiService,
PSSParamsAdapter paramsAdapter, PssProperties properties) {
PSSParamsAdapter paramsAdapter, LogService logService, PssProperties properties) {
this.pssProxy = pssProxy;
this.rcr = rcr;
this.historyRepo = historyRepo;
this.paramsAdapter = paramsAdapter;
this.nsiService = nsiService;
this.logService = logService;
this.properties = properties;
}

Expand All @@ -63,7 +68,7 @@ public void generateConfig(Connection conn) throws PSSException {
commands.addAll(this.dismantleCommands(conn));
commands.addAll(this.opCheckCommands(conn));
for (Command cmd : commands) {
log.info("asking PSS to gen config for device " + cmd.getDevice()+" connId: "+conn.getConnectionId());
log.info("asking PSS to gen config for device " + cmd.getDevice() + " connId: " + conn.getConnectionId());
try {
GenerateResponse resp = pssProxy.generate(cmd);
log.info(resp.getGenerated());
Expand Down Expand Up @@ -104,7 +109,26 @@ public State build(Connection conn) throws PSSException {

if (st.getConfigStatus().equals(ConfigStatus.ERROR)) {
result = State.FAILED;
Event ev = Event.builder()
.connectionId(conn.getConnectionId())
.description("PSS build failed")
.type(EventType.ERROR)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(conn.getConnectionId(), ev);
} else {
Event ev = Event.builder()
.connectionId(conn.getConnectionId())
.description("built successfully")
.type(EventType.BUILT)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(conn.getConnectionId(), ev);
}


}
this.triggerNsi(conn, result);
return result;
Expand All @@ -129,6 +153,23 @@ public State dismantle(Connection conn) throws PSSException {
historyRepo.save(rch);
if (st.getConfigStatus().equals(ConfigStatus.ERROR)) {
result = State.FAILED;
Event ev = Event.builder()
.connectionId(conn.getConnectionId())
.description("PSS dismantle failed")
.type(EventType.ERROR)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(conn.getConnectionId(), ev);
} else {
Event ev = Event.builder()
.connectionId(conn.getConnectionId())
.description("dismantled successfully")
.type(EventType.DISMANTLED)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(conn.getConnectionId(), ev);
}
}
this.triggerNsi(conn, result);
Expand Down Expand Up @@ -236,7 +277,7 @@ public List<CommandStatus> pollStatuses(List<String> commandIds)
throws InterruptedException, ExecutionException {
List<CommandStatus> statuses = new ArrayList<>();
int threadNum = commandIds.size();
if (threadNum == 0 ) {
if (threadNum == 0) {
return statuses;
}
ExecutorService executor = Executors.newFixedThreadPool(threadNum);
Expand All @@ -261,15 +302,15 @@ public List<Command> buildCommands(Connection conn) throws PSSException {
log.info("gathering build commands for " + conn.getConnectionId());
List<Command> commands = new ArrayList<>();

for (VlanJunction j: conn.getReserved().getCmp().getJunctions()) {
for (VlanJunction j : conn.getReserved().getCmp().getJunctions()) {
RouterCommands existing = existing(conn.getConnectionId(), j.getDeviceUrn(), CommandType.BUILD);
if (existing != null) {
log.info("dismantle commands already exist for "+conn.getConnectionId());
log.info("dismantle commands already exist for " + conn.getConnectionId());
}
commands.add(paramsAdapter.command(CommandType.BUILD, conn, j, existing));
}

log.info("gathered "+commands.size()+" commands");
log.info("gathered " + commands.size() + " commands");

return commands;
}
Expand All @@ -291,25 +332,25 @@ public List<Command> dismantleCommands(Connection conn) throws PSSException {

List<Command> commands = new ArrayList<>();

for (VlanJunction j: conn.getReserved().getCmp().getJunctions()) {
for (VlanJunction j : conn.getReserved().getCmp().getJunctions()) {
RouterCommands existing = existing(conn.getConnectionId(), j.getDeviceUrn(), CommandType.DISMANTLE);
if (existing != null) {
log.info("dismantle commands already exist for "+conn.getConnectionId());
log.info("dismantle commands already exist for " + conn.getConnectionId());
}
commands.add(paramsAdapter.command(CommandType.DISMANTLE, conn, j, existing));
}

log.info("gathered "+commands.size()+" commands");
log.info("gathered " + commands.size() + " commands");


return commands;
}

public RouterCommands existing(String connId, String deviceUrn, CommandType commandType) {
public RouterCommands existing(String connId, String deviceUrn, CommandType commandType) {
List<RouterCommands> existing = rcr.findByConnectionIdAndDeviceUrn(connId, deviceUrn);
for (RouterCommands rc: existing) {
for (RouterCommands rc : existing) {
if (rc.getType().equals(commandType)) {
return rc;
return rc;
}
}
return null;
Expand All @@ -319,11 +360,11 @@ public List<Command> opCheckCommands(Connection conn) throws PSSException {
log.info("gathering op check commands for " + conn.getConnectionId());
List<Command> commands = new ArrayList<>();

for (VlanJunction j: conn.getReserved().getCmp().getJunctions()) {
for (VlanJunction j : conn.getReserved().getCmp().getJunctions()) {
commands.add(paramsAdapter.command(CommandType.OPERATIONAL_STATUS, conn, j, null));
}

log.info("gathered "+commands.size()+" commands");
log.info("gathered " + commands.size() + " commands");


return commands;
Expand Down
64 changes: 62 additions & 2 deletions backend/src/main/java/net/es/oscars/resv/svc/ConnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,40 @@ public ConnectionList filter(ConnectionFilter filter) {
}
}
}
List<Connection> intervalFiltered = vlanFiltered;
if (filter.getInterval() != null) {
Instant fBeginning = filter.getInterval().getBeginning();
Instant fEnding = filter.getInterval().getEnding();
intervalFiltered = new ArrayList<>();
for (Connection c : vlanFiltered) {
boolean add = true;
Schedule s = null;
if (c.getPhase().equals(Phase.ARCHIVED)) {
s = c.getArchived().getSchedule();
} else if (c.getPhase().equals(Phase.HELD)) {
s = c.getReserved().getSchedule();
} else {
// shouldn't happen!
log.error("invalid phase for " + c.getConnectionId());
continue;
}

if (s.getBeginning().isBefore(fBeginning) &&
s.getEnding().isBefore(fEnding)) {
add = false;
}
if (s.getBeginning().isAfter(fBeginning) &&
s.getEnding().isAfter(fEnding)) {
add = false;
}
if (add) {
intervalFiltered.add(c);
}
}
}


List<Connection> finalFiltered = vlanFiltered;
List<Connection> finalFiltered = intervalFiltered;
List<Connection> paged = new ArrayList<>();

// pages start at 1
Expand Down Expand Up @@ -411,6 +443,15 @@ public ConnChangeResult release(Connection c) {
// we haven't started yet; can delete without consequence
log.debug("deleting unstarted connection during release"+c.getConnectionId());
connRepo.delete(c);
Event ev = Event.builder()
.connectionId(c.getConnectionId())
.description("released (unstarted)")
.type(EventType.RELEASED)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(c.getConnectionId(), ev);

return ConnChangeResult.builder()
.what(ConnChange.DELETED)
.when(Instant.now())
Expand All @@ -430,9 +471,28 @@ public ConnChangeResult release(Connection c) {
c.setState(State.FAILED);
log.error(ex.getMessage(), ex);
}
Event ev = Event.builder()
.connectionId(c.getConnectionId())
.description("released (active)")
.type(EventType.RELEASED)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(c.getConnectionId(), ev);


} else {
slack.sendMessage("Cancelling non-active connection: " + c.getConnectionId());
slack.sendMessage("Releasing non-active connection: " + c.getConnectionId());
log.debug("Releasing non-active connection: "+c.getConnectionId());
Event ev = Event.builder()
.connectionId(c.getConnectionId())
.description("released (inactive)")
.type(EventType.RELEASED)
.at(Instant.now())
.username("system")
.build();
logService.logEvent(c.getConnectionId(), ev);

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.es.oscars.web.beans;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.es.oscars.resv.enums.Phase;

import java.time.Instant;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MinimalConnEndpoint {
private String router;
private String port;
private Integer vlan;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.es.oscars.web.beans;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.es.oscars.resv.enums.Phase;

import java.time.Instant;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MinimalConnEntry {
private List<MinimalConnEndpoint> endpoints;
private Integer start;
private Integer end;
private String description;
}
Loading

0 comments on commit dcd44cd

Please sign in to comment.